DDD - 构建存储库
如果我有一个设计用于获取聚合根(由 Eric Evans DDD 定义)的存储库,例如一个订单实体(根聚合),它将以 OrderLine 对象作为子项。
在某些情况下,我只想检索顶级对象,即没有 OrderLines 的 Order,而在其他情况下,我想带回更多一点,可能下降 2 个级别等,即 Order 和关联的 OrderLines。
Order / OrderLine 场景是一个简单的示例,但如果我的聚合根比这更深,可能会下降 3 或 4 个级别,该怎么办?
将其构建到存储库中的最佳/可接受的方法是什么(使用急切加载)?
If I have a Repository designed for obtaining my Aggregate Root (as defined by Eric Evans DDD) e.g. an Order Entity (the Root Aggregate) which would have OrderLine objects as children.
In some cases I just want to retrieve the top level object i.e. the Order without the OrderLines and on other occasions I would like to bring back a bit more, maybe down 2 levels etc. i.e. the Order and the associated OrderLines.
The Order / OrderLine scenario is a simple example but what if my Aggregate Root was deeper than this, possibly going down 3 or 4 levels.
What is the best / accepted way of building this into the Repository (using eager loading)??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Udi Dahan 在 TechEd 2008 上谈论了意向接口。在他的演讲中,他谈到了如何有意识地从存储库中获取实体。您可以观看他的演示或幻灯片。
背后的想法是,您根据想要完成的任务来获取实体。例如,如果您想完成订单,则可以使用
Complete
方法创建接口ICompleteOrder
并将特定的FetchingStrategy
映射到它。然后,您可以使用Repository.Find(orderIdentity)
之类的内容并按照您在FetchingStrategy
中指定的方式获取此实体。Udi Dahan talked about Intentional Interfaces at TechEd 2008. In his presentation he talked about how to fetch entities from repository intentionally. You can watch his presentation or slides.
The idea behind is that you fetch entities based on what to you want to accomplish. For example if you want to complete order then you create interface
ICompleteOrder
withComplete
method and map specificFetchingStrategy
to it. Then you can use something likeRepository.Find<ICompleteOrder>(orderIdentity)
and get this entity as you specified inFetchingStrategy
.单一模型不可能适合报告、搜索和事务(工作的业务单元)行为。您的域模型应主要关注业务工作单元的事务行为(以及一些查询......例如(假设的例如)Customer.allAccounts())。
我建议您在此处查看名为 CQRS 的 DDD 扩展思维过程 http://gojko.net/2010/06/11/evolution-of-ddd-cqrs-and-event-source/
A single model cannot be appropriate for reporting, searching, and transactional (business unit of work) behaviors. Your domain model should mainly focus on transactional behavior of a business unit of work (and also some queries...say (hypothetical e.g) Customer.allAccounts()).
I suggest you look at extended thought process on DDD called CQRS here http://gojko.net/2010/06/11/evolution-of-ddd-cqrs-and-event-sourcing/
不太清楚聚合根是什么意思。据我所知,存储库模式通常与属于域模型一部分的对象集群一起使用,在 DDD 中,这通常指的是 聚合。如何从存储库访问对象取决于您在应用程序中使用的数据访问层类型。我通常使用 NHibernate 作为 ORM 来管理我的类和数据库表之间的所有关系,因此当我实现存储库时,我可以使用属于我的域的任何对象并根据需要访问它们。
以下是使用不同对象的存储库的示例:
Not quite sure what do you mean by Aggregate Root. As far as I know the Repository Pattern usually works with a cluster of objects that are part of your domain model, in DDD this usually refers as aggregates. How do you access you objects from a repository depends on what kind of data access layer you use in you application. I usually use NHibernate as and ORM that manages all the relationships between my classes and database tables, so when I implement a repository I can use any objects that are a part of my domain and access them as needed.
Here is an example of a repository that uses different objects: