How to define DDD Aggregate root for hierarchical data structure?

不羁岁月 提交于 2019-12-06 12:14:06

You should model your aggregates around invariants. One rule of thumb is that aggregate should be loaded in one go to memory (with all it's child objects) no lazy loading.

Do you really have an invariant that requires whole hierarchy to be loaded? Is there situation where you need to traverse all nodes in hierarchy? To answer this question you need to think about use cases of your aggregate.

If you need all data then your aggregate has proper size it just couldn't be smaller. If your invariant require only small portion of graph then maybe you are missing some domain concept that will describe this graph part.

If you care only about updating time of ancestors then perhaps your task can contain only Parent property you don't need child collection. Then you can perform things like

public void RegisterTime(TimeSpan time)
{
    TimeSpent += time;
    // maybe more stuff here
    Parent.RegisterTime(time)
}

Then your repository will get only Task with all of his ancestors and aggregate will be small enough.

I'm just guessing because it always depends of use cases.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!