2

For DDD in PHP, how do we exactly protect methods on an entity in an aggregate that is not the root? How do we protect them from being used outside the aggregate (since only methods on the aggregate root should be used)?

Bonus: Also, is it normal to have duplicated entities? I mean, where say an identical product entity class is found to be a child in two seperate aggregates? Both these product classes have the same behaviors/rules for working with the domain concept of a "product". But I duplicated the class because, again, entities shouldn't be accessed outside of their aggregate.

1 Answer 1

3

You don't expose those entities, basically your AR shouldn't be able to return an instance of it. But in practice, I didn't have this problem mainly because my aggregates are 99% made from 1 entity + bunch of value objects.

Bonus: Your aggregates define concepts. Entities and value objects are just technical terms for objects that do have business meanings, so they are implementations of concepts. The concept of a Product should be unique per bounded context.

When another concept somehow references (that doesn't mean it contains) Product, it actually deals with the Product id, which again is more of a technical implementation detail. From a business point of view, you have one unique concept (aggregate) per context.

Since an aggregate should contain only objects that define the concept, having duplicate entities used 'inside' an aggregate is 99.99% sign of improper modelling.

An aggregate is not just a group of entities, where one entity acts as a container for others. That's 100% wrong. An aggregate consists of all objects needed to properly define the business concept. An aggregate root is Never a container, it's the object in charge of maintaining the aggregate consistency (that's why you have to work only with it).

For parents holding children there is the database and the repository pattern.

10
  • "You don't expose those entities". Again, the question is how do you actually do this in PHP. The AR has methods, but so do the entities that it is responsible for in the aggregate. What's to keep another dev from going to those entities and using their methods (bypassing the AR)? Aug 21, 2015 at 19:44
  • "The concept of a Product should be unique per bounded context." We are talking about aggregates (unless aggregate boundaries and bounded contexts are the same thing?). I understand that some aggregate roots can just use a "product id". I'm concerned about aggregates that both have an identical "product" entity (class) in them. For example, a product has a name and price property. It also has a barcode behavior. These properties and behaviors are useful in 2 different aggregates. Examples of 2 aggregates that need a product entity could be a "cart" AR and an "inventory" AR. Aug 21, 2015 at 19:47
  • "where one entity acts as a container for the others". I'm not sure what you mean by this? I understand an aggregate to be a group of entities where one entity will be responsible for "representing" the others (in charge of maintaining the invariants, etc). This is the AR. You may only call methods on the AR. I'm not disputing that there may be other objects/value objects/etc. that make up an aggregate. I just see folks saying in DDD that you don't access entities in the aggregate, you only go to the AR. Aug 21, 2015 at 19:59
  • @prograhammer There is no 'cart' AR and 'inventory' AR, those are bounded contexts. Aggregates have their own boundaries, a BC has boundaries based on business language, aggregates based on concept. Your example is still improper modelling (in this case, bounded contexts are merged when they shouldn't be).
    – MikeSW
    Aug 21, 2015 at 20:17
  • Sorry, couldn't edit answer. I was saying I have 2 aggregates represented by 2 ARs "cart" and "inventory" (both those word choices do indicate more of an aggregate instead of an AR entity). I can say that better.... Perhaps we could say "shopping aggregate" has Cart AR entity and Product entity. And "inventory aggregate" has Inventory Order (or Purchase Order) AR entity and Product entity. Aug 21, 2015 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.