1
votes

I'm trying to use Simple Injector in a project which has the following architecture :

  • DAL layer(owns a repositories),
  • BLL layer(owns a services that talks to the repositories),
  • MVC layer(talks to the services in the BLL layer).

when it comes to register with the container the classes and the interfaces, I`m facing a problem, Simple Injector needs me to register the repository with its interface (as my classes in the service layer accepts a repository in their constructor)

So, actually, Simple Injector forces me to add a references to my DAL layer in my MVC layer which i really like to avoid.

My question is, is it possible/right to make an external project that will hold only Simple Injector, and this project will have reference to all other projects and that way i would be able to register what i want and still keep my project abstraction?

or there is any other easy way to solve this ?

1
yes, thanks . though i still don't completely get it, so my MVC application (which has the Composition Root - global.asax) should reference all the other projects? and by the diagram gave in the seleced answer, all other projects don't refernec any other project. how could i not reference my BLL layer to my DAL layer ? i need that to know what type i get in my constructor. or should i put all interfaces in a Common project ? Thanks again. - jony89
Did you read the second answer (which is mine)? The Composition Root must reference all other projects, either implicitly or explicitly; there is no way around this, this is good and you shouldn't try to prevent this. Since you decided to put the Composition Root and your presentation layer in the same assembly, that assembly also references all other projects. This however doesn't mean that your presentation layer references all other layers. - Steven
Especially read the last part carefully about deployment artifacts and logical artifacts. - Steven
The difference is that: your presentation layer (PL) depends on your DAL -only- when (some of) the classes of the PL (your controllers, views, etc) depend on classes in the DAL. It doesn't matter whether the PL's assembly takes a dependency on the DAL assembly, as long as the PL's types don't depend on DAL types. - Steven

1 Answers

2
votes

A DI Container (e.g. your Simple Injector) should only be referenced from the Composition Root. All other modules should have no reference to the container.

You can read more about the Composition Root here:

http://blog.ploeh.dk/2011/07/28/CompositionRoot/

What is more DI Container should be applied using the Register Resolve Release pattern entirely from within the Composition Root. More about this pattern here:

http://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasepattern/