AutoMapper registration in Unity DI

2019-04-11 17:59发布

I'm not that familiar with Unity or StructureMap. How do you convert the following StructureMap registration sample into Unity registration syntax?

    public class ConfigurationRegistry : Registry
    {
        public ConfigurationRegistry()
        {
            ForRequestedType<ConfigurationStore>()
                .CacheBy(InstanceScope.Singleton)
                .TheDefault.Is.OfConcreteType<ConfigurationStore>()
                .CtorDependency<IEnumerable<IObjectMapper>>().Is(expr => expr.ConstructedBy(MapperRegistry.AllMappers));

            ForRequestedType<IConfigurationProvider>()
                .TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<ConfigurationStore>());

            ForRequestedType<IConfiguration>()
                .TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<ConfigurationStore>());

            ForRequestedType<IMappingEngine>().TheDefaultIsConcreteType<MappingEngine>();

            ForRequestedType<ITypeMapFactory>().TheDefaultIsConcreteType<TypeMapFactory>();
        }
    }

1条回答
三岁会撩人
2楼-- · 2019-04-11 18:11

I think I got it. Let me know if I missed something.

Container
 .RegisterType<ConfigurationStore, ConfigurationStore>
                              (
                                 new ContainerControlledLifetimeManager()
                               , new InjectionConstructor(typeof(ITypeMapFactory)
                               , MapperRegistry.AllMappers())
                              )
 .RegisterType<IConfigurationProvider, ConfigurationStore>()
 .RegisterType<IConfiguration, ConfigurationStore>()
 .RegisterType<IMappingEngine, MappingEngine>()
 .RegisterType<ITypeMapFactory, TypeMapFactory>();
查看更多
登录 后发表回答