AutoMapper in Asp.Net Core 2

Automapper Logo

There are countless situations in an Asp.Net Core application when you need to map objects by each other in terms of creating new instances by getting values from an existing instance that’s quite similar or completed different in rare cases.

What is AutoMapper?

AutoMapper is a simple little library built to solve a deceptively complex problem – getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?

How to implement in Asp.Net Core 2 Project?

After creating a new Asp.Net Core Web Application Project (no ), add two NuGet Packages:
AutoMapper (version: 6.1.1 at this time of writing of this post)
AutoMapper.Extensions.Microsoft.DependencyInjection (version: 3.0.1)

In Startup.cs, in ConfigureServices method you can activate AutoMapper by adding:

services.AddAutoMapper();

Then, you have to tell the library mapping logic or connection of entities with each other by creating a special class inherited by Profile. Here’s an example of two custom classes mapped to each other.

    public class MappingsProfile : Profile
    {
        public MappingsProfile()
        {
            CreateMap<Author, AuthorShort>()
                .ForMember(dest => dest.StyleOfWritting, config => config.Ignore())
                .ReverseMap();
        }
    }

To add profile to AutoMapper you have to change code in Startup class as in sample below:

services.AddAutoMapper(x=> x.AddProfile(new MappingsProfile()));

Now we’re ready to use the library in our project while Asp.Net exposes it through dependency injection system.
Here’s a usage example in our home controller.

        <strong>private readonly IMapper _iMapper;</strong>

        public HomeController(IMapper iMapper)
        {
            _iMapper = iMapper;
        }

        public IActionResult Index()
        {
            var list = new List<Author>
            {
                new Author
                {
                    ID = 1,
                    Name = "Author1",
                    Surname = "Auth1",
                    Birthdate = new DateTime(15,11,1971),
                    RegisteredDate = DateTime.UtcNow
                },
                new Author
                {
                    ID = 2,
                    Name = "Author2",
                    Surname = "Auth2",
                    Birthdate = new DateTime(15,11,1972),
                    RegisteredDate = DateTime.UtcNow
                },
                new Author
                {
                    ID = 3,
                    Name = "Author3",
                    Surname = "Auth3",
                    Birthdate = new DateTime(15,11,1973),
                    RegisteredDate = DateTime.UtcNow
                }
            };

            var authShortList = _iMapper.Map<AuthorShort>(list);

            return View(authShortList);
        }

Happy mapping!

Leave a Reply

Your email address will not be published.