I got a very simple question... Is it possible to setup a AutoMapper like this:
public IMapper Init()
{
var config = new MapperConfiguration(cfg =>
{
cfg = MappingModelsToViewModels(cfg);
});
return config.CreateMapper();
}
I splitted each mapping in to method like this:
public IMapperConfigurationExpression MappingModelsToViewModels(IMapperConfigurationExpression cfg)
{
cfg = SKU(cfg);
cfg = Lot(cfg);
cfg = SalesRate(cfg);
cfg = SpecialSalesRate(cfg);
cfg = Order(cfg);
//...
return cfg;
}
public IMapperConfigurationExpression SKU(IMapperConfigurationExpression cfg)
{
// HTTPGET
cfg.CreateMap<SKU, SKUViewModel>() //...
return cfg;
}
I ask because I got this error:
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.
And I did a test by moving a part of the mapping code in to the new MapperConfiguration and it was working.
Thanks,
David
I usually accomplish this by using the
profile
optionautomapper
has. If gives both flexibility and less places to modify later on-The Profile definition, done by deriving from
AutoMapper
Profile
class-Then initialise automapper and load profiles -
Or you can let
AutoMapper
detect the profiles by itself -more can be found here -
https://github.com/AutoMapper/AutoMapper/wiki/Configuration#profile-instances