StructureMap HowTo: Bedingte Konstruktion auf einem tiefen Objekt

Ich habe Probleme, bedingt eine Abhängigkeit zu erstellen. Googeln, ich habe noch kein gutes Beispiel für die Verwendung der BuildStack- und Bedingten Prädikate gefunden.

Folgendes mache ich in der 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

Hier sind Unit-Tests, die zeigen, was ich erwarte

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

Ich möchte, dass StructureMap AutoWire mit der richtigen Abhängigkeit von IFoo verwendet:

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

Ich kann nicht herausfinden, wie ich kommeGiveUsProperFoosWhenWeDontAskDirectly Test zu bestehen.

ich möchte bekommenFooA zu initialisieren, wenn ich braucheIDoStuffWithFooA, FooB wannIDoStuffWithFooB, unabhängig davon, wann es in der Grafik benötigt wird.Was ist die richtige Syntax für das bedingte Prädikat?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage