3

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
}

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

I am thinking three approaches:

  1. Call all the parents then ask them politely who owns this child.
  2. Have a special routine in the ParentRepository called get GetChildByID, which kinda fails the repository's abstraction.
  3. Modify the request to include the parent, but seems unnecessary since you already have a unique identity.
6
  • 1
    Create a GetChildByID(..) method in a ChildRepository class? Nov 25, 2013 at 10:16
  • 5
    Child should be an Aggregate if you could identify them without parents. Nov 25, 2013 at 10:20
  • @Hippoom My knowledge of Aggregate Root is limited, are you saying that as long as an entity has a unique identity then they can be considered as a root? I have updated code sample.
    – Yorro
    Nov 25, 2013 at 10:23
  • 5
    The entity should be an aggregate if domain experts want to trace it alone. For example, assuming Domain Experts will not trace an orderline without the Order, then Order is an aggregate but orderLine is just a local entity. Even if technically, we give orderLine an unique id. Nov 25, 2013 at 10:31
  • @Hippoom +1 I think you've hit the nail on the head there. It could be that this child is an aggregate root in one bounded context and a value object in another context.
    – MattDavey
    Nov 26, 2013 at 8:16

3 Answers 3

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.

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
    }
}

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.

1
  • Sorry for the late reply by thanks. I will accept this as the closest answer that I need. I now understand that the aggregate root changes depending on the context. I also found a nice article that further helped my understanding on this matter. sapiensworks.com/blog/post/2012/04/18/…
    – Yorro
    Dec 16, 2013 at 9:10
0

If you require the child for display/reporting/viewing/reporting then a simple query layer will do.

If you are manipulating the child in any way then it has a consistency boundary and it sounds an awful lot like an aggregate.

Try not to query your domain objects. Another simple rule of thumb is not not include an aggregate reference in another aggregate but to rather use just the referenced aggregate's Id or even a value object representing the relationship.

0

Entity navigation is not a purpose of a domain model.
An Aggregate Root is a composition of Entities and Values that expose business operations.
As a side effect, you can still perform some simple query or navigation through your AR, but, for complex querying, creating and using a Query Model is more efficient.
I'm talking about CQRS.

Hope this can help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.