title: Ninject : Constructor parameter
body: <p>I am using Ninject together with ASP.NET MVC 4. I am using repositories and want to do constructor injection to pass in the repository to one of the controllers.</p>
  
  <p>This is my Repository interface:</p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : TableServiceEntity
  {
      void Add(T item);
      void Delete(T item);
      void Update(T item);
      IEnumerable&lt;T&gt; Find(params Specification&lt;T&gt;[] specifications);
      IEnumerable&lt;T&gt; RetrieveAll();
      void SaveChanges();
  }
  </code></pre>
  
  <p>The <code>AzureTableStorageRepository</code> below is an implementation of <code>IRepository&lt;T&gt;</code>:</p>
  
  <pre><code>public class AzureTableRepository&lt;T&gt; : IRepository&lt;T&gt; where T : TableServiceEntity
  {
      private readonly string _tableName;
      private readonly TableServiceContext _dataContext;
  
      private CloudStorageAccount _storageAccount;
      private CloudTableClient _tableClient;
  
      public AzureTableRepository(string tableName)
      {
          // Create an instance of a Windows Azure Storage account
          _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
  
          _tableClient = _storageAccount.CreateCloudTableClient();
          _tableClient.CreateTableIfNotExist(tableName);
          _dataContext = _tableClient.GetDataServiceContext();
          _tableName = tableName;
      }
  </code></pre>
  
  <p>Note the tableName parameter needed because I was using a generic table repository to persist data to Azure.</p>
  
  <p>And finally I have the following controller.</p>
  
  <pre><code>public class CategoriesController : ApiController
  {
      static IRepository&lt;Category&gt; _repository;
  
      public CategoriesController(IRepository&lt;Category&gt; repository)
      {
          if (repository == null)
          {
              throw new ArgumentNullException("repository");
          }
  
          _repository = repository;
      }
  </code></pre>
  
  <p>Now I want to inject a repository into the controller. So I have created a module that contains the bindings:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Ninject module to handle dependency injection of repositories
  /// &lt;/summary&gt;
  public class RepositoryNinjectModule : NinjectModule
  {
      public override void Load()
      {
          Bind&lt;IRepository&lt;Category&gt;&gt;().To&lt;AzureTableRepository&lt;Category&gt;&gt;();
      }
  }
  </code></pre>
  
  <p>The loading of the module is done in the <code>NinjectWebCommon.cs</code></p>
  
  <pre><code>/// &lt;summary&gt;
      /// Creates the kernel that will manage your application.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;The created kernel.&lt;/returns&gt;
      private static IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);
          kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();
  
          RegisterServices(kernel);
          return kernel;
      }
  
      /// &lt;summary&gt;
      /// Load your modules or register your services here!
      /// &lt;/summary&gt;
      /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt;
      private static void RegisterServices(IKernel kernel)
      {
          // Load the module that contains the binding
          kernel.Load(new RepositoryNinjectModule());
  
          // Set resolver needed to use Ninject with MVC4 Web API
          GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
      } 
  </code></pre>
  
  <p>The <code>DependencyResolver</code> was created because Ninject's <code>DependencyResolver</code> implements <code>System.Web.Mvc.IDependencyResolver</code> and this cannot be assigned to <code>GlobalConfiguration.Configuration</code> of the WebApi Application.</p>
  
  <p>So with all this in place, the Ninject part is actually injecting the right type in the Controller but Ninject cannot inject the tableName parameter in the constructor of <code>AzureTableRepository</code>.</p>
  
  <p>How would I be able to do this in this case? I have consulted a lot of articles and the ninject documentation to see how I could use parameters, but I cannot seem to get it working.</p>
  
  <p>Any help would be appreciated.</p>