2

Our application module uses Unity IoC container to register concrete types against interfaces, e.g. which logger we should use.

An existing static library also performs logging currently has no dependency on Unity at all but the thing is when this is build against the application, it should use that module's logger (so same log file is written to, etc).

How can I approach this so the library is not bound to only work with this specific application, and so the application controls which logger is used?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Since you are already making changes to that library, my advise is to make the classes in that library non-static and use *constructor injection* to inject the dependencies that those classes need. – Steven Jan 19 '17 at 12:13
  • @Steven so every single class is now passed an `ILogger` ctor argument? Isn't that exactly the kind of mess Unity container is supposed to help with? – Mr. Boy Jan 19 '17 at 12:15
  • 1
    If every class depends on `ILogger`, you are in a mess already, as clearly explained [here](https://stackoverflow.com/a/17614683/264697) and [here](https://stackoverflow.com/questions/9892137/windsor-pulling-transient-objects-from-the-container/9915056#9915056). A DI container can't help you with a flawed design, but they do tend to make it more clear where you are violating the SOLID principles. – Steven Jan 19 '17 at 12:22

0 Answers0