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.
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.
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.
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.
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).
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.
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.
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.
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.
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With