Guest User

Untitled

a guest
Jan 6th, 2015
116
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×