在总根中寻找一个孩子 [英] Searching for a Child across Aggregate Roots

查看:39
本文介绍了在总根中寻找一个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储库模式建议您只能拉聚合根。但是,如果您不知道单亲孩子的父母(根),您将如何仅使用其唯一身份(Child.ID)来检索它呢?

The repository pattern suggest that you can only pull aggregate roots. But how would you retrieve a single child using only it's uniqiue identity(Child.ID) if you do not know it's parent(root)?

class Parent
{
    public int ID { get; set; }
    IEnumerable<Child> Children { get; private set; }
}

class Child
{
    public int ID { get; private set; }
    public virtual Parent Parent { get; private set; } // Navigational model
}

我的应用程序是无状态的(网络),为简单起见,该请求仅包含孩子的ID。

My application is stateless (web), for simplicity, the request only contains the ID of the child.

我正在考虑三种方法:


  1. 打电话给所有父母,然后礼貌地问他们是谁拥有这个孩子。

  2. 在ParentRepository中有一个名为getGetChildByID的特殊例程,这有点使存储库的抽象失败。

  3. 修改请求以包括父项,但由于您已经具有唯一标识,因此似乎不必要。


推荐答案

似乎您实际上在这里查看的是另一种有界上下文。您在问题中提到存储库...只能提取聚合根。 ;这是对的。另一个答案还提到,如果您需要查询子对象,则子对象也可能是聚合根。 在其他有界上下文中,这可能也是正确的。

It seems likely that you're actually looking at a different bounded context here. You mentioned in your question that "repository ... can only pull aggregate roots."; this is correct. Another answer also mentions that if you need to query a child object, the child object may also be an aggregate root. This may also be correct within a different bounded context. It's quite possible for an entity to be an aggregate root in one context, and a value entity in another.

例如,一个 Users的域是一个实体在一个上下文中是一个聚合根,而在另一个上下文中是一个价值实体。 和他们已在其设备上安装的移动/平板电脑 Apps 。在用户的上下文中,我们可能需要用户的基本属性,例如姓名,年龄等,并且还可能需要用户在其设备上安装的应用程序的列表。在这种情况下, User 是聚合根,而 App 是值对象。

Take for example the domain of Users and the mobile/tablet Apps they have installed on their devices. In the context of the user, we might want the users basic properties such as name, age etc, and we might also want a list of apps the user has installed on their device. In this context User is the aggregate root and App is a value object.

bounded context UserApps
{
    aggregate root User
    {
        Id : Guid
        Name : string
        Age : int
        InstalledApps : App list
    }

    value object App
    {
        Id : Guid
        Name : string
        Publisher : string
        Category : enum
    }
}

在另一种情况下,我们可以以 App 为中心的世界观,并确定 App 是总根。例如,我们要报告哪些用户已安装给定应用程序。

In another context we may take an App centric view of the world and decide that App is the aggregate root. Say for example we wanted to report which users have installed a given app.

bounded context AppUsers
{
    aggregate root App
    {
        Id : Guid
        Name : string
        InstalledBy : User list
    }

    value object User
    {
        Id : Guid
        Name : string
        InstalledOn : Date
    }
}

这两个有界上下文都将拥有自己的存储库,该存储库将返回各自的聚合根。您对数据的看法存在细微但至关重要的差异。

Both of these bounded contexts would have their own repository which returns the respective aggregate root. There's a subtle but crucial difference in your perspective of the data.

我认为如果您退后一步,想想为什么为什么查询子对象,您可能会发现您实际上处于完全独立的有界上下文中。

I think if you take a step back and think about why you want to query for a child object, you might find that you're actually in an entirely separate bounded context.

这篇关于在总根中寻找一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆