Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top