InterData

Get a Quote

If you require any of our services listed on this site, please fill out this form with as many details as possible in the description field. This will enable our support team to understand and make a better decision regarding your requirements.

Edit Template

Get a Quote

If you require any of our services listed on this site, please fill out this form with as many details as possible in the description field. This will enable our support team to understand and make a better decision regarding your requirements.

Edit Template

Entity To DTO Efficient Mapping Without Auto Mapper

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:

  1. Inject the IEmployeeService into the IndexModel.

  2. Use the service to get the data within the OnGet method.

Here’s how you can do it:

  1. Inject the IEmployeeService into your IndexModel:

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.

Leave a Reply

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

  • All Posts
  • Articles
  • Default
  • IT Services
  • Software
  • Web
    •   Back
    • Computer Hardware
    • Networking
    •   Back
    • Design
    • Marketing
    • Social Media
    • Web Development
    •   Back
    • News
    •   Back
    • Programming
Monolithic Architecture

Wednesday, 2, October, 2024/

Understanding Monolithic Architecture in Software Development 📢 Monolithic architecture is a traditional model for designing software applications. It is a...

Load More

End of Content.

Stay Tuned...

© 2023 – 2025  InterData.au

Black Friday Sale - 50% Discount

Day
Hr
Min
Sec
Yes, I Want This!
No Thanks, I don't want to save