avatarAshish Patel

Summary

AutoMapper is a library for transforming one object type to another in .NET and .NET Core, with configuration and usage explained for ASP.NET Core applications.

Abstract

The content of the webpage titled "Use AutoMapper in ASP.NET or ASP.NET Core" provides a detailed guide on how to use AutoMapper, a convention-based object-object mapper, in .NET and .NET Core applications. The guide includes steps for installing required NuGet packages, configuring AutoMapper in ASP.NET Core applications, and organizing mapping configurations using profiles. The article also covers AutoMapper features such as projection, nested mappings, conditional mappings, flattening, and reverse mapping and unflattening. The guide ends with a summary of the benefits of using AutoMapper and links to related resources.

Bullet points

  • AutoMapper is a convention-based object-to-object mapper used for transforming one object type to another in .NET and .NET Core.
  • To use AutoMapper, install the required NuGet packages, including AutoMapper and AutoMapper.Extensions.Microsoft.DependencyInjection.
  • Configure AutoMapper in ASP.NET Core applications by adding the configuration in the ConfigureServices method at application startup time.
  • Organize mapping configurations using profiles, which allow grouping common configuration and organizing mappings by usage.
  • AutoMapper features include projection, nested mappings, conditional mappings, flattening, and reverse mapping and unflattening.
  • AutoMapper provides simple configuration of types and simple testing of mappings.

Use AutoMapper in ASP.NET or ASP.NET Core

Getting started — How to use AutoMapper in .NET and .NET Core?

TL;DR

AutoMapper is a simple little library that helps to transform one object type to another. It is a convention-based object-to-object mapper that requires very little configuration.

AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B.

Most popular .NET Libraries every developer should know.

Configure AutoMapper in ASP.NET Core application

1. NuGet: To use AutoMapper, you need to install below NuGet packages.

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

2. Configuration: It should only happen once per AppDomain. That means the best place to put the configuration code is in application startup. ConfigureServices method is called by .NET Core at application startup time to register additional services. You define the configuration using profiles. And then you let AutoMapper know in what assemblies are those profiles defined by calling the IServiceCollection extension method AddAutoMapper at startup:

AutoMapper Profile

A good way to organize your mapping configurations is with profiles. It allow us to group common configuration and organize mappings by usage. This lets us put mapping configuration closer to where it is used, instead of a single file of configuration that becomes difficult to maintain.

Create classes that inherit from Profile and put the configuration in the constructor.

  • Profiles can be added to the main mapper configuration in a number of ways, either directly.
  • AutoMapper will scan the designated assemblies for classes inheriting from Profile and add them to the configuration.

3. Usage: You are done with AutoMapper configuration. Most applications can use dependency injection to inject the created IMapper instance.

AutoMapper uses a programming concept called Reflection to retrieve the type metadata of objects.

AutoMapper Features

Sample source code on GitHub.

Summary

AutoMapper provides simple configuration of types, as well as simple testing of mappings.

View more from .NET Hub

Happy Coding!!!

Automapper
Dotnet
Aspnetcore
Dotnet Core
Aspnet
Recommended from ReadMedium