MVC3控制器构造函数+ Ninject [英] MVC3 Controller constructor + Ninject

查看:209
本文介绍了MVC3控制器构造函数+ Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在MVC3 Web应用程序的工作,ecountered一个新的问题与Ninject。

I'm at the moment working on a MVC3 Web application and ecountered a new problem with Ninject.

我用下面的code在我的控制器:

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();
    }
}

和我的Ninject模块:

And my Ninject Module:

public class RepositoryModule : NinjectModule
{
    public override void Load()
    {
        Bind(typeof(IRepository<>)).To(typeof(Repository<>));
    }
}

然而,这只是抛出我system.missingMethodException而:此对象定义无参数的构造函数。当我试图渲染索引视图。

However this just throws me "System.MissingMethodException: No parameterless constructor defined for this object." when I try to render the Index view.

如果我再补充一下:

public TestController() : this(new Repository<CustomerModel>(new XenCRMEntities())) { }

所以我实际上看起来的TestController像:

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();
    }
}

它的工作原理,但你可以看到新的构造函数pretty多少打破国际奥委会整点。

It works, but as you can see the new constructor pretty much break the whole point of IoC.

我该如何解决这个问题?

How do I fix this?

先谢谢了。

推荐答案

原来,它不是控制器这就是搞乱它,但Ninject不绑定我的通用信息库和IRepository正确的 - 因此,我已创建了一个新职位: Ninject +绑定通用库

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

这篇关于MVC3控制器构造函数+ Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆