StructureMap .ForConcreteType().Configure.SetProperty 配置与 BuildUp 逻辑不起作用

发布于 2024-12-01 04:49:59 字数 3580 浏览 1 评论 0原文

试图弄清楚如何配置 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);
    }
}

Trying to figure out how to configure StructureMap for property setter injection. I have scenarios where I resolve an instance through the container as well as scenarios where I get an instance elsewhere and then call the congainer.BuildUp() feature.

I have included a sample set of classes below where I want to configure injection for the concrete type Bar. If I use the different options marked 1)-5) below I will get different behaviors from StructureMap. Some of the cases must fail since they are incomplete but all cases where I configure injection on both the Foo and the Qux properties should work. They do not. Especially the .BuildUp() feature does not work.

Rather than me explaining each outcome in detail perhaps some StructureMap wiz out there can be so kind as to point out the flaw in my logic and/or run the sample code?

There are other work around configurations I guess but I'd like to be able to be explicit in what injections I expect on Bar.

Thanks in advance!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-12-08 04:49:59

您可以使用 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>();
})); 

You can use the OnCreationForAll method for registering the properties of 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>();
})); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文