Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DTO(Data Transfer Object) and Proxy Object in c#

In Entity framework these two terms are used DTO and Proxy Object. i am confused if DTO is used to transfer the state then why this proxy object is used?

like image 906
user3603255 Avatar asked Jun 20 '14 05:06

user3603255


2 Answers

Proxy objects are objects that inherit from a model class, but add some functionalities like state management. This is normally used so that your model don't have to inherit from any special class.

Data transfer objects are objects that are not from your model, but they represent the same data. You usually use them to communicate with a service that is not aware of your model.

like image 107
André Pena Avatar answered Sep 30 '22 17:09

André Pena


DTO and proxy objects are not EF concepts, but general concepts.

A DTO, or "Data Transfer Object", is an object created specifically to move data between layers or services of an application. There are several reasons to use DTOs, but most of the time is that the required information carried between layers or services differs between the layers or service sides. For example:

  • you have an entity with lots of properties and a service client or another layer only needs a few of them: create a DTO with the needed properties, map the original properties from the original entity to the DTO, and expose the DTO on the service / layer.

  • you need to expose an object which has properties coming from different entities: create a DTO with all the required properties, map the properties from the entities into the DTO and expose it. For example you may need an flat object which has properties from an EF entity and "master tables" related to it (for example descriptions or names associated to an FK)

  • you need to expose entities with a different shape, aggregated data or whichever other changes...

You can implement mappings in many ways: for example creating a DTO constructor which accepts the original entities as parameters, creating an static class with an static method for mapping. You can make the mapping a little easier using things like ValueInjecter or AutoMapper. Sometimes you can even use them directly without implementing any other thing to make the mapping. However, if the mapping is a little complex you usually have to make some of the mapping manually.

A proxy object is something completely different. Literraly is an object that represents another object. I.e., and object which basically behaves like the original object but differs from it. This is generally done to add extra functionality to the object, keeping it compatible with its original intent. For example, an object can be proxied to intercept method calls for login, or validation. The usual technique is to use a framework that dynamically inherits from the original object, ading the required behaviors (this is the typical technique of AOP).

In the case of entity framework, the proxies that you can find a "change tracking proxies". These are objects created dynamically inheriting from the original entity, but adding some functionality that allows them to track their changes: they can hold the original values, the changed values, the new states, etc. And also allows them to implement lazy loading. As they are subclasses of the original object they have the same properties, but a different behavior. Here is a short description of EF proxies.

like image 45
JotaBe Avatar answered Sep 30 '22 15:09

JotaBe