using System; using System.Transactions; namespace Abp.Domain.Uow { /// /// This attribute is used to indicate that declaring method is atomic and should be considered as a unit of work. /// A method that has this attribute is intercepted, a database connection is opened and a transaction is started before call the method. /// At the end of method call, transaction is committed and all changes applied to the database if there is no exception, /// otherwise it's rolled back. /// /// /// This attribute has no effect if there is already a unit of work before calling this method, if so, it uses the same transaction. /// [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)] public class UnitOfWorkAttribute : Attribute { /// /// Scope option. /// public TransactionScopeOption? Scope { get; set; } /// /// Is this UOW transactional? /// Uses default value if not supplied. /// public bool? IsTransactional { get; set; } /// /// Timeout of UOW As milliseconds. /// Uses default value if not supplied. /// public TimeSpan? Timeout { get; set; } /// /// If this UOW is transactional, this option indicated the isolation level of the transaction. /// Uses default value if not supplied. /// public IsolationLevel? IsolationLevel { get; set; } /// /// Used to prevent starting a unit of work for the method. /// If there is already a started unit of work, this property is ignored. /// Default: false. /// public bool IsDisabled { get; set; } /// /// Creates a new UnitOfWorkAttribute object. /// public UnitOfWorkAttribute() { } /// /// Creates a new object. /// /// /// Is this unit of work will be transactional? /// public UnitOfWorkAttribute(bool isTransactional) { IsTransactional = isTransactional; } /// /// Creates a new object. /// /// As milliseconds public UnitOfWorkAttribute(int timeout) { Timeout = TimeSpan.FromMilliseconds(timeout); } /// /// Creates a new object. /// /// Is this unit of work will be transactional? /// As milliseconds public UnitOfWorkAttribute(bool isTransactional, int timeout) { IsTransactional = isTransactional; Timeout = TimeSpan.FromMilliseconds(timeout); } /// /// Creates a new object. /// is automatically set to true. /// /// Transaction isolation level public UnitOfWorkAttribute(IsolationLevel isolationLevel) { IsTransactional = true; IsolationLevel = isolationLevel; } /// /// Creates a new object. /// is automatically set to true. /// /// Transaction isolation level /// Transaction timeout as milliseconds public UnitOfWorkAttribute(IsolationLevel isolationLevel, int timeout) { IsTransactional = true; IsolationLevel = isolationLevel; Timeout = TimeSpan.FromMilliseconds(timeout); } /// /// Creates a new object. /// is automatically set to true. /// /// Transaction scope public UnitOfWorkAttribute(TransactionScopeOption scope) { IsTransactional = true; Scope = scope; } /// /// Creates a new object. /// /// Transaction scope /// /// Is this unit of work will be transactional? /// public UnitOfWorkAttribute(TransactionScopeOption scope, bool isTransactional) { Scope = scope; IsTransactional = isTransactional; } /// /// Creates a new object. /// is automatically set to true. /// /// Transaction scope /// Transaction timeout as milliseconds public UnitOfWorkAttribute(TransactionScopeOption scope, int timeout) { IsTransactional = true; Scope = scope; Timeout = TimeSpan.FromMilliseconds(timeout); } internal UnitOfWorkOptions CreateOptions() { return new UnitOfWorkOptions { IsTransactional = IsTransactional, IsolationLevel = IsolationLevel, Timeout = Timeout, Scope = Scope }; } } }