title: Ninject - how and when to inject
body: <p>I'm a newbie when it comes to DI and ninject and I'm struggling a bit
  about when the actual injection should happen and how to start the
  binding.</p>
  
  <p>I'm using it already in my web application and it working fine there,
  but now I want to use injection in a class library.</p>
  
  <p>Say I have a class like this:</p>
  
  <pre class="lang-c# prettyprint-override"><code>public class TestClass
  {
      [Inject]
      public IRoleRepository RoleRepository { get; set; }
      [Inject]
      public ISiteRepository SiteRepository { get; set; }
      [Inject]
      public IUserRepository UserRepository { get; set; }
  
      private readonly string _fileName;
  
      public TestClass(string fileName)
      {
          _fileName = fileName;
      }
  
      public void ImportData()
      {
          var user = UserRepository.GetByUserName("myname");
          var role = RoleRepository.GetByRoleName("myname");
          var site = SiteRepository.GetByID(15);
          // Use file etc
      }
  
  }
  </code></pre>
  
  <p>I want to use property injection here because I need to pass in a
  filename in my constructor.  Am I correct in saying that if I need to
  pass in a constructor parameter, I cannot use constructor injection?
  If I can use constructor injection with additional parameters, how do
  I pass those parameters in?</p>
  
  <p>I have a console app that consumes by Test class that looks as
  follows:</p>
  
  <pre class="lang-c# prettyprint-override"><code>class Program
  {
      static void Main(string[] args)
      {
          // NinjectRepositoryModule Binds my IRoleRepository etc to concrete
          // types and works fine as I'm using it in my web app without any
          // problems
          IKernel kernel = new StandardKernel(new NinjectRepositoryModule());
  
          var test = new TestClass("filename");
  
          test.ImportData();
      }
  }
  </code></pre>
  
  <p>My problem is that when I call <code>test.ImportData()</code> my repositories are null - nothing has been injected into them. I have tried creating another module and calling </p>
  
  <pre class="lang-c# prettyprint-override"><code>Bind&lt;TestClass&gt;().ToSelf();
  </code></pre>
  
  <p>as I thought this might resolve all injection properties in <code>TestClass</code> but I'm getting nowhere.</p>
  
  <p>I'm sure this is a trivial problem, but I just can't seem to find out
  how to go about this.</p>