Created
February 10, 2016 13:31
-
-
Save martijnburgers/d077b050b50f04fa36c3 to your computer and use it in GitHub Desktop.
Our Autofac module for AutoMapper 2.1.267
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 AutoMapperModule : Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterType<ConfigurationStore>() | |
.As<ConfigurationStore>() | |
.WithParameter(new TypedParameter(typeof (IEnumerable<IObjectMapper>), MapperRegistry.AllMappers())) | |
.SingleInstance(); | |
builder.Register(c => c.Resolve<ConfigurationStore>()).As<IConfigurationProvider>(); | |
builder.Register(c => c.Resolve<ConfigurationStore>()).As<IConfiguration>(); | |
builder.RegisterType<TypeMapFactory>().As<ITypeMapFactory>(); | |
builder.RegisterType<MappingEngine>().As<IMappingEngine>().SingleInstance(); | |
var assembliesToScan = GetAssembliesToScan(); | |
builder.RegisterAssemblyTypes(assembliesToScan) | |
.AsClosedTypesOf(typeof(ITypeConverter<,>)) | |
.AsSelf(); | |
builder.RegisterAssemblyTypes(assembliesToScan) | |
.AsClosedTypesOf(typeof(ValueResolver<,>)) | |
.AsSelf(); | |
builder.RegisterAssemblyTypes(assembliesToScan) | |
.AssignableTo<Profile>() | |
.Where(i => i.GetConstructors().Any()) | |
.As<Profile>() | |
.AsSelf(); | |
} | |
private static Assembly[] GetAssembliesToScan() | |
{ | |
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
if (String.IsNullOrWhiteSpace(path)) | |
throw new InvalidOperationException("AutoMapperModule failed because it could not determine a path to scan."); | |
DirectoryInfo di = new DirectoryInfo(path); | |
var assemblies = di.GetFiles("TODO_PATTERN*.dll") | |
.Select(fi => fi.FullName) | |
.Select(Assembly.LoadFrom); | |
return assemblies.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment