Im planning to use the Entities generated by the EF (POCO) in sending data to the client instead of creating DTOs? Is this a good practice? Basically, my EDMX file is on my DAL layer. So the UI will have direct access on my DAL. Thanks.
It depends on how close the client is to your object domain. If it is your client, then maybe - and indeed this is pretty much how ADO.NET Data Services (etc) work - directly exposing your model.
However, if the client is anything else I would suggest a dedicated DTO. In fact, I'd suggest it anyway ;p Otherwise, it gets somewhat complex:
Orders
member... but is that lazily loaded? do we want that?)DataContractSerializer
In most cases, having a separate DTO makes most of these problems just go away
Basically, I don't think it's a good idea to send DAL objects to your interface, so I would use DTOs. To minimize the effort doing so I would take a look at an DTO generator, to generate DTO code which lets you convert from DAL object to DTO and vice versa.
EDIT: Sorry, didn't see you are using POCO. Have a look at this SO post
First of all, I believe you cannot use Entities generated by Entity Framework as return types in your Service, at least in a WCF Service.
But, why you would like to use entities all across your application? If you have a common architecture with a client-server structure, your client won't need all the information that an EntityObject has, like the ObjectContext where it is contained, the state on it, and lots of other info that your client not only won't use, but more important: don't have to know.
In that case you should use the DTO pattern, or other design pattern you think better that decouples the Server side from the Client Side. I believe DTO Pattern is the most widely used and recommended. If you are using Entity Framework you can go to http://entitiestodtos.codeplex.com , it is and AddIn for Visual Studio that I published, it is free and open source. It generates your DTOs from your Entity Framework Data Model (EDMX).
Regards, Fabian Fernandez
DTO is a good practice to minimise the amount of data you transfer over the wire to only include relevant fields. This is amongst other benefits. Checkout Automapper and the ProjectTo method which can automatically convert DAL to DTO and vice versa. ProjectTo under the bonnet will only select columns included in the configured mapping.
https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With