Proper way to use dependency injection when some arguments will be known only at runtime
<p>I read a bunch of questions and all docs at Ninject wiki, but I still doubt and ask questions to myself like &quot;Am I doing it right?&quot;</p>
<ol>
<li>What if I need to use controller for initialization of object? And <strong>field</strong> argument will be known only at <strong>runtime</strong>?</li>
</ol>
<p><strong>A)</strong> So I need something like this:</p>
<pre><code>    public class MyClass : IMyInterface
    {
         private string _field;
         readonly IRepository _repo;
         public MyClass(string field, IRepository repo)
         {
             _field = field;
             _repo = repo;
         }
     }
</code></pre>
<p>But how to Bind it properly? Or should I forever forget about using constructor for any reason but Constructor Dependency Injection?</p>
<p><strong>B)</strong> And do it like:</p>
<pre><code>public class MyClass : IMyInterface
{
    private string _field;
    readonly IRepository _repo;
    public MyClass(IRepository repo)
    {
        _repo = repo;
    }
    public void Initialize(string field)
    {
        _field = field;
    }
}
</code></pre>
<p>I think it isn't ok to call anytime when I need object this Initialize function or I'm wrong?</p>
<p>According to this answers <a href="https://stackoverflow.com/questions/1926826/cant-combine-factory-di">Can&#39;t combine Factory / DI</a> and <a href="https://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657">Dependency Inject (DI) &quot;friendly&quot; library</a> and <a href="https://stackoverflow.com/questions/1943576/is-there-a-pattern-for-initializing-objects-created-via-a-di-container/1945023#1945023">Is there a pattern for initializing objects created via a DI container</a></p>
<blockquote>
<p>Use Abstract Factory if you need a short-lived object</p>
<p>Dependencies injected with Constructor Injection tend to be long-lived, but sometimes you need a short-lived object, <strong>or to construct the dependency based on a value known only at run-time.</strong></p>
</blockquote>
<p><strong>C)</strong> I should do it like:</p>
<pre><code>public interface IMyInterface  {  }
  public interface IMyFactory
  {
      MyClass Create(string field);
  } 
  public class MyFactory : IMyFactory
  {
      private readonly IRepository _repo;
      public MyFactory (IRepository repo)
      {
          _repo = repo;
      }
      public MyClass Create(string field)
      {
          return new MyClass(_repo, field);
      }
  }
  public class MyClass : IMyInterface
  {
      private string _field;
      readonly IRepository _repo;
      public MyClass(IRepository repo, string field)
      {
          _repo = repo;
          _field = field;
      }
  }
</code></pre>
<p>And if I need MyClass from another class I gonna use it like</p>
<pre><code>public class OtherClass
{
    private IMyInterface _myClass;
    public OtherClass(IMyFactory factory)
    {
        _myClass = factory.Create(&quot;Something&quot;);
    }
}
</code></pre>
<p>Isn't it too bulky?</p>
<p><strong>And my question is: What I have to use A, B or C case? And why? Or maybe something else?</strong></p>