注入控制器时,Ninject不会注入类

时间:2014-10-24 12:32:23

标签: c# asp.net-mvc dependency-injection ninject

我正在使用Ninject来解决MVC项目中的简单依赖。我在NinjectWebCommon.cs中绑定它:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    //kernel.Load(Assembly.GetExecutingAssembly());
    kernel.Bind<DataAccess.IDataAccess>().To<DataAccess.DataAccess>();
}   

我的基本控制器如下所示:

public partial class BaseController : Controller
{
    //Reference to Data Access layer, managed by IoC container
    protected IDataAccess _dataAccess;

    [Inject]
    public IDataAccess DataAccess
    {
        get { return _dataAccess; }
        set { _dataAccess = value; }
    }
}

工作正常。但它不适用于简单且非常相似的类。

public partial class FooEditVMBuilder
{
    //Reference to Data Access layer, managed by IoC container
    public IDataAccess _dataAccess;

    [Inject]
    public IDataAccess DataAccess
    {
        get { return _dataAccess; }
        set { _dataAccess = value; }
    }
}

NinjectWebCommon中的方法是否只绑定控制器?

0 个答案:

没有答案