我有以下问题: 我有一个具有多层一对多关系的聚合根。
Root -> has many
Child -> has many
GrandChild
我有 Controller
\s来处理聚合根每一层上完成的逻辑。
我不知道如何处理数据访问层。
我应该为聚合根创建一个存储库,并通过它处理所有 Child
和 GrandChild
操作,还是为每个级别创建一个存储库就可以了?
此外,在我的例子中,GrandChildren
实体占用了大量空间(它们包含文本),因此我将使用文档数据库 - RavenDB
。
public class Root
{
public int ID{get;set;}
public IEnumerable<Child>Children;
}
public class Child
{
public int ChildID{get;set;}
public IEnumerable<Child>GrandChildren; //occupy a loot of space !
}
public class GrandChild
{
public int GrandChildID{get;set;}
}
public interface IGenericRepository<T>
{
bool Add<T>(T newValue);
T Get<T>(int id);
IEnumerable<T> GetAll();
bool Delete(int id);
bool Update<T>(T value);
}
Controller
public class ParentController
{
IGenericRepository<Root> repo;
public IActionResult<Root> Get(int rootId)
{
return this.repo.Get(rootId);
}
}
public class ChildControiller_V1
{
IGenericRepository<Child>repo;
public IActionResult<Child> Get(int childid)
{
this.repo.Get(childid); //the id is unique
}
}
通过root访问
public class RootRepository:IGenericRepository<Root>
{
/// implementations
public IGenericRepository<Child> GetChildRepository()
{
return //some implementation of IGenericRepository for Child
}
}
public class ChildController_V2
{
IGenericRepository<Root>repo;
public IActionResult<Child> Get(int rootId,int childid)
{
var root=this.repo.Get(rootId);
var childRepo=root.GetChildRepository();
var get= childRepo.Get(childId);
}
}
我希望你明白了。对于更多的层,我会一直这样做。考虑到最低的实体与其他实体相比占据了很多空间,什么是一个好的方法?
更新
Root
必须支持创建
、删除
- 这里没有发生太多事情
Child
必须支持 Create
、Delete
-(重点将放在 GET
,例如从index=10开始获取5个 child
这里)
Grandchildren
必须支持完整的 CRUD
,并在 Update
上进行大量密集工作。他们的 GrandChildren
表大小将>>>>> 所有其他组合。每个 孙
将有一个纯文本列。
当我说表
或列
时,我指的是典型SQL数据库中它们的等价物
最佳答案
从(经典)DDD 的角度来看,存储库返回完全物化的聚合,其中聚合代表一致性/事务边界。拥有子存储库意味着你放弃了它,这也是 DDD 的一大好处。话虽这么说,您需要确定一致性边界在哪里。实体之间是否存在约束?如果没有,它们可以有自己的聚合。请记住,聚合不会出现在其他聚合中,并且来自实体的引用只能转到聚合根,而不是另一个聚合层次结构中的其他实体。
我已经在这里提到了我的回答中可能有趣的其他几点,特别是数据太多时的方向。 https://stackoverflow.com/a/59189413/2613363
最后,我想说 Raven 有版本(etag)。如果您最终更新了许多子项目,请使用它们。
关于asp.net-core - 如何处理多层一对多关系的DAL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59189966/