9

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

Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • 5
    There should be exactly one Composition Root per end-application. In your case you only seem to have only one end-application, so you should have one composition root. – Steven Apr 20 '16 at 10:17
  • Related: https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application – Steven Apr 20 '16 at 10:18

2 Answers2

4

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

Erkan Demirel
  • 4,302
  • 1
  • 25
  • 43
  • Assuming A & B being our modules, would you please say how it's different from the solution 1? In my Solution 1, the different parts of a given module are placed in the respective layer and are accessed in the standard Service, Business, Data order. – Mohsen Afshin Apr 23 '16 at 12:11
  • I added a diagram of classes. I'm doubtful on the meaning of the module in your answer. Here by module, I mean the set of related classes with strong dependencies between themselves and weak dependency with other parts of the application. In my diagram there are only two of them, in reality there may be more than 10 or more modules. – Mohsen Afshin Apr 24 '16 at 08:13
  • I concluded with the proposed diagram. Now in terms of Autofac, Should I register all sub-layers of a subdomain (InventoryUIComponent, InventoryServiceComponent, InventoryBusinessComponent, InventoryDataComponent) as a single InventoryModule? There may be more such subdomains in the future and most of them may be optional for end-user. – Mohsen Afshin May 01 '16 at 06:09
  • You have answered yourself already. Some for end applications may be not use these implemantations or they use other implemantations. Even horizantal or vertical design; your classes shouldn't be coupled (just coupled interfaces (not implemantations). Check [here](http://stackoverflow.com/a/9503612/3432471) – Erkan Demirel May 01 '16 at 18:27
1

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.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43