I am using Automapper 5.2. I have used as a basis this link. I will describe, in steps, the process of setting up Automapper that I went through.
First I added Automapper to Project.json as indicated:
PM> Install-Package AutoMapper
Second I created a folder to hold all files relating to mapping called "Mappings"
Third I set up the configuration of Automapper in its own file in the mappings folder :
public class AutoMapperConfiguration
{
public MapperConfiguration Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<ViewModelToDomainMappingProfile>();
cfg.AddProfile<DomainToViewModelMappingProfile>();
cfg.AddProfile<BiDirectionalViewModelDomain>();
});
return config;
}
}
Forth Also in its own file in the mappings folder I set up the mapping profile as follows:
public class DomainToViewModelMappingProfile : Profile
{
public DomainToViewModelMappingProfile()
{
CreateMap<Client, ClientViewModel>()
.ForMember(vm => vm.Creator, map => map.MapFrom(s => s.Creator.Username))
.ForMember(vm => vm.Jobs, map => map.MapFrom(s => s.Jobs.Select(a => a.ClientId)));
}
}
Fifth, I added an extension method as its own file also in the mappings folder... this was used next in startup.cs:
public static class CustomMvcServiceCollectionExtensions
{
public static void AddAutoMapper(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
var config = new AutoMapperConfiguration().Configure();
services.AddSingleton<IMapper>(sp => config.CreateMapper());
}
}
Sixth I added this line into the ConfigureServices method in Startup.cs
// Automapper Configuration
services.AddAutoMapper();
Finally I used it in a controller to convert a collection...
IEnumerable<ClientViewModel> _clientVM = Mapper.Map<IEnumerable<Client>, IEnumerable<ClientViewModel>>(_clients);
Based on the question I referred to earlier I thought I had everything set so I could use it however I got an error on the line above in the Controller...
"Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."
What have I missed.. Is this the correct way to set up Automapper? I suspect I am missing a step here.. any help greatly appreciated.
See Question&Answers more detail:
os