When you work as a developer, sometimes you need to interchange the data of two software systems. For this purpose, we use DTO which refers to the Data Transfer object. This technique is used to make conversation easy between API and server without revealing sensitive information. The process of DTO is quite costly as it takes time to exchange data between client and server. We can minimize server requests by consolidating data exchanges between the client and server into a single object, which represents various calls made over time. A DTO is primarily designed to reduce the volume of data that must be sent over a network or exchanged between different parts of a system. It is used in object-oriented programming languages such as Java, C++, Python, and ASP.NET. Now let’s understand how to use it in ASP.NET language.
Use of DTO in ASP.NET
Following are the steps to use Data Transfer Objects in ASP.NET
Step 1: Create a DTO class
Create a new class containing the attributes you intend to share between the API controller and the client application.
public class StudentDTO
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Class { get; set; }
}
Step 2: Domain Model and DTO mapping
Develop a transformation function responsible for aligning the attributes of the Data Transfer Object (DTO) with those of the Domain Model. This mapping function facilitates the correlation of DTO properties to corresponding properties in the Domain Model.
public static StudentDTO ToDTO(this Student student)
{
return new StudentDTO
{
Id = student.Id,
Name = student.Name,
Class = student.Class
};
}
Step 3: Use the DTO for Data Transfer
DTO is used to exchange the data between the controller and the client application.
public async Task<ActionResult<IEnumerable<StudentDTO>>> GetStudents()
{
var products = await _repository.GetProductsAsync();
return Ok(students.Select(student => student.ToDTO()));
}
Step 4: DTO in the Client Application
In the client application, utilize the Data Transfer Object (DTO) to convert the response data received from the API controller into a structured and usable format through deserialization.
public async Task<IEnumerable<StudentDTO>> GetStudentsAsync()
{
var response = await httpClient.GetAsync("api/students");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<IEnumerable<StudentDTO>>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
Why use Data transfer object?
- Employing Data Transfer Objects (DTOs) facilitates a distinct separation of responsibilities in your codebase. This simplifies long-term code maintenance, enabling modifications to the data model without disrupting the API response.
- DTOs provide a mechanism to filter out sensitive information or restrict the volume of data transmitted to the client.
- DTOs enable the selective transfer of essential data, contributing to enhanced application performance.
- Data Transfer Objects (DTOs) promote the separation of the data model from the API response, empowering developers to modify the data model independently of the API response. This separation streamlines code maintenance by dividing concerns.