NashTech Blog

Table of Contents

AutoMapper in .NET is an object-to-object mapping library that simplifies copying data between objects with different structures, especially in scenarios like transforming data transfer objects (DTOs) into domain models. It eliminates the need for manual property mapping by automatically matching properties by name, reducing boilerplate code and improving maintainability.

Installing AutoMapper

There are two ways to install AutoMapper into your project. You can either use Package Manager Console or you can use Dotnet CLI.

Using Package Manager Console

Install-Package AutoMapper
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

Using Dotnet CLI

dotnet add package AutoMapper
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection

These commands fetch and install the latest AutoMapper package into your project. AutoMapper.Extensions.Microsoft.DependencyInjection is an extension package that is used for dependency injection of AutoMapper.

Setting Up AutoMapper in Program.cs

After the packages are installed, we moved on to configuring them in the Program.cs file.

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

This code snippet ensures that AutoMapper is registered with the .NET Core dependency injection system.

Designing Class & Mappings

Consider a Movie class that contains various properties, such as strings, a child class, a list of child classes, DateTime, integers, and more.

public class Movie
{
   public string Title { get; set; }
   public DateTime ReleaseDate { get; set; }
   public Director Director { get; set; }
   public List<Actor> Cast { get; set; }
   public int Rating { get; set; }
   // Other properties...
}
public class MovieDto
{
   public string Title { get; set; }
   public int ReleaseYear { get; set; }
   public string DirectorName { get; set; }
   public List<string> CastNames { get; set; }
   public int Rating { get; set; }
   // Other properties...
}
public class Director
{
   public string Name { get; set; }
   // Other properties...
}
public class Actor
{
   public string Name { get; set; }
   public bool IsLead { get; set; }
   // Other properties...
}

Defining Mapping Profiles

A mapping profile in AutoMapper serves as a blueprint for converting one object type to another.

public class MovieProfile : Profile
{
   public MovieProfile()
   {
       CreateMap<Movie, MovieDto>();
       // Add more mappings here
   }
}

The code snippet defines a class MovieProfile that extends the Profile class from AutoMapper to streamline object transformations.

  • MovieProfile is a public class inheriting from the Profile class.
  • Inside the constructor public MovieProfile(), the mappings are set up.
  • CreateMap<Movie, MovieDto>() establishes a mapping between Movie (source) and MovieDto (destination).
  • Movie represents a movie entity in the database, while MovieDto is a simplified or tailored version of Movie used in API responses.

Benefits of AutoMapper

  • Reduces Boilerplate Code: AutoMapper automates the process of mapping properties between objects, reducing the need for repetitive, manual property assignments.
  • Improves Code Maintainability: It centralizes mapping logic, making the code cleaner and easier to maintain.
  • Simplifies Complex Mappings: AutoMapper can handle complex object hierarchies, including nested objects and collections, with minimal configuration.
  • Enhances Readability: It makes code more readable by abstracting the mapping logic, allowing developers to focus on core business logic.
  • Supports Custom Mapping Rules: AutoMapper provides the ability to configure custom mapping rules for cases where the property names or types don’t directly match.

Conclusion

In conclusion, AutoMapper is a powerful and efficient library in .NET that significantly simplifies the process of object-to-object mapping. By automating property assignments, it reduces boilerplate code and enhances maintainability. With its flexibility to handle complex mappings, support for custom rules, and seamless integration with the .NET Core dependency injection system, AutoMapper is an indispensable tool for developers looking to streamline data transformation tasks in their applications.

Picture of Vipul Kumar

Vipul Kumar

Leave a Comment

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

Suggested Article

Scroll to Top