domain-driven-designentitiesvalue-objects

Actual difference between Value Objects and Entities. DDD


Domain-Driven Design, I'm trying to figure out what the difference some kind of mathematically and not intuitively. In my current project, we have some kind of payment transfers between banks. In this system, the bank is carried not like a physical building but an abstract bank. It has BankCode for sure, country and Bank Name. And the question is: Is the bank should be an entity or value object. I intuitively feel that it should be entity, but some people say that it could be like a value object.

As I know, entities MIGHT have the same attributes but still be different. On the other hand, value objects must not have an identity, they should be the same in case all attributes are the same, they should answer what they are, not who or which they are. Please If I'm wrong - correct me.

In the system, banks are not changing during any flow, like another Value object Country or Currency, probably we could say, that banks with the same bank code, name, and country are the same banks. As well as we can say that Country with the same country code (ISO format) is the same. But still, for me, I feel like they are entities. Could someone prove where I'm wrong, and give mathematic proofs that they should be either Value object or Entities.

Currently, I end up with that: "The one difference between Entity and Value Object is the entity can contain the all same attributes and still be different, the Value Object can't", like transaction and people can have same amounts and goods, same names, but still be different, and like address should be the same if the country, city, and house number are the same. Please correct me, and maybe there are more differences


Solution

  • The classic example difference between the two, is based on the context (as DDD always is), so lets go for it...

    Classic Example: An airplane flight (allocated seat or any seat)...

    Entity

    Each seat has a number, and you allocate a passenger a specific seat (Seat: 10 E)

    Value Object

    The plane has a number of seats, and you give a passenger 1 (or more) seat(s), on the plane (non specific, sit anywhere).

    The key question is, do you care about the individual entity (a seat) or just the total number of available seats?

    You can see the DDD - context separation here, even if you allocate seats, not every context is going to care. If I'm doing a capacity piece, I don't care about the entities, I just want to know

    var unused = available - used.
    

    Your initial Question

    Simple Answer

    It's an entity

    Deeper Answer

    It depends on the context.

    And then...

    In some contexts it may be a Domain Model in itself, like your context.

    If I want to move money from Bank1 and Bank2 then, I'm going to do...

    
    // Both domain models
    Bank sendingBank = _bankStore.Get(fromBank.Id);
    Bank receivingBank = _bankStore.Get(toBank.Id);
    
    _moneyTransfer(sendingBank, receivingBank, amount);
    
    

    Or alternatively

    Calling code

    Customer customer = _customerStore.Get(customerId);
    
    var balanace = customer.GetBalance();
    

    Entity Version: Customer

    public class Customer {
        public Bank {get; private set;}
    
        public Decimal GetBalance () {
           return this.Bank.GetBalance();
        }
    }
    
    

    Value Object Version: Customer

    public class Customer {
       public Balance {get; private set;}
    
       public Customer (..., decimal bankBalance) {
          this.Balance = bankBalance;
       }
    }