我对依赖注入完全陌生。我正在使用asp mvc和ninject。 我从'DefaultControllerFactory'扩展了一个类,并添加了'AddBindings'代码:
private void AddBindings()
{
ninjectKernel.Bind<ICitiesRepository>().To<CitiesRepository>().WithConstructorArgument(
"connectionString", ConfigurationManager.ConnectionStrings["cstr"].ConnectionString);
ninjectKernel.Bind<ICountriesRepository>().To<CountriesRepository>().WithConstructorArgument(
"connectionString", ConfigurationManager.ConnectionStrings["cstr"].ConnectionString);
ninjectKernel.Bind<IUserRepository>().To<UserRepository>().WithConstructorArgument(
"connectionString", ConfigurationManager.ConnectionStrings["cstr"].ConnectionString);
}
在我的'AccountController'中,我通过contructor注入IUserRepository:
private IUserRepository userRepository;
public AccountController(IUserRepository userRepository)
{
this.userRepository = userRepository;
}
在这里,我来发布。帐户控制器包含“RegisterUser”操作。并且用户必须使用标准参数(用户名,firstName等)选择一些附加参数,例如Country和City。如何在某些控制器中注入更多的存储库类?对此最好的策略是什么?
答案 0 :(得分:1)
How to use Ninject with ASP.NET MVC
在这里,您可以找到有关如何使用ASP.NET MVC设置Ninject的完整教程
private IUserRepository userRepository;
private IAddressRepository addressRepository;
public AccountController(IUserRepository userRepository, IAddressRepository addressRepository)
{
this.userRepository = userRepository;
this.addressRepository = addressRepository;
}