N 层架构中的 Autofac 模块

Autofac Modules in N-Tier Architecture

目前我正在使用 Autofac 用于 IoC 和两个组合根(一个用于前端,一个用于后端)我注册并解析跨越 Service 的组件, BusinessData 层。

截至目前,我只有一个像 'AccountingModule'。现在我要向应用程序添加几个新模块,名称如 InventoryModule、...

我的问题是我应该将每个模块 类 拆分为多个层(解决方案 1)还是为每个模块单独设置所有层(解决方案 2)

解决方案一:

Service Layer

(AccountingMoudle, InventoryModule, ...)

Business Layer

(AccountingMoudle, InventoryModule, ...)

Data Layer

(AccountingModule, InventoryModule, ...)

方案二:

AccountingModule
(
 Service Layer,
 Business Layer,
 Data Layer
)

InventoryModule
(
 Service Layer,
 Business Layer,
 Data Layer
)

编辑 1

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

编辑 2 架构:

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

我知道这只是一个包含前端和后端部分的应用程序。 首先,你应该有一个组合根,否则你的设计会在某个时候失败。不管你有什么解决方案。

我们假设它们是两个不同的驱动程序应用程序(例如 Web 服务和网站)。

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

编辑: 其实你的问题是 "Are horizantal (solution 1) or vertical (solution 2) slices better ?" (Partition Your Application into Modules)

这篇 Horizontal vs Vertical slicing 文章解释得很好。它说

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

又来一个好东西article

编辑后,我看到您已经垂直实现了模块,所以继续垂直(解决方案 2)。

您不应该尝试将 Autofac 模块移动到库中,您可能正在尝试做同样的事情,这就是您首先问这个问题的原因。

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)

取决于注册的数量,如果它是一个非常小的数字那么你可以使用AccountingModuleInventoryModule

如果会计登记的数量多,库存的数量少,那么可以有AccountingServiceModule, AccountingBusinessModule, AccountingDataModuleInventoryModule。您可以从应用程序的每个子域(库存、帐户等)一个模块开始,然后根据需要拆分它们。