0

I have 4 Projects in a solution

  • DAL_Project
  • BLL_Project
  • Interface_Project
  • WebApi_Project

Interface_Project has two interfaces ICar_DAL and ICar_BLL

DAL_Project has a class Car_DAL that implements ICar_DAL

BLL_Project has a class Car_BLL that implements ICar_BLL and its constructor takes in ICar_DAL

WebApi_Project has an api controller CarApiController and its constructor takes in ICar_BLL

the dependency resolution of WebApi Controller's constructor is done by Unity.WebApi using this in Bootstrapper.cs:

container.RegisterType<ICar_BLL, Car_BLL>();

this would have worked if my Car_BLL further didn't require ICar_DAL in its constructor.

to make it work i can do some thing like this:

container.RegisterType<ICar_BLL, Car_BLL>();
container.RegisterType<ICar_DAL, Car_DAL>();

but that would mean that i need to add reference to DAL_Project in my WebApi_Project which is something i would never want to do. DAL_Project should only be referred by BLL_Project

How can i solve this issue?

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
M. Ali Iftikhar
  • 3,125
  • 2
  • 26
  • 36
  • possible duplicate of [Ioc/DI - Why do I have to reference all layers/assemblies in entry application?](http://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application) – Steven Jul 24 '13 at 18:47
  • Thanks steven ... your answer and other answers in that post have given me few more things to study, I'll look into Composition Root concept in more details. I think that was the missing piece. Thanks again. – M. Ali Iftikhar Jul 24 '13 at 23:27

1 Answers1

2

but that would mean that i need to add reference to DAL_Project in my WebApi_Project which is something i would never want to do.

Oh you seem to have some misunderstanding about how Dependency should be done if you don't want to do that. The DI container is configured in the outermost layer of your application which is actually the host. It is also referred to as the Composition Root. In your case this is the hosting application of your Web API. If you are using ASP.NET to host your Web API then this is the right place to do the composition root and reference all the other underlying projects.

Personally in complex project I tend to have a ProjectName.Composition class library which serves me as a Composition root. this is where I configure my DI container and this is the project that references all the others - coz obviously in order to configure your DI root you need all the dependent projects and implementations. This .Composition assembly is then references in the hosting application and the Bootstrapper.Initialize method called in the Initialize method of the hosting application.

  • In the case of ASP.NET host that would be Application_Start in Global.asax
  • In case of a desktop application or a self-host that would be the Main method which is the entry point.
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin. I think i got your point of Composition Root. I'll read more about it. If Composition Root should be my Web Api project, do you think it is a good idea to leave the Web Api project to just do the Composition Root job, and take Controllers out of it in a new Class Library project. Then all of projects will have only references to the InterfaceProject, and the WebAPI project will have references of every project as its the Composition Root. Do you this would be a correct architecture? – M. Ali Iftikhar Jul 24 '13 at 23:24
  • Don't forget that a layer is a logical separation, not physical. Layers are to reduce complexity, assemblies are for creating units of deployment. So from a layering perspective, it is fine to have multiple layers in the same assembly. The composition root (CR) is a layer of its own, but since a CR is specific to the end application, it will always be deployed with your WebAPI app. And even when your WebAPI assembly references all other assemblies, your Web API UI layer does not reference all other layers. The CR layer on the other hand, will reference all other layers. – Steven Jul 25 '13 at 07:14
  • So does that mean i am going in the right direction by moving controllers and views in their own separate projects out of web api project. And leave web api project as CR layer? Also i guess the same can be applied to the MVC web application, where the Web Application is CR, and its Controllers an Views (UI) are in separate projects that do not reference all other projects. Last thing: keeping all my Interfaces in the separate Interface project is a good idea right? – M. Ali Iftikhar Jul 25 '13 at 09:40