Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

like image 871
Rachel Avatar asked Jan 28 '11 01:01

Rachel


1 Answers

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.

like image 145
Guy Starbuck Avatar answered Nov 10 '22 01:11

Guy Starbuck