1

In my ASP.NET MVC 4 c# project I have 3 layers. Data, Business and Web. I follow repository pattern and services.

I use Autofac for dependency injection. What I did was registering components in global.asax. By this way, I had to reference my Data layer where I implemented repositories.

I think this breaks the rule, because my web project has a reference of data layer.

I have read some articles on solving this problem, and found Autofac Modules. However, I couldn't find a method to implement it without referencing data layer as well.

Is there a way to register the components without referencing data layer?

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SherleyDev
  • 335
  • 5
  • 21
  • Where are the interfaces that the data layer implements defined? – Keith Payne Nov 10 '13 at 12:23
  • Data layer consist of generic repository and unit of work classes as well as the repository classes derived from generic repository. In fact I have 4 projects, 3 of them mentioned in the question and 4th one is domain project where I implement entities (code-first). Services are in the business layer. – SherleyDev Nov 10 '13 at 12:29
  • 1
    Related: http://stackoverflow.com/a/9503612/126014 – Mark Seemann Nov 10 '13 at 13:34
  • Thank you Mark. I've read that post and your "composition root" article. I'm now confused about the usage of Autofac Modules. If it is okay to register the components within my presentation layer (MVC application - global.asax) by adding project references, then why would I need to use modules? Thank you very much. – SherleyDev Nov 10 '13 at 14:17
  • @SherleyDev: you misunderstood. The composition root is **not** in your presentation *layer*. The composition root is a different layer (its own layer), even though you might physically place it in the same assembly as the presentation layer. – Steven Nov 10 '13 at 15:12
  • 1
    Why would you need modules? That's a good question. I would say you would hardly ever need such a feature. – Steven Nov 10 '13 at 15:15
  • @Steven: So, what I should do is to create a separate library project for composition root, which references all needed projects (like domain, business, etc.) and then reference this project in mvc application, right? – SherleyDev Nov 10 '13 at 15:18
  • @SherleyDev: No! Absolutely not. A layer != assembly. An assembly is a deployment artifact. An layer is a logical artifact. Extracting the composition root to its own assembly will only make things harder and has no benefits. Please also read [my answer](http://stackoverflow.com/a/9505530/264697) to the question Mark pointed at. – Steven Nov 10 '13 at 16:23
  • @Steven: I'm totally confused. So, in order to follow the practice you mentioned, what should I do? Should I use a bootstrapper file and call it from global.asax? Do you have an article/blog sampling this situation? – SherleyDev Nov 10 '13 at 16:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40908/discussion-between-steven-and-sherleydev) – Steven Nov 10 '13 at 16:44

1 Answers1

1

In my opinion the situation you have is exactly what you're looking for.

You don't want a reference structure like this: LibA => LibB => LibC => ...

In the end, what you want is one DLL/Project/component to tie all your projects together (known as the composition root) and have no dependencies between your components That way your domain layer does not depend on anything else.

It's OK to reference your data layer from your presentation layer (if that is where your composition root is).

Modules are useful if you want different composition roots for different parts of your application. A hypothetical example would be an administration area and a customer area. In case their dependencies are different enough you might want to use a composition root for each area, although they live in the same project.

Kenneth
  • 28,294
  • 6
  • 61
  • 84