AutoMapper registration in Unity DI

[亡魂溺海] 提交于 2020-01-13 04:55:21

问题


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:


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>();


来源:https://stackoverflow.com/questions/7797083/automapper-registration-in-unity-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!