UnitOfWorkInterceptor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Reflection;
  3. using System.Threading.Tasks;
  4. using Abp.Threading;
  5. using Castle.DynamicProxy;
  6. namespace Abp.Domain.Uow
  7. {
  8. /// <summary>
  9. /// This interceptor is used to manage database connection and transactions.
  10. /// </summary>
  11. internal class UnitOfWorkInterceptor : IInterceptor
  12. {
  13. private readonly IUnitOfWorkManager _unitOfWorkManager;
  14. private readonly IUnitOfWorkDefaultOptions _unitOfWorkOptions;
  15. public UnitOfWorkInterceptor(IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkDefaultOptions unitOfWorkOptions)
  16. {
  17. _unitOfWorkManager = unitOfWorkManager;
  18. _unitOfWorkOptions = unitOfWorkOptions;
  19. }
  20. /// <summary>
  21. /// Intercepts a method.
  22. /// </summary>
  23. /// <param name="invocation">Method invocation arguments</param>
  24. public void Intercept(IInvocation invocation)
  25. {
  26. MethodInfo method;
  27. try
  28. {
  29. method = invocation.MethodInvocationTarget;
  30. }
  31. catch
  32. {
  33. method = invocation.GetConcreteMethod();
  34. }
  35. var unitOfWorkAttr = _unitOfWorkOptions.GetUnitOfWorkAttributeOrNull(method);
  36. if (unitOfWorkAttr == null || unitOfWorkAttr.IsDisabled)
  37. {
  38. //No need to a uow
  39. invocation.Proceed();
  40. return;
  41. }
  42. //No current uow, run a new one
  43. PerformUow(invocation, unitOfWorkAttr.CreateOptions());
  44. }
  45. private void PerformUow(IInvocation invocation, UnitOfWorkOptions options)
  46. {
  47. if (invocation.Method.IsAsync())
  48. {
  49. PerformAsyncUow(invocation, options);
  50. }
  51. else
  52. {
  53. PerformSyncUow(invocation, options);
  54. }
  55. }
  56. private void PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
  57. {
  58. using (var uow = _unitOfWorkManager.Begin(options))
  59. {
  60. invocation.Proceed();
  61. uow.Complete();
  62. }
  63. }
  64. private void PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options)
  65. {
  66. var uow = _unitOfWorkManager.Begin(options);
  67. try
  68. {
  69. invocation.Proceed();
  70. }
  71. catch
  72. {
  73. uow.Dispose();
  74. throw;
  75. }
  76. if (invocation.Method.ReturnType == typeof(Task))
  77. {
  78. invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(
  79. (Task) invocation.ReturnValue,
  80. async () => await uow.CompleteAsync(),
  81. exception => uow.Dispose()
  82. );
  83. }
  84. else //Task<TResult>
  85. {
  86. invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
  87. invocation.Method.ReturnType.GenericTypeArguments[0],
  88. invocation.ReturnValue,
  89. async () => await uow.CompleteAsync(),
  90. exception => uow.Dispose()
  91. );
  92. }
  93. }
  94. }
  95. }