views:

54

answers:

4

I am working on a project where we need to fetch data from a WCF service. The service code looks up the database thru the Entity Framework. However inorder to prevent sending down EF generated classes across the wire into the proxy generated by the client we have decided to map the values from the EF classes to custom built DTO classes, where the mapper class is responsible for picking out values from the EF generated classes and putting them into the DTO class. We then use those DTO classes for the service method's request and response. The EF builds classes from tables that are related to each other. I get various classes with properties that look something like these below:

 public global::System.Data.Objects.DataClasses.EntityCollection<SubAttachment> Attachments
    {}

 public global::System.Data.Objects.DataClasses.EntityReference<Gl> GlCodeReference
    {}

A few of the properties have the keyword Reference appended to them which I am guessing provides some way for the EF to look up the related table on that field.

Is there a better/different approach than this mapping approach if I dont want to send heavy EF classes across? If not, is there some reference material that will help me understand how the the classes are built by the Entity framework.

Thanks for your time

A: 

I've used something very similar to the approach in the link below along with some custom partial classes and it worked quite nicely.

http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx

Chuck
A: 

Since you need to fetch data from WCF service which is backed by EF framework, have you considered using OData to expose EF objects? Check out some links below:

http://www.odata.org/

http://www.hanselman.com/blog/ODataBasicsAtTheAZGroupsDayOfNETWithScottGu.aspx

http://blogs.msdn.com/b/adonet/archive/2010/10/27/entity-framework-and-odata-pdc10.aspx

zam6ak
A: 

There is a difference between DTO and POCO entities in this case you should to use POCO (in java called POJO) see this to understanding difference also as others linked you can use POCO entity Generator to do your request. POCO = Plain Old CLR Object, DTO = Data Transfer Object

SaeedAlg
A: 

When you create classes in EF, they have the [DataMember] attributes on their fields, and that's the only data that get's sent accross the wire. So, it's not as heavy as it seems...

But, since you're passing through WCF, the entities should be generated to be self-tracking, so when they get back to the service, you know what's changed and don't have to refetch every entity from db to do comparing.

If you still want the DTO's, you can generate them as well. If you're using EF4.0 you have an option of extracting a T4 file (.tt) that practically does the code generation - use that and alter to suit your needs and generate DTO's as well as mapper classes...

To get a .tt file from edmx (only for EF4): right click your model, choose Add code generation items, and choose EntityObject generator, or the other one if you want to have objects transfered through wcf. This will create a tt file that you can run by issuing a save command (you'll get a prompt if you want to allow it to run). When saved, it will generate a file that's exactly the same as the file generated by edmx model in the case of EntityObject generator, or you'll have two .tt files if you're using the other generator...

veljkoz