Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any tool to generate DTOs from entities?

I want to generate DTOs from entities in a totally automated way. By that I mean some tool reading my .edmx model and converting it to DTO's. I've seen this in stackoverflow but that is a superficial level of automation. I do not want to write too much code... if possible not to write any code at all.

like image 809
JPCF Avatar asked Dec 28 '10 16:12

JPCF


People also ask

Can entity be used as DTO?

IF the entity and DTO are exactly the same, and IF you can guarantee that there will never be any changes to the data model; then you are correct that you can omit the DTO. But the thing is that you can never guarantee that the data model will never change.

How do you map entity to DTO?

Thus, we can use model mapper to convert entity to dto or dto to entities. First, we need to add model mapper dependency. Next, we can create a @Bean factory method to create ModelMapper instance. This way, the model mapper instance will be available for injection on the application level.

What is DTO in API?

A data transfer object (DTO) is an object that carries data between processes. You can use this technique to facilitate communication between two systems (like an API and your server) without potentially exposing sensitive information.


1 Answers

Normally, the Entity Framework generates C# entity types for your model using an internal templating system. In Entity Framework 4, you can replace this with a custom T4 template. In addition to emitting the entity types, you could change the template to admit DTOs at the same time.

Right-click the model designer surface. Click "Add Code Generation Item." Customize the emitted T4 template.

like image 171
Craig Stuntz Avatar answered Oct 20 '22 16:10

Craig Stuntz