DTOs: best practices

dto example
dto vs entity
dto assembler
dto java
dto vs model
dto acronym
dto mapper pattern
dto conversion java

I am considering to use DTOs instead of passing around my domain objects. I have read several posts here as well as elsewhere, and i understand there are several approaches to getting this done.

If i only have about 10 domain classes in all, and considering that i want to use DTOs rather than domain objects for consumption in my Views (WPF front ends), what is the recommended approach. I think using tools like automapper etc maybe an overkill for my situation. So i am thinking of writing my custom mapper class that will have methods for converting a domain type to a DTO type.

What is the best way to do this, are there any sample to get me started to do this?

Second question: When writing those methods that will create DTOs, how do i deal with setting up all the data, especially when the domain type has references to other domain objects? Do i write equivalent properties in the DTO for mapping to those refernece types in the domain class? Please ask if i have not put my second question in proper words. But i think you understand what i am trying to ask.

Thrid question: When writing DTOs, should i write multiple DTOs, each containing partial data for a given domain model, so that each of it can be used to cater to a specific View's requirement, or should the DTO have all the data that are there in the corresponding model class.

I've been reading a few posts here regarding DTO's and it seems to me that a lot of people equate them to what I would consider a ViewModel. A DTO is just that, Data Transfer Object - it's what gets passed down the wire. So I've got a website and services, only the services will have access to real domain/entity objects, and return DTO's. These may map 1:1, but consider that the DTO's may be populated from another service call, a database query, reading a config - whatever.

After that, the website then can take those DTO and either add them to a ViewModel, or convert into one. That ViewModel may contain many different types of DTO's. A simple example would be a task manager - the ViewModel contains both the task object you are editing, as well as a group of Dto.User objects that the task can be assigned to.

Keep in mind that the services returning DTO's maybe used by both a website, and maybe a tablet or phone application. These applications would have different views to take advantage of their displays and so the ViewModels would differ, but the DTO's would remain the same.

At any rate, I love these types of discussions, so anyone please let me know what you think.

Matt

On DTOs, DTOs, or data-transfer objects, are commonly used. For smaller projects this is usually a good way to go. if we use DTOs everywhere we should be consistent and stick to that practice); Don't use the mappers/entity-to-dto  Using Façades and DTOs - Best Practice SAP Commerce offers recommended best practices for implementing facades and defining DTOs. Following these best practice models can reduce potential headaches for implementing your custom extensions.

I'm kind of using DTOs in a project. I tend to make the DTOs only to show the data I need for an specified view. I fetch all the data shown in the view in my data access class. For example, I may have an Order object which references a Client object:

public class Client{
    public int Id{get;set;}
    public string Name{get;set;}
}

public class Order{
    public int OrderID{get;set;}
    public Client client{get;set;}
    public double Total{get;set;}
    public IEnumerable<OrderLine> lines {get;set;}
}

Then in my OrderListDTO I may have something like:

public class OrderListDTO{
    public int OrderId{get;set;}
    public string ClientName{get;set;}
    ...
 }

Which are the fields I want to show in my view. I fetch all these fields in my Database access code so I don't have to bother with entity asociations in my view or controller code.

Rethinking the Java DTO, DTOs help translate data between server and client. By going A good DTO should encourage API best practices and clean code principles. Omit some properties in order to reduce payload size. Flatten object graphs that contain nested objects, to make them more convenient for clients. Avoid "over-posting" vulnerabilities. (See Model Validation for a discussion of over-posting.) Decouple your service layer from your database layer.

Best Way to develop DTOs

The way to start developing DTOs is to understand that their sole purpose is to transfer subset of data of your business entities to different clients(could be UI, or an external service). Given this understanding you could create seperate packages for each client...and write your DTO classes. For mapping you could write your own mapper defining interfaces to be passed to a factory creating DTO objects based on which data from the entity for which the DTO is being created would be extracted. You could also define annotations to be placed on your entity fields but personally given the number of annotations used I would prefer the interface way. The main thing to note about DTOs is that they are also classes and data among the DTOs should be reused, in other words while it may seem tempting to create DTOs for each use case try to reuse existing DTOs to minimize this.

Getting started

Regarding getting started as stated above the sole purpose of the DTO is to give the client the data it needs....so you keeping in mind you could just set data into the dto using setters...or define a factory which creates a DTO from an Entity based on an interface.....

Regarding your third question, do as is required by your client :)

Dealing with Complexity with DTOs and Command Objects -- Visual , While on that journey he has some best practices around designing I'll also discuss a best practice in creating Data Transfer Objects (DTOs). If you want to avoid an explosion of DTOs, you can now use ValueTuples. Especially if the DTO would only have a handful of properties. But no, using domain models in your view (even if an API) is most definitely a code smell.

I come to project with spring-jdbc and there are used DAO layer. Some times existing entities doesn't cover all possible data from DB. So I start using DTO.

By applying '70 structure programming rule I put all DTOs into separate package:

package com.evil.dao;     // DAO interfaces for IOC.
package com.evil.dao.impl; // DAO implementation classes.
package com.evil.dao.dto; // DTOs

Now I rethink and decide to put all DTO as inner classes on DAO interfaces for result-sets which have no reuse. So DAO interface look like:

interface StatisticDao {
    class StatisticDto {
        int count;
        double amount;
        String type;

        public static void extract(ResultSet rs, StatisticDto dto) { ... }
    }

    List<StatisticDto> getStatistic(Criteria criteria);
}


class StatisticDaoImpl implements StatisticDao {
    List<StatisticDto> getStatistic(Criteria criteria) {
        ...
        RowCallbackHandler callback = new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                StatisticDao.StatisticDto.extract(rs, dto);
                // make action on dto
            }
        }
        namedTemplate.query(query, queryParams, callback);
    }
}

I think that holding related data together (custom DTO with DAO interface) make code better for PageUp/PageDown.

RESTful API Best Practices and Common Pitfalls, Technical best practices. 1. Use DTOs to move data back and forth. Consider the following example:  A good DTO should encourage API best practices and clean code principles. They should allow developers to write APIs that are internally consistent. Knowledge about a parameter on one endpoint should apply to parameters with the same name on all related endpoints.

I'm going to assume that your domain model objects have a primary key ID that may correspond to the ID's from the database or store they came from.

If the above is true, then your DTO will overcome type referecning to other DTO's like your domain objects do, in the form of a foreign key ID. So an OrderLine.OrderHeader relationship on the domain object, will be OrderLine.OrderHeaderId cin the DTO.

Hope that helps.

Can I ask why you have chosen to use DTO's instead of your rich domain objects in the view?

Entity To DTO Conversion for a Spring REST API, What is the good practice? 1. Convert in the service layer and return DTOs to controller, so controller just return DTOs 2. As you implemented,  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Best Practices For Mapping DTO to Domain Object?

Think Before You Use The DTO Pattern, Should I really be calling everything a DTO? Is half my application calling Automapper really a good practice? View Models vs DTOs. I don't  RESTful API Best Practices and Common Pitfalls. Follow established best practices. Technical best practices 1. Use DTOs to move data back and forth.

What is the use of DTO instead of Entity?, Good practice doesn't always pay off immediately. It might start paying off in the future, when you need to revisit an old application. The main killer  Defense Travel System (DTS) Best Practices. May 26, 2020 . Table of Contents . 05/26/20 ii This document is maintained on the DTMO website at https://www

Why Remote Services should use DTOs, DTOs are Best practices for remote services. In following of Martin Fowler's recommendation for using DTOs (Data Transfer Objects) for remote services (​MSDN),  So, the best practice is to keep the ConfigureServices method clean and readable as much as possible. Of course, we need to write the code inside that method to register the services, but we can do that in a more readable and maintainable way by using the Extension methods.

Comments
  • Be prepared to also write multiple specific Data Transfer Objects for specific Service Methods, not only for specific Domain Models.
  • Just to clarify a DTO is not a ViewModel. Its not a DisplayModel its a TransferModel agnostic of the UI. Even more when you do REST Services transfering DTOs they should know nothing of the UI structure.
  • How do you handle the "lines" property in your DTO objects? Do you make OrderListDTO flat or do you load the "lines" collection some how?
  • Depends on the context. If I need the lines in the view, I load them; if not, I don't. Sometimes I may have a LineCount property on my OrderListDTO and I do LineCount=order.lines.Count(), or I show a total: LineSum=order.lines.Sum(t=>t.Quantity)...
  • Can the DTOs have ID properties in them? - i.e. OrderlineID in your sample. I thought DTOs are fully self contained data objects which won;t have any reference to database,and other external dependencies. As for why DTOs, my project will eveolve into a large system in future, and i want to ensure I build it now to adhere to being able to expose data over web service requests etc in future. Beeter to follow good practise from day 0 i suppose. Do you have any idea for my 3rd question (which i added only a few minute back).