我怎么能得到Ninject。扩展。拦截基本上让我将特定的拦截器绑定到任何具有属性的方法。..[医]假码:

Kernel.Intercept(context => context.Binding.HasAttribute<TransactionAttribute>())
      .With<TransactionInterceptor>

像这样的班级:

public SomeClass
{
    [TransactionAttribute]
    public void SomeTransactedMethod()
    { /*do stuff */ }
}
有帮助吗?

解决方案

假设你正在使用 第九。扩展。拦截 这应该做的伎俩

public class TransactionInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        // Do something...
    }
}

public class TransactionAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return new TransactionInterceptor();
    }
}

public class SomeClass
{
    [Transaction]
    public virtual void SomeTransactedMethod() { }
}

确保应该拦截的方法标记为虚拟。

何时 SomeTransactedMethod() 被称为它应该被拦截。

var kernel = new StandardKernel();
kernel.Bind<SomeClass>().ToSelf();

var someClass = kernel.Get<SomeClass>();
someClass.SomeTransactedMethod();

更新资料

您可以创建自定义规划策略。

public class CustomPlanningStrategy<TAttribute, TInterceptor> :
    NinjectComponent, IPlanningStrategy
        where TAttribute : Attribute
        where TInterceptor : IInterceptor
{
    private readonly IAdviceFactory adviceFactory;
    private readonly IAdviceRegistry adviceRegistry;

    public CustomPlanningStrategy(
        IAdviceFactory adviceFactory, IAdviceRegistry adviceRegistry)
        {
            this.adviceFactory = adviceFactory;
            this.adviceRegistry = adviceRegistry;
        }

        public void Execute(IPlan plan)
        {
            var methods = GetCandidateMethods(plan.Type);
            foreach (var method in methods)
            {
                var attributes = method.GetCustomAttributes(
                    typeof(TAttribute), true) as TAttribute[];
                if (attributes.Length == 0)
                {
                    continue;
                }

                var advice = adviceFactory.Create(method);

                advice.Callback = request => request.Kernel.Get<TInterceptor>();
                adviceRegistry.Register(advice);

                if (!plan.Has<ProxyDirective>())
                {
                    plan.Add(new ProxyDirective());
                }
            }
        }
    }

    private static IEnumerable<MethodInfo> GetCandidateMethods(Type type)
    {
        var methods = type.GetMethods(
            BindingFlags.Public | 
            BindingFlags.NonPublic | 
            BindingFlags.Instance
        );

        return methods.Where(ShouldIntercept);
    }

    private static bool ShouldIntercept(MethodInfo methodInfo)
    {
        return methodInfo.DeclaringType != typeof(object) &&
               !methodInfo.IsPrivate &&
               !methodInfo.IsFinal;
    }
}

这应该现在工作。

var kernel = new StandardKernel();
kernel.Components.Add<IPlanningStrategy, 
    CustomPlanningStrategy<TransactionAttribute, TransactionInterceptor>>();

kernel.Bind<SomeClass>().ToSelf();

var someClass = kernel.Get<SomeClass>();
someClass.SomeTransactedMethod();

其他提示

这是我用于相同目的的代码 通用标签

通用标签

以及用于TransactionAttribute的代码 通用标签

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top