C# 如何使用StructureMap指定要注入控制器的命名服务实例

C# 如何使用StructureMap指定要注入控制器的命名服务实例,c#,dependency-injection,structuremap,asp.net-core,C#,Dependency Injection,Structuremap,Asp.net Core,Microsoft Unity DI提供了一项功能,您可以在其中为同一界面注册多个对象并为它们命名。然后在构造函数中,可以使用带有参数的属性,通过名称指定要注入的对象 我想知道如何使用StructureMap实现同样的功能 例如: container .RegisterType<IMappingEngine, MappingEngine>("MappingEngineOne", new HierarchicalLifetimeManager(), new InjectionConst

Microsoft Unity DI提供了一项功能,您可以在其中为同一界面注册多个对象并为它们命名。然后在构造函数中,可以使用带有参数的属性,通过名称指定要注入的对象

我想知道如何使用StructureMap实现同样的功能

例如:

container
.RegisterType<IMappingEngine, MappingEngine>("MappingEngineOne", new HierarchicalLifetimeManager(), new InjectionConstructor(typeof(MappingEngineOneConfiguration)))
.RegisterType<IMappingEngine, MappingEngine>("MappingEngineTwo", new HierarchicalLifetimeManager(), new InjectionConstructor(typeof(MappingEngineTwoConfiguration)))

....

public class MyServiceAgent {
    private readonly IMappingEngine _mapper;
    public MyServiceAgent([Dependency("MappingEngineOne")] IMappingEngine mapper) {
        _mapper = mapper;
    }
}

public class MyOtherServiceAgent {
    private readonly IMappingEngine _mapper;
    public MyOtherServiceAgent ([Dependency("MappingEngineTwo")] IMappingEngine mapper) {
        _mapper = mapper;
    }
}
容器
.RegisterType(“MappingEngineOne”,新层次结构CallifeTimeManager(),新注入构造函数(typeof(MappingEngineOneConfiguration)))
.RegisterType(“MappingEngineTwo”,新层次结构CallifeTimeManager(),新注入构造函数(typeof(MappingEngineTwoConfiguration)))
....
公共类MyServiceAgent{
专用只读IMappingEngine映射器;
公共MyServiceAgent([Dependency(“MappingEngineOne”)]IMappingEngine映射器){
_映射器=映射器;
}
}
公共类服务代理{
专用只读IMappingEngine映射器;
公共MyOtherServiceAgent([Dependency(“MappingEngineTwo”)]IMappingEngine映射器){
_映射器=映射器;
}
}

为什么需要这样做?这是个坏习惯。您的类不应该知道应该接收哪些实现。我们有多个服务需要使用特定配置实例化IMappingEngine实例。你将如何着手解决这个问题?看看。