bespoyasov / frontend-clean-architecture

React + TypeScript app built using the clean architecture principles in a more functional way.
https://bespoyasov.me/blog/clean-architecture-on-frontend/
2.35k stars 258 forks source link

Isn't it superfluous to export functions in adapters instead of constants? #15

Closed budarin closed 1 year ago

budarin commented 1 year ago
import { NotificationService } from "../application/ports";

export function useNotifier(): NotificationService {
  return {
    notify: (message: string) => window.alert(message),
  };
}

vs

import { NotificationService } from "../application/ports";

export const notifier: NotificationService = { 
  notify: (message: string) => window.alert(message) 
}

Calling functions that return a new service object each time leads to an overhead during code execution and also generates a bunch of garbage for GC. This is practically nothing - for the client, but if such code is used in SSR, this is already a problem

bespoyasov commented 1 year ago

It is an implementation detail; the concept from the post forces no constraints on how to do it.

The example in the text uses Hooks as “poor man's DI” and, for consistency, uses it when exposing adapters, too.

budarin commented 1 year ago

in this case, doesn't the module itself act like a handmade DI? 😊

bespoyasov commented 1 year ago

If, in testing, there are no preferences or constraints on mocking a module versus injecting a stub as a dependency, it might.