Entity in DDD

  
    

public class Patient
{
    public int Id { get; private set; }
    public string Name { get; private set; }
    public DateTime DateOfBirth { get; private set; }
    public Address Address { get; private set; }

    public Patient(int id, string name, DateTime dateOfBirth, Address address)
    {
        Id = id;
        Name = name;
        DateOfBirth = dateOfBirth;
        Address = address;
    }
}

public class Address
{
    public string Street { get; private set; }
    public string City { get; private set; }
    public string State { get; private set; }
    public string PostalCode { get; private set; }

    public Address(string street, string city, string state, string postalCode)
    {
        Street = street;
        City = city;
        State = state;
        PostalCode = postalCode;
    }
}



 

  • 🔸 In this example, Patient is an entity with a distinct identity (Id) and attributes that define it.
  • .
  • 🔸 Address is a value object that represents an address based on its attributes, without a distinct identity.


Identity: Identity: Entities have a distinct identity that differentiates them from other entities, typically represented by an ID or key.

Equality: Implementations of Equals and GetHashCode methods are provided to ensure proper value-based equality comparisons. This is crucial because value objects are compared based on their attribute values rather than references.

Lifecycle: Entities have mutable state, meaning their attributes can change over time while preserving their identity.

Entities have a lifecycle, meaning they are created, updated, and eventually deleted from the system.

References:Entities are referenced by identity, meaning equality is based on identity rather than attribute values.

Comments