StructureMap如何:在深层对象上的条件构造 [英] StructureMap HowTo: Conditional Construction on Deep Object

查看:126
本文介绍了StructureMap如何:在深层对象上的条件构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法有条件地创建依赖关系。 Googling,我还没有找到一个使用BuildStack和条件谓词的好例子。

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

我期待

//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

使用正确的IFoo依赖关系将StructureMap转换为AutoWire:

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

我不知道如何获得 GiveUsProperFoosWhenWeDontAskDirectly 测试通过。

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

我想得到 时需要 IDoStuffWithFooA FooB > IDoStuffWithFooB ,无论它在图中何时需要。

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?

推荐答案

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

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

这篇关于StructureMap如何:在深层对象上的条件构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆