简体   繁体   English

无法继承没有默认构造函数的Base Controller类

[英]Cannot inherit a Base Controller class which has no default constructor

I have a BaseController which inherited by another controllers, 我有一个由另一个控制器继承的BaseController,

BaseController: BaseController:

public partial class BaseController : Controller
{
        private readonly IUnitOfWork _uow;
        private readonly INewsService _newsService;

        protected BaseController(IUnitOfWork uow, INewsService newsService)
        {
            _uow = uow;
            _newsService = newsService;
        }

//...

}

one of controllers: 控制器之一:

public partial class HomeController : BaseController
{

    private readonly IUserService _userService;
    private readonly IMemberService _memberService;
    private readonly IUnitOfWork _uow;
    public HomeController(IUnitOfWork uow, IUserService userService, IMemberService memberService, INewsService newsService): base(uow, newsService)
    {
        _userService = userService;
        _memberService = memberService;
        _uow = uow;
    }

//...
}

But i get to this error for each controllers in t4mvc generated classes, for example this error: 但是我在t4mvc生成的类中为每个控制器得到了这个错误,例如这个错误:

'MvcApp.Controllers.BaseController' does not contain a constructor that takes 0 arguments.

in HomeController.generated.cs file. HomeController.generated.cs文件中。

SOLUTION

https://stackoverflow.com/a/15320890/1719207 https://stackoverflow.com/a/15320890/1719207

Sounds like your IOC container (if it exists) is not overwriting the default controller factory. 听起来像你的IOC容器(如果存在)不会覆盖默认的控制器工厂。 If using Ninject, you should install the Ninject.MVC3 project. 如果使用Ninject,则应安装Ninject.MVC3项目。 Each container needs to override the default controller factory so it can build them and inject the dependencies. 每个容器都需要覆盖默认的控制器工厂,以便它可以构建它们并注入依赖项。

For StructureMap, you'll need StructureMap.MVC3 or StructureMap.MVC4 对于StructureMap,您需要StructureMap.MVC3StructureMap.MVC4

A controller with this kind dependencies smells. 具有这种依赖性的控制器闻起来。 That's the technical term I recently learned ^_^ 这是我最近学到的技术术语^ _ ^

For one, I have UoW in an action filter and use attribute to mark the action transaction (in my case, I combined it with auditing). 首先,我在动作过滤器中使用UoW并使用属性来标记动作事务(在我的例子中,我将它与审计相结合)。 The rest of them, if they are really that complicated, there probably should be a application service to wrap them. 其余的,如果真的那么复杂,可能应该有一个应用程序服务来包装它们。

Just a thought. 只是一个想法。 I'm a true beginner. 我是一个真正的初学者。

This is similar to T4MVC Base controller doesn't have default constructor . 这类似于T4MVC Base控制器没有默认构造函数

Can you change your base controller to be abstract? 你能改变你的基本控制器是抽象的吗? If so, I believe that will make things work. 如果是这样,我相信这将使事情奏效。

Addition to David Ebbo Solution,this is 除了David Ebbo Solution,这是

Another Solution: 另一种方案:

I just added to my Base Controller a default constructor, like: 我刚刚在我的Base Controller中添加了一个默认构造函数,如:

protected BaseController(){}

Now that works Fine (without CodeRush 'Base type constructor are not implemented' issue in sub-classes). 现在它工作正常(没有CodeRush'基本类型构造函数未在子类中实现'问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM