Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to define AutoMapper mappings?

Where in my ASP.NET MVC application should I define my AutoMapper mappings?

Mapper.CreateMap<User, UserViewModel>();

Presently I am defining these in the constructor of a BaseController, which all my Controlllers derive from. Is this the best place?

like image 554
Petrus Theron Avatar asked Apr 25 '11 09:04

Petrus Theron


2 Answers

I think it's kind of late to answer the question, but maybe someone can use my answer.

I use Ninject to resolve dependencies, so I've created Ninject Module for AutoMapper. Here is the code:

public class AutoMapperModule : NinjectModule
{
    public override void Load()
    {
        Bind<IConfiguration>().ToMethod(context => Mapper.Configuration);
        Bind<IMappingEngine>().ToMethod(context => Mapper.Engine);

        SetupMappings(Kernel.Get<IConfiguration>());

        Mapper.AssertConfigurationIsValid();
    }

    private static void SetupMappings(IConfiguration configuration)
    {
        IEnumerable<IViewModelMapping> mappings = typeof(IViewModelMapping).Assembly
            .GetExportedTypes()
            .Where(x => !x.IsAbstract &&
                        typeof(IViewModelMapping).IsAssignableFrom(x))
            .Select(Activator.CreateInstance)
            .Cast<IViewModelMapping>();

        foreach (IViewModelMapping mapping in mappings)
            mapping.Create(configuration);
    }
}

As you can see on load it scans assembly for implementaion of IViewModelMapping and then runs Create method.

Here is the code of IViewModelMapping:

interface IViewModelMapping
{
    void Create(IConfiguration configuration);
}

Typical implementation of IViewModelMapping looks like this:

public class RestaurantMap : IViewModelMapping
{
    public void Create(IConfiguration configuration)
    {
        if (configuration == null)
            throw new ArgumentNullException("configuration");

        IMappingExpression<RestaurantViewModel, Restaurant> map =
            configuration.CreateMap<RestaurantViewModel, Restaurant>();
// some code to set up proper mapping

        map.ForMember(x => x.Categories, o => o.Ignore());
    }
}
like image 149
Alexander Romanov Avatar answered Sep 28 '22 04:09

Alexander Romanov


As mentioned in this answer, AutoMapper has now introduced configuration profiles to organise your mapping configuration.

So for example, you could define a class to set up your mapping configuration:

public class ProfileToOrganiseMappings : Profile 
{
    protected override void Configure() 
    {
        Mapper.CreateMap<SourceModel, DestinationModel>();
        //other mappings could be defined here
    }
}

And then define a class to initialise the mappings:

public static class AutoMapperWebConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile(new ProfileToOrganiseMappings());
            //other profiles could be registered here
        });
    }
}

And then finally, call that class in your global.asax Application_Start() to configure those mappings:

protected void Application_Start()
{
    ...
    AutoMapperWebConfiguration.Configure();
}
like image 24
Dangerous Avatar answered Sep 28 '22 06:09

Dangerous