Question

I have a BaseController which inherited by another controllers,

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:

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

in HomeController.generated.cs file.

SOLUTION

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

Was it helpful?

Solution 4

Addition to David Ebbo Solution,this is

Another Solution:

I just added to my Base Controller a default constructor, like:

protected BaseController(){}

Now that works Fine (without CodeRush 'Base type constructor are not implemented' issue in sub-classes).

OTHER TIPS

Sounds like your IOC container (if it exists) is not overwriting the default controller factory. If using Ninject, you should install the Ninject.MVC3 project. 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

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). 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.

Can you change your base controller to be abstract? If so, I believe that will make things work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top