Search code examples
dependency-injectionunity-container

How to resolve dependecies across class libraries?


I am working on an MVC4 app using dependency injection and a Unity container. I am able to resolve the dependencies by implementing the IDependencyResolver class in the Web project. Is there a similar way to resolve dependencies at startup in supporting class libraries?


Solution

  • Is there a similar way to resolve dependencies at startup in supporting class libraries?

    Calling directly into the container or a facade (such as the DependencyResolver) is a pattern that is known as the Service Locator pattern and is described as an anti-pattern in both the book Dependency Injection in .NET and Dependency Injection Principles, Practices, and Patterns.

    So instead of calling into a service locator from within the classes in your class library projects, use the dependency injection pattern where you inject all dependencies a class needs in its constructor.