title: IoC with static and dynamic dependencies
body: <p>I'm trying to implement IoC in my app. I have this model:</p>
  
  <pre><code>interface IService;
  interface IComponent;
  
  class Service : IService
      Service()
  
  class Component : IComponent
      Component(IService service, object runtimeValue) { }
  </code></pre>
  
  <p>At some point in my app I need to get a <code>IComponent</code>. My app uses a IoC container (Unity). I can register <code>Service</code> with the container but I can't do the same for <code>Component</code> b/c of its dependency <code>runtimeValue</code>. According to <a href="https://stackoverflow.com/questions/1943576/is-there-a-pattern-for-initializing-objects-created-via-a-di-container/1945023#1945023">this</a> I have to use a factory and inject that wherever I need to get a <code>IComponent</code>:  </p>
  
  <pre><code>interface IComponentFactory
       IComponent CreateComponent(object runtimeValue)
  
  class ComponentProvider : IComponentProvider
       ComponentProvider(IComponentFactory factory) { }
  
       IComponent CreateAndCacheComponent(object runtimeValue) {
           _component = factory.CreateComponent(runtimeValue)
           return _component
       }
  
       // other methods
  </code></pre>
  
  <p>I must be able to register the factory with the container, so it must have only static dependencies. At the same time it must be able to provide a service instance of type <code>IService</code> required to create a component.<br>
  Here is the factory implementation. The only thing I could think of was to use a <code>Func&lt;&gt;</code> delegate as dependency:</p>
  
  <pre><code>class ComponentFactory : IComponentFactory
      ComponentFactory(Func&lt;IService&gt; serviceFactoryDelegate)
  
      IComponent CreateComponent(object runtimeValue) {
          return new Component(serviceFactoryDelegate.Invoke(), runtimeValue)
      }
  </code></pre>
  
  <p>... and register the delegate with the container as static factory, so that it calls back the container to resolve the service (I'm using Unity 1.2 on .net 2.0):</p>
  
  <pre><code>Container
      .Configure&lt;IStaticFactoryConfiguration&gt;()
      .RegisterFactory&lt;Func&lt;IService&gt;&gt;(container =&gt; (Func&lt;IService&gt;)container.Resolve&lt;IService&gt;)
  </code></pre>
  
  <p>Now I can use the container to resolve a <code>ComponentProvider</code> and get a component based on a runtime value:</p>
  
  <pre><code>// this happens inside CompositionRoot
  provider = Container.Resovle&lt;IComponentProvider&gt;()
  component = provider.CreateAndCacheComponent("the component")
  </code></pre>
  
  <p>Now I have some questions about this:</p>
  
  <ol>
  <li><p>I'm not happy that the factory calls <code>new Component(...)</code>. Isn't this poor man's DI?</p></li>
  <li><p>Does the Hollywood principle still stand when using <code>Func&lt;IService&gt;</code> on factory's constructor? I mean, it ultimately calls container.Resolve&lt;>... kind of like SL. The only difference is the code is in the container registration part of the app rather than inside the factory class.</p></li>
  <li><p>Is there anything (else) wrong with this implementation, as far as DI and IoC are concerned?</p></li>
  </ol>