Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Domain Driven Design when an entity clones itself who adds it to its container?

For example, we have two domain objects: Cell and Body (as in human cell and body).

The Body class is just a collection of Cells, e.g.

class Body
{
    IList<Cell> cells;
    public void AddCell(Cell c) { ... }
    public void RemoveCell(Cell c) { ... }
}

The Cell has a Split method, which internally creates a clone of itself, e.g.

Class Cell
{
    public Cell Split()
    {
        Cell newCell = new Cell();
        // Copy this cell's properties into the new cell.
        return Cell;
    }
}

Now, in DDD when the cell splits should:

  1. The cell add the newly created cell to the Body (which would mean that each Cell object held a reference to its containing body)?
  2. Or should the service layer which received the intitial user request call Split, collect the returned Cell and add it to the Body? (feels like a more anemic design using controllers rather than domain objects)
  3. Or should the Body contain a SplitCell method?

Thanks in advance.

like image 631
ng5000 Avatar asked Jun 02 '09 08:06

ng5000


People also ask

What is the main idea of Domain Driven Design?

Domain-Driven Design(DDD) is a collection of principles and patterns that help developers craft elegant object systems. Properly applied it can lead to software abstractions called domain models. These models encapsulate complex business logic, closing the gap between business reality and code.

What is the role of an aggregate in Domain Driven Design?

Aggregates are a design pattern that play a big role in domain-driven development. In many systems, the relationships between entities can become so interwoven that attempting to eager-load an entity and all of its related entities from persistence results in attempting to download the entire database.

What is an entity in domain driven design?

In domain-driven design, an entity is a representation of an object in the domain. It is defined by its identity, rather than its attributes. It encapsulates the state of that object through its attributes, including the aggregation of other entities, and it defines any operations that might be performed on the entity.

What is domain driven design pattern in microservices?

Domain-driven design (DDD) advocates modeling based on the reality of business as relevant to your use cases. In the context of building applications, DDD talks about problems as domains.


2 Answers

I'd think that the Body would simply call the splitCell() on the Cell. Then the Body can do what it wants with the new Cell - add to itself, consume it, throw it away, whatever. The Body contains the Cell anyway.

like image 93
G Fernandes Avatar answered Oct 03 '22 04:10

G Fernandes


In DDD, it often depends on, well, the domain. Here, the example - and thus the domain - seems a bit weird but I think I would go for a SplitCell method on Body.

Although it's not very clear to me what the cell splitting means, and what should trigger this action, I guess the body is responsible for splitting its cells. I would be more comfortable with a Regenerate method, or something like that, on the Body, that splits internal cells by calling the Split method on each ones.

Ok, this example is definitively bizarre...

like image 23
Romain Verdier Avatar answered Oct 03 '22 04:10

Romain Verdier