1

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 ?

Steven
  • 166,672
  • 24
  • 332
  • 435
jony89
  • 5,155
  • 3
  • 30
  • 40
  • 1
    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 Jan 25 '14 at 20:10
  • 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 Jan 25 '14 at 20:31
  • 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 Jan 25 '14 at 21:03
  • Especially read the last part carefully about deployment artifacts and logical artifacts. – Steven Jan 25 '14 at 21:06
  • yes, i did read your comment. if i add a reference to my DAL project from my MVC project, i actually adding a reference to my DAL layer, i can't see the difference. also, please see in my comment my other question regarding the diagram. I don't see why "And even though that assembly references the assembly containing the DAL, the Presentation Layer still does not reference the Data Access Layer. " – jony89 Jan 25 '14 at 21:14
  • 1
    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 Jan 25 '14 at 21:16
  • Got it ! what about my other question ? how can i not reference/not having a dependency from my BLL layer to my DAL layer ? the classes do depend on each other, though the Root Composition diagram shows that there should be no dependency . am i missing something or this is ok ? – jony89 Jan 25 '14 at 21:20
  • You can move the DAL interfaces to its own assembly and let both the BLL and DAL depend on that new assembly, and remove the reference between BLL and DAL. – Steven Jan 25 '14 at 22:06
  • ok, this is what i was i asking, shall i move all of that king interfaces to a Common project? I've never seen a pattern like that. but this is really the way to do it ? – jony89 Jan 25 '14 at 22:16
  • What's the advantage of moving those interfaces to a separate library? Does it improve testability? Does it improve flexibility? Maintainability? – Steven Jan 25 '14 at 23:22
  • It gives us an independent layers... the question is, is this really that matters ? because i do want that IRepository will stay in the DAL layer. – jony89 Jan 25 '14 at 23:48
  • That's up to you to decide whether this matters for the project you are working on. – Steven Jan 25 '14 at 23:59

1 Answers1

2

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/

Steven
  • 166,672
  • 24
  • 332
  • 435
Paweł Bejger
  • 6,176
  • 21
  • 26
  • What your articles saying is exactly what i did, I've a MVC application and thus i put the Register commands within the global.asax ( actually, i used SimpleInjector quick install so it uses an external class for this, yet it does using global.asax ) yet, this doesnt solve my problem cause even if i write the registrations in my global.asax i still need a reference from my MVC layer to my DAL layer. – jony89 Jan 25 '14 at 19:52