What is the difference between DTO and Command in DDD?
Asked Answered
S

1

6

I am new to DDD-Domain Driven Development implementation. I have checked out a few projects source code from online which they have implemented using the DDD pattern. In the project structure, I found DTO as well as Commands. I need to understand what is difference between these two? and is it not code duplication?

Below are two example classes of DTO and Command that exist in the project.

public class BookCargoCommand {
    private String bookingId;
    private int bookingAmount;
    private String originLocation;
    private String destLocation;
    private Date destArrivalDeadline;

    public BookCargoCommand(){}

    public BookCargoCommand(int bookingAmount,
                            String originLocation, String destLocation, Date destArrivalDeadline){

        this.bookingAmount = bookingAmount;
        this.originLocation = originLocation;
        this.destLocation = destLocation;
        this.destArrivalDeadline = destArrivalDeadline;
    }

    public void setBookingId(String bookingId){ this.bookingId = bookingId; }

    public String getBookingId(){return this.bookingId;}
    public void setBookingAmount(int bookingAmount){
        this.bookingAmount = bookingAmount;
    }


    public int getBookingAmount(){
        return this.bookingAmount;
    }


    public String getOriginLocation() {return originLocation; }

    public void setOriginLocation(String originLocation) {this.originLocation = originLocation; }

    public String getDestLocation() { return destLocation; }

    public void setDestLocation(String destLocation) { this.destLocation = destLocation; }

    public Date getDestArrivalDeadline() { return destArrivalDeadline; }

    public void setDestArrivalDeadline(Date destArrivalDeadline) { this.destArrivalDeadline = destArrivalDeadline; }
    }

and

public class BookCargoResource {

    private int bookingAmount;
    private String originLocation;
    private String destLocation;
    private LocalDate destArrivalDeadline;

    public BookCargoResource(){}

    public BookCargoResource(int bookingAmount,
                             String originLocation, String destLocation, LocalDate destArrivalDeadline){

        this.bookingAmount = bookingAmount;
        this.originLocation = originLocation;
        this.destLocation = destLocation;
        this.destArrivalDeadline = destArrivalDeadline;
    }


    public void setBookingAmount(int bookingAmount){
        this.bookingAmount = bookingAmount;
    }


    public int getBookingAmount(){
        return this.bookingAmount;
    }

    public String getOriginLocation() {return originLocation; }

    public void setOriginLocation(String originLocation) {this.originLocation = originLocation; }

    public String getDestLocation() { return destLocation; }

    public void setDestLocation(String destLocation) { this.destLocation = destLocation; }

    public LocalDate getDestArrivalDeadline() { return destArrivalDeadline; }

    public void setDestArrivalDeadline(LocalDate destArrivalDeadline) { this.destArrivalDeadline = destArrivalDeadline; }

    }

Note: Since I could not find the exact question, I placed it. Kindly someone let me know if a similar question exists in StackOverflow.

Stonyhearted answered 8/11, 2020 at 11:49 Comment(0)
B
2

A DTO (Data Transfer Object) is more like a technical concept referring to a class that is only dedicated to represent data that belongs together and is used to transfer these data from one place to another.

A command in this context is technically a DTO. But it also represents something that has a special meaning in the business domain - something that shall happen, i.e. a command to be executed in the system.

So the command can be regarded as what shall happen along with all data required by the receiving part of the system to perform this intended action.

The command can contain primitive data fields or more complex data which again could be some DTO.

So concerning your question about the duplication, here is how I would model your command and cargo DTO:

public class BookCargoCommand {

    private String bookingId;
    private CargoResource cargo;

    public BookCargoCommand() {}

    public BookCargoCommand(String bookingId, CargoResource cargo) {
        this.bookingId = bookingId;
        this.cargo = cargo;
    }

    public String getBookingId() {
        return this.bookingId;
    }

    public CargoResource getCargo() {
        return this.cargo;
    }
}

and

public class CargoResource {

    private int bookingAmount;
    private String originLocation;
    private String destLocation;
    private LocalDate destArrivalDeadline;

    public CargoResource(
            int bookingAmount,
            String originLocation,
            String destLocation,
            LocalDate destArrivalDeadline
    ) {
        this.bookingAmount = bookingAmount;
        this.originLocation = originLocation;
        this.destLocation = destLocation;
        this.destArrivalDeadline = destArrivalDeadline;
    }

    public int getBookingAmount() {
        return this.bookingAmount;
    }

    public String getOriginLocation() {
        return originLocation;
    }

    public String getDestLocation() {
        return destLocation;
    }

    public LocalDate getDestArrivalDeadline() {
        return destArrivalDeadline;
    }
}

Note: the same concept also applies to events (which represent something that has happened in the system).

A side note: from my point-of-view a DTO is the only exception for classes that don't combine data and behaviour. In other use cases I would consider such classes as anemic (see also anemic domain model

Bainmarie answered 8/11, 2020 at 20:44 Comment(1)
This answer covers it. I regard anything that needs to go over-the-wire as a DTO as it is typically serialized. As such it doesn't contain any behaviour. If I do need to add some method to a DTO I tend to use extension methods (in the C# world). A DTO is somewhat of a hypernym for any other representation of the concept such as command/event, request/response, or model.Sayyid

© 2022 - 2024 — McMap. All rights reserved.