带有 Ninject 和 HierarchicalLifetimeManager 的 ASP.Net MVC 3 项目?

发布于 2024-12-27 01:37:11 字数 1366 浏览 1 评论 0原文

首先,依赖注入对我来说相对较新。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2025-01-03 01:37:11

首先,通过 NuGet 添加这些引用,以确保您拥有一组兼容的包。

然后,如果您添加 Ninject.Web.MVC,它将通过 power shell 脚本为您设置项目初始化代码。

最后创建一个像这样的 BindModule 类,并将其添加到第二步创建的 CreateKernel 方法中的模块中。

public class BindModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<IControllerActivator>().To<CustomControllerActivator>().InRequestScope();
        this.Bind<MembaseClient>().ToMethod(context => new MembaseClient()).InSingletonScope();
        this.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
        this.Bind<ISessionFactory>().ToMethod(o => MyAutoMapper.sessionFactory).InSingletonScope();
        this.Bind<ISession>().ToMethod(o => MyAutoMapper.sessionFactory.OpenSession()).InRequestScope();
        }
  }

NinjectMVC3 类的一部分

public static class NinjectMVC3
{
    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var modules = new NinjectModule[] { new BindModule() };
        var kernel = new StandardKernel(modules);

        RegisterServices(kernel);

        return kernel;
    }
}

正如您在上面所看到的,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.

public class BindModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<IControllerActivator>().To<CustomControllerActivator>().InRequestScope();
        this.Bind<MembaseClient>().ToMethod(context => new MembaseClient()).InSingletonScope();
        this.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
        this.Bind<ISessionFactory>().ToMethod(o => MyAutoMapper.sessionFactory).InSingletonScope();
        this.Bind<ISession>().ToMethod(o => MyAutoMapper.sessionFactory.OpenSession()).InRequestScope();
        }
  }

Part of NinjectMVC3 class

public static class NinjectMVC3
{
    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var modules = new NinjectModule[] { new BindModule() };
        var kernel = new StandardKernel(modules);

        RegisterServices(kernel);

        return kernel;
    }
}

As you can see above Ninjet has built in functions to take care of different life cycles for each object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文