Introduction
This article should help you to wire up automapper into your project.
I am using .NET Core 2.0 and AutoMapper 6.1.1.
Install
First, install SyrianBallaS.AutoMapper.Extensions.Microsoft.DependencyInjection.Signed
nuget package. (I am using version 3.2.0.)
Code
In Startup.cs, add this line to ConfigureServices
method.
services.AddAutoMapper();
Result:
Startup.cs
using AutoMapper;
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAutoMapper();
}
Now, you just have to create your Mapping profiles.
Here is an example of mine:
using AutoMapper;
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<V1Response, ConfigurationResponse>()
.ForMember(a => a.ModelId, b=> b.MapFrom(c=> c.ModelName));
}
}
Mapping
And here is how you can map your objects.
Here is an example of mine:
var result = Mapper.Map<V1Response, ConfigurationResponse>(v1);
Summary
If you want to know how to setup automapper for ASP MVC, have a look at my previous article.