title: .NET MVC Dependency Injection with Ninject
body: <p>I've just started programming in .NET and I'm having some problems with implementing <code>dependency injection (using Ninject)</code>.</p>
  
  <p>I'm creating some sort of catering application where user can browse towns, in towns browse restaurants and in restaurants browse food.</p>
  
  <p>I'm using UnitOfWork and repository pattern where for example I access town by id like this:</p>
  
  <pre><code>_unitOfWork.TownRepository.GetByID(id);
  </code></pre>
  
  <p>Now I started implementing services into application and I have encountered need for <code>dependency injection</code>.</p>
  
  <p>I have created <code>ITownService</code>, <code>IRestaurantService</code> and <code>IFoodService</code> (since I've <code>TownRepository</code>, <code>RestaurantRepository</code> and <code>FoodRepository</code> in my <code>UnitOfWork</code>).</p>
  
  <p>Sample look of TownService:</p>
  
  <pre><code>public class TownService : ITownService
      {
          // initialize UnitOfWork
          private IUnitOfWork _unitOfWork;
  
          public TownService()
              : this(new UnitOfWork())
          {
          }
  
          public TownService(IUnitOfWork unitOfWork)
          {
              _unitOfWork = unitOfWork;
          }
  
          public Town GetByID(object id)
          {
              return _unitOfWork.TownRepository.GetByID(id);
          }
  
          public IEnumerable&lt;Town&gt; GetAll()
          {
              return _unitOfWork.TownRepository.Get();
          }
  
          public bool Insert(Town town)
          {
              // validation logic
              if (!ValidateTown(town))
                  return false;
  
              try
              {
                  _unitOfWork.TownRepository.Insert(town);
                  _unitOfWork.Save();
              }
              catch
              {
                  return false;
              }
  
              return true;
          }
  
          public bool Delete(object id)
          {
              try
              {
                  _unitOfWork.TownRepository.Delete(id);
                  _unitOfWork.Save();
              }
              catch
              {
                  return false;
              }
  
              return true;
          }
  
          public bool Update(Town townToUpdate)
          {
              // validation logic
              if (!ValidateTown(townToUpdate))
                  return false;
  
              try
              {
                  _unitOfWork.TownRepository.Update(townToUpdate);
                  _unitOfWork.Save();
              }
              catch
              {
                  return false;
              }
  
              return true;
          } 
      }
  </code></pre>
  
  <p>I haven't implemented <code>FoodService</code> and <code>RestaurantService</code> yet, but they should be similar with of course some additinal methods exepct this that I have. For example in <code>RestaurantService</code> I might have <code>public Restaurant GetRestaurantsInTown(Town town){}</code> or something like that.</p>
  
  <p>I hope that you got the feel of application a bit. Now lets back to <code>Ninject</code>.</p>
  
  <p>In my <code>TownController</code> I would have something like this:</p>
  
  <pre><code> public class TownController : Controller
      {
  
          private ITownService _townService;
  
          public TownController(ITownService townService)
          {
              _townService = townService;
          }
      }
  </code></pre>
  
  <p>Similar would be for <code>RestaurantController</code> and <code>FoodController</code> of course just constructor injecting. </p>
  
  <p>How do I use <code>Ninject</code> in such example? Do I need some global <code>IService</code> and not <code>ITownService</code>, <code>IRestaurantService</code> and <code>IFoodService</code> which I'd inherid in <code>TownService</code>, <code>RestaurantService</code> and <code>FoodService</code> or is it okay like this?</p>
  
  <p>When binding what do I need to bind?</p>
  
  <pre><code>kernel.Bind&lt;IUnitOfWork&gt;().To&lt;UnitOfWork&gt;();
  kernel.Bind&lt;ITownService&gt;().To&lt;TownService&gt;();
  kernel.Bind&lt;IRestaurantService&gt;().To&lt;RestaurantService&gt;();
  kernel.Bind&lt;IFoodService&gt;().To&lt;TownService&gt;();
  </code></pre>
  
  <p>Something like this?</p>
  
  <p>In short - what I need for adding dependency injection with <code>Ninject</code>?</p>
  
  <p>I'm really having problems with this and would need help.</p>
  
  <p>Thanks a lot in forward.</p>