Wednesday, June 22, 2016

Setting up configuration for AutoMapper

I began using AutoMapper for mapping my Domain Model objects to my ViewModel objects on a relatively recent project and when the same requirement once again arose, I grabbed the latest version of AutoMapper from NuGet and began using it.

Unfortunately, the latest release of AutoMapper deprecated numerous methods of performing mappings, therefore, I was left to determine how to follow the new method of creating mappings in AutoMapper.

After reading this article on AutoMapper's GitHub wiki (https://github.com/AutoMapper/AutoMapper/wiki/Configuration), I still could not figure out how to map multiple objects!

After doing some peer programming with a colleague, we were able to determine the solution to this dilemma as follows:


Mapper.Initialize(cfg =>
{
                cfg.CreateMap<Foo, Bar>();
                cfg.CreateMap<Bar, Foo>();
});

All that was required was to add an additional configuration for each model mapping that was required and surround the entire mapping configuration in curly braces ({})!!


No comments:

Post a Comment