Search code examples
entitydomain-driven-designaggregaterootvalue-objects

Domain Driven Design can an Aggragate Root be an Value Object


me and my colleges are discussing about Value Objects and Entities in the Context of Domain Driven Design. It is a little bit theoretical so maybe not that easy to Answer.

First question: Can an Aggregate Root be an Value Object ?

Second Question: In the following picture you can see an Domain Model. It contains Aggregate Roots and the related Value Objects which contain the IDs of the Aggregate Root.

Domain model diagram

Are they entities or value objects?

Which concept of identity do they have?

Which concept of identity do the members of the aggregates have?

Link to the article: https://www.mirkosertic.de/blog/2013/04/domain-driven-design-example/

Thanks for any help


Solution

  • Difference between value object and entity is, that you don't care about value object identity. It really doesn't matter whether you're operating on one or another value object, because, well... they whole purpose of existence is serving you only value. Entity however is distinguished between other entities and it matters whether you're referring to one or other, even if they properties have the same values.

    For example you could have following value object

    PersonalData
    {
        string Firstname;
        string Lastname;
    }
    

    and entity like this

    Person
    {
        PersonalData personData;
    }
    

    It doesn't matter if you are using one instance of value object or the other since everything you care about is the value itself. On the other hand, you will care about which person are you operating on. Even if two objects have the same Firstname and Lastname values they can be completely different entities!

    So, answering your question - there is no point in value object being aggregate root since aggregates are distinguished between themselves by unique ID and value object doesn't care about identity at all. Please keep in mind that I'm not talking about technical ID (used for example to store value object in some persistence store), but about some domain-relevant ID.

    As for your second question, providing such value objects helps you follow your domain experts stories more closely in your code, since he (or she) would not say get client by his id which is some string, but most probably will say something like get client by client id. Whole purpose of DDD is your code being close representation of domain it supports.