DDD - 构建存储库

发布于 2024-09-30 15:33:19 字数 298 浏览 2 评论 0原文

如果我有一个设计用于获取聚合根(由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

且行且努力 2024-10-07 15:33:19

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 with Complete method and map specific FetchingStrategy to it. Then you can use something like Repository.Find<ICompleteOrder>(orderIdentity) and get this entity as you specified in FetchingStrategy.

<逆流佳人身旁 2024-10-07 15:33:19

单一模型不可能适合报告、搜索和事务(工作的业务单元)行为。您的域模型应主要关注业务工作单元的事务行为(以及一些查询......例如(假设的例如)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/

瘫痪情歌 2024-10-07 15:33:19

不太清楚聚合根是什么意思。据我所知,存储库模式通常与属于域模型一部分的对象集群一起使用,在 DDD 中,这通常指的是 聚合。如何从存储库访问对象取决于您在应用程序中使用的数据访问层类型。我通常使用 NHibernate 作为 ORM 来管理我的类和数据库表之间的所有关系,因此当我实现存储库时,我可以使用属于我的域的任何对象并根据需要访问它们。

以下是使用不同对象的存储库的示例:

public interface IStoryRepository : IRepository<Story>
{
   List<Image> GetStoryImage(int id);
   List<FacebookUser> GetFbUserById(string id);
}

public class StoryRepository : Repository<Story>, IStoryRepository
{
   public List<Image> GetStoryImage(int id)
   {
       var criteria = Session.CreateCriteria(typeof(Image))
       .Add(Restrictions.Eq("Id", id))
       .SetResultTransformer(new DistinctRootEntityResultTransformer());
        return criteria.List<Image>() as List<Image>;
    }

    public List<FacebookUser> GetFbUserById(string id)
    {
       var criteria = Session.CreateCriteria(typeof(FacebookUser))
       .Add(Restrictions.Eq("Id", id))
       .SetResultTransformer(new DistinctRootEntityResultTransformer());

        return criteria.List<FacebookUser>() as List<FacebookUser>;
     }
}

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:

public interface IStoryRepository : IRepository<Story>
{
   List<Image> GetStoryImage(int id);
   List<FacebookUser> GetFbUserById(string id);
}

public class StoryRepository : Repository<Story>, IStoryRepository
{
   public List<Image> GetStoryImage(int id)
   {
       var criteria = Session.CreateCriteria(typeof(Image))
       .Add(Restrictions.Eq("Id", id))
       .SetResultTransformer(new DistinctRootEntityResultTransformer());
        return criteria.List<Image>() as List<Image>;
    }

    public List<FacebookUser> GetFbUserById(string id)
    {
       var criteria = Session.CreateCriteria(typeof(FacebookUser))
       .Add(Restrictions.Eq("Id", id))
       .SetResultTransformer(new DistinctRootEntityResultTransformer());

        return criteria.List<FacebookUser>() as List<FacebookUser>;
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文