Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 720
Mohsen Afshin Avatar asked Apr 20 '16 06:04

Mohsen Afshin


2 Answers

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).

like image 54
Erkan Demirel Avatar answered Nov 05 '22 03:11

Erkan Demirel


You should not try move Autofac modules to libraries, You probably are trying to do the same and that's why you are asking this question in the first place.

If you have multiple composition roots, then you should create the modules in each one. There is a good chance that different composition roots might use different modules. Further you probably don't want to add Autofac as a reference to every library in your solution..

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

Depends on number of registrations, if it's a very small number then you could just use AccountingModule and InventoryModule.

If there are a lot of registrations in accounting but small number in inventory, then you could have AccountingServiceModule, AccountingBusinessModule, AccountingDataModule and InventoryModule. You can start with one module per sub domain (inventory,account,etc..) of the application and split them as needed.

like image 1
Low Flying Pelican Avatar answered Nov 05 '22 01:11

Low Flying Pelican