Reputation: 2033
Jimmy Bogart has an article on using Automapper with an IoC container. He has an example using StructureMap but I am using Unity and I'm not sure how to use an InjectionConstructor properly.
Below is the code from the Article and below that is my poor attempt. Can anyone tell me how to do this properly?
public class ConfigurationRegistry : Registry
{
public ConfigurationRegistry()
{
ForRequestedType<Configuration>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.OfConcreteType<Configuration>()
.CtorDependency<IEnumerable<IObjectMapper>>().Is(expr => expr.ConstructedBy(MapperRegistry.AllMappers));
ForRequestedType<IConfigurationProvider>()
.TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>());
ForRequestedType<IConfiguration>()
.TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>());
}
}
My attempt:
container.RegisterType<IConfiguration, Configuration>(new SingletonLifetime())
.Configure<InjectedMembers>()
.ConfigureInjectionFor<Configuration>(
new InjectionConstructor(typeof(IEnumerable<IObjectMapper>)), MapperRegistry.AllMappers);
Upvotes: 1
Views: 1040
Reputation: 2033
This is what I ended up doing:
IEnumerable<IObjectMapper> allMappers = new List<IObjectMapper>() {
new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()),
new StringMapper(),
new FlagsEnumMapper(),
new EnumMapper(),
new ArrayMapper(),
new DictionaryMapper(),
new EnumerableMapper(),
new AssignableMapper(),
//new TypeConverterMapper(),
new NullableMapper(),
};
container.RegisterType<Configuration>(new SingletonLifetime())
.Configure<InjectedMembers>()
.ConfigureInjectionFor<Configuration>(
new InjectionConstructor(allMappers));
container.RegisterType<IConfigurationProvider, Configuration>();
container.RegisterType<IConfiguration, Configuration>();
container.RegisterType<IMappingEngine, MappingEngine>();
This works but if someone else has a better implementation I'm all ears and this still has a bounty on it.
Upvotes: 1