实体更新、领域驱动设计
假设我有一个订单在 UI 上更新了一些值(它们可能可以/不可以确保保存)
1. 我们如何验证所做的更改?是否应该验证将订单传回服务层的 DTO 的完整性?
验证完成后?服务如何返回验证错误?我们是否构建一个 ReponseDTO 对象并像
ResponseDTO saveOrder(OrderDTO); 那样返回它?
- 我们如何更新域实体顺序? DTO 汇编器是否应该负责使用最新更改更新订单实体?
如果我们想象一种典型的分层方法,Web 服务器上的 ASP .NET,应用程序服务器上的 WCF。 使用网络上的数据更新订单并保存时。 WCF 接收 OrderDTO。 现在我们如何更新来自 DTO 的订单?我们是否使用汇编程序通过 DTO 的更改来更新域对象?类似的东西
class OrderDTOAssembler {
updateDomainObject(Order, OrderDTO)
}
Lets say I have an order which got updated on the UI with some values (they could be ok/not ok to ensure save)
1. How do we validate the changes made? Should the DTO which carries the order back to service layer be validated for completeness?
Once the validation is complete? How does the service return the validation errors? Do we compose a ReponseDTO object and return it like
ResponseDTO saveOrder(OrderDTO);
- How do we update the domain entity order? Should the DTO Assembler take care of updating the order entity with the latest changes?
If we imagine a typical tiered' approach, ASP .NET on Web Server, WCF on Application Server.
When the Order form is updated with data on the web and saved. The WCF receives a OrderDTO.
Now how do we update the order from DTO? Do we use an assembler to update the domain object with changes from DTO? something like
class OrderDTOAssembler {
updateDomainObject(Order, OrderDTO)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将尝试根据我的经验回答您的一些问题以及我应该如何解决您的问题。
首先,我不应该让 DTO 进行任何验证,但普通的 POCO DTO 通常具有特定数据类型的不同属性,因此需要进行某种验证。我的意思是,您必须应用整数街道号码和街道名称字符串等。
第二,正如您所指出的。让 ORderDTOAssembler 从 OrderDTO 转换为 Order,反之亦然。这是在应用层完成的。
第三,我将使用访问者模式域驱动设计中的验证,如示例所示。 OrderService 将使用 IOrderRepository 来保存/更新订单。但是使用访问者验证方法,OrderService 将调用 Order.ValidatePersistance(请参阅示例中的链接 - 但这是在基础设施层中实现的扩展方法,因为它具有“数据库知识”)来检查其状态是否有效。如果为 true,那么我们使用 IOrderRepository.Save(order)。
最后,如果 Order.ValidatePersistance 失败,我们会收到一条或多条 BrokenRules 消息。这些应该在 ResponseDTO 中返回给客户端。然后科学家可以根据消息采取行动。这里的问题可能是您将拥有 ResponseOrderDTO 消息,但也许(现在才想到)所有 ResponseDTO 都可以从 ResponseBaseDTO 类继承,该类公开传递 BrokenRule 消息所需的属性。
我希望您发现我的想法有用并祝您好运。
I will try answer some of your questions from my experience and how I should approach your problem.
First I should not let DTO conduct any validations, but just plain POCO DTO's usually have different properties with specific datatypes, so some kind of validation is done. I mean you have to apply an integer street number and string for street name etc.
Second as you point out. Let a ORderDTOAssembler convert from OrderDTO to Order and vice versa. This is done in the application layer.
Third I would use Visitor pattern Validation in a Domain Driven Design like the example. The OrderService will use an IOrderRepository to save/update the order. But using the visitor-validation approach the OrderService vill call Order.ValidatePersistance (see link in example - but this is a extension method that is implemented in infrastructure layer since it has "db knowledge") to check its state is valid. If true, then we to IOrderRepository.Save(order).
at last, if Order.ValidatePersistance fails we get one or more BrokenRules messages. These should be returned to client in a ResponseDTO. Then cient can act on messages and take action. Problem here can be that you will have a ResponseOrderDTO messages but maybe (just came up with this now) all your ResponseDTO can inherit from ResponseBaseDTO class that expose necessary properties for delivering BrokenRule messages.
I hope you find my thoughts useful and good luck.