简体   繁体   中英

For DDD in PHP, how do we exactly protect methods on an entity in an aggregate that is not the root?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM