I'm at the moment working on a MVC3 Web application and ecountered a new problem with Ninject.
I'm using the following code in my controller:
public class TestController : Controller
{
public IRepository<CustomerModel> rep;
public TestController(IRepository<CustomerModel> repository)
{
this.rep = repository;
}
public ActionResult Index()
{
return View();
}
}
And my Ninject Module:
public class RepositoryModule : NinjectModule
{
public override void Load()
{
Bind(typeof(IRepository<>)).To(typeof(Repository<>));
}
}
However this just throws me "System.MissingMethodException: No parameterless constructor defined for this object." when I try to render the Index view.
If I then add:
public TestController() : this(new Repository<CustomerModel>(new XenCRMEntities())) { }
so my actually TestController looks like:
public class TestController : Controller
{
public IRepository<CustomerModel> rep;
public TestController() : this(new Repository<CustomerModel>(new XenCRMEntities())) { }
public TestController(IRepository<CustomerModel> repository)
{
this.rep = repository;
}
public ActionResult Index()
{
return View();
}
}
It works, but as you can see the new constructor pretty much break the whole point of IoC.
How do I fix this?
Thanks in advance.