MVC3 Controller constructor + Ninject
Asked Answered
L

4

3

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.

Lieb answered 6/12, 2010 at 17:17 Comment(3)
what controller factory implementation are you using?Longlegged
I'm just using the regular MVC controller factory implementation. I read that you could make a custom controller factory to fix this, however I have only been able to find examples where people use Structuremap...Lieb
see planetgeek.ch/2010/11/13/…Longlegged
L
2

It turns out that its not the Controller thats messing it up, but that Ninject dont Bind my generic Repository and IRepository correctly - I have therefore created a new post: Ninject + Bind generic repository

Lieb answered 6/12, 2010 at 20:15 Comment(0)
F
4

A short test showed that there is not problem with generic bindings in the MVC3 extension. I guess that the problem is not in the Controller but that the Repository can not be created because it has some unknown dependencies.

But this brought me to change the dependency resolver a bit to show the Ninject stacktrace whenever the requested type can be resolved but one of its dependencies fails to be resolved. Update to the latest version on the build server to get a better stack trace.

Fi answered 6/12, 2010 at 20:19 Comment(0)
G
2

You will need to change the controller factory, as the regular MVC controller factory does not do DI.

You can look here for more information on how to setup MVC + Ninject: MVC3 + Ninject - How to?

Glede answered 6/12, 2010 at 17:59 Comment(4)
That dosent solve my problem since the problem I encounter is of a different type and not related to that post.Lieb
Sorry, maybe I wasn't too clear. If you don't want to add a parameterless constructor, you should change your controller factory to one which does Dependency Injection using Ninject. Adding one is fairly easy, as there is one provided by the Ninject team. You can get the download and instructions hereGlede
Hmm... Cant find any controller factory at the link you provided o:o (atleast not for MVC3)Lieb
It is all handled by the NinjectHttpApplication class. Download the package for your ASP.NET version (3.5 or 4), by clicking Download->Download Packages. Add the DLL as a reference to your MVC project. Follow the instructions to enable Ninject. Hope it helps!Glede
L
2

It turns out that its not the Controller thats messing it up, but that Ninject dont Bind my generic Repository and IRepository correctly - I have therefore created a new post: Ninject + Bind generic repository

Lieb answered 6/12, 2010 at 20:15 Comment(0)
S
0

Don't use this binding!!!

Bind(typeof(IRepository<>)).To(typeof(Repository<>));

I changed my code use this binding and server was crashed, it works for one user but for thousands requests it's really bad

Use

Bind(typeof(IRepository<IClass>)).To(typeof(Repository<Class>))
Screwed answered 19/8, 2014 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.