/src/LinFu.AOP.Interfaces/AroundMethodBodyRegistry.cs

http://github.com/philiplaureano/LinFu · C# · 57 lines · 36 code · 6 blank · 15 comment · 4 complexity · 12d1c1f5081f793c8a0afefd49044afa MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace LinFu.AOP.Interfaces
  4. {
  5. /// <summary>
  6. /// Represents a registry class that handles all class-level interception operations for all modified types.
  7. /// </summary>
  8. public static class AroundMethodBodyRegistry
  9. {
  10. private static readonly List<IAroundInvokeProvider> _providers = new List<IAroundInvokeProvider>();
  11. private static readonly object _lock = new object();
  12. private static readonly BootStrapRegistry _registry = BootStrapRegistry.Instance;
  13. /// <summary>
  14. /// Obtains the <see cref="IAroundInvoke" /> instance for the given <paramref name="context" />.
  15. /// </summary>
  16. /// <param name="context">The <see cref="IInvocationInfo" /> instance that describes the current method call.</param>
  17. /// <returns>An <see cref="IAroundInvoke" /> instance that will be used to wrap a method call or method body.</returns>
  18. public static IAroundInvoke GetSurroundingImplementation(IInvocationInfo context)
  19. {
  20. var resultList = (from p in _providers
  21. where p != null
  22. let aroundInvoke = p.GetSurroundingImplementation(context)
  23. where aroundInvoke != null
  24. select aroundInvoke).ToList();
  25. if (resultList.Count == 0)
  26. return null;
  27. return new CompositeAroundInvoke(resultList);
  28. }
  29. /// <summary>
  30. /// Adds an <see cref="IAroundInvokeProvider" /> to the list of provider instances.
  31. /// </summary>
  32. /// <param name="provider">The <see cref="IAroundInvokeProvider" /> instance.</param>
  33. public static void AddProvider(IAroundInvokeProvider provider)
  34. {
  35. lock (_lock)
  36. {
  37. _providers.Add(provider);
  38. }
  39. }
  40. /// <summary>
  41. /// Clears the list of <see cref="IAroundInvokeProvider" /> instances.
  42. /// </summary>
  43. public static void Clear()
  44. {
  45. lock (_lock)
  46. {
  47. _providers.Clear();
  48. }
  49. }
  50. }
  51. }