根据业务上下文基于同一实体的多个聚合根

问题描述 投票:0回答:1

我正在尝试使用 .Net C# 来实践 DDD 模式。我检查了多个灵魂、教程、文章,但大多数都使用相同的简单想法 - 订单实体,带有项目等。我想要理解的是什么,什么是适合下面案例的设计(为了讨论而过于简单化) .

假设我们有 3 个实体。

class Company
{
public int Id {get;set;}
public string Name {get;set;}
}

class BusinessService{
public int Id {get;set;}
public int CompanyId {get;set;}
public string Name {get;set;}
public double Price {get;set;}
}

class Vehicle{
public int Id {get;set;}
public int CompanyId {get;set;}
public string CarPlate {get;set;}
}

在这种情况下,公司可以拥有 BusinessServices,它应该用于创建发票和车辆,可以在 Web 应用程序中使用它来呈现与其公司绑定的公司用户车辆。作为 DDD 点,我应该创建包含 BusinessServices 和 Vehicles 的只读集合的 CompanyRootAggregate。但这没有多大意义,因为当我处理业务/财务范围时,我不需要有关车辆的信息,而当用户处理应用程序上下文时,他不需要有关业务服务的信息。根据调用上下文,将两个集合都放在根中是多余的。它会向应用程序的某些部分提供大量数据,更不用说数据库过载了。

应该采用什么方法来解决这个问题?应用程序是否应该划分为子域,在其中我们可以定义 CompanyAggretationRoot,该子域根据子域上下文而有所不同(即带有 BusinessService 集合的 CompanyBusinessAggregationRoot 和带有 Vehicle 集合的 CompanyVehicleAggregationRoot?)还有其他想法吗?

c# .net-core architecture domain-driven-design
1个回答
0
投票

两种解决方案,

-第一个解决方案拆分在不同的文件夹或项目中

您可以拆分多个 Web 应用程序(微服务),每个应用程序都有自己的聚合,但具有相同的数据库。

您可以拆分相同的项目,但按功能拆分

Compagnie.Domain
 / Business
    / Aggregates
       / CompanyBusinessAggregationRoot.cs
    / Commands
    / Queries
 / Vehicle
    /Aggregates 
       / CompanyVehicleAggregationRoot.cs
    / Commands
    / Queries

-出于性能目的的第二个解决方案,您可以使用具有不同包含的存储库,并且不会出于某种目的加载所有数据。 示例:将 ICompanyBusinessAggregationRoot 接口放在聚合上。 当您调用存储库时,您会执行

GetCompagny<ICompanyBusinessAggregationRoot>()
操作,并将包含内容放在由接口确定的 DbContext 上。 在可以使用接口访问命令或查询使用的数据之后,对 ICompanyBusinessAggregationRoot 执行相同的操作

public class CompanyAggregationRoot : ICompanyVehicleAggregationRoot, ICompanyBusinessAggregationRoot
© www.soinside.com 2019 - 2024. All rights reserved.