开发者

How to model Aggregates using Entity Framework?

While I have been dealing with domain-driven design (DDD) for quite some time now, I'm relatively new to Entity Framework (EF), and one question that came to my mind when using the Entity Framework Designer in Visual Studio was how Aggregates should be represented/modeled in EF.

Following DDD best practices, Entities should only reference other Entities (or Value Objects) within the same Aggregate, and references to other Entities are restricted to Root Entities of Aggregates (Aggregate Roots). However, I don't see any of these concepts present in EF (i.e., all Entities are treated alike, and consequently no restrictions are applied on referen开发者_如何转开发ces between Entities).

Thus, I'm asking: did I miss something in EF, or is it completely agnostic about Aggregates, Aggregate Roots and references between Entities? If the latter is the case, how do you model Aggregates when using Entity Framework?


I think DDD is other level of abstraction so my answer is no EF by default does not follow these practicies. It is up to you to model your entities and repositories to follow DDD. You will use repositories to build your aggregate root with loaded related entities related only to current aggregate root and you will use domain services to work with different repositories.


I just wanted to correct a small (but pretty important detail):

You state that "Entities should only reference other Entities (or Value Objects) within the same Aggregate".

Of course, there could be some arguments for this, but it's stricter than what at least one resource on DDD recomends: "Objects inside an Aggregate should be allowed to hold references to roots of other Aggregates." ("Domain-Driven Design Quickly" by Avram & Marinescu).

Best regards, Simon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜