DAL -> BLL <- GUI + composition root. How to setup DI-bindings?
asp.net-mvc, dependency-injection, inversion-of-control
Solution
Mark Seemann's answer gave me the idea for this variant:
DAL with Repositories -> BLL with services and IRepository <- Asp.net mvc-app
^------------------------^--------- Composition Root <-------´
This is meant to illustrate that instead of letting the Web project reference the DAL it references a separate Composition Root-project that references both DAL and BLL. The composition-root-project has a single class with one method that define the bindings. It gives these additional benefits:
- Only three layers. Four layers would be a tough sell in the team.
- Loose coupling is ensured. It is not possible to access the DAL from view-code.
- Better tool support. Controllers remain at the standard location so "Add Controller" is accessible on the context-menu and missing views are highlighted in the controller-code. Also there is no need to configure or write a custom controller factory.
I don't see any big drawbacks.
Problem
I have made a three-layer application with refrences going as described in this answer: ``` DAL with Repositories -> BLL with services and IRepository <- Asp.net mvc-app ``` To get this running with dependency injection I see a few options: 1. Add a reference to DAL from the web-app to be able to setup bindings on application start. 2. Use a container with xml-configuration (3. Use reflection to load the dal-assembly and find types) Option 1. is easy and also makes the DAL.dll be copied to bin but then I suddenly reintroduce the reference I worked so hard to get rid of. The repositories can now be accessed directly. Option 2 and 3 seems unnecessarily complex. Is there no other way?