In AutoMapper 4.2 version, static implementation is removed with instance approach. It could be a nightmare to migrate the codes to the newer version with instance approach. I will be putting up guide for migrating codes from the older version to instance approach if someone has used Profile based approach.
Previous implementation (old implementation)
Profile based implementation mapping
Single class for registering all AutoMapper configuration
The global mapping initialization under global.asax
Usage under controllers
Automapper Queryable Extensions implementation
These above processes were old approach with static methods calls.
In the newer version, there has to be instance based approach, the biggest advantage is dependency injection. I am hoping the API would improve and with this new move someone can create a standard for Mapping the domain models and view models later.
AutoMapper new implementation based on instance approach
Let's start with the order that I followed with the older approach. I will be using constructor dependency injection to wire the whole process.
Profile based implementation mapping
The IMapperConfiguration is a new interface for mapping models which we would be checking at a later stage.
Single class for registering all AutoMapper configuration
If you check changes, the static declaration is removed from class and function both. Implementation is also changed based on dependency injection.
Dependency injection initialization through Ninject (The global mapping initialization under global.asax)
I am including possible mappings which may depend based on application. You can choose based on need.
The MapperConfiguration has Action<IMapperConfiguration> type constructor parameter. In this, the anonymous action is created and IMapperConfiguration is created to be passed as a parameter for AutoMapperConfiguration .
The other key point is the creation of IMapper implementation instance through CreateMapper() method. This IMapper will be passed across all application to consume mappings.
IConfigurationProvider and IExpressionBuilder can be used for projecting LINQ queries to targetted mapped model.
Usage under controllers
In the controllers, the constructor parameters are taking care of instances for mappers. Mainly, IMapper is used of mapping objects.
Automapper Queryable Extensions implementation
For extension method ideally, IExpressionBuilder needs to be used for LINQ projection but due to some issue or incompatibilty with QueryableExtensions, I am using IConfigurationProvider to do projection.
Previous implementation (old implementation)
Profile based implementation mapping
public class UserProfileMapping
: Profile
{
protected override void Configure()
{
Mapper.CreateMap<UserProfile, PreferenceViewModel>()
.ForMember(vm => vm.UserId, m => m.MapFrom(profile => profile.Id));
}
}
Single class for registering all AutoMapper configuration
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new UserProfileMapping());
});
}
}
The global mapping initialization under global.asax
AutoMapperConfiguration.Configure();
Usage under controllers
var dbUserProfile = // call for getting info from DB.
var preference = new PreferenceViewModel();
Mapper.Map(dbUserProfile, preference);
Automapper Queryable Extensions implementation
var dbQuery = // Db call
dbQuery.Project().To<PreferenceViewModel>()
These above processes were old approach with static methods calls.
In the newer version, there has to be instance based approach, the biggest advantage is dependency injection. I am hoping the API would improve and with this new move someone can create a standard for Mapping the domain models and view models later.
AutoMapper new implementation based on instance approach
Let's start with the order that I followed with the older approach. I will be using constructor dependency injection to wire the whole process.
Profile based implementation mapping
public class UserProfileMapping
: Profile
{
private readonly IMapperConfiguration MappingConfiguration;
public UserProfileMapping(IMapperConfiguration config)
{
MappingConfiguration = config;
}
protected override void Configure()
{
MappingConfiguration
.CreateMap<UserProfile, PreferenceViewModel>()
.ForMember(vm => vm.UserId, m => m.MapFrom(profile => profile.Id));
}
}
The IMapperConfiguration is a new interface for mapping models which we would be checking at a later stage.
Single class for registering all AutoMapper configuration
public class AutoMapperConfiguration
{
public AutoMapperConfiguration(IMapperConfiguration mapperConfiguration)
{
Configure(mapperConfiguration);
}
public void Configure(IMapperConfiguration mapperConfiguration)
{
mapperConfiguration.AddProfile(new UserProfileMapping(mapperConfiguration));
}
}
If you check changes, the static declaration is removed from class and function both. Implementation is also changed based on dependency injection.
Dependency injection initialization through Ninject (
I am including possible mappings which may depend based on application. You can choose based on need.
// AutoMapper mapping
kernel.Bind<MapperConfiguration>()
.ToSelf()
.InRequestScope()
.WithConstructorArgument<Action<IMapperConfiguration>>(
cfg => new AutoMapperConfiguration(cfg));
kernel.Bind<IConfigurationProvider>().ToMethod(ctx => ctx.Kernel.Get<MapperConfiguration>());
kernel.Bind<IMapper>().ToMethod(maper => kernel.Get<MapperConfiguration>().CreateMapper()).InSingletonScope();
kernel.Bind<IExpressionBuilder>().ToConstructor(ctx => new ExpressionBuilder(kernel.Get<MapperConfiguration>()));
The MapperConfiguration has Action<IMapperConfiguration> type constructor parameter. In this, the anonymous action is created and IMapperConfiguration is created to be passed as a parameter for AutoMapperConfiguration .
The other key point is the creation of IMapper implementation instance through CreateMapper() method. This IMapper will be passed across all application to consume mappings.
IConfigurationProvider and IExpressionBuilder can be used for projecting LINQ queries to targetted mapped model.
Usage under controllers
In the controllers, the constructor parameters are taking care of instances for mappers. Mainly, IMapper is used of mapping objects.
public class AccountController
: Controller
{
private readonly IMapper Mapper;
public AccountController(IMapper modelMapper)
{
// Initialization codes
}
public ActionResult UserPreference()
{
var dbUserProfile = // call for getting info from DB.
var preference = new PreferenceViewModel();
Mapper.Map(dbUserProfile, preference);
// Code
}
}
Automapper Queryable Extensions implementation
For extension method ideally, IExpressionBuilder needs to be used for LINQ projection but due to some issue or incompatibilty with QueryableExtensions, I am using IConfigurationProvider to do projection.
public class AccountController
: Controller
{
private readonly IConfigurationProvider ConfigurationProvider;
public AccountController(IMapper modelMapper,
IConfigurationProvider configurationProvider)
{
// Initialization codes
}
public ActionResult UserPreference()
{
var dbQuery = // Db call
var res = dbQuery.ProjectTo<PreferenceViewModel>(ConfigurationProvider)
// Code
}
}
Comments
Post a Comment