/src/LinFu.IoC/Interceptors/AroundInvokeAdapter.cs

http://github.com/philiplaureano/LinFu · C# · 61 lines · 31 code · 8 blank · 22 comment · 0 complexity · 63e9046084b326980234aa8cbed6ebd6 MD5 · raw file

  1. using System;
  2. using System.Reflection;
  3. using LinFu.AOP.Interfaces;
  4. using LinFu.IoC.Configuration.Interfaces;
  5. namespace LinFu.IoC.Interceptors
  6. {
  7. /// <summary>
  8. /// Adapts a <see cref="IAroundInvoke" /> instance into an <see cref="IInterceptor" />.
  9. /// </summary>
  10. internal class AroundInvokeAdapter : BaseInterceptor
  11. {
  12. private readonly Func<object> _getTarget;
  13. private readonly IAroundInvoke _wrapper;
  14. /// <summary>
  15. /// Initializes the <see cref="AroundInvokeAdapter" /> class.
  16. /// </summary>
  17. /// <param name="getTarget">The functor responsible for obtaining the target instance.</param>
  18. /// <param name="methodInvoke">The method invoker.</param>
  19. /// <param name="aroundInvoke">The target <see cref="IAroundInvoke" /> instance.</param>
  20. internal AroundInvokeAdapter(Func<object> getTarget, IMethodInvoke<MethodInfo> methodInvoke,
  21. IAroundInvoke aroundInvoke)
  22. : base(methodInvoke)
  23. {
  24. _wrapper = aroundInvoke;
  25. _getTarget = getTarget;
  26. }
  27. /// <summary>
  28. /// Converts the call to <see cref="IInterceptor.Intercept" /> to an
  29. /// <see cref="IAroundInvoke" /> method call.
  30. /// </summary>
  31. /// <param name="info">The <see cref="IInvocationInfo" /> that describes the context of the method call.</param>
  32. /// <returns>The return value from the target method.</returns>
  33. public override object Intercept(IInvocationInfo info)
  34. {
  35. object result = null;
  36. // Signal the beginning of the method call
  37. _wrapper.BeforeInvoke(info);
  38. // Call the target method
  39. result = base.Intercept(info);
  40. // Postprocess the results
  41. _wrapper.AfterInvoke(info, result);
  42. return result;
  43. }
  44. /// <summary>
  45. /// Gets the target object instance.
  46. /// </summary>
  47. /// <param name="info">The <see cref="IInvocationInfo" /> instance that describes the current execution context.</param>
  48. protected override object GetTarget(IInvocationInfo info)
  49. {
  50. return _getTarget();
  51. }
  52. }
  53. }