3

条件付きで依存関係を作成するのが困難です。グーグルでは、BuildStack と Conditional Predicates を使用する良い例をまだ見つけていません。

レジストリで私がやっていることは次のとおりです。

//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 に変換したいものです。

//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テストに合格する方法がわかりません。

グラフでいつ必要なのかに関係なく、 必要なときFooAに初期化したいと思っています。条件述語の適切な構文は何ですか?IDoStuffWithFooAFooBIDoStuffWithFooB

4

1 に答える 1

1

多くの場合、その構文では条件付き構文を避けるのが最善です。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();
            }); 
于 2011-04-04T07:52:04.623 に答える