1

According to dependency inversion principle higher level module does not depend on lower level module rather they depend on abstraction. So it is a top down approach. I have a web project that has three tier. Tier 1 contain the view pages and controllers and viewmodels. Tier 2 is the Service layer and Tier 3 is repository. As the Web project is the most higher level module, it contains the interface that should be implemented by Service layer and Service layer contains the interface that should be implemented by repository. So Service layer has the reference of web project and repository has the reference of service layer. I am using autofac as IOC Container. As Service layer implement the interfaces of web project, I need to register interfaces to classes in Service layer in autofac module and also need to register the interfaces of service layer in repository layer in autofac module. As far as I know, I need to register the autofac module when the application starts. If I want to do so, I need to give the reference of service layer to web project and reference of repository layer to either in service layer or web project. But it will create circular build dependency if I want to follow the top down approach of DIP. My Question is How can I register the modules of autofac and maintain DIP?

Edit 1: Title of the question changes as I believe it would be a same approach for any DI Container

jubair
  • 597
  • 1
  • 6
  • 23
  • 1
    Related: https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application – Steven Apr 04 '15 at 10:54

1 Answers1

1

Dependency Inversion refers to the inversion of a dependency in a graph of dependencies .. it doesn't mean inverting the dependencies between assemblies (or inverting where objects live).

Your service layer interfaces should still reside in your service layer assembly or in another assembly somewhere else - not in your UI. However, your dependencies between the UI layer and the Service layer should be inverted.

Here is what a normal dependency looks like (arrow points down and the UI depends on a concrete object):

Non DI

Here is what Dependency Inversion looks like (an arrow is inverted and the UI depends on the abstraction instead):

Dependency Inversion

Notice that this has no concept of where things should be .. but hopefully it simplifies your understanding of the concept.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Are you sure about service layer interface live in service layer cause if I want to change the service layer, it would break the UI layer. Here is a nice diagram on where should the interface live: http://en.wikipedia.org/wiki/Dependency_inversion_principle. Uncle Bob also showed a similar diagram in his book Agile - Principles - Pattern and Practices. He also mentioned an approach of separate interface project in case of multi client. As far as I know, If you put the interfaces in service layer it would also violate separation of concerns as the UI layer becomes heavily dependent on service. – jubair Apr 04 '15 at 12:06
  • Notice I said `or in another assembly somewhere else` as well. – Simon Whitehead Apr 04 '15 at 13:27