I am following the pattern used here, but it seems that it doesn't work for mappings unless I call AutoMapperWebConfiguration.Configure()
directly before the mapping takes place.
I was under the impression that configuration for AutoMapper was very expensive -- what's the point of following this pattern if I need to call Configure()
before every call?
AutoMapperWebConfiguration:
public static class AutoMapperWebConfiguration
{
private static List<Profile> _profiles;
public static void Configure(List<Profile> profiles)
{
_profiles = profiles;
Configure();
}
public static void Configure()
{
Mapper.Initialize(cfg =>
{
foreach (var profile in GetProfiles())
{
cfg.AddProfile(profile);
}
});
}
public static List<Profile> GetProfiles()
{
var profiles = new List<Profile>
{
new UserViewModelProfile(), new OrderViewModelProfile()
};
profiles.AddRange(_profiles);
return profiles;
}
}
public class UserViewModelProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDetailViewModel>();
}
}
}
The mapping:
var userDetailViewModels = Mapper.Map<List<User>, List<UserDetailViewModel>>(users);
The call to AutoMapperWebConfiguration.Configure()
occurs in Application_Start()
:
AutoMapperWebConfiguration.Configure(AutoMapperCoreConfiguration.GetProfiles());
When this action method is loaded (where the mapping appears), I get the exception:
Missing type map configuration or unsupported mapping.
This disappears when I call AutoMapperWebConfiguration.Configure() directly before the mapping takes place. Am I doing something wrong?