实体框架代码优先映射如何反映领域驱动设计?
我希望在新项目中使用实体框架代码优先。所以我决定做一些研究并构建一些演示,以便我可以看到它是如何工作的。通过,我有一个主要问题,或者可能还有更多我不清楚的问题,涉及实体框架代码优先映射到实体和领域驱动设计的方式。
当我们构建应用程序时,我们定义域实体。 (我们定义聚合根并根据我所听到的业务情况为它们创建存储库)
没关系,但是实体框架代码优先映射似乎像实体之间的关系方式一样工作。那么两者如何共存呢?
举个例子(在领域驱动设计方面思考):
Journal 包含 JournalEnty 包含 任务、问题、注释
斜体字是实体。在分析之后,我会以某种方式说日记是聚合日记和日记条目的聚合根,因为这是直接组合。每个任务都包含一个小时值,以了解完成任务需要多少小时,因此有很多方法可以计算总小时数以及由此产生的工资。该期刊具有小时费率属性。
其他实体都是聚合根,它们可能引用日记条目,以便我们知道任务、注释和问题所属的位置。
但问题来了..实体框架代码优先映射如何反映这一点?从直观的角度来看,我们会说日记包含日记条目,而日记条目包含注释、问题和任务。但从 DDD 来看,情况可能并非如此。如果我错了,请纠正我,但代码优先就像关系数据库一样工作。
那么我们如何在代码优先中映射上面的示例呢?
非常感谢。
I wish to use Entity Framework Code-first for a new project. So i decided to do some research and build some demo so that i can see how its doing. Through, i have a major problem or probably more something that isnt clear to me that involve the way entity framework code-first map to the entities and the domain driven design.
As we build an application, we define the domain entities. (we define aggregates roots and make repositories for them depending on the business situation from what ive heard)
That okay but Entity Framework Code-First mapping seem to work like a relational way between entities. So how both can coexist ?
As an example (Thinking at domain driven design side) :
Journal contains JournalEnty contains tasks, problems, notes
Italic words are entities. In some way after analysis i would say that the journal is the aggregate root of the aggregate journal and journalentry since this is a direct composition. Each tasks contain an hour value to know how much hours it taken to finish the tasks so there much be a way to calculate the total hours and also the salary coming from this. The journal have the hour rate property.
The others entities are each an aggregate root and they might have a reference to the journalentry so we know where the tasks, notes and problems belong.
But the problem come here.. how the Entity Framework Code-First mapping can reflect this ? From an intuitive view we would say that the journal contain a journal entry and that the journal entry contains notes, problems, and tasks. But from DDD view that probably not the case. Correct me if i am wrong but code-first work like a relational database.
So how we would map the example above in code-first ?
Thanks a lots.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为如果每个域实体在数据库中都有相应的表也不错。这并不意味着它是关系结构,因为 Journal 对象具有 JournalEntity 属性(在关系结构中,journalEntity 仅具有 JournalID)。
此外,可以将对象层次结构映射到一张表并在映射中创建复杂类型。这意味着您可以拥有比按表分类更复杂的映射场景。
这是 ScottGu 关于它的博客文章。
I think that it is not bad if each domain entity have corresponding table in the database. And it doesn't mean that it is relational structure as Journal object has JournalEntity property (in relational structure journalEntity just has JournalID).
Moreover it is possible to map objects hierarchy to one table and create Complex Types in your mappings. It means that you can have more complex mapping scenarios then class per table.
Here is ScottGu blog post about it.