-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISP complicated or violated with wessberg/DI #4
Comments
Looks like you can do something like that const consoleLogger = new ConsoleLogger(); // or another way to instantiate
container.registerSingleton<IInfoLogger>(() => consoleLogger);
container.registerSingleton<IWarnLogger>(() => consoleLogger);
container.registerSingleton<IErrorLogger>(() => consoleLogger); |
Oh! ) But, what if some service have dependency like: |
I am not a FE dev and not that familiar with ADT but something like that should work here too (so you just add one more registration) const consoleLogger = new ConsoleLogger(); // or another way to instantiate
container.registerSingleton<IInfoLogger>(() => consoleLogger);
container.registerSingleton<IWarnLogger>(() => consoleLogger);
container.registerSingleton<IErrorLogger>(() => consoleLogger);
type IInfoWarnLogger = IInfoLogger & IWarnLogger;
container.registerSingleton<IInfoWarnLogger>(() => consoleLogger);
//or even
container.registerSingleton<IInfoLogger & IWarnLogger>(() => consoleLogger); You could even write your own transformer that would register all the interfaces and their combinations automatically |
@Serg046 , Sure you can declare: interface IInfoWarnLogger extends IInfoLogger, IWarnLogger {}
// or as you've proposed with type conjunction But how many interfaces should we declare? How many times should we register the same factory/class? What if we need not a singleton, but we need to create a logger for each "request"? What about if we need a different instances implementing the same interface for different dependent services (service A needs Logger with log level "errors only", but service B needs Logger with log level "verbose")?
|
Hey, guys,
the library uses interface names as tokens and it complicates following to Interface Segregation Princinple.
In example:
All of these interfaces belongs to the clients of some logger, for example:
and finally I have the implementation
So, with wessberg/DI I have to register the ConsoleLogger three times with different client owned interfaces, and it could be ok, but when we need to make ConsoleLogger a singleton shared between all clients, -- we can't do it with wessberg/DI, can we?
The text was updated successfully, but these errors were encountered: