在MVC 5中,我们得到了这样的帐户控制器.
public class AccountController : Controller { public AccountController() : this(new UserManager(new UserStore (new DatePickerDbContext()))) { } public AccountController(UserManager userManager) { UserManager = userManager; } }
我安装了Ninject来处理我的依赖注入.我有两个存储库,我想在我的AccountController中使用,现在代码看起来像这样
public class AccountController : Controller { private readonly ILicenserepository _licenserepository; private readonly IUserRepository _userRepository; public AccountController(ILicenserepository licenserepository, IUserRepository userRepository) : this(new UserManager(new UserStore (new DatePickerDbContext()))) { _licenserepository = licenserepository; _userRepository = userRepository; } public AccountController(UserManager userManager) { UserManager = userManager; } }
在ninject.web.common中,这就是我所做的
private static void RegisterServices(IKernel kernel) { kernel.Bind(typeof (ILicenserepository)).To(typeof (Licenserepository)); kernel.Bind(typeof (IUserRepository)).To(typeof (UserRepository)); }
但是当我运行应用程序时,我在浏览器上收到错误,说没有找到参数化的构造函数.如果我创建参数较少的构造函数,我的存储库不会被实例化.因此,无论我从存储库调用该方法,值都为null.我怎么解决这个问题?或者让MVC或Ninject知道要调用哪个构造函数?