【发布时间】:2020-03-30 01:53:54
【问题描述】:
我有以下问题: 我有一个具有多层一对多关系的聚合根。
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);
}
控制器
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
}
}
通过根访问
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 将不得不支持Create,Delete - 这里不会发生太多事情Child 必须支持 Create,Delete - (重点将放在 GET
,类似于GET 5 children starting from index=10
这里)Grandchildren 必须支持完整的 CRUD 并在 Update 上进行大量密集工作。GrandChildren 的表大小将是 >>>>> 所有其他的组合。每个 Grandchild 将有一个纯文本柱子。
当我说table 或column 时,我指的是典型 SQ L 数据库中的等价物
【问题讨论】:
-
你好,我觉得这篇文章可以回答你的问题ayende.com/blog/96257/document-based-modeling-auctions-bids
-
你想支持什么样的查询?一般来说,除了上面的注释之外,您还可以创建递归地图索引来查询关系或使用较新版本的 Graph API
-
好吧,我将不得不支持
create,delete支持Parent,create,delete,get支持Child和Create,Delete,Update and many others支持Grandchild。正如我所说,密集的工作将在@ 上完成987654352@ ,以及GrandChildren>>>> 所有其他的表(尚不知道ravenDB中的等价物)大小。孙子将被集中编辑。
标签: asp.net-core domain-driven-design repository-pattern ravendb data-access-layer