c# - Ninject ASP.NET MVC 项目中的数据注入
问题描述
我正在开发一个在我的 ASP.NET MVC5 项目中使用 Ninject 数据注入的 Web 应用程序。我设置了一个继承自 IDependencyResolver 的 NinjectDependencyResolver,如下所示:
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
// here I have all my bindings set up
kernel.Bind<ProConnect.Domain.Abstract.IMyRepository>().To<MyRepository>();
}
}
这是在应用程序启动时触发并注册服务的 NinjectWebCommon 类:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyApp.WebUI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(MyApp.WebUI.App_Start.NinjectWebCommon), "Stop")]
namespace ProConnect.WebUI.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using Ninject.Web.Common.WebHost;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application.
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
// creating the bridge between the NinjectDependencyResolver class and the MVC support for dependency injection.
System.Web.Mvc.DependencyResolver.SetResolver(new
MyApp.WebUI.Infrastructure.NinjectDependencyResolver(kernel));
// maybe I have to add some code here to create the bridge between
// NinjectDependencyResolver class and my standard classes that
// have nothing to do with MVC????
}
}
}
我使用它来将来自域的 Repository 对象注入到我的 MVC 控制器中,这一切都运行良好。
我遇到的问题是我想将一些其他存储库对象注入一个设置类,该类将从数据库中检索一些设置数据。由于我的设置类不是 MVC 控制器,而只是一个普通的旧类,它不知道如何注入数据。
有人可以帮我解决这个问题吗?我尝试像这样再次设置数据注入,但没有成功:
public class Settings
{
private Domain.Abstract.ISettingRepository settingRepository;
private StandardKernel kernel;
public Settings()
{
this.kernel = new StandardKernel();
// I don't know if this loads a new kernel or the one that is used in the rest of the MVC application
kernel.Load(Assembly.GetExecutingAssembly());
// I tryed re-specifying the bindings but this didn't help
kernel.Bind<Domain.Abstract.ISettingRepository>().To<Domain.Concrete.SettingRepository>();
this.settingRepository = kernel.Get<Domain.Abstract.ISettingRepository>();
}
public void DoSomethigWithSettings(){
this.settingRepository.Settings()......
}
}
解决方案
好的,感谢https://stackoverflow.com/a/19585471/11011693 ,我找到了答案
我只是从应用程序启动时设置的当前 MVC DependencyResolver 中获得了我想注入到非 MVC 类中的对象
public class Settings
{
private Domain.Abstract.ISettingRepository settingRepository;
public Settings()
{
// simply get the object you want to inject in a non MVC class
// from the current MVC DependencyResolver that is set up on Application Start
this.settingRepository = (Domain.Abstract.IAppSettingService)System.Web.Mvc.DependencyResolver.Current.GetService(typeof(Domain.Abstract.IAppSettingService));
}
public void DoSomethigWithSettings(){
this.settingRepository.Settings()......
}
}
推荐阅读
- javascript - Internet Explorer 错误 SCRIPT1014 无效字符`
- reactjs - React jsx - 在地图函数内进行条件渲染时出现意外令牌错误
- laravel - 在 Redis 中使用标签缓存键会降低性能
- java - 在intellij的一个包中创建两个包
- r - R:如何修改槽的值?
- ios - 自定义 UINavigationBar 不尊重给定的高度
- python - 为什么 pandas read_csv 只返回前 1024 列?
- sql - SQL Group By 在某个时间范围内
- ruby-on-rails - 如何使用 shoulda-matchers gem 测试 rspec 中的关联?
- apache-kafka - cassandra中json数据插入的kafka-cassandra-sink错误