代码之家  ›  专栏  ›  技术社区  ›  Brett Veenstra

StructureMap HowTo:深对象的条件构造

  •  3
  • Brett Veenstra  · 技术社区  · 16 年前

    以下是我在注册表中所做的:

    //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的正确依赖关系自动关联的内容:

    //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 在我需要的时候初始化 IDoStuffWithFooA , FooB IDoStuffWithFooB

    1 回复  |  直到 16 年前
        1
  •  1
  •   PHeiberg    15 年前

    通常最好避免使用这种语法进行条件构造,因为它比“可供使用”语法更难解释。您的方案可以通过以下方式解决:

            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();
                }); 
    
    推荐文章