1

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?

AnxiousdeV
  • 373
  • 1
  • 3
  • 20

1 Answers1

1

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.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Before injecting the dependency into the constructor I will need to register the dependencies. This is handled in MVC by implementing the IDependencyResolver interface during initialization. I would like to register the dependencies of a supporting class library with business logic at assembly start up. I am looking for an interface or context to register the dependencies at start up of the class library project. – AnxiousdeV Jun 27 '13 at 17:35
  • It doesn't matter that those components are defined in an other assembly. They should all be registered in the startup path of your application (in MVC this is the `Application_Start` event). This unique place is called the [Composition Root](http://blog.ploeh.dk/2011/07/28/CompositionRoot/). – Steven Jun 27 '13 at 20:29
  • Ok, that seems to make sense but wouldn't this cause the MVC project to have a dependency on the supporting class library projects? I have a 3 tier application, UI(MVC Project) Business(Class Library) and Data(Class library). I want to inject dependencies into the Business project from the Data project, and into the UI project from the Business project. If I register the dependencies from all projects into the UI project I will need to add a reference to the UI project for both Business and Data projects and thus create a dependency. – AnxiousdeV Jun 27 '13 at 20:36
  • No, your presentation layer (mind the word 'layer') still does not know anything about your business layer, but the application assembly (that happens to hold your presentation layer) does. The difference is that layers are logical architectural boundaries, while assemblies are physical. You should strive to logical separation, but physical separation is only needed if things have to be deployed separately. Take a look at [this related question and the two main answers](http://stackoverflow.com/questions/9501604). – Steven Jun 27 '13 at 21:59
  • "and is by most experts" is a very generic term and I disagree with it @Steven I think that service locator pattern is useful when you need to inject too many interfaces inside a single class then the cost of creating the dependency tree is huge and you might only need to use single injected property based on the called method, so service locator pattern is a great solution to this issue (in my opinion of course) where you can create the necessary services for each method at runtime – Mahmoud Heretani Nov 19 '19 at 11:46
  • 1
    @MahmoudHeretani, it might be useful to read the description of the Service Locator Anti-Pattern in [DIPP&P](https://manning.com/seemann2/) or at least [the excerpt](https://freecontent.manning.com/the-service-locator-anti-pattern/). The pattern does not forbid your particular case, but it does restrict where in the application you are allowed to call the container directly. The distinction lies in the concept of the [Composition Root](https://freecontent.manning.com/dependency-injection-in-net-2nd-edition-understanding-the-composition-root/). – Steven Nov 19 '19 at 12:21
  • 1
    Thanks @Steven for the links, I will give them a deep look, besides that I already start reading your book – Mahmoud Heretani Nov 19 '19 at 22:49