Skip to content

Data Transfer Object (DTO)

Aaron Hanusa edited this page Feb 6, 2021 · 17 revisions

The Peasy Framework considers the data transfer object (DTO) to be the currency of exchange. The DTO flows from a service command method to a data proxy and back via the command execution pipeline.

DTOs should be simple property bags and should not contain any business logic directly in the class. Business logic should be stored in business rules, commands, or exposed in the form of extension methods. This promotes clarity and a clearer separation of concerns.

Here is a sample DTO:

public class Customer: IDomainObject<int>
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
}

Notice that the Customer DTO implements IDomainObject<TKey>. This interface exposes an ID field which Peasy relies on and becomes the type (int) specified in TKey.

Also note that the Customer DTO does not expose lists of children (or aggregates). Peasy was designed to free ourselves from complications we often face with domain-driven designs, and favors a more linear approach to design and development.

What if my DTO.ID property isn't named "ID"?

In this scenario, all that needs to be done is to create an ID property on your DTO with a name of your choosing and marshal that call to IDomainObject.ID.

Here's an example:

    public class Customer: IDomainObject<int>
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public int CustomerID
        {
            get { return ID; }
            set { ID = value; }
        }
    }