UnitOfWorkAttribute.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Transactions;
  3. namespace Abp.Domain.Uow
  4. {
  5. /// <summary>
  6. /// This attribute is used to indicate that declaring method is atomic and should be considered as a unit of work.
  7. /// A method that has this attribute is intercepted, a database connection is opened and a transaction is started before call the method.
  8. /// At the end of method call, transaction is committed and all changes applied to the database if there is no exception,
  9. /// otherwise it's rolled back.
  10. /// </summary>
  11. /// <remarks>
  12. /// This attribute has no effect if there is already a unit of work before calling this method, if so, it uses the same transaction.
  13. /// </remarks>
  14. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)]
  15. public class UnitOfWorkAttribute : Attribute
  16. {
  17. /// <summary>
  18. /// Scope option.
  19. /// </summary>
  20. public TransactionScopeOption? Scope { get; set; }
  21. /// <summary>
  22. /// Is this UOW transactional?
  23. /// Uses default value if not supplied.
  24. /// </summary>
  25. public bool? IsTransactional { get; set; }
  26. /// <summary>
  27. /// Timeout of UOW As milliseconds.
  28. /// Uses default value if not supplied.
  29. /// </summary>
  30. public TimeSpan? Timeout { get; set; }
  31. /// <summary>
  32. /// If this UOW is transactional, this option indicated the isolation level of the transaction.
  33. /// Uses default value if not supplied.
  34. /// </summary>
  35. public IsolationLevel? IsolationLevel { get; set; }
  36. /// <summary>
  37. /// Used to prevent starting a unit of work for the method.
  38. /// If there is already a started unit of work, this property is ignored.
  39. /// Default: false.
  40. /// </summary>
  41. public bool IsDisabled { get; set; }
  42. /// <summary>
  43. /// Creates a new UnitOfWorkAttribute object.
  44. /// </summary>
  45. public UnitOfWorkAttribute()
  46. {
  47. }
  48. /// <summary>
  49. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  50. /// </summary>
  51. /// <param name="isTransactional">
  52. /// Is this unit of work will be transactional?
  53. /// </param>
  54. public UnitOfWorkAttribute(bool isTransactional)
  55. {
  56. IsTransactional = isTransactional;
  57. }
  58. /// <summary>
  59. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  60. /// </summary>
  61. /// <param name="timeout">As milliseconds</param>
  62. public UnitOfWorkAttribute(int timeout)
  63. {
  64. Timeout = TimeSpan.FromMilliseconds(timeout);
  65. }
  66. /// <summary>
  67. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  68. /// </summary>
  69. /// <param name="isTransactional">Is this unit of work will be transactional?</param>
  70. /// <param name="timeout">As milliseconds</param>
  71. public UnitOfWorkAttribute(bool isTransactional, int timeout)
  72. {
  73. IsTransactional = isTransactional;
  74. Timeout = TimeSpan.FromMilliseconds(timeout);
  75. }
  76. /// <summary>
  77. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  78. /// <see cref="IsTransactional"/> is automatically set to true.
  79. /// </summary>
  80. /// <param name="isolationLevel">Transaction isolation level</param>
  81. public UnitOfWorkAttribute(IsolationLevel isolationLevel)
  82. {
  83. IsTransactional = true;
  84. IsolationLevel = isolationLevel;
  85. }
  86. /// <summary>
  87. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  88. /// <see cref="IsTransactional"/> is automatically set to true.
  89. /// </summary>
  90. /// <param name="isolationLevel">Transaction isolation level</param>
  91. /// <param name="timeout">Transaction timeout as milliseconds</param>
  92. public UnitOfWorkAttribute(IsolationLevel isolationLevel, int timeout)
  93. {
  94. IsTransactional = true;
  95. IsolationLevel = isolationLevel;
  96. Timeout = TimeSpan.FromMilliseconds(timeout);
  97. }
  98. /// <summary>
  99. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  100. /// <see cref="IsTransactional"/> is automatically set to true.
  101. /// </summary>
  102. /// <param name="scope">Transaction scope</param>
  103. public UnitOfWorkAttribute(TransactionScopeOption scope)
  104. {
  105. IsTransactional = true;
  106. Scope = scope;
  107. }
  108. /// <summary>
  109. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  110. /// </summary>
  111. /// <param name="scope">Transaction scope</param>
  112. /// <param name="isTransactional">
  113. /// Is this unit of work will be transactional?
  114. /// </param>
  115. public UnitOfWorkAttribute(TransactionScopeOption scope, bool isTransactional)
  116. {
  117. Scope = scope;
  118. IsTransactional = isTransactional;
  119. }
  120. /// <summary>
  121. /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
  122. /// <see cref="IsTransactional"/> is automatically set to true.
  123. /// </summary>
  124. /// <param name="scope">Transaction scope</param>
  125. /// <param name="timeout">Transaction timeout as milliseconds</param>
  126. public UnitOfWorkAttribute(TransactionScopeOption scope, int timeout)
  127. {
  128. IsTransactional = true;
  129. Scope = scope;
  130. Timeout = TimeSpan.FromMilliseconds(timeout);
  131. }
  132. internal UnitOfWorkOptions CreateOptions()
  133. {
  134. return new UnitOfWorkOptions
  135. {
  136. IsTransactional = IsTransactional,
  137. IsolationLevel = IsolationLevel,
  138. Timeout = Timeout,
  139. Scope = Scope
  140. };
  141. }
  142. }
  143. }