Domain driven design DDD-使用值对象更新聚合根

Domain driven design DDD-使用值对象更新聚合根,domain-driven-design,Domain Driven Design,下面用一个示例说明如何在域实体中封装数据: // WRONG ACCORDING TO DDD PATTERNS – CODE AT THE APPLICATION LAYER OR // COMMAND HANDLERS // Code in command handler methods or Web API controllers //... (WRONG) Some code with business logic out of the domain classes ... OrderI

下面用一个示例说明如何在域实体中封装数据:

// WRONG ACCORDING TO DDD PATTERNS – CODE AT THE APPLICATION LAYER OR
// COMMAND HANDLERS
// Code in command handler methods or Web API controllers
//... (WRONG) Some code with business logic out of the domain classes ...
OrderItem myNewOrderItem = new OrderItem(orderId, productId, productName,
    pictureUrl, unitPrice, discount, units);

//... (WRONG) Accessing the OrderItems collection directly from the application layer // or command handlers
myOrder.OrderItems.Add(myNewOrderItem);
//...
但是如果我有一长串地图要通过,那么做这样的事情有什么问题吗

myOrder.AddOrderItem(new OrderItem(orderId, productId, productName,
    pictureUrl, unitPrice, discount, units))

就我个人而言,如果您传递基元类型或imutable,我看不出有什么区别,因为您可以将许多基元封装在一个不可变的对象中,代码的可读性会更好。缺点是您的应用程序层对模型有很强的依赖性,但这可以通过让聚合根创建对象来避免,例如使用factorymethod。通常,一切都取决于您的设计,并非所有DDD规则都必须完全实现。哪些部分你更关注,哪些更少取决于你的设计和你的愿望。
myOrder.AddOrderItem(new OrderItem(orderId, productId, productName,
    pictureUrl, unitPrice, discount, units))