Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use or not to use Data transfer objects(DTO) in a Web Api Chat Application Backend Server

I have a chat application backend built using Web Api where I am exposing several database entities directly to clients. I was wondering whether there is any positive points to map the entities to DTOs or should I continue exposing the entities as I am currently. Just to clarify I am not asking a DTO vs non-DTO general question but just advantages of using it in this scenario since most of the fields in the entities would probably be used by the client.

like image 317
Lamin Sanneh Avatar asked Jan 14 '13 06:01

Lamin Sanneh


People also ask

Should I use data transfer objects?

The reason you want to use DTOs is that you want clients to couple to that contract, not to your internal data structures. This allows you to modify and evolve your internals freely without breaking clients.

When should we use DTO?

A Data Transfer Object (commonly known as a DTO) is usually an instance of a POCO (plain old CLR object) class used as a container to encapsulate data and pass it from one layer of the application to another. You would typically find DTOs being used in the service layer to return data back to the presentation layer.

Do we need DTO objects?

A DTO is helpful whenever you need to group values in ad hoc structures for passing data around. From a pure design perspective, DTOs are a solution really close to perfection. DTOs help to further decouple presentation from the service layer and the domain model.

Is using DTO a good practice?

Transfering data using Dtos between "local" services is a good practice but have a huge overhead on your developer team. There is some facts: Clients should not see or interact with Entities ( Daos ). So you always need Dtos for transferig data to/from remote (out of the process).

What is DTO (data transfer object)?

Data transfer object (DTO), formerly known as value objects or VO, is a design pattern used to transfer data between software application subsystems. DTOs are often used in conjunction with data access objects to retrieve data from a database. A value object is not a DTO.

What is DTO in REST API?

Show activity on this post. DTO stands for D ata T ransfer O bject. This pattern was created with a very well defined purpose: transfer data to remote interfaces, just like web services. This pattern fits very well in a REST API and DTOs will give you more flexibility in the long run.

Can I have different DTOs for each version of the API?

You can have different DTOs for each version of your API. You'll have more flexibility when mapping relationships. You can have different DTOs for different media types. Your DTOs can have a list of links for HATEOAS. That's the kind of thing that shouldn't be added to persistence objects.

What are DTOs and why should I use them?

The basic idea of using DTOs is to separate data from business logic and app-specific functionality. This is especially interesting in cases you need to pass data to a different layer (e.g. from Model layer to View layer) or receive data from a different service (external API, microservice etc.


2 Answers

Yes, you can expose your entities if this is a small application developed by one person and you only have few days to finish it.

If you intend to build an application that may grow up in the future, you should consider using DTO because Domain Entities is not optimal for representation of data. Domain Entities always have more or less, not exactly what you need on the client side.

You can use a tool called AutoMapper to map Domain Entities to DTO.

Some demo: http://www.codeproject.com/Articles/61629/AutoMapper

like image 133
phnkha Avatar answered Oct 03 '22 13:10

phnkha


Same advantage as in any other application. There's no specific advantage in your app. Using DTO's is essentially a decoupling exercise, segregating properties from methods. At the moment you are passing database objects. Doing that could mean you are passing more than required and exposing more than needed. You are also implying a great deal, what and how operations are carried out. There again what are you are going to get out of the effort of splitting things up?

like image 20
Tony Hopkinson Avatar answered Oct 03 '22 14:10

Tony Hopkinson