title: StructureMap HowTo: Conditional Construction on Deep Object
body: <p>I'm having difficulty conditionally creating a dependency.  Googling, I have yet to find a good example of using the BuildStack and Conditional Predicates.</p>
  
  <p>Here's what I'm doing in the Registry:</p>
  
  <pre><code>//snip
  
  public SomeRegistry()
  {
      this.InstanceOf&lt;IFoo&gt;().Is.Conditional(
          c =&gt;
              {
                  c.TheDefault.Is.ConstructedBy(() =&gt; new FooZ());
  
                  c.If(
                      p =&gt; p.ParentType.IsAssignableFrom(typeof(IBar)) &amp;&amp;
                              p.BuildStack.Current.RequestedType.IsAssignableFrom(typeof(IDoStuffWithFooA)))
                              .ThenIt.Is.ConstructedBy(() =&gt; new FooA());
                  c.If(
                      p =&gt; p.ParentType.IsAssignableFrom(typeof(IBar)) &amp;&amp;
                              p.BuildStack.Current.RequestedType.IsAssignableFrom(typeof(IDoStuffWithFooB)))
                              .ThenIt.Is.ConstructedBy(() =&gt; new FooB());
              });
  
      this.Scan(
          s =&gt;
              {
                  s.TheCallingAssembly();
                  s.WithDefaultConventions();
              });
  }
  
  //snip
  </code></pre>
  
  <p>Here's unit tests showing what I'm expecting</p>
  
  <pre><code>//snip
  
  [TestFixture]
  public class ConditionalCreationTest
  {
      [Test]
      public void GiveUsFooAWhenDoingStuffWithA()
      {
          var dependA = ObjectFactory.GetInstance&lt;IDoStuffWithFooA&gt;();
  
          Assert.IsInstanceOfType&lt;FooA&gt;(dependA.Foo);
      }
  
      [Test]
      public void GiveUsFooBWhenDoingStuffWithB()
      {
          var dependB = ObjectFactory.GetInstance&lt;IDoStuffWithFooB&gt;();
  
          Assert.IsInstanceOfType&lt;FooB&gt;(dependB.Foo);
      }
  
      [Test]
      public void GiveUsFooZByDefault()
      {
          var foo = ObjectFactory.GetInstance&lt;IFoo&gt;();
  
          Assert.IsInstanceOfType&lt;FooZ&gt;(foo);
      }
  
      [Test]
      public void GiveUsProperFoosWhenWeDontAskDirectly()
      {
          var bar = ObjectFactory.GetInstance&lt;IBar&gt;();
  
          Assert.IsInstanceOfType&lt;FooA&gt;(bar.DoStuffA.Foo);
          Assert.IsInstanceOfType&lt;FooB&gt;(bar.DoStuffB.Foo);
      }
  
      [SetUp]
      public void SetUp()
      {
          ObjectFactory.Initialize(a =&gt; a.AddRegistry&lt;SomeRegistry&gt;());
      }
  }
  //snip
  </code></pre>
  
  <p>This is what I want StructureMap to AutoWire with the correct dependency of IFoo:</p>
  
  <pre><code>//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
  </code></pre>
  
  <p>I cannot figure out how to get <code>GiveUsProperFoosWhenWeDontAskDirectly</code> test to pass.</p>
  
  <p>I want to get <code>FooA</code> to get initialized when I need <code>IDoStuffWithFooA</code>, <code>FooB</code> when <code>IDoStuffWithFooB</code>, regardless of when it's needed in the graph.  <em>What is the proper syntax for the conditional predicate?</em></p>