Where to define AutoMapper mappings?

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

Mapper.CreateMap<User, UserViewModel>(); 

I am currently defining them in the BaseController constructor, from which all my Controllers are derived. Is this the best place?

+7
source share
3 answers

I think itโ€™s too late to answer the question, but maybe someone can use my answer.

I use Ninject to resolve dependencies, so I created the 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 when loading, it scans the assembly to implement IViewModelMapping, and then runs the Create method.

Here is the IViewModelMapping code:

 interface IViewModelMapping { void Create(IConfiguration configuration); } 

A typical implementation of IViewModelMapping is as follows:

 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()); } } 
+9
source

As mentioned in this answer , AutoMapper has now introduced the profiles configuration in the organize display configuration.

So, for example, you can define a class to configure the display 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 initialize the mappings:

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

And finally, call this class in your global.asax Application_Start() to configure these mappings:

 protected void Application_Start() { ... AutoMapperWebConfiguration.Configure(); } 
+1
source

The code you refer to looks like AutoMapper, not StructureMap.

If you use the static Mapper method, the configuration should only occur once in the AppDomain. This means the best place for the configuration code is in the launch application, for example, the Global.asax file for ASP.NET applications. Typically, the bootstrapper configuration class is in its class, and this bootstrapper class is called from the start method.

http://automapper.codeplex.com/wikipage?title=Getting%20Started&referringTitle=Home

0
source

All Articles