Ninject有一个基本控制器?

cho*_*bo2 5 asp.net-mvc ninject asp.net-mvc-3

我想知道如果你有一个基本控制器,你如何使用ninject 2.0进行构造函数注入?

我有

        private readonly IBaseService baseService;

        public BaseController(IBaseService baseService)
        {
            this.baseService = baseService;

        }


Bind<IBaseService>().To<BaseService>();


public class OtherController : BaseController
{
        private readonly IOtherService otherService;

        public OtherController(IOtherService otherService, IBaseService baseService) 
        {
            this.otherService = otherService;
        }
Run Code Online (Sandbox Code Playgroud)

但我得到了

'BaseController'不包含带0参数的构造函数

Mat*_*ott 13

您需要将两个服务注入OtherController并调用基础构造函数,并传递它所需的服务:

public OtherController(IOtherService otherService, IBaseService baseService)
    : base(baseService) { this.otherService = otherService; }
Run Code Online (Sandbox Code Playgroud)

  • @ chobo2忽略依赖注入,但查看继承...如果基类需要服务,继承类负责提供它.否则,无法正确初始化基本类型. (3认同)