Skip to content

Instantly share code, notes, and snippets.

@wshirey
Created February 10, 2016 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wshirey/71d1d0d5ff6d7ceeefd2 to your computer and use it in GitHub Desktop.
Save wshirey/71d1d0d5ff6d7ceeefd2 to your computer and use it in GitHub Desktop.
How to register AutoMapper profiles with IoC dependencies using structuremap
public class AutoMapperRegistry : Registry
{
public AutoMapperRegistry()
{
Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.AddAllTypesOf<Profile>();
});
For<MapperConfiguration>().Use("Build AutoMapper config", ctx =>
{
var profiles = ctx.GetAllInstances<Profile>();
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
return config;
});
For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
}
}
@dataneek
Copy link

Nice!

@dataneek
Copy link

You may want to give the MappingConfiguration a lifecycle of singleton (Line 22). Depends if you want the mapping configuration to be created once or for every nested container.

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