Created
February 10, 2016 15:31
-
-
Save wshirey/71d1d0d5ff6d7ceeefd2 to your computer and use it in GitHub Desktop.
How to register AutoMapper profiles with IoC dependencies using structuremap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
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
Nice!