带有 Ninject 和 HierarchicalLifetimeManager 的 ASP.Net MVC 3 项目?
首先,依赖注入对我来说相对较新。我使用 Unity.MVC3 做了第一个项目,现在我想在新项目上切换到 Ninject,因为它似乎是 .Net 项目中最流行的依赖注入器。所以现在,我尝试在我的项目中将 Ninject v2.2.1.4 与 Ninject.MVC3 v2.2.2.0 一起使用。
在我之前使用 Unity 的项目中,我的 Bootstrapper 类中有类似以下代码的内容:
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<ITestService, TestService>();
container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HierarchicalLifetimeManager());
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<ILoggingService, LoggingService>();
container.RegisterControllers();
return container;
}
现在,我的新项目中,我将其替换为 NinjectMVC3 类 (App_Start) 中类似以下代码的内容:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ITestService>().To<TestService>();
//This does not compile:
//kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>(new HierarchicalLifetimeManager());
kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
kernel.Bind<ILoggingService>().To<LoggingService>();
}
但是,我不不知道我应该如何处理 DatabaseFactory 绑定,因为它通常需要使用 HierarchicalLifetimeManager。谁能告诉我如何正确创建 DatabaseFactory 的绑定?
First of all, dependency injection is relatively new to me. I did a first project using Unity.MVC3, and now I would like to switch to Ninject on a new project, since it seems to be the most popular dependency injector for .Net projects. So now, I am trying to use Ninject v2.2.1.4 with Ninject.MVC3 v2.2.2.0 in my project.
In my previous project where I was using Unity, I had something like the following code in my Bootstrapper class:
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<ITestService, TestService>();
container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HierarchicalLifetimeManager());
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<ILoggingService, LoggingService>();
container.RegisterControllers();
return container;
}
Now, I my new project, I replaced this with something like the following code in the NinjectMVC3 class (App_Start):
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ITestService>().To<TestService>();
//This does not compile:
//kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>(new HierarchicalLifetimeManager());
kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
kernel.Bind<ILoggingService>().To<LoggingService>();
}
However, I don't know what I should do with the DatabaseFactory binding, since it normally requires the use of HierarchicalLifetimeManager. Can anyone tell me how to properly create the binding for DatabaseFactory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,通过 NuGet 添加这些引用,以确保您拥有一组兼容的包。
然后,如果您添加 Ninject.Web.MVC,它将通过 power shell 脚本为您设置项目初始化代码。
最后创建一个像这样的 BindModule 类,并将其添加到第二步创建的 CreateKernel 方法中的模块中。
NinjectMVC3 类的一部分
正如您在上面所看到的,Ninjet 具有内置函数来处理每个对象的不同生命周期。
First of all, add these references bu NuGet to be sure that you have a compatible set of packages.
Then, if you add the Ninject.Web.MVC it will setup the project initialization code for you through a power shell script.
And last make a BindModule class like this and add it to module in
CreateKernel
method that have been created in second step.Part of NinjectMVC3 class
As you can see above Ninjet has built in functions to take care of different life cycles for each object.