Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Sep 5, 2023

Start using C# records for DTOs instead of regular classes

Start using C# records for DTOs instead of regular classes
Автор:
Edson Moisinho
Источник:
Просмотров:
3763

Simplifying data transport in C#.

Start using C# records for DTOs instead of regular classes

In modern C# development, data transport objects (DTOs) play a crucial role in exchanging information between different layers of an application, such as between a client and a server, and traditionally, developers have used classes to define DTOs, which involves writing boilerplate code for properties, constructors, comparison methods, and string representations.

With the introduction of C# 9.0, a new and more efficient alternative has emerged: records.

In this article, we will explore the benefits of using records over classes for DTOs, demonstrating how records can significantly improve code conciseness, readability, and reliability while enhancing the data transport process.

First things first. What is a DTO?

DTO stands for “Data Transfer Object” It is a design pattern used in software development to transfer data between different layers or components of an application.

The primary purpose of a DTO is to encapsulate data and transport it from one part of the application to another without exposing the underlying data structures or implementation details.

DTOs are commonly used in client-server architectures, where data needs to be exchanged between the client-side and server-side components. They help in decoupling the data representation from the business logic or database schema, providing a clear separation of concerns.

• • •

Key characteristics of a DTO

Data Carrier:
DTOs are simple data containers that hold data and may include properties to represent the data fields. They typically contain no behavior or business logic.

Serializable:
DTOs are often designed to be easily serialized and deserialized, allowing them to be sent over the network or stored in a persistent storage system.

Immutable:
Immutable DTOs ensure that the transported data remains unchanged during its journey through different layers, reducing the chances of unintended modifications.

To check other DTO definitions and similar concepts, check my specific article about it here.

• • •

How to create a DTO

The following example shows how to define and use a regular class as DTO to hold an API result data:

public class UserDTO
{
   public string Name { get; set; }
   public int Age { get; set; }
}

This is a really simple DTO implementation but it is ready to be used as in this deserialization example below:

// Receiving the result of an HTTP call
var httpResponse = await httpClient.GetAsync("https://api.example.com/user/1");
var content = await httpResponse.Content.ReadAsStringAsync();

var userDTO = JsonConvert.DeserializeObject<UserDTO>(content);

• • •

But where is the boilerplate code then?

The previous DTO is a very simple piece of code since it just has two properties defined, but in a real application we are used to creating additional features inside DTO, as constructors and other customization as we can see below:

public class UserDTO
{
    public string Name { get; set; }
    public int Age { get; set; }

    // Default constructor
    public UserDTO()
    {
        // Empty constructor
    }

    // Parameterized constructor
    public UserDTO(string name, int age)
    {
        Name = name;
        Age = age;
    }

    // Equals method for comparison
    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        UserDTO other = (UserDTO)obj;
        return Name == other.Name && Age == other.Age;
    }

    // GetHashCode method to calculate hash code for comparison
    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 17;
            hash = hash * 23 + (Name != null ? Name.GetHashCode() : 0);
            hash = hash * 23 + Age.GetHashCode();
            return hash;
        }
    }

    // String representation for UserDTO
    public override string ToString()
    {
        return $"Name: {Name}, Age: {Age}";
    }
}

The UserDTO class has properties Name and Age, constructors (default and parameterized), custom comparison methods Equals and GetHashCode, and a custom string representation method ToString.

• • •

What are records in C#

A record is a structure that combines immutable properties with some predefined behaviors, such as comparison and string representation.

Instead of creating traditional classes with properties, constructors, `Equals`, `GetHashCode`, and `ToString` methods, you can use the records’ syntax to define simple and efficient data types.

The declaration of a record includes properties directly in the record’s header, making the syntax more concise and readable.

We can rewrite the last example using the following statement:

public record UserRecord(string Name, int Age);

When utilizing records in C# to represent DTOs, you can benefit from several advantages:

Conciseness and readability:
Records’ syntax is more compact, reducing the amount of code required to define a data transfer object. This makes the code easier to read and understand, especially when compared to traditional classes.

Immutability by default:
Records are immutable by default, meaning their properties cannot be changed after the object is created. This immutability ensures that the transported data remains unmodified, avoiding unintended side effects.

Default method implementations:
Records automatically generate `Equals`, `GetHashCode`, and `ToString` method implementations. This ensures proper comparison and allows records to be used in collections like dictionaries and sets without issues.

With expression for updates:
Records enable the creation of updated copies of objects using the straightforward with expression feature. This is particularly useful when you need to modify some properties without altering the original object.

Comparison records as value type:
Records are value types and have value semantics. This means that when you compare two records, you are comparing their values, not their memory references.

Unlike classes, which have reference semantics, records have a comparison implementation that considers the values of their properties.

• • •

How do general classes compare objects?

It is possible to compare reference types in C#. However, when comparing reference types, you are comparing the memory addresses (references) of the objects, not their actual content.

Two references will be considered equal only if they point to the same memory location, i.e., if they refer to the same object instance in memory.

Default class comparison example:

class UserDTO
{
   public string Name { get; set; }
   public int Age { get; set; }
}

UserDTO user1 = new UserDTO { Name = "John", Age = 30 };
UserDTO user2 = new UserDTO { Name = "John", Age = 30 };

bool areEqualClasses = user1.Equals(user2); // Returns 'False'

It will return false since there are two different objects pointing to different memory references.

If you want to compare the actual content of objects of a reference type (the property values, for example), you need to override the Equals method and, optionally, the GetHashCode method. By doing so, you can specify how the comparison should be performed based on the properties or fields of the object.

Custom class comparison example:

public class UserDTO
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        UserDTO other = (UserDTO)obj;
        return Name == other.Name && Age == other.Age;
    }
}

UserDTO user1 = new UserDTO { Name = "John", Age = 30 };
UserDTO user2 = new UserDTO { Name = "John", Age = 30 };

bool areEqualClasses = user1.Equals(user2); // Returns 'True'

Overring the Equals method is enough to get true in a class comparison, although comparison using the == equality operator still returns false:

UserDTO user1 = new UserDTO { Name = "John", Age = 30 };
UserDTO user2 = new UserDTO { Name = "John", Age = 30 };

bool areEqualClasses = person1 == person2; // Returns 'False'

But we can solve it, by overriding the equality operator:

public class UserDTO
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        UserDTO other = (UserDTO)obj;
        return Name == other.Name && Age == other.Age;
    }

    // Override the equality operator ==
    public static bool operator ==(UserDTO left, UserDTO right)
    {
        if (left is null || right is null)
            return false;

        return left.Equals(right);
    }

    // Override the inequality operator !=
    public static bool operator !=(UserDTO left, UserDTO right)
    {
        return !(left == right);
    }
}

UserDTO user1 = new UserDTO { Name = "John", Age = 30 };
UserDTO user2 = new UserDTO { Name = "John", Age = 30 };

bool areEqualClasses = person1 == person2; // Returns 'True'

Overriding the == operator obligates us to also override the != operator, but now everything works like a charm.

• • •

How to use a record?

The following example shows how to define and use a record as DTO to hold an API result data.

public record UserRecord(string Name, int Age);

And it will automatically generate everything we defined earlier:

UserRecord user1 = new UserRecord("John", 30);
UserRecord user2 = new UserRecord("John", 30);

bool areEqualRecords2 = user1.Equals(user2);  //returns True
bool areEqualRecords1 = user1 == user2;       //returns True

user1.ToString();       //returns UserRecord { Name = John, Age = 30 }

user1.GetHashCode();    //returns 725737345
user2.GetHashCode();    //returns 725737345

As we can see records also internally implements the ToString and GetHashCode methods.

• • •

Final considerations

Using records in C# to represent DTOs is a powerful and efficient approach. They offer concise syntax, immutability by default, and automatic implementations of important methods, simplifying data transportation and manipulation.

By utilizing records for DTOs, you can improve code readability, ensure the immutability of transported data, and take advantage of automatically generated method implementations.

Thank you for reading it until here, I hope it can help you and your team make your code cleaner, less error-prone, and easier to maintain over time, see you next time.

Похожее
Jan 9
Author: Juan España
Content index Understanding HttpClientHandler and its importance in C# What is HttpClientHandler in C# Significance of HttpClientHandler in .NET applications Delve deeper into HttpClient C# How to use HttpClient C# C# HttpClient example for beginners Role of System.Net.Http.HttpClient in network...
Jan 22
Author: LeadEngineer
The Software Interview Question that you should be aware of! Exception handling is a crucial aspect of software development that allows you to manage and recover from unexpected or exceptional situations that can occur during program execution. These exceptional situations...
Sep 6, 2023
Author: Kenji Elzerman
To write files with C#, you have to know how to do it. The basics aren’t rocket science. There are a few lines of code you need to know. But writing and reading files with C# is something every beginning...
Feb 7, 2021
Author: Manikanta Pattigulla
Overview In this article, I'll explain about the best possible ways to implement the web API, designing of great web API and finally, the things to remember while implementing API. How to design or build great Web API Applications? Basically...
Написать сообщение
Тип
Почта
Имя
*Сообщение