WCF服务应该返回一个EntityObject还是一个POCO / DTO类? [英] Should a WCF service return an EntityObject or a POCO/DTO class?
问题描述
我一直在使用EntityFramework查看很多WCF示例,并且大多数人似乎都会向客户端返回一些POCO或DTO类。
I've been looking at a lot of WCF examples using EntityFramework and most of them seem to return some kind of POCO or DTO class to the client.
我想知道为什么这是因为默认 EntityObject
包括 [DataContract] / code>属性和实现
INotifyPropertyChanged
。返回一个DTO或POCO类比一个 EntityObject
(或者反之亦然)?有没有特定的情况,最好是使用一个返回值超过另一个?
I was wondering why this was since the default EntityObject
includes the [DataContract]
attributes and implements INotifyPropertyChanged
. Is returning a DTO or POCO class better than an EntityObject
(or vise versa)? And is there specific instances where it is better to use one return value over another?
推荐答案
作为一个最佳实践,你一定要它返回一个明确设计为数据合同并且没有持久性逻辑的DTO / POCO类。
As a best practice, you should definitely have it return a DTO/POCO class that is explicitly designed as a data contract and has no persistence logic.
原因是,如果传递了一个EntityObject,那么您正在制作假设服务的消费者将引用相同的数据上下文,这违反了明确边界的SOA原则。它降低了您的服务的可重用性。
The reason is, if you pass an EntityObject, you are making an assumption that the consumer of the service will have a reference to the same data context, and this violates the SOA tenet of explicit boundaries. It reduces the reusability of your service.
Microsoft在EntityObject上实现了DataContract可能支持其一些基于WCF的数据库访问工具,如RIA。 INotifyPropertyChanged用于WPF绑定支持,与WCF或数据合约无关。
It is probable that Microsoft implemented DataContract on the EntityObject to support some of their WCF-based database access tools like RIA. The INotifyPropertyChanged is for WPF binding support, and is not related to WCF or data contracts.
这篇关于WCF服务应该返回一个EntityObject还是一个POCO / DTO类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!