/// <summary> /// Gets a <see cref="UnitOfWorkScopeTransaction"/> instance that can be used by a <see cref="UnitOfWorkScope"/> instance. /// </summary> /// <param name="scope">The <see cref="UnitOfWorkScope"/> instance that is requesting the transaction.</param> /// <param name="isolationLevel">One of the values of <see cref="IsolationLevel"/> that specifies the transaction isolation level.</param> /// <param name="options">One of the values of <see cref="UnitOfWorkScopeTransactionOptions"/> that specifies options for using existing /// transacitons or creating new ones.</param> /// <returns>A <see cref="UnitOfWorkScopeTransaction"/> instance.</returns> public static UnitOfWorkScopeTransaction GetTransactionForScope(UnitOfWorkScope scope, IsolationLevel isolationLevel, UnitOfWorkScopeTransactionOptions options) { var useCompatibleTx = (options & UnitOfWorkScopeTransactionOptions.UseCompatible) == UnitOfWorkScopeTransactionOptions.UseCompatible; var createNewTx = (options & UnitOfWorkScopeTransactionOptions.CreateNew) == UnitOfWorkScopeTransactionOptions.CreateNew; Guard.Against <InvalidOperationException>(useCompatibleTx && createNewTx, "Cannot start a transaction with both UseCompatible and CreateNew specified " + "as a UnitOfWorkScopeTransactionOptions"); if (options == UnitOfWorkScopeTransactionOptions.UseCompatible) { var transaction = (from t in CurrentTransactions where t.IsolationLevel == isolationLevel select t).FirstOrDefault(); if (transaction != null) { transaction.AttachScope(scope); return(transaction); } } var factory = ServiceLocator.Current.GetInstance <IUnitOfWorkFactory>(); var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel); newTransaction.AttachScope(scope); CurrentTransactions.AddFirst(newTransaction); return(newTransaction); }
/// <summary> /// Overloaded Constructor. /// Creates a new instance of <see cref="UnitOfWorkScope"/> with the specified transaction isolation level, option connection and /// a transaction option that specifies if an existing transaction should be used or to create a new transaction. /// </summary> /// <param name="isolationLevel"></param> /// <param name="transactionOptions"></param> public UnitOfWorkScope(IsolationLevel isolationLevel, UnitOfWorkScopeTransactionOptions transactionOptions) { _disposed = false; _autoComplete = (transactionOptions & UnitOfWorkScopeTransactionOptions.AutoComplete) == UnitOfWorkScopeTransactionOptions.AutoComplete; _currentTransaction = UnitOfWorkScopeTransaction.GetTransactionForScope(this, isolationLevel, transactionOptions); RegisterScope(this); }
/// <summary> /// Gets a <see cref="UnitOfWorkScopeTransaction"/> instance that can be used by a <see cref="UnitOfWorkScope"/> instance. /// </summary> /// <param name="scope">The <see cref="UnitOfWorkScope"/> instance that is requesting the transaction.</param> /// <param name="isolationLevel">One of the values of <see cref="IsolationLevel"/> that specifies the transaction isolation level.</param> /// <param name="options">One of the values of <see cref="UnitOfWorkScopeTransactionOptions"/> that specifies options for using existing /// transacitons or creating new ones.</param> /// <returns>A <see cref="UnitOfWorkScopeTransaction"/> instance.</returns> public static UnitOfWorkScopeTransaction GetTransactionForScope(UnitOfWorkScope scope, IsolationLevel isolationLevel, UnitOfWorkScopeTransactionOptions options) { var useCompatibleTx = (options & UnitOfWorkScopeTransactionOptions.UseCompatible) == UnitOfWorkScopeTransactionOptions.UseCompatible; var createNewTx = (options & UnitOfWorkScopeTransactionOptions.CreateNew) == UnitOfWorkScopeTransactionOptions.CreateNew; Guard.Against<InvalidOperationException>(useCompatibleTx && createNewTx, "Cannot start a transaction with both UseCompatible and CreateNew specified " + "as a UnitOfWorkScopeTransactionOptions"); if (options == UnitOfWorkScopeTransactionOptions.UseCompatible) { var transaction = (from t in CurrentTransactions where t.IsolationLevel == isolationLevel select t).FirstOrDefault(); if (transaction != null) { transaction.AttachScope(scope); return transaction; } } var factory = ServiceLocator.Current.GetInstance<IUnitOfWorkFactory>(); var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel); newTransaction.AttachScope(scope); CurrentTransactions.AddFirst(newTransaction); return newTransaction; }
/// <summary> /// Overloaded Constructor. /// Creates a new instance of the <see cref="UnitOfWorkScope"/> with the /// specified <see cref="UnitOfWorkScopeTransactionOptions"/> and default <see cref="IsolationLevel"/> /// </summary> /// <param name="options"></param> public UnitOfWorkScope(UnitOfWorkScopeTransactionOptions options) : this(GetScopeIsolationLevel(), options) { }