Should a WCF service return an EntityObject or a POCO/DTO class?

隐身守侯 提交于 2019-12-04 05:13:29

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.

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.

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.

It is worth to return the POCO in some cases where you don't aware of persistence logic. I mean the same POCO can plugged to other ORM or for other purpose. Ok this is advantage of POCO over ORM but it also gives you performance boost over EntityObject which does add proxy/notifiers run time.

Returning POCO - You have to manually update the state of entity when received from WCF.

Returning EntityObject - You receive the entity with maintained state.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!