Ninject:每个被拦截的类实例有一个拦截器实例?

发布于 2024-10-24 01:36:11 字数 519 浏览 2 评论 0原文

我目前遇到一个问题,尝试为每个被拦截的类实例连接一个拦截器实例。

我正在 InterceptorRegistrationStrategy 中创建 Advice 并设置回调以解析来自内核的拦截器(它有一个注入构造函数)。请注意,我只能在回调中实例化拦截器,因为 InterceptorRegistrationStrategy 没有引用 Kernel 本身。

            IAdvice advice = this.AdviceFactory.Create(methodInfo);
            advice.Callback = ((context) => context.Kernel.Get<MyInterceptor>());
            this.AdviceRegistry.Register(advice);

我得到每个方法的拦截器实例。

有没有办法为每个被拦截的类型实例创建一个拦截器实例?

我正在考虑命名范围,但拦截类型和拦截器不互相引用。

I'm currently having a problem, trying to wire up exactly one interceptor instance per one instance of class being intercepted.

I'm creating and Advice in the InterceptorRegistrationStrategy and setting the callback to resolve an interceptor from the kernel (it has an injection constructor). Please note that I can only instantiate interceptor in the callback because InterceptorRegistrationStrategy does not have reference to Kernel itself.

            IAdvice advice = this.AdviceFactory.Create(methodInfo);
            advice.Callback = ((context) => context.Kernel.Get<MyInterceptor>());
            this.AdviceRegistry.Register(advice);

I'm getting an instance of interceptor per method.

Is there any way to create one interceptor instance per type instance being intercepted?

I was thinking about Named Scope, but intercepted type and interceptor do not reference each other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蛮可爱 2024-10-31 01:36:11

这是不可能的,因为为绑定的所有实例的每个方法创建一个拦截器。

但是您可以做的不是直接在拦截器中执行拦截代码,而是获取将处理拦截的类的实例。

public class LazyCountInterceptor : SimpleInterceptor
{
    private readonly IKernel kernel;

    public LazyCountInterceptor(IKernel kernel)
    {
        this.kernel = kernel;
    }

    protected override void BeforeInvoke(IInvocation invocation)
    {
        this.GetIntercpetor(invocation).BeforeInvoke(invocation);
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        this.GetIntercpetor(invocation).AfterInvoke(invocation);
    }

    private CountInterceptorImplementation GetIntercpetor(IInvocation invocation)
    {
        return this.kernel.Get<CountInterceptorImplementation>(
            new Parameter("interceptionTarget", new WeakReference(invocation.Request.Target), true));                
    }
}

public class CountInterceptorImplementation
{
    public void BeforeInvoke(IInvocation invocation)
    {
    }

    public void AfterInvoke(IInvocation invocation)
    {
    }
}

kernel.Bind<CountInterceptorImplementation>().ToSelf()
      .InScope(ctx => ((WeakReference)ctx.Parameters.Single(p => p.Name == "interceptionTarget").GetValue(ctx, null)).Target);

That's not possible as one single interceptor is created per method for all instances of a binding.

But what you can do is not to execute the interception code directly in the interceptor but to get an instance of a class that will handle the interception.

public class LazyCountInterceptor : SimpleInterceptor
{
    private readonly IKernel kernel;

    public LazyCountInterceptor(IKernel kernel)
    {
        this.kernel = kernel;
    }

    protected override void BeforeInvoke(IInvocation invocation)
    {
        this.GetIntercpetor(invocation).BeforeInvoke(invocation);
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        this.GetIntercpetor(invocation).AfterInvoke(invocation);
    }

    private CountInterceptorImplementation GetIntercpetor(IInvocation invocation)
    {
        return this.kernel.Get<CountInterceptorImplementation>(
            new Parameter("interceptionTarget", new WeakReference(invocation.Request.Target), true));                
    }
}

public class CountInterceptorImplementation
{
    public void BeforeInvoke(IInvocation invocation)
    {
    }

    public void AfterInvoke(IInvocation invocation)
    {
    }
}

kernel.Bind<CountInterceptorImplementation>().ToSelf()
      .InScope(ctx => ((WeakReference)ctx.Parameters.Single(p => p.Name == "interceptionTarget").GetValue(ctx, null)).Target);
全部不再 2024-10-31 01:36:11

您是否尝试过使用 Fluent API 来配置您的拦截?

Bind<IInterceptor>().To<MyInterceptor>().InSingletonScope();
Bind<IService>().To<Service>().Intercept().With<IInterceptor>();

注意 Intercept() 扩展方法位于 Ninject.Extensions.Interception.Infrastruct.Language.ExtensionsForIBindingSyntax

Have you tried using the fluent API to configure your interception?

Bind<IInterceptor>().To<MyInterceptor>().InSingletonScope();
Bind<IService>().To<Service>().Intercept().With<IInterceptor>();

N.B. The Intercept() extension method is in Ninject.Extensions.Interception.Infrastructure.Language.ExtensionsForIBindingSyntax

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