19

I'm fairly new to DDD world and after reading couple of books about it (Evans DDD among them) I was unable to find the answer to my question on internet: what the proper way of creating child entities with DDD? You see, a lot of information on internet operates on some simple level. But devils in the details and they're always omitted in dozens of DDD samples for the sake of simplicity.

I'm coming from my own answer on similair question here on stackoverflow. I'm not completely satisfied with my own vision on this problem so I thought I need to elaborate on this matter.

For example, I need to create simple model that represent cars naming: company, model and modification (for example, Nissan Teana 2012 - that will be "Nissan" company, "Teana" model and "2012" modification).

The sketch of the model I want to create looks like this:

CarsCompany
{
    Name
    (child entities) Models
}

CarsModel
{
    (parent entity) Company
    Name
    (child entities) Modifications
}


CarsModification
{
    (parent entity) Model
    Name
}

So, now I need to create code. I'll use C# as language and NHibernate as ORM. This is important and what typically does not shown in vast DDD samples on the internet.

The first approach.

I'll start with simple approach with typical object creation via factory methods.

public class CarsCompany
{
    public virtual string Name { get; protected set; }
    public virtual IEnumerable<CarsModel> Models { get { return new ImmutableSet<CarsModel> (this._models); } }


    private readonly ISet<CarsModel> _models = new HashedSet<CarsModel> ();


    protected CarsCompany ()
    {
    }


    public static CarsCompany Create (string name)
    {
        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        return new CarsCompany
        {
            Name = name
        };
    }


    public void AddModel (CarsModel model)
    {
        if (model == null)
            throw new ArgumentException ("Model is not specified.");

        this._models.Add (model);
    }
}


public class CarsModel
{
    public virtual CarsCompany Company { get; protected set; }
    public virtual string Name { get; protected set; }
    public virtual IEnumerable<CarsModification> Modifications { get { return new ImmutableSet<CarsModification> (this._modifications); } }


    private readonly ISet<CarsModification> _modifications = new HashedSet<CarsModification> ();


    protected CarsModel ()
    {
    }


    public static CarsModel Create (CarsCompany company, string name)
    {
        if (company == null)
            throw new ArgumentException ("Company is not specified.");

        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        return new CarsModel
        {
            Company = company,
            Name = name
        };
    }


    public void AddModification (CarsModification modification)
    {
        if (modification == null)
            throw new ArgumentException ("Modification is not specified.");

        this._modifications.Add (modification);
    }
}


public class CarsModification
{
    public virtual CarsModel Model { get; protected set; }
    public virtual string Name { get; protected set; }


    protected CarsModification ()
    {
    }


    public static CarsModification Create (CarsModel model, string name)
    {
        if (model == null)
            throw new ArgumentException ("Model is not specified.");

        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        return new CarsModification
        {
            Model = model,
            Name = name
        };
    }
}

The bad thing about this approach is that creation of the model does not adding it to the parent models collection:

using (var tx = session.BeginTransaction ())
{
    var company = CarsCompany.Create ("Nissan");

    var model = CarsModel.Create (company, "Tiana");
    company.AddModel (model);
    // (model.Company == company) is true
    // but (company.Models.Contains (model)) is false

    var modification = CarsModification.Create (model, "2012");
    model.AddModification (modification);
    // (modification.Model == model) is true
    // but (model.Modifications.Contains (modification)) is false

    session.Persist (company);
    tx.Commit ();
}

After the transaction is committed and the session is flushed, the ORM will correctly write all into the database and next time we load that company it's models collection will correctly holds our model. The same goes to modification. So this approach leaves our parent entity in inconsistent state until it's been reload from database. No go.

The second approach.

This time we'll gonna use language specific option to solve the problem of setting protected properties of other classes - namely we'll gonna use "protected internal" modifier on both setters and constructor.

public class CarsCompany
{
    public virtual string Name { get; protected set; }
    public virtual IEnumerable<CarsModel> Models { get { return new ImmutableSet<CarsModel> (this._models); } }


    private readonly ISet<CarsModel> _models = new HashedSet<CarsModel> ();


    protected CarsCompany ()
    {
    }


    public static CarsCompany Create (string name)
    {
        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        return new CarsCompany
        {
            Name = name
        };
    }


    public CarsModel AddModel (string name)
    {
        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        var model = new CarsModel
        {
            Company = this,
            Name = name
        };

        this._models.Add (model);

        return model;
    }
}


public class CarsModel
{
    public virtual CarsCompany Company { get; protected internal set; }
    public virtual string Name { get; protected internal set; }
    public virtual IEnumerable<CarsModification> Modifications { get { return new ImmutableSet<CarsModification> (this._modifications); } }


    private readonly ISet<CarsModification> _modifications = new HashedSet<CarsModification> ();


    protected internal CarsModel ()
    {
    }


    public CarsModification AddModification (string name)
    {
        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        var modification = new CarsModification
        {
            Model = this,
            Name = name
        };

        this._modifications.Add (modification);

        return modification;
    }
}


public class CarsModification
{
    public virtual CarsModel Model { get; protected internal set; }
    public virtual string Name { get; protected internal set; }


    protected internal CarsModification ()
    {
    }
}

...

using (var tx = session.BeginTransaction ())
{
    var company = CarsCompany.Create ("Nissan");
    var model = company.AddModel ("Tiana");
    var modification = model.AddModification ("2011");

    session.Persist (company);
    tx.Commit ();
}

This time each entity creation leaves both parent and child entity in consistent state. But validation of the child entity state leaked into the parent entity (AddModel and AddModification methods). Since I'm nowhere expert in DDD I'm not sure if it's ok or not. It could create more problems in future when child entities properties could not be simply set via properties and setting up some state based on passed parameters would require more complex work that assigning parameter value to property. I was under impression that we should concentrate logic about entity inside that entity wherever it's possible. For me this approach turns parent object into some kind of Entity&Factory hybrid.

The third approach.

Ok, we'll gonna invert the responsibilities of maintaining parent-child relations.

public class CarsCompany
{
    public virtual string Name { get; protected set; }
    public virtual IEnumerable<CarsModel> Models { get { return new ImmutableSet<CarsModel> (this._models); } }


    private readonly ISet<CarsModel> _models = new HashedSet<CarsModel> ();


    protected CarsCompany ()
    {
    }


    public static CarsCompany Create (string name)
    {
        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        return new CarsCompany
        {
            Name = name
        };
    }


    protected internal void AddModel (CarsModel model)
    {
        this._models.Add (model);
    }
}


public class CarsModel
{
    public virtual CarsCompany Company { get; protected set; }
    public virtual string Name { get; protected set; }
    public virtual IEnumerable<CarsModification> Modifications { get { return new ImmutableSet<CarsModification> (this._modifications); } }


    private readonly ISet<CarsModification> _modifications = new HashedSet<CarsModification> ();


    protected CarsModel ()
    {
    }


    public static CarsModel Create (CarsCompany company, string name)
    {
        if (company == null)
            throw new ArgumentException ("Company is not specified.");

        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        var model = new CarsModel
        {
            Company = company,
            Name = name
        };

        model.Company.AddModel (model);

        return model;
    }


    protected internal void AddModification (CarsModification modification)
    {
        this._modifications.Add (modification);
    }
}


public class CarsModification
{
    public virtual CarsModel Model { get; protected set; }
    public virtual string Name { get; protected set; }


    protected CarsModification ()
    {
    }


    public static CarsModification Create (CarsModel model, string name)
    {
        if (model == null)
            throw new ArgumentException ("Model is not specified.");

        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("Invalid name specified.");

        var modification = new CarsModification
        {
            Model = model,
            Name = name
        };

        modification.Model.AddModification (modification);

        return modification;
    }
}

...

using (var tx = session.BeginTransaction ())
{
    var company = CarsCompany.Create ("Nissan");
    var model = CarsModel.Create (company, "Tiana");
    var modification = CarsModification.Create (model, "2011");

    session.Persist (company);
    tx.Commit ();
}

This approach got all validation/creation logic inside corresponding entities and I don't know if it's good or bad, but by simple creation of the object with factory method we implicitly adding it to the parent object children collection. After transaction commit and session flush there will be 3 inserts into the database even tho I never written some "add" command in my code. I don't know maybe it's just me and my vast experience outside of DDD world but it feels a bit unnatural for now.

So, what's most correct way of adding child entities with DDD?

3
  • I could remove all code examples and make this whole question a lot harder to understand - but I don't think it's gonna help community. I was not aware that SO is not for complex questions. Commented Jun 13, 2012 at 20:15
  • The whole question (with or without code) is inappropriate here. As I said, it asks for discussion. Read the links I posted. I can provide another one: Not a discussion board or forum. As I said before, this site is for clear, concise questions that can be answered without discussion. Since your entire question is about discussing "the most correct way" and asking to "hear oppinions[sp]" (which is also not appropriate here), it's not a proper fit here.
    – Ken White
    Commented Jun 13, 2012 at 21:38
  • 12
    I'm asking pretty much concrete question and awaiting for a very particular answer to it. With my examples I merely show the research efforts I put into this matter. I don't need any discussion - I need clear answer how to do this thing right. Just because you don't know answer to this question does not mean it has discussion nature. DDD world use many patterns and I'm asking to point me to one that I should apply to solve this particular problem. Commented Jun 14, 2012 at 6:51

4 Answers 4

3

So, what's most correct way of adding child entities with DDD?

The third approach is called Tight Coupling. Company, Car and Modification know almost everything about each other.

The second approach is widely proposed in DDD. An domain object is responsible for creating a nested domain object AND registering it inside.

The first approach is classic OOP style. Creation of an object is separated from adding an object into some collection. This way code consumer can substitute an object of a concrete class (e.g. Car) with an object of any derived class (e.g. TrailerCar).

// var model = CarsModel.Create (company, "Tiana");

var model = TrailerCarsModel.Create (
    company, "Tiana", SimpleTrailer.Create(company));

company.AddModel (model);

Try adopting this business logic change in the 2nd / 3rd approach.

3

I've got acceptable answer here: https://groups.yahoo.com/neo/groups/domaindrivendesign/conversations/messages/23187

Basically, it's a combination of method 2 and 3 - put AddModel method into the CarsCompany and make it call protected internal constructor of the CarsModel with name parameter which is validated inside the CarsModel's constructor.

7
  • Interesting, but where do add objects to parent and child collections?
    – Erik Z
    Commented Feb 13, 2014 at 14:34
  • In the parent .AddChild method, which creates child by protected internal constructor (passing self as parameter for child to set it's parent in constructor), adds it to child collection and returns it. Commented Feb 13, 2014 at 17:49
  • Thanks, that make sense. Have you used this pattern in conjunction with Entity Framework?
    – Erik Z
    Commented Feb 14, 2014 at 5:12
  • 1
    Actually I moved away from DDD because I realized that scale and CRUD nature of typical web applications are not warranted the overhead of using pure DDD and forgetting about you're working with real DB (which are slow and all current ORMs are not that effective). Commented Feb 14, 2014 at 6:31
  • 3
    Error Description: Could not process this "GET" request. This is StackOverflow 101 of why you should include the answer instead of linking to other sites. Commented Sep 11, 2014 at 15:21
1

Interesting. DDD vs Repository / ORM navigation properties. I think the answer depends on whether you are dealing with one aggregate or two. Should CarsModel be part of CarsCompany aggregate, or perhaps its own aggregate?

Approach one is to make the problem go away. MikeSW hinted at this. If CarsCompany and CarsModel don't need to be part of the same aggregate then they should only reference each other by identity, navigation properties should not be visible in the Domain.

Approach two is to treat adding to a relationship the same way we treat fetching an aggregate - make the Application Services call a method from the repository, which is correct place for your ORM-specific concerns to be addressed. Such a method could populate both ends of the relationship.

-1

Here's a very concrete and brutally honest answer: all your approaches are wrong, because you broke the 'first rule' of DDD, that is, the DB does not exist.

What you're defining is PERSISTENCE model for an ORM (nhibernate). In order to design the domain objects, first you have to identify the Bounded Context, its Model,the Entities and Value objects for that model and the Aggregate Root (which will deal internally with the children and busines rules).

Nhibernate or db schema have no place here, you need only pure C# code and a clear understanding of the domain.

6
  • 6
    Yeah, thats all nice and sounds just like any DDD book into but the problem here is not about designing the domain. The problem in implementing it. In development we have a very specific patterns to solve a very specific problem - an problem of forging the domain into the working code. And I'm asking to point me what the correct way to implement it. I could write it with pure fictional language and it could work in that fictional world (I don't have problem with that). Commented Jun 15, 2012 at 12:53
  • 5
    The difficult thing IS designing the domain. In fact establishing the BC and the Aggreagates are the trickiest things. Once you know them (in pseduo code) it's just a formality to write them in C#. This is not theory, the hardest part of DDD is UNDERSTANDING the DOMAIN and having a very clear distinction of which is which. Also there is no silver bullet or a whatever pattern to apply. DDD is about concepts and guidelines. You want to implement some relations in Nhibernate. That's NOT DDD.
    – MikeSW
    Commented Jun 15, 2012 at 13:11
  • 2
    You need the DOMAIN EXPERT tohelp you understand the problem and the subtilities. Neither I or other people around here are the domain expert. You have to talk to the expert to clearly understand what is the problem to solve. And honestly without clearly knowing the BC you can't design or implement an Entity or an Aggregate.
    – MikeSW
    Commented Jun 15, 2012 at 13:15
  • 3
    MikeSW, why you don't take your time an put an example, instead of just talking. I shared FuriCuri doubts this matter, and everything is fine about the DDD, but when you have to put the hands in the code, everything change. And most important is very important how we DESING the DOMAIN in order to later desing the DAOs and Repositories. Is that what is asking furicuri, how to design POJOs properly for parent-child relations.
    – Victor
    Commented Nov 12, 2013 at 16:38
  • 2
    Downvote: question is about a common concern when doing DDD and using ORM. Stating 'I am not your domain expert' doesn't help much.
    – Chalky
    Commented Dec 24, 2016 at 2:15

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.