简体   繁体   English

如何使用StructureMap将不同的混凝土绑定到属性

[英]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));
} 

Update 更新

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

Original Answer 原始答案

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.

相关问题 如何使用structuremap在基类上为所有子类设置属性? - How do I set a property on a base class for all classes that subclass it, using structuremap? 如何绑定到属性? - How do I bind to a property? 如何使用 WPF 中的代码绑定 DataGridTextColumn 的可见性属性? - How do I bind the visibility property of a DataGridTextColumn using code in WPF? 如何使用StructureMap将属性注入自定义ActionFilterAttribute中? - How to inject a property into a custom ActionFilterAttribute using StructureMap? WPF中的依赖属性 - 如何绑定它? - dependent property in WPF — how do I bind to it? 如何将标签绑定到Xamarin中的属性? - How do I bind a Label to a property in Xamarin? 如何配置StructureMap以创建DbConnection - How do I configue StructureMap to create a DbConnection 如何将样式属性绑定到模板的属性 - How do I bind a style property to the property of a template 当我只有类型名称中的字符串时,如何使用structuremap解析类型 - Using structuremap how do I resolve a type when I only have a string from the type name 如何使用Scan与“贪婪”构造函数一起使用StructureMap和通用非闭合类型 - How do I use StructureMap with generic unclosed types using Scan with a “greedy” constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM