3

Im planning to implement an NTier design in EF 4. I know that the EF itself creates entities base on the tables it mapped in the Database. My question is, what is the use of DTO (Data Transfer Object) or is it really needed? It looks like it promotes redunduncy since you have to create another DTO entity for every entity the EFs generated. Please guide me. thanks..

2 Answers 2

9

DTO is data transfer object used to transfer only needed data between physical tiers (when tiers are in another processes or on another servers). If you need to expose only person's Name and Age you don't need to transfer her address, employment, children, etc. So you will create simple transport object which will contain only name and age.

EF will create entities which map database records to properties. EF entity can also be extended (by partial classes) to full domain object with custom computed properties and methods. Domain objects should not be exposed to different tier directly and that is another case where DTOs are used.

Edit:

The last situation where DTOs are used is optimization of cross boundary calls. If you have tiered application where one tier calls methods on another tier over process boundary you should minimize those calls because they decrease performance (are slow). To do that you can create special DTOs transferring complex data structures (several entities) to some master operation (facade) on remote tier which will further use data to execute multiple business operations.

Sign up to request clarification or add additional context in comments.

5 Comments

Im planning to use the Entity generated by the EF accross the layers of my 3 tier design as I would like to avoid creating DTOs, meaning UI has direct access to the DAL (which violates the ntier rule). Is there any implication on that approach??
Layers and tiers are not same. Layer is logical boundary whereas tier is physical boundary.
so what I really mean is Layer, the layers which is represented by DLL (BL,DAL) will be deployed in the same machine.
In such case DTOs can be handy only if you build full Domain model (entities with business methods) and you don't want to expose these entities to upper layer (UI).
Try using EntitiesToDTOs to generate automatically DTOs and Assemblers from your EDMX. It saves a lot of coding time.
0

DTO's are most helpful when you need to pass a portion of an entity or a few entities bundled together. Also, your Entities are actually tied to some entity context so when you pass an entity outside, they can execute the methods on it, change the data, etc. If you just want to pass an object outside, in this case you can benefit from using DTOs.

4 Comments

so you mean, DTOs are actually optional when creating a tiered EF application?
as I will not be using WCF/WS, it's just a simple single web project.
You can do without DTOs, but in that case your entities turn into your DTOs, then you must avoid adding methods to your entities (or make them internal methods), and in the end, it turns into a procedural application model. So, n-tier is not enough, how many layers are you going to have in your n-tier applicaiton model. Since you are using EntityFramework, your DAL and Domain Model are the same, if you are not going to use DTOs then your PresentationModel will also be the same, and if you are not going to use View Models in your UI Layer, you are going to have a n-tiered application w/ 1 layer
for a simple web project, you are better off without DTO. You might need DTOs at some point, like for JSON, but in that case, you can do with anonymous types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.