StructureMap HowTo:深层对象的条件构造

发布于 2024-08-24 22:38:43 字数 3037 浏览 5 评论 0原文

我很难有条件地创建依赖关系。谷歌搜索,我还没有找到使用 BuildStack 和条件谓词的好例子。

这是我在注册表中所做的事情:

//snip

public SomeRegistry()
{
    this.InstanceOf<IFoo>().Is.Conditional(
        c =>
            {
                c.TheDefault.Is.ConstructedBy(() => new FooZ());

                c.If(
                    p => p.ParentType.IsAssignableFrom(typeof(IBar)) &&
                            p.BuildStack.Current.RequestedType.IsAssignableFrom(typeof(IDoStuffWithFooA)))
                            .ThenIt.Is.ConstructedBy(() => new FooA());
                c.If(
                    p => p.ParentType.IsAssignableFrom(typeof(IBar)) &&
                            p.BuildStack.Current.RequestedType.IsAssignableFrom(typeof(IDoStuffWithFooB)))
                            .ThenIt.Is.ConstructedBy(() => new FooB());
            });

    this.Scan(
        s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
            });
}

//snip

这是显示我所期望的单元测试

//snip

[TestFixture]
public class ConditionalCreationTest
{
    [Test]
    public void GiveUsFooAWhenDoingStuffWithA()
    {
        var dependA = ObjectFactory.GetInstance<IDoStuffWithFooA>();

        Assert.IsInstanceOfType<FooA>(dependA.Foo);
    }

    [Test]
    public void GiveUsFooBWhenDoingStuffWithB()
    {
        var dependB = ObjectFactory.GetInstance<IDoStuffWithFooB>();

        Assert.IsInstanceOfType<FooB>(dependB.Foo);
    }

    [Test]
    public void GiveUsFooZByDefault()
    {
        var foo = ObjectFactory.GetInstance<IFoo>();

        Assert.IsInstanceOfType<FooZ>(foo);
    }

    [Test]
    public void GiveUsProperFoosWhenWeDontAskDirectly()
    {
        var bar = ObjectFactory.GetInstance<IBar>();

        Assert.IsInstanceOfType<FooA>(bar.DoStuffA.Foo);
        Assert.IsInstanceOfType<FooB>(bar.DoStuffB.Foo);
    }

    [SetUp]
    public void SetUp()
    {
        ObjectFactory.Initialize(a => a.AddRegistry<SomeRegistry>());
    }
}
//snip

这是我想要的 StructureMap 与 IFoo 的正确依赖关系 AutoWire:

//snip
    public class Bar : IBar
    {
        private IDoStuffWithFooA doStuffA;
        private IDoStuffWithFooB doStuffB;

        public Bar(IDoStuffWithFooA doStuffA, IDoStuffWithFooB doStuffB)
        {
            this.doStuffA = doStuffA;
            this.doStuffB = doStuffB;
        }

        public IDoStuffWithFooA DoStuffA
        {
            get
            {
                return this.doStuffA;
            }
        }

        public IDoStuffWithFooB DoStuffB
        {
            get
            {
                return this.doStuffB;
            }
        }
    }
//snip

我不知道如何进行 GiveUsProperFoosWhenWeDontAskDirectly 测试通过。

我希望在需要 IDoStuffWithFooA 时初始化 FooA,在需要 IDoStuffWithFooB 时初始化 FooB,无论何时需要在图中。 条件谓词的正确语法是什么?

I'm having difficulty conditionally creating a dependency. Googling, I have yet to find a good example of using the BuildStack and Conditional Predicates.

Here's what I'm doing in the Registry:

//snip

public SomeRegistry()
{
    this.InstanceOf<IFoo>().Is.Conditional(
        c =>
            {
                c.TheDefault.Is.ConstructedBy(() => new FooZ());

                c.If(
                    p => p.ParentType.IsAssignableFrom(typeof(IBar)) &&
                            p.BuildStack.Current.RequestedType.IsAssignableFrom(typeof(IDoStuffWithFooA)))
                            .ThenIt.Is.ConstructedBy(() => new FooA());
                c.If(
                    p => p.ParentType.IsAssignableFrom(typeof(IBar)) &&
                            p.BuildStack.Current.RequestedType.IsAssignableFrom(typeof(IDoStuffWithFooB)))
                            .ThenIt.Is.ConstructedBy(() => new FooB());
            });

    this.Scan(
        s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
            });
}

//snip

Here's unit tests showing what I'm expecting

//snip

[TestFixture]
public class ConditionalCreationTest
{
    [Test]
    public void GiveUsFooAWhenDoingStuffWithA()
    {
        var dependA = ObjectFactory.GetInstance<IDoStuffWithFooA>();

        Assert.IsInstanceOfType<FooA>(dependA.Foo);
    }

    [Test]
    public void GiveUsFooBWhenDoingStuffWithB()
    {
        var dependB = ObjectFactory.GetInstance<IDoStuffWithFooB>();

        Assert.IsInstanceOfType<FooB>(dependB.Foo);
    }

    [Test]
    public void GiveUsFooZByDefault()
    {
        var foo = ObjectFactory.GetInstance<IFoo>();

        Assert.IsInstanceOfType<FooZ>(foo);
    }

    [Test]
    public void GiveUsProperFoosWhenWeDontAskDirectly()
    {
        var bar = ObjectFactory.GetInstance<IBar>();

        Assert.IsInstanceOfType<FooA>(bar.DoStuffA.Foo);
        Assert.IsInstanceOfType<FooB>(bar.DoStuffB.Foo);
    }

    [SetUp]
    public void SetUp()
    {
        ObjectFactory.Initialize(a => a.AddRegistry<SomeRegistry>());
    }
}
//snip

This is what I want StructureMap to AutoWire with the correct dependency of IFoo:

//snip
    public class Bar : IBar
    {
        private IDoStuffWithFooA doStuffA;
        private IDoStuffWithFooB doStuffB;

        public Bar(IDoStuffWithFooA doStuffA, IDoStuffWithFooB doStuffB)
        {
            this.doStuffA = doStuffA;
            this.doStuffB = doStuffB;
        }

        public IDoStuffWithFooA DoStuffA
        {
            get
            {
                return this.doStuffA;
            }
        }

        public IDoStuffWithFooB DoStuffB
        {
            get
            {
                return this.doStuffB;
            }
        }
    }
//snip

I cannot figure out how to get GiveUsProperFoosWhenWeDontAskDirectly test to pass.

I want to get FooA to get initialized when I need IDoStuffWithFooA, FooB when IDoStuffWithFooB, regardless of when it's needed in the graph. What is the proper syntax for the conditional predicate?

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

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

发布评论

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

评论(1

贪恋 2024-08-31 22:38:43

通常最好尝试避免使用该语法的条件构造,它比 For - Use 更难解释。您的场景可以使用以下方法解决:

        For<IDoStuffWithFooA>().Use<DoStuffWithFooA>().Ctor<IFoo>().Is<FooA>();
        For<IDoStuffWithFooB>().Use<DoStuffWithFooB>().Ctor<IFoo>().Is<FooB>();

        For<IFoo>().Use<FooZ>();

        Scan(
            s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
            }); 

It's often best to try to avoid conditional construction with that syntax, it is much harder to interpret than the For - Use. Your scenario can be solved using:

        For<IDoStuffWithFooA>().Use<DoStuffWithFooA>().Ctor<IFoo>().Is<FooA>();
        For<IDoStuffWithFooB>().Use<DoStuffWithFooB>().Ctor<IFoo>().Is<FooB>();

        For<IFoo>().Use<FooZ>();

        Scan(
            s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
            }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文