In this post, we explore what data transfer objects ( DTO ) are and how we can use them. Data Transfer Objects can be used to repackage models or they can be used to encapsulate multiple models in one object and transfer them across systems.

Did anyone ever ask you for your banking details? What information did you give them? Did you give them all of your banking information including your ID number and PIN? Or did you only give them the required information?

It can similarly be a bad idea to transfer unnecessary sensitive data in objects when moving data around. This is where Data Transfer Object comes into play.

The principle behind Data Transfer Object is to create new Data Objects that only include the necessary properties you need for a specific data transaction.

Benefits include:

  • Make data transfer more secure
  • Reduce transfer size if you remove all unnecessary data.

There is also the scenario where you need to transfer multiple models in a single transaction. This can be achieved by creating a Data transfer object that has the required models as object properties.

This can simplify the transfer of data between systems.

But first, let’s look at repackaging data.

 

Repackaging data with Data Object Models ( DTO’s)

Let’s say we have the following totally unrealistic ( I would hope so)  Bank database table that  contains the following

  • Id
  • FirstName
  • SecondName
  • DateOfBirth
  • AccountNumber
  • IdNumber
  • AccoumtNumberPIN
  • AccountBalance

 

You would typically have a data model in your code in the same format to let your program know how the data is structured in the database. However, you can’t change anything about this model as it will most likely break the mapping between your app and your database, or overwrite your custom code when you re-scaffold your database.

Original Data Class

public class BankAccount
    {
        public long Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public DateTime DateOfBirth { get; set; }
        public int AccountNumber { get; set; }
        public int IdNumber { get; set; }
        public int PIN { get; set; }
        public double AccountBalance { get; set; }
    }

We don’t want to transfer all of this data around seeing that it contains sensitive data.
Using Data Transfer Objects we can repackage the data by creating an object with the following properties:

Repackaged Data Transfer Object

public class BankAccountDTO
    {
        public long Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public DateTime DateOfBirth { get; set; }
        public int AccountNumber { get; set; }
    }

This new data transfer object now contains no sensitive or unwanted data. This will give you the freedom to add or remove properties without breaking your database calls.

We can now create a map to convert the data from the database model to the data transfer object.

Map code from original class to DTO

public BankAccountDTO BankAccountDtoMapping(BankAccount bankAccount)
        {
            var bankAccountDto = new BankAccountDTO();
            bankAccountDto.Id = bankAccount.Id;
            bankAccountDto.Firstname = bankAccount.Firstname;
            bankAccountDto.Lastname = bankAccount.Lastname;
            bankAccountDto.DateOfBirth = bankAccount.DateOfBirth;
            bankAccountDto.AccountNumber = bankAccount.AccountNumber;

            return bankAccountDto;
        }

A good alternative is to use an auto mapper. I use Auto Mapper by Jimmy Bogard

If you use .Net Core, install both of the following:

Just remember to have the same Data Transfer Object on both sending and receiving systems.

Multi-Object Data Transfer Objects.

Sometimes you might need to transfer multiple different class objects in a single transaction. Data Transfer objects can help by simplifying this as well.

Original Classes

public class Address
    {
        public long Id { get; set; }
        public string StreetName { get; set; }
        public string HouseNumber { get; set; }
        public DateTime MovedIn { get; set; }
    }


    public class CarDetail
    {
        public long Id { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public DateTime FirstRegistration { get; set; }
        public int SeatCount { get; set; }
        public int EngineCapacity { get; set; }
    }

    public class HouseDetail
    {
        public long Id { get; set; }
        public string WallType { get; set; }
        public string RoofType { get; set; }
        public DateTime BuildCompleteData { get; set; }
        public int BathroomCount { get; set; }
        public int RoomCount { get; set; }
    }

Simply create a Data Transfer Object and make the properties of the data transfer object the classes that you need. Remember to initialize them with a constructor.
Just remember to have the same data transfer object on the other side as well.

Multi Object DTO

public class AssetDetails
    {
        public Address ClientAddress { get; set; }
        public CarDetail ClientCar { get; set; }
        public HouseDetail ClientHouse { get; set; }
    }

DTO’s can realy simplify your code if used right.

Let me know what you think on Twitter:

Cheers