domain-driven-designaggregateroot

Explicit State Modeling in DDD


I've been looking managing Root Aggregate state/life-cycle and found some content about the benefits of using Explicit State Modeling or Explicit Modeling over State Pattern, it's much cleaner and I like how I can let explicit concepts of my domain handle their own behavior.

One of the things I read was this article that is influenced by Chapter 16 in "Patterns, Principles, and Practices of Domain-Driven Design - Scott Millett with Nick Tune" book (here's a code sample for the full example).

The problem is the idea is described very briefly and there is not much content around it and that appeared when I started to implement it and given that I am new to DDD, the concepts started to overlap, and here are some of the questions that I am hoping more experienced engineers in DDD would help or at least someone has interpreted the text better than me.

  1. Following the article's example, how would I retrieve a list of all doors (that are both open and closed), what Domain Entity would this result-set map to?
  2. If all the explicit states models are entities/aggregates, what would be the root aggregate?
  3. would it be normal that there is no reference between Root Aggregate and those explicitly modeled entities?
  4. And if the Aggregate Root (let's say a generic Door entity) returns an explicit state entity, how would the repository save it without exposing the state of the entity or aggregate?
  5. Or are all these explicit entities root of their own aggregate?

I am not expecting to get all the above answered, I am just sharing the thoughts that am stuck at, so you are able to see where I am standing, as there is too much ambiguity for me to share code but I hope the snippets from the article and the book can help.

A git repository or a sample project addressing how would other DDD components with Explicit modeling would be really helpful (I have checked a million repositories but 90% with no luck).

Note: I am not using CQRS

Example from Medium Article:

interface ClosableDoor 
{ 
    public function close(); 
}
// Explicit State. (third attempt) 
class CloseDoorService() 
{ 
    // inject dependencies
   
    public function execute($doorId) 
    {
        $door = $this->doorRepository->findClosableOfId($doorId);
        if (!$door) { 
            throw new ClosableDoorNotFound(); 
        }
        $door = $door->close(); 
        $this->doorRepository->persist($door); 
    }
}

Example from the book:

// these entities collectively replace the OnlineTakeawayOrder entity (that used the state pattern)
public class InKitchenOnlineTakeawayOrder
{
 public InKitchenOnlineTakeawayOrder(Guid id, Address address)
 {
 ...
 this.Id = id;
 this.Address = address;
}
 public Guid Id { get; private set; }
 public Address Address { get; private set; }
 // only contains methods it actually implements
 // returns new state so that clients have to be aware of it
 public InOvenOnlineTakeawayOrder Cook()
 {
 ...
 return new InOvenOnlineTakeawayOrder(this.Id, this.Address);
 }
}
public class InOvenOnlineTakeawayOrder
{
 public InOvenOnlineTakeawayOrder(Guid id, Address address)
 {
 ...
 this.Id = id;
 this.Address = address;
 }
 public Guid Id { get; private set; }
 public Address Address { get; private set; }
 public CookedOnlineTakeawayOrder TakeOutOfOven()
 {
 ...
 return new CookedOnlineTakeawayOrder(this.Id, this.Address);
 }
}

Solution

  • Note: I am not using CQRS

    I think this is the biggest challenge you have.

    Retrieving explicitly modelled entities for the purpose of the use case being implemented would not cause such a headache if you were not also trying to use them for queries that may not be constrained to an explicit model designed for a specific use case.

    I use Entity Framework which supports "table-splitting" that could help in this situation. Using this, many entities can be mapped to the same table but each can deal with a subset of the fields in the table and have dedicated behaviour.

    // Used for general queries

    class Door
    {
        public Guid Id { get; private set; }
        public State State { get; private set; }
    
        // other props that user may want included in query but are not
        // relevant to opening or closing a door
        public Color Color { get; private set; }
        public Dimensions Dimensions { get; private set; }
        public List<Fixing> Fixings { get; private set; }
    }
    
    class DoorRepository
    {
        List<Door> GetDoors()
        {
            return _context.Doors;
        }
    }
    

    // Used for Open Door use case

    class ClosedDoor
    {
        public Guid Id { get; private set; }
        public State State { get; private set; }
    
        public void Open()
        {
            State = State.Open;
        }
    }
    
    class ClosedDoorRepository
    {
        List<ClosedDoor> GetClosedDoors()
        {
            return _context.ClosedDoors.Where(d => d.State == State.Closed);
        }
    }
    

    // Used for Close Door use case

    class OpenDoor
    {
        public Guid Id { get; private set; }
        public State State { get; private set; }
    
        public void Close()
        {
            State = State.Closed;
        }
    }
    
    class OpenDoorRepository
    {
        List<OpenDoor> GetOpenDoors()
        {
            return _context.OpenDoors.Where(d => d.State == State.Open);
        }
    }