StructureMap .ForConcreteType <t>()。Configure.SetProperty配置无法使用BuildUp逻辑播放</t>

时间:2011-08-24 12:52:08

标签: dependency-injection structuremap

试图弄清楚如何为属性setter注入配置StructureMap。我有通过容器解析实例的场景以及我在其他地方获取实例然后调用congainer.BuildUp()功能的场景。

我在下面列出了一组示例类,我想为具体类型的Bar配置注入。如果我使用下面标记为1)-5)的不同选项,我将从StructureMap获得不同的行为。有些情况必须失败,因为它们不完整,但我在Foo和Qux属性上配置注入的所有情况都应该有效。他们不。特别是.BuildUp()功能不起作用。

而不是我详细解释每个结果,或许某些StructureMap可能会如此善意地指出我的逻辑中的缺陷和/或运行示例代码?

我猜测还有其他一些配置工作,但我希望能够明确我对Bar的注意事项。

提前致谢!

[TestClass]
public class StructureMapTests
{
    public interface IFoo { }
    public interface IBaz { }
    public interface IQux { }

    private class Foo : IFoo { }

    private class Baz : IBaz { }

    private class Qux : IQux { }

    private class Bar
    {
        public IFoo Foo { get; set; }

        public IFoo FooSomething { get; set; }

        public IBaz Baz { get; set; }

        public IQux Qux { get; set; }
    }

    Container Container;

    [TestInitialize]
    public void Configure_StructureMap()
    {
        Container = new Container();
        Container.Configure(x =>
            {
                x.For<IFoo>().Use<Foo>();
                x.For<IBaz>().Use<Baz>();
                x.For<IQux>().Use<Qux>();

                x.FillAllPropertiesOfType<IBaz>();
                x.SetAllProperties(policy =>
                {
                    policy.NameMatches(name => name.EndsWith("Something"));
                });

                // 1)
                //x.ForConcreteType<Bar>().Configure.SetProperty(r => r.Foo = new Foo());
                //x.ForConcreteType<Bar>().Configure.Setter<IQux>().IsTheDefault();

                // 2)
                //x.ForConcreteType<Bar>().Configure.SetProperty(r => r.Foo = new Foo());

                // 3)
                //x.ForConcreteType<Bar>().Configure.Setter<IQux>().IsTheDefault();

                // 4)
                //x.ForConcreteType<Bar>().Configure.Setter<IQux>().IsTheDefault().SetProperty(r => r.Foo = new Foo());

                // 5)
                //x.ForConcreteType<Bar>().Configure.SetProperty(r => r.Foo = new Foo()).Setter<IQux>().IsTheDefault();
            });
    }

    [TestMethod]
    public void Resolve_Bar_through_container()
    {
        var bar = Container.GetInstance<Bar>();

        Debug.WriteLine("Resolve:");
        Debug.WriteLine(string.Format("Foo is {0}null.", bar.Foo == null ? "" : "not "));
        Debug.WriteLine(string.Format("FooSomething is {0}null.", bar.FooSomething == null ? "" : "not "));
        Debug.WriteLine(string.Format("Baz is {0}null.", bar.Baz == null ? "" : "not "));
        Debug.WriteLine(string.Format("Qux is {0}null.", bar.Qux == null ? "" : "not "));

        Assert.IsTrue(bar.Foo != null && bar.FooSomething != null && bar.Baz != null && bar.Qux != null);
    }

    [TestMethod]
    public void BuildUp_instance_of_bar()
    {
        var bar = new Bar();
        Container.BuildUp(bar);

        Debug.WriteLine("Build up:");
        Debug.WriteLine(string.Format("Foo is {0}null.", bar.Foo == null ? "" : "not "));
        Debug.WriteLine(string.Format("FooSomething is {0}null.", bar.FooSomething == null ? "" : "not "));
        Debug.WriteLine(string.Format("Baz is {0}null.", bar.Baz == null ? "" : "not "));
        Debug.WriteLine(string.Format("Qux is {0}null.", bar.Qux == null ? "" : "not "));

        Assert.IsTrue(bar.Foo != null && bar.FooSomething != null && bar.Baz != null && bar.Qux != null);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用OnCreationForAll方法注册Bar的属性:

container.Configure(r => r.For<Bar>().OnCreationForAll(bar =>
{
    bar.Foo = container.GetInstance<IFoo>();
    bar.FooSomething = container.GetInstance<IFoo>();
    bar.Baz = container.GetInstance<IBaz>();
    bar.Qux = container.GetInstance<IQux>();
}));