/src/LinFu.AOP.Interfaces/AroundInvokeMethodCallRegistry.cs

http://github.com/philiplaureano/LinFu · C# · 58 lines · 36 code · 6 blank · 16 comment · 4 complexity · 431229cb166168d4b7a81b9dcd09e127 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 method call interception operations for all modified
  7. /// types.
  8. /// </summary>
  9. public static class AroundInvokeMethodCallRegistry
  10. {
  11. private static readonly List<IAroundInvokeProvider> _providers = new List<IAroundInvokeProvider>();
  12. private static readonly object _lock = new object();
  13. private static readonly BootStrapRegistry _registry = BootStrapRegistry.Instance;
  14. /// <summary>
  15. /// Obtains the <see cref="IAroundInvoke" /> instance for the given <paramref name="context" />.
  16. /// </summary>
  17. /// <param name="context">The <see cref="IInvocationInfo" /> instance that describes the current method call.</param>
  18. /// <returns>An <see cref="IAroundInvoke" /> instance that will be used to wrap a method call or method body.</returns>
  19. public static IAroundInvoke GetSurroundingImplementation(IInvocationInfo context)
  20. {
  21. var resultList = (from p in _providers
  22. where p != null
  23. let aroundInvoke = p.GetSurroundingImplementation(context)
  24. where aroundInvoke != null
  25. select aroundInvoke).ToList();
  26. if (resultList.Count == 0)
  27. return null;
  28. return new CompositeAroundInvoke(resultList);
  29. }
  30. /// <summary>
  31. /// Adds an <see cref="IAroundInvokeProvider" /> to the list of provider instances.
  32. /// </summary>
  33. /// <param name="provider">The <see cref="IAroundInvokeProvider" /> instance.</param>
  34. public static void AddProvider(IAroundInvokeProvider provider)
  35. {
  36. lock (_lock)
  37. {
  38. _providers.Add(provider);
  39. }
  40. }
  41. /// <summary>
  42. /// Clears the list of <see cref="IAroundInvokeProvider" /> instances.
  43. /// </summary>
  44. public static void Clear()
  45. {
  46. lock (_lock)
  47. {
  48. _providers.Clear();
  49. }
  50. }
  51. }
  52. }