AutoMapping is the process of automatically mapping data between different objects or models. It is commonly used to simplify the code and reduce the amount of manual mapping required when transferring data between the client and server.
Advantages of AutoMapping
- Reduces Manual Mapping
- Saves Development Time
- Improves Code Maintainability
- Supports Complex Mapping Scenarios
- Enhances Code Readability Facilitates Testing
- Supports Reverse Mapping
- Integrates with Dependency Injection
Here is a step by step explanation on how to use automapping in WebAPI along with an example.
Step 1: Install the required packages.
To use automapping we need to install the AutoMapper package, you can do this by using a package manager NuGet. Open the NuGet Package Manager and download the following packages:
AutoMapper.Etensions.Microsoft.DependencyInjection
AutoMapper
Don’t forget to add, “using AutoMapper;”
Step 2: Create required models
using System.ComponentModel.DataAnnotations;
namespace Employee.models
{
public class EmployeeDetails
{
[Key]
public int EmpId { get; set; }
public string? EmpName { get; set; }
public string? Position { get; set; }
public string? Address { get; set; }
public string? EmpPhone { get; set; }
}
}
namespace Employee.models
{
public class EmployeeInsert
{
public string? EmpName { get; set; }
public string? Position { get; set; }
public string? Address { get; set; }
public string? EmpPhone { get; set; }
}
}
Step 3: Configure AutoMapper in your Web API project. Create a new class called MappingProfile (or any other name you prefer) to configure AutoMapper mappings. Here’s an example
using AutoMapper;
namespace Employee.models
{
public class Empprofile : Profile
{
public Empprofile()
{
CreateMap<EmployeeInsert, EmployeeDetails>().ForMember(dest => dest.EmpName, opt => opt.MapFrom(src => src.EmpName));
}
}
}
Step 4: Register AutoMapper in your application and add the below code in your Program.cs file
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
Step 5: Use Automapping in your methods
Now you can use AutoMapper in your Web API methods for mapping. Inject the IMapper interface into your controller and use it to perform the mappings.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Employee.models;
using AutoMapper;
namespace Employee.Controllers
{
[ApiController]
public class EmpController : ControllerBase
{
private readonly EmployeeContext _context;
private IMapper Mapper;
public EmpController(EmployeeContext context,IMapper mapper)
{
_context = context;
this.Mapper = mapper;
}
[HttpPost("InsertEmpDetail")]
public IActionResult enterEmpDetail(EmployeeInsert emp)
{
var emplo = Mapper.Map<EmployeeDetails>(emp);
_context.EmployeeDetails.Add(emplo);
_context.SaveChanges();
return Ok();
}
//OtherMethods
}
}
In this example, mapper is an instance of IMapper injected into the controller’s constructor.
Summary
AutoMapper is a great tool to map your business objects to data transfer objects, eliminate the need of writing extensive code and hence reduce the clutter in your application’s code. You should use AutoMapper when you need a complex mapping of incompatible types in your application.
Thanks for posting, this blog is one of the best ones now on AutoMapper. I’ve been searching for easy understanding. I think the search is over now.
Thank you. Keep checking our blogs in our website. It will be more helpful for your learning.