Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming that you are using <a href="http://ninject.org/extensions" rel="noreferrer">Ninject.Extensions.Interception</a> this should do the trick</p> <pre><code>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() { } } </code></pre> <p>Make sure that the method that should be intercepted is marked as virtual.</p> <p>When <code>SomeTransactedMethod()</code> is called it should be intercepted.</p> <pre><code>var kernel = new StandardKernel(); kernel.Bind&lt;SomeClass&gt;().ToSelf(); var someClass = kernel.Get&lt;SomeClass&gt;(); someClass.SomeTransactedMethod(); </code></pre> <p><strong>UPDATE</strong></p> <p>You could create a custom planning strategy.</p> <pre><code>public class CustomPlanningStrategy&lt;TAttribute, TInterceptor&gt; : 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 =&gt; request.Kernel.Get&lt;TInterceptor&gt;(); adviceRegistry.Register(advice); if (!plan.Has&lt;ProxyDirective&gt;()) { plan.Add(new ProxyDirective()); } } } } private static IEnumerable&lt;MethodInfo&gt; 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) &amp;&amp; !methodInfo.IsPrivate &amp;&amp; !methodInfo.IsFinal; } } </code></pre> <p>This should now work.</p> <pre><code>var kernel = new StandardKernel(); kernel.Components.Add&lt;IPlanningStrategy, CustomPlanningStrategy&lt;TransactionAttribute, TransactionInterceptor&gt;&gt;(); kernel.Bind&lt;SomeClass&gt;().ToSelf(); var someClass = kernel.Get&lt;SomeClass&gt;(); someClass.SomeTransactedMethod(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload