Using property injection instead of constructor injection

后端 未结 5 1018
时光取名叫无心
时光取名叫无心 2021-01-04 09:47

Long story short, I\'m trying to use ELMAH with MVC 2 and Ninject, and I need to use parameterless constructors. I created an initial post about it here: Using a parameterle

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 10:13

    I had a similar problem. Have a look at my questions: Using Ninject with Membership.Provider.

    Basically when you initialise DepartmentsController you need to injectthis (i.e. your departments controller into your Ninject kernal. So its something like:

    public class DepartmentsController : Controller
    {
      private IDepartmentsRepository _departmentsRepository;
    
      [Inject]
      public IDepartmentsRepository DepartmentsRepository
      {
        get { return _departmentsRepository; }
        set { _departmentsRepository = value; }
      }
    
      public DepartmentsController()
      {
        NinjectHelper.Kernel.Inject(this);
      }
    }
    

    Where NinjectHelper in this case gets the current Ninject Kernel.

提交回复
热议问题