views:

270

answers:

2

Hi , After reading about Enitity Framework , i have some questions:

1] What is the best way to transfer Entities between tiers ? a] do i must create lighter DTOs for this or i can Effectively serialize the Entitiy and transfer it ?

b] if i must create light DTOs,for Efficency, and after i saw the nice usage of Automapper , i didnt quite understand how it saves extra coding (if it is its goal) , i mean, we need to write the flattern class(DTO) properties , so its only saves the ctor.

2] is there any point Building Entitiy Classes back from DTOs ?

thanks.

A: 

You want to look at RIA Services. It's built for just this.

Craig Stuntz
+1  A: 

Answer to 1: The best way to transfer entities between tiers depends on your application. You can create DTOs which is my preferred solution though serializing entities is possible but you need to make sure this is actually what you want to do and remember:

"When you use binary serialization and WCF data contract serialization, if the object being serialized has related objects in the object graph, those objects are also serialized. XML serialization does not serialize related objects."

Automapper works by mapping properties automatically rather than having to write all of the plumbing statements such as:

dto.id = entity.id;
...
all other dto assignment operations

you soon see the amount of plumbing code add up so will save quite a lot of this = that lines of code especially if you have view classes as well as dto objects and I found this fitted what I wanted perfectly. As far as i am aware automapper will only work if the names are the same between the entity and DTO and it can map between the types of these properties. You can add mapping rules for converting between types though if the dto and entity store the data in a different type.

Answer to 2: If you convert objects to DTOs and modify them at higher tiers how do you plan on saving them back through the entity framework? Usually you would just convert these back onto the related entities / create a new entity and submit these changes.

bobwah
saving coding dto.id = entity.id ?,not quite wow,anyway im still left with unanswerd questions , is transfering entities to dtos is the best way ? or serializing the entities is also possible option ?
_Avishay_
what did you want it to do? I can't really say which is "best" instead it depends on your application and preferences. I have updated my answer with a few points which should answer all of your questions.
bobwah
About automapper , i wanted it to map without forcing me opening the code and write new DTO/modifying exisitng DTO new properties. [can be from an xml configuration].because if im already opening the code and writing the DTO class and its properties, coding dto.id = entitiy.id , is not such a time saver,it is a nice one,but very little one. 2] do you also use automapper for, dto -> entity ? i've seen in stackoverflow it has some problem ?
_Avishay_