服务契约与域对象
假设我的应用程序有两个接口:
- Web 前端
- 提供数据的后端
它们都与 Web 服务通信,而该 Web 服务又处理业务逻辑并与单独的数据层通信,它保留了对象。
因此,如果 Web 服务的每个客户端都使用该 Web 服务的 DataContracts,我需要域对象做什么?
领域驱动设计在这里适合什么,它给我的架构带来了哪些优势?
或者是我已经拥有的很好,并且我根本不需要域对象?
我是否误解了领域驱动设计的含义和目的?
Say I have two interfaces to my application:
- A web front-end
- A back-end which provides data
Both of them talk to a web-service, and that web-service in turn, handles business logic and talks to a separate data layer, which persists the objects.
So, if each client of the web-service uses the DataContracts of that web-service, what do I need domain objects for?
Where does domain-driven design fit in here, and what advantages does it bring to my architecture?
Or is it that case that what I have already is fine, and I don't need domain objects at all?
Am I misunderstanding the meaning and purpose of domain-driven-design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据契约只不过是客户端和服务器之间交换的消息。
您的 WCF 服务是接受消息并处理它们的层,以便您的业务逻辑可以处理它们。
您的域对象将是您的业务逻辑,它接受处理后的消息,执行必要的操作,然后应用需要应用的任何事件。
如果您遵循更多命令-查询分离原则(CQS),那么您的命令(插入/updates/deletes) 将被发送到 WCF 服务并且不会返回任何内容。您的客户端将请求从您的 WCF 服务中独立于您的命令进行读取(这意味着 InsertOrder 命令不会返回订单 - 您必须为此发出单独的请求)。
总而言之,您的数据协定就是往返于 WCF 服务的消息。您的域位于该服务的后面,处理需要发生的所有业务逻辑,以使您的读取尽可能准确。
我更多地从 CQRS(命令查询责任分离)的角度来回答这个问题,但我希望这能解释我的出发点。
回答你的另一个问题:
- 您需要域对象吗 -->我会说是的,你应该
The data contracts are nothing more than messages that your client and server exchange between each other.
Your WCF service is that layer which accepts the messages and processes them so that they can be handled by your business logic.
Your domain objects would be your business logic, which accepts the processed messages, performs the necessary actions, and then applies any events that need to be applied.
If you follow a more Command-Query Separation principle (CQS), then your commands (inserts/updates/deletes) would be fired off to the WCF service and not return anything. Your client would request reads from your WCF service separate from your commands (meaning a InsertOrder command doesn't return an Order - you have to issue a separate request for that).
In all of that, your data contracts are the messages to and from your WCF service. Your domain is behind that service handling all of the business logic that needs to happen in order to make your reads as accurate as possible.
I'm answering this from more of a CQRS (command-query responsibility segregation) perspective, but I hope this explains where I'm coming from.
To answer your other question:
- do you need domain objects --> I'd say yes, you should
您的应用程序中可能不需要域对象。通常,DDD 将通过以下方式融入服务层: 服务层公开其操作契约和数据契约。数据契约类通常对应于域中的对象,但它们不是域对象,因为它们没有任何行为,它们只是特定服务所涉及的数据的表示。以下是服务中数据协定对象和域对象之间交互的简单示例:
在本例中,MyEntityDto 是一个 DTO MyEntity 对象,它用于公开服务希望向其客户端提供的 MyEntity 的特定属性。
当您的域更加复杂并且具有关联行为时,DDD 的价值就会发挥作用:
在这种情况下,我的 ChangeStateCommand 携带的数据用于对您的域实体进行操作。
You may not need domain objects in your application. Typically, DDD would fit into a service layer in the following way: The service layer exposes its operation contracts and data contracts. Data contract classes often correspond to objects in your domain, but they are not domain objects because they don't have any behavior, they are only a representation of the data with which a particular service is concerned. Here is a simple example interaction between data contract objects and domain objects in a service:
In this case, MyEntityDto is a DTO object for MyEntity, it serves to expose the specific properties of MyEntity which the service wishes to provide to its clients.
The value of DDD comes into play when your domain is more complex and has associated behavior:
In this case, the data carried my ChangeStateCommand is used to operate upon your domain entity.