[英]How Do I Bind Different Concretes to a Property Using StructureMap
They use StructureMap for IoC where I am currently working. 他们在我目前正在工作的IoC中使用StructureMap。
I have an application class that will implement multiple properties of the same interface...and I need to bind DIFFERENT IMPLEMENTATIONS 我有一个应用程序类,将实现相同接口的多个属性...我需要绑定不同的实现
...and no, I cannot do this: IProvider<T>
......不,我不能这样做:
IProvider<T>
FOR INSTANCE: 例如:
public class MyApplication
{
[SetterProperty]
public IProvider OneProvider { get; set; }
[SetterProperty]
public IProvider TwoProvider { get; set; }
}
public class FooProvider: IProvider {
// I would like to force this one to bind-on OneProvider ...
}
public class BarProvider: IProvider {
// I would like to force this one to bind-on TwoProvider ...
}
In Unity, there are many ways to do this, for instance; 例如,在Unity中,有很多方法可以做到这一点;
[Dependency("FooProvider")]
public IProvider OneProvider { get; set; }
[Dependency("BarProvider")]
public IProvider TwoProvider { get; set; }
...however, StructureMaps SetterProperty attribute doesnt allow for this. ...但是,StructureMaps SetterProperty属性不允许这样做。
QUESTION: 题:
How do I bind different implementations into an instance-property? 如何将不同的实现绑定到实例属性?
SAMPLE REGISTRY: 样本登记:
Here is an example of what my registry might look like... 这是我的注册表可能是什么样子的一个例子......
public ContainerRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.LookForRegistries();
scan.AssembliesFromApplicationBaseDirectory(f => f.FullName.StartsWith("My.Awesome.Company", true, null));
scan.AddAllTypesOf(typeof(IApplication));
scan.AddAllTypesOf(typeof(IManager<>));
scan.AddAllTypesOf(typeof(IProvider));
scan.AddAllTypesOf(typeof(IUnitOfWorkFactory<>));
scan.SingleImplementationsOfInterface();
});
For(typeof(IApplication)).Use(typeof(MyApplication));
}
According to documentation, you can use Inline Setter Configuration 根据文档,您可以使用Inline Setter配置
the setter policies in the next section can still be filled by StructureMap if an inline dependency is configured matching that setter property as shown in the example below:
如果内联依赖项配置为匹配该setter属性,则下一部分中的setter策略仍可由StructureMap填充,如下例所示:
public class MyApplication : IApplication {
public IProvider OneProvider { get; set; }
public IProvider TwoProvider { get; set; }
}
public class RuleWithSettersRegistry : Registry {
public RuleWithSettersRegistry() {
//...other code removed for brevity
For<IApplication>().Use<MyApplication>()
// I would like to force FooProvider to bind-on OneProvider
.Setter(x => x.OneProvider).Is<FooProvider>()
// I would like to force BarProvider to bind-on TwoProvider
.Setter(x => x.TwoProvider).Is<BarProvider>();
}
}
If you are able to modify the implementations and the target class then try to differentiate the intended dependencies. 如果您能够修改实现和目标类,那么尝试区分预期的依赖项。
public interface IFooProvider : IProvider { }
public interface IFooProvider : IProvider { }
Have the implementations 有实现
public class MyApplication {
[SetterProperty]
public IFooProvider OneProvider { get; set; }
[SetterProperty]
public IBarProvider TwoProvider { get; set; }
}
public class FooProvider: IFooProvider { }
public class BarProvider: IBarProvider { }
That way when registering all IProvider
derived implementations with 这样就可以注册所有
IProvider
派生的实现
scan.AddAllTypesOf(typeof(IProvider));
it will grab the intended implementations and bind them. 它将获取预期的实现并绑定它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.