问题
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