0

I did put my mapping literally inside the Application_Start() just like this:

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();

  WebApiConfig.Register(GlobalConfiguration.Configuration);
  FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  RouteConfig.RegisterRoutes(RouteTable.Routes);
  BundleConfig.RegisterBundles(BundleTable.Bundles);
  AuthConfig.RegisterAuth();

  Mapper.CreateMap<Item, StockItems>(); <-- Here
}

Is there a better way of doing it?

Boy Pasmo
  • 8,021
  • 13
  • 42
  • 67
  • 3
    Possible duplicate of http://stackoverflow.com/questions/6825244/where-to-place-automapper-createmaps – Perfect28 Jun 26 '14 at 14:24

1 Answers1

0

I'm assuming that when you say "better", you're accepting alternatives? If so, if the performance hit is acceptable, you can use DynamicMap<TSource, TDestination>(TSource) whenever you need to map. This prevents having to create mappings:

var stockItem = Mapper.DynamicMap<Item, StockItems>(item);
George Howarth
  • 2,767
  • 20
  • 18
  • Wow! Didn't know you could do this. Thanks mate. What's the difference with `Mapper.Create` to `Mapper.DynamicMap`? I would really love to hear your thoughts. – Boy Pasmo Jun 26 '14 at 14:50
  • @BoyPasmo Basically, `DynamicMap` both creates a mapping configuration and maps the object, whereas `Map` requires you to call `CreateMap` first. To be honest, I'm not sure if AutoMapper internally caches the configuration on a call to `DynamicMap` which is why you should be aware of the performance implications in a per-request scenario. – George Howarth Jun 26 '14 at 14:55