如何在结构图3中进行setter注入

时间:2014-04-09 12:37:34

标签: c# dependency-injection ioc-container structuremap structuremap3

我已经更新到了structmap 3,现在我无法使用FillAllPropertiesOfType进行setter注入。

是否已弃用,我应该使用什么?

1 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。看起来新方法是通过Registry.PoliciesExpression

public interface IInjectable
{
    string Test();
}

public class Injectable : IInjectable
{
    public string Test()
    {
        return this.GetType().ToString();
    }
}

public class InjectTarget
{

    public IInjectable Injectable
    {
        get;
        set;
    }

}

static class Program
{
    static void Main()
    {

        ObjectFactory.Configure(x =>
        {
            //Setter injection
            x.Policies.FillAllPropertiesOfType<IInjectable>().Use<Injectable>();

            //Standard registration
            x.For<IInjectable>().Use<Injectable>();
            x.For<InjectTarget>().Singleton().Use<InjectTarget>();
        });

        var test = ObjectFactory.GetInstance<InjectTarget>();

        Console.WriteLine(test.Injectable.Test()); //WindowsFormsApplication3.Injectable
    }
}