1

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

Community
  • 1
  • 1
user197508
  • 1,282
  • 2
  • 18
  • 31

4 Answers4

1

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

Jon
  • 38,814
  • 81
  • 233
  • 382
1

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.

Whoever
  • 1,295
  • 1
  • 14
  • 21
1

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.

Community
  • 1
  • 1
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Hi David Ebbo, +1 vote, I tested your solution and worked for me, But CodeRush get a issue in sub-class which 'Base type constructor are not implemented.' .please see my solution. – user197508 Mar 10 '13 at 10:41
  • David Ebbo, Also in your solution some of overload of methods which have a extra 'ActionResult result' argument Not accessable in Base Controller! – user197508 Mar 10 '13 at 10:48
1

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

user197508
  • 1,282
  • 2
  • 18
  • 31