Skip to content

Instantly share code, notes, and snippets.

@JasonMore
Forked from darrencauthon/gist:1036421
Created June 20, 2011 19:58
Show Gist options
  • Save JasonMore/1036431 to your computer and use it in GitHub Desktop.
Save JasonMore/1036431 to your computer and use it in GitHub Desktop.
AutoMapperAssist example
Mapper.CreateMap<TestClass, TestClassViewModel>()
.ForMember(dest => dest.Bar, opt => opt.MapFrom(src => src.Foo));
var map = new Mapper<TestClass, TestClassViewModel>(Mapper.Engine);
@darrencauthon
Copy link

Ah, that's not a bad idea! It's actually why I created those constructors that accept a mapping engine or an AutoMapper IConfiguration instance: To make it possible to string these mappers together. You can still have a single instance of a configuration or a mapping engine, like you'd probably have using AutoMapper itself, but the mapping can still be executed behind these mappers.

If there's a "recommended" way to use AutoMapperAssist, it is to share the same IConfiguration object just like one would do with AutoMapper itself. I believe much of the cost of using AutoMapper is the CreateMap<> method, and if you use AutoMapperAssist's default constructor on the mapper, you'll pay that cost every time the mapper is instantiated. There are a number of ways to handle that, but the cleanest and simplest way might just be to load an instance of IConfiguration into your IoC container and let the mappers resolve from it.

@JasonMore
Copy link
Author

Interesting idea, I'll have to play around with resolving an IConfiguration from my IoC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment