Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DAL -> BLL <- GUI + composition root. How to setup DI-bindings?

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?

like image 512
Grastveit Avatar asked Mar 11 '11 00:03

Grastveit


People also ask

What is DI Composition?

Using a DI Container in a Composition Root A DI Container is a software library that can automate many of the tasks involved in composing objects and managing their lifetimes. A DI Container can be misused as a Service Locator, but it should only be used as an engine that composes object graphs.

What is the root of composition?

The word composition comes from the Latin componere, meaning "put together" and its meaning remains close to this. Writing classes are often called composition classes, and writing music is also called composition.


1 Answers

Split up the ASP.NET MVC application in two:

  • One part is your original ASP.NET MVC application, but without any logic whatsover. Just keep the Composition Root and your Views (.aspx, etc.) in this project. Since this is the Composition Root, you can have references to all your other projects. However, since all logic would have been extracted, this is now a Humble Object, so it's okay to have all the references at this level.
  • Extract all the logic (Controllers, etc.) into an Application Model project, which would just be a normal library project (.dll) that references the ASP.NET MVC binaries. This project would need to reference the BLL to get at the interfaces, but that's okay. However, both the Application Model and the BLL are effectively shielded from the DAL.

The resulting layering would look like this:

  • ASP.NET MVC application
  • Application Model
  • BLL
  • DAL
like image 165
Mark Seemann Avatar answered Oct 12 '22 00:10

Mark Seemann