配置NinjectNinject

2023-09-06 06:00:42 作者:裸奔中擦伤下体

在与雷莫Gloor(主要开发)对我们的Ninject配置,.NET是否有泄漏/ Ninject /直接委托根内存分析/风险,我想获得一些清晰度上正确配置它为ASP.NET web表单应用程序。

Following a discussion with Remo Gloor (main dev) about our Ninject configuration, .NET memory profiling / risks for leaks / Ninject / Direct delegate roots, I'd like to get some clarity on configuring it properly for an ASP.NET webforms application.

我们拥有目前,我们正在做如下要求:

We have a requirement currently where we are doing the following:

Bind<ISearchService>()
    .ToMethod(ctx => new BaseSearchService(ctx.Kernel.GetDefault<IDataRetrievalService>()))
    .InSingletonScope()
    .Named("BaseSearchService");

Bind<ISearchService>()
    .ToMethod(ctx => new HttpSearchService(
        ctx.Kernel.GetNamedOrDefault<ISearchService>("BaseSearchService"),
        HttpContext.Current))
    .InRequestScope();

GetNamedOrDefault是,我们有一个扩展方法:

GetNamedOrDefault is an extension method we have:

public static T GetDefault<T>(this IKernel kernel)
{
    return kernel.Get<T>(m => m.Name == null);
}

public static object GetDefault(this IKernel kernel, Type type)
{
    return kernel.Get(type, m => m.Name == null);
}

public static T GetNamedOrDefault<T>(this IKernel kernel, string name)
{
    T result = kernel.TryGet<T>(name);

    if (result != null)
        return result;

    return kernel.GetDefault<T>();
}

public static object GetNamedOrDefault(this IKernel kernel, Type type, string name)
{
    var result = kernel.TryGet(type, name);

    if (result != null)
        return result;

    return kernel.GetDefault(type);
}

是如何最好的,我们重新present这Ninject?如果我们用WhenParentNamed,让Ninject决定传递给构造什么对象?

How best are we to represent this in Ninject? Should we use "WhenParentNamed" and let Ninject decide what object to pass to the constructor?

同样,我们如何结合当前HttpContext.Current对象,这样Ninject知道使用,每当一个构造函数接受一个HttpContext对象作为它的参数之一?如果它是在这里看到的是一样的吗?

Likewise, how do we bind the current HttpContext.Current object so that Ninject knows to use that whenever a constructor takes a HttpContext object as one of its parameters? Should it be the same as seen here?

的https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/MvcModule.cs

如果我们正在使用的请求范围内,我们是否应该使用OnePerRequestModule和应用程序的Web.config配置此?

If we are making use of the request scope, should we be using the OnePerRequestModule and configuring this in the Web.config of the application?

如果我们还可以使用:

https://github.com/ninject/Ninject.Web.Common/blob/master/src/Ninject.Web.Common/NinjectHttpApplication.cs

要确保我们的对象是妥善处理?

To ensure our objects are disposed properly?

这看起来很简单了一些,但我只是想明确的方法大家都需要。

This may seem quite simplistic to some but I just want to be clear on the approach everyone takes.

感谢

推荐答案

在使用的情况下装修的一些有条件的结合(如WhenParentNamed,WhenClassHas,WhenTargetHas或custon当)是最好的一段路要走。

In case of decoration using some conditional binding (e.g. WhenParentNamed, WhenClassHas, WhenTargetHas or a custon When) is the best way to go.

Bind<ISearchService>()
  .To<BaseSearchService>()
  .InSingletonScope()
  .WhenParentNamed("HttpServiceDecorator");

Bind<ISearchService>()
    .To<HttpSearchService>()
    .Named("HttpServiceDecorator")
    .InRequestScope();

Bind<HttpContext>().ToMethod(ctx => HttpContext.Current);

要获取服务的最佳方式是把它注入到需要它的类的构造函数。没有什么特别需要接收HttpSearchService的一个实例。它将被作为默认值。

The best way to get the service is to inject it to the constructor of the class that needs it. There is nothing special necessary to receive an instance of HttpSearchService. It will be passed as default.

由于Ninject.Web 2.2 OnePerRequestModule默认情况下使用。因此,没有改变是必需的。

Since Ninject.Web 2.2 the OnePerRequestModule is used by default. So no change is required.

Ninject.Web.Common引入了upcomming Ninject 2.4版本。它是由所有的网络扩展一个基本组件。这意味着,只要你留在2.2,则不能使用它。当您切换到2.4(或2.3开发版本)将不得不使用它。

Ninject.Web.Common is introduced for the upcomming Ninject 2.4 release. It is a base component used by all web extensions. This means as long as you stay on 2.2 you must not use it. As soon as you switch to 2.4 (or a 2.3 development build) will have to use it.