Data Transfer Object pattern in C#

|
| By Manik Malhotra

Data Transfer Object (DTO) is a design pattern used to transfer data between software application subsystems or layers, often between the data access layer and the business logic layer. The main purpose of a DTO in C# is to encapsulate data and send it across different parts of an application, providing a simple and consistent way to exchange information.

A simple example to illustrate the use of the DTO pattern in C#:


// Define a DTO class
public class EmployeeDTO
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set; }
}

// Business Logic Layer - Service class
public class EmployeeService
{
// Method to get employee data from the data access layer
public EmployeeDTO GetEmployeeById(int employeeId)
{
// Assume fetching employee data from a database
// In a real application, this could involve using an ORM (Object-Relational Mapper) or direct database queries

// For demonstration purposes, creating a dummy employee object
Employee employee = GetEmployeeFromDatabase(employeeId);

// Mapping employee data to DTO
EmployeeDTO employeeDTO = MapEmployeeToDTO(employee);

return employeeDTO;
}

// Dummy method to simulate fetching employee data from a database
private Employee GetEmployeeFromDatabase(int employeeId)
{
// In a real application, this would involve database queries
// For simplicity, creating a dummy employee object
return new Employee
{
EmployeeId = employeeId,
FirstName = "John",
LastName = "Doe",
Department = "IT"
};
}

// Map employee data to DTO
private EmployeeDTO MapEmployeeToDTO(Employee employee)
{
// Mapping relevant data from the Employee entity to the EmployeeDTO
return new EmployeeDTO
{
EmployeeId = employee.EmployeeId,
FirstName = employee.FirstName,
LastName = employee.LastName,
Department = employee.Department
};
}
}

// Entity class representing an employee
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set; }
}

// Application entry point
class Program
{
static void Main()
{
// Create an instance of the EmployeeService
EmployeeService employeeService = new EmployeeService();

// Get employee data by employeeId
EmployeeDTO employeeDTO = employeeService.GetEmployeeById(123);

// Displaying employee data from the DTO
Console.WriteLine($"Employee ID: {employeeDTO.EmployeeId}");
Console.WriteLine($"Name: {employeeDTO.FirstName} {employeeDTO.LastName}");
Console.WriteLine($"Department: {employeeDTO.Department}");

// The application can now use the employeeDTO object in the business logic layer
}
}

In this example,
The `EmployeeDTO` class is a Data Transfer Object representing the data structure transferred between layers.

The `EmployeeService` class acts as the service in the business logic layer, and it retrieves data from the data access layer (simulated by the `GetEmployeeFromDatabase` method).
The data is then mapped to the `EmployeeDTO` using the `MapEmployeeToDTO` method before being returned to the calling code in the business logic layer.

This pattern helps to separate concerns and maintain a clean architecture and organized code structure by encapsulating data transfer operations within DTOs, making it easier to manage and understand.

Leave a Reply

Your email address will not be published. Required fields are marked *