简体   繁体   English

对于PHP中的DDD,我们如何在不是根的聚合中完全保护实体上的方法?

[英]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? 对于PHP中的DDD,我们如何在不是根的聚合中完全保护实体上的方法? 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. 您不会公开那些实体,基本上您的AR不能返回它的实例。 But in practice, I didn't have this problem mainly because my aggregates are 99% made from 1 entity + bunch of value objects. 但实际上,我并没有这个问题,主要是因为我的汇总是由1个实体+一堆价值对象构成的,占99%。

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. 当另一个概念以某种方式引用(并不意味着它包含)产品时,它实际上处理的是产品ID,这又是技术实施的更多细节。 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. 由于聚合应包含定义概念的对象,因此在聚合内部“使用”重复的实体表示不正确建模的99.99%。

An aggregate is not just a group of entities, where one entity acts as a container for others. 集合不仅是一组实体,其中一个实体充当其他实体的容器。 That's 100% wrong. 那是100%的错误。 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. 对于抱着孩子的父母,有数据库和存储库模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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