English 中文(简体)
结构:深层物体的有条件建筑
原标题:StructureMap HowTo: Conditional Construction on Deep Object

我很难有条件地造成依赖。 我尚未找到一个利用建筑标准和条件指数的良好范例。

这里我是在书记官处做的:

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

这是我想要的“结构”到“自治”,而“红十字与红新月”的正确依赖是:

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

我无法说明如何获得<代码>。 GiveUsProperFoosWhen WeDontAskDirectly 考试通过。

我想到FooA,以便在我需要、>IDoStuffWithFooB时,无论在图表中需要什么时间,先入门。 有条件的前提是否适当?>

问题回答

试图避免以这种辛迪加进行有条件的建筑,比“使用”解释要困难得多。 您的设想可以通过以下办法解决:

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




相关问题
Possible to sandbox Python configuration file?

I m thinking of implementing a configuration file written in Python syntax, not unlike what Django does. While I ve seen one or two SO questions about the merits of using executable code in ...

How to validate Java logging properties files?

I have a basic facility for allowing users to remotely apply changes to the logging files in my application. Some logs are configured using java.util.logging properties files, and some are configured ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

Dependency bundle (jar-files/sources/API docs) in Eclipse

I m developing various in-house extensions for JIRA, the issue tracker we use. So far I worked with Netbeans and everything worked like a charm. However, now I need to switch to Eclipse and I m ...

热门标签