如何建立与Ninject一个可选的方法拦截? [英] How to set up an optional method interception with Ninject?

查看:228
本文介绍了如何建立与Ninject一个可选的方法拦截?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有,我想有时*(不过现在总是)拦截一些(但不是全部)方法的类。我的理解是,这是可以做到无论是与,比方说, InterceptAround()在我的Ninject模块(在更高级别的代码),或与InterceptAttribute衍生属性上的这些方法(在执行层面)。

Suppose I have a class in which I want to sometimes* (but now always) intercept some (but not all) methods. The way I understand it, this can be done either with, say, InterceptAround() in my Ninject module (in the higher-level code), or with an InterceptAttribute-derived attribute on those methods (at the implementation level).

我真的不喜欢做它的第一种方式,因为它要求消费者知道的细节,有马上就很多类有许多方法。但我不喜欢第二种方式要么,因为我看不出如何禁用(或者说,不启用)拦截,因为属性与代码融合。

I don't really like the first way of doing it, because it requires the consumer to know the details, there'll be many classes with many methods. But I don't like the second way either, since I don't see how to disable (or, rather, not to enable) the interception, as the attribute is fused with the code.

有一些已知的方法来解决这个问题

Is there some known approach to solve this problem?

*:用于应用的生命周期

*: for the lifetime of the application.

推荐答案

这听起来就好像你指的是一个普通的动态拦截,这是Ninject拦截扩展是如何工作的默认情况下。

It sounds as though you are referring to an ordinary dynamic interceptor, which is how the Ninject Interception extension works by default.

下面是一个有条件拦截的例子:

Here's an example of a conditional interception:

class CustomInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        if (invocation.Request.Method.Name == "MethodToIntercept")
            Console.WriteLine("Intercepted!");
        invocation.Proceed();
    }
}

您绑定直接到像这样一个类:

You bind it directly to a single class like so:

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IFoo>().To<MyFoo>().Intercept().With<CustomInterceptor>();
    }
}



这是相当多,如果你所有你需要做的。要动态拦截一个类

And that's pretty much all you have to do if you want to dynamically intercept a single class.

内核扩展的前途,因为他们让你直接写入条件进入声明:

The kernel extensions look promising because they let you write conditions directly into the declaration:

kernel.Intercept(ctx => ctx.Request.Service == typeof(IFoo))
    .With<CustomInterceptor>();



不过,这个,如果你正在努力使基础上的法的执行,因为这不仅可以访问的绑定上下文,而不是调用。主要是,这个扩展的存在,让您可以选择的服务的(相对的方法的),在运行时拦截。

However, this isn't particularly useful if you're trying to make decisions based on the method being executed, because this only gives you access to the binding context, and not the invocation. Mainly, this extension exists so that you can choose which classes or services (as opposed to methods) to intercept at runtime.

更好地坚持到绑定语法,并直接写入运行或鸵鸟政策运行逻辑放到拦截,在第一个例子说明。

Better to stick to the binding syntax, and write the run-or-don't-run logic directly into the interceptor, as illustrated in the first example.

要注意的一个重要的事情是一个动态的拦截器实际上是这是极其低效的每一个的(公共/虚拟)在任何类势必方法,跑。不幸的是,Ninject拦截扩展了,因为它被设计为支持多个代理库采取最低公分母的方法。如果你直接使用城堡,你可以的使用代理代钩子和拦截选择器细粒度控制,这实际上是推荐的方法。据我可以从Ninject-DP2源代码告诉,这不是与Ninject扩展支持。

One important thing to note is that a dynamic interceptor will actually run for every (public / virtual) method on whichever class it is bound to, which can be very inefficient. Unfortunately, the Ninject Interception extension has to take a lowest-common-denominator approach because it is designed to support multiple proxy libraries. If you use Castle directly, you can use proxy generation hooks and interceptor selectors for fine-grained control, which is actually the recommended approach. As far as I can tell from the Ninject-DP2 source code, this is not supported with the Ninject extension.

就个人而言,我从来没有过不少成功的用在Ninject拦截扩展正是这个原因,而且往往坚持使用直接DP2城堡。但是,如果你正在做这个小规模的,不写一个性能敏感的应用程序,你应该罚款编写动态拦截器。

Personally, I've never had a lot of success with the Ninject Interception extension for exactly this reason, and tend to stick to using Castle DP2 directly. However, if you're doing this on a small scale and aren't writing a performance-sensitive app, you should be fine writing dynamic interceptors.

这篇关于如何建立与Ninject一个可选的方法拦截?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆