I have two domain objects, a Parent
and Child
. A Parent
can have zero or more children, and a Child
can belong to zero or more parents (in case of divorced parents).
So thinking about this in the context of Domain Driven Design I'd say that both Parent
and Child
are an AggregateRoot
. Because this is a many-to-many relationship.
Now I'm wondering how I should model this in code. I need to be able to access the parents from the Child
class, but also all the children from the Parent
class.
So I'm thinking about doing it like this:
public class Parent : AggregateRoot
{
public string FamilyName {get;set;}
public IList<Guid> ChildrenIds {get;set;}
}
public class Child : AggregateRoot
{
public string Name {get;set;}
public IList<Guid> ParentIds {get;set;}
}
Is it OK to have both classes reference each other via their id
's?
Or is there perhaps a better way to model this?
Background info
Parents and their children can be registered in our application. Our system then keeps a log of events that that child goes through. Swimming, going to the museum etc.
For example;
- a mom and her only child are registered in our application.
- In the mean time events are logged to the child entity. Visited a museum, went swimming etc.
- Two months later Dad needs to be registered in our system as well (coparenting). Mom and Das obviously should have a reference to the same child entity.
I need to be able to work with the following use case:
- On all even weeks of the year Mom is responsible for the child.
- On all uneven weeks Dad is responsible.
- If something happends on an even week (Mom), then this event will be registered.
- If the application later on retrieves the Dad aggregate, then he should not only get a reference of the same child as Mom, but should also see all the events that happend the week before.
So therefor a Parent can have many Children, and a Child can hsve many Parents. My problem is that I'm not sure how to model this in DDD.