SHARE
TWEET

Untitled

a guest Jan 6th, 2015 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Reflection;
  8. using EIT.Foundation.Diagnostics;
  9. using Ninject;
  10. using Ninject.Components;
  11. using Ninject.Extensions.Interception;
  12. using Ninject.Extensions.Interception.Advice;
  13. using Ninject.Extensions.Interception.Planning.Directives;
  14. using Ninject.Extensions.Interception.Registry;
  15. using Ninject.Planning;
  16. using Ninject.Planning.Strategies;
  17.  
  18. #endregion
  19.  
  20. namespace EIT.Foundation.Interception.Ninject
  21. {
  22.         public class AttributeInterceptionStrategy<TAttribute, TInterceptor>
  23.                 : NinjectComponent, IPlanningStrategy
  24.                 where TAttribute : Attribute
  25.                 where TInterceptor : IInterceptor
  26.         {
  27.                 private readonly IAdviceFactory _adviceFactory;
  28.                 private readonly IAdviceRegistry _adviceRegistry;
  29.  
  30.                 public AttributeInterceptionStrategy(IAdviceFactory adviceFactory, IAdviceRegistry adviceRegistry)
  31.                 {
  32.                         Guard.AgainstNullArgument("adviceFactory", adviceFactory);
  33.                         Guard.AgainstNullArgument("adviceRegistry", adviceRegistry);
  34.  
  35.                         _adviceFactory = adviceFactory;
  36.                         _adviceRegistry = adviceRegistry;
  37.                 }
  38.  
  39.                 public void Execute(IPlan plan)
  40.                 {
  41.                         var methods = GetCandidateMethods(plan.Type);
  42.                         foreach (var advice in from method in methods
  43.                                 let attributes = method.GetCustomAttributes(
  44.                                         typeof (TAttribute), true) as TAttribute[]
  45.                                 where attributes == null ||
  46.                                       attributes.Length != 0
  47.                                 select _adviceFactory.Create(method))
  48.                         {
  49.                                 advice.Callback = request => request.Kernel.Get<TInterceptor>();
  50.                                 _adviceRegistry.Register(advice);
  51.  
  52.                                 if (!plan.Has<ProxyDirective>())
  53.                                 {
  54.                                         plan.Add(new ProxyDirective());
  55.                                 }
  56.                         }
  57.                 }
  58.  
  59.                 private static IEnumerable<MethodInfo> GetCandidateMethods(IReflect type)
  60.                 {
  61.                         var methods = type.GetMethods(
  62.                                 BindingFlags.Public |
  63.                                 BindingFlags.NonPublic |
  64.                                 BindingFlags.Instance
  65.                                 );
  66.  
  67.                         return methods.Where(ShouldIntercept);
  68.                 }
  69.  
  70.                 private static bool ShouldIntercept(MethodInfo methodInfo)
  71.                 {
  72.                         var result = methodInfo.DeclaringType != typeof (object) &&
  73.                                !methodInfo.IsPrivate &&
  74.                                !methodInfo.IsFinal;
  75.                         return result;
  76.                 }
  77.         }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top