Using AutoMapper
AutoMapper is a popular library that uses a convention-based, object-to-object mapper. Hence, AutoMapper lets you map objects without writing a ton of code, which you will see in a bit.
Now we need to set up our mapping of objects to objects automatically using the AutoMapper
NuGet package, which is authored by the same person who wrote the MediatR
library. I love AutoMapper
because it simplifies mapping and projections.
The following command installs AutoMapper
:
dotnet add package AutoMapper
The following command installs the AutoMapper
extensions for ASP.NET Core:
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
Now let's create the files for the mapper. Create a new directory and name it Mappings
inside Common
of the Travel.Application
project.
After creating the Mappings
folder, create two C# files: ImapFrom.cs
and MappingProfile.cs
:
// IMapFrom.cs
using AutoMapper; namespace Travel.Application.Common.Mappings...