Search code examples
c#dependency-injectioninversion-of-controlautofacautofac-module

Autofac Modules in N-Tier Architecture


Currently I'm using Autofac for IoC and at two composition roots (one for the front-end and one for the back-end) I register and resolve the components spanned across Service, Business and Data layers.

As of now I have a single one like 'AccountingModule'. Now I'm going to add several new Modules to the application, with a name like InventoryModule, ...

My question is should I split each module classes among the layers (Solution 1) or have all layers separately for each module (Solution 2)

Solution 1:

Service Layer

(AccountingMoudle, InventoryModule, ...)

Business Layer

(AccountingMoudle, InventoryModule, ...)

Data Layer

(AccountingModule, InventoryModule, ...)

or

Solution 2:

AccountingModule
(
 Service Layer,
 Business Layer,
 Data Layer
)

InventoryModule
(
 Service Layer,
 Business Layer,
 Data Layer
)

Edit 1

+-----------------------------+                              +----------------------------+
+--+AccountingServiceComponent                               +-+InventoryServiceComponent
|                                      Weak Dependency       |
+--+AccountingBusinessComponent      <------------------+    +-+InventoryBusinessComponent
|                                                            |
+--+AccountingDataComponent                                  +-+InventoryDataComponent
       +                                                         +
       +-+ GetDocumentByID(int id)                               +--+GetProductByID(int id)
       |                                                         |
       +-+ SaveDocument(Document d)                              +--+SaveProduct(Product p)

Edit 2 Architecture:

enter image description here


Solution

  • Currently I'm using Autofac for IoC and at two composition roots (one for the front-end and one for the back-end)

    I understand this is just one application which has front-end and back-end parts. First of all you should have one composition root or your design will fail at some point. It doesn't matter which solution you have.

    Let's asssume they are two different driver applications (like web service and web site).

    My question is should I split each module classes among the layers (Solution 1) or have all layers separately for each module (Solution 2)

    Edit: Actually your question is "Are horizantal (solution 1) or vertical (solution 2) slices better ?" (Partition Your Application into Modules)

    This Horizontal vs Vertical slicing article explains it well. And it says

    Go vertical when you can. And horizontal when you have too.

    Here is another good article

    After your edit, I see you have already implemented your modules as vertically so go on vertically (solution 2).