Entity To DTO Efficient Mapping
What is the built in lightweight asp.net core Entity to DTO auto maping method, alternative to Auto Mapper?
A built-in lightweight alternative to AutoMapper for entity-to-DTO mapping in ASP.NET Core is using LINQ projections. This method leverages LINQ to project your entities into DTOs without needing an external library.
Here’s an example of how you can use LINQ projections to map an entity to a DTO:
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set; }
}
public class EmployeeDto
{
public int EmployeeId { get; set; }
public string FullName { get; set; }
public string Department { get; set; }
}
public static class EmployeeMapper
{
public static EmployeeDto MapToDto(Employee employee)
{
return new EmployeeDto
{
EmployeeId = employee.EmployeeId,
FullName = $"{employee.FirstName} {employee.LastName}",
Department = employee.Department
};
}
}
Let’s call it from the GetEmployees
method in the service layer, which can be called from the Razor Page model.
public class EmployeeService
{
private readonly ApplicationDbContext _dbContext;
public EmployeeService(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public List GetEmployees()
{
return _dbContext.Employees
.Select(e => new EmployeeDto
{
EmployeeId = e.EmployeeId,
FullName = $"{e.FirstName} {e.LastName}",
Department = e.Department
})
.ToList();
}
}
To call the EmployeeService
in your Employee/index.cshtml.cs
Razor Page, you need to:
Inject the
IEmployeeService
into theIndexModel
.Use the service to get the data within the
OnGet
method.
Here’s how you can do it:
Inject the
IEmployeeService
into yourIndexModel
:
public class IndexModel : PageModel
{
private readonly IEmployeeService _employeeService;
public IndexModel(IEmployeeService employeeService)
{
_employeeService = employeeService;
}
public List Employees { get; set; }
public void OnGet()
{
Employees = _employeeService.GetEmployees();
}
}
Display the data in your Index.cshtml
:
@page
@model Employee.IndexModel
<h2>Employees</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
@foreach (var employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.FullName</td>
<td>@employee.Department</td>
</tr>
}
</tbody>
</table>
This setup ensures that when the OnGet
method is called (which happens when the page is loaded), it retrieves the list of employees from the EmployeeService
and assigns it to the Employees
property. The Razor Page then uses this property to render the data in an HTML table.
If you are using traditional MVC, in your controller, you can call the GetEmployee:
public class EmployeeController : Controller
{
private readonly IEmployeeService _employeeService;
public EmployeeController(IEmployeeService employeeService)
{
_employeeService = employeeService;
}
public IActionResult GetEmployees()
{
var employees = _employeeService.GetEmployees();
return Ok(employees);
}
}
You can use LINQ to project the entities to DTOs if you don’t have a Service Layer:
public class EmployeeController : Controller
{
public IActionResult GetEmployees()
{
var employees = _dbContext.Employees.Select(e => EmployeeMapper.MapToDto(e)).ToList();
return Ok(employees);
}
}
This approach keeps your code simple and avoids the overhead of an external library like AutoMapper.