Example #1
0
        /// <summary>
        /// Creates a new transaction scope associated with the specified connection,
        /// creating the inner scope using the specified factory method.
        /// The scope can be nested inside another scope in which case the underlying
        /// db transaction is only committed once both the outer and inner transaction(s)
        /// have been committed. The returned ITransactionScope implements IDisposable
        /// and should be wrapped in a using statement.
        /// </summary>
        /// <param name="dbConnection">
        /// <para>
        /// The DbConnection instance to manage transactions for. Transaction scopes
        /// created by this instance only apply to a single DbConnection, so if you want
        /// the scope to span additional data access mechanism then they must share the
        /// same connection.
        /// </para>
        /// <para>
        /// You can use the ICofoundryDbConnectionManager to get a reference to the shared
        /// connection directly.
        /// </para>
        /// </param>
        /// <returns>ITransactionScope, which is IDisposable and must be disposed.</returns>
        public ITransactionScope Create(DbConnection dbConnection, Func <System.Transactions.TransactionScope> transactionScopeFactory)
        {
            if (dbConnection == null)
            {
                throw new ArgumentNullException(nameof(dbConnection));
            }
            if (transactionScopeFactory == null)
            {
                throw new ArgumentNullException(nameof(transactionScopeFactory));
            }

            ITransactionScope scope;
            var connectionHash = dbConnection.GetHashCode();
            var primaryScope   = _primaryTransactionScopes.GetOrDefault(connectionHash);

            if (primaryScope == null)
            {
                primaryScope = new PrimaryTransactionScope(this, transactionScopeFactory);
                _primaryTransactionScopes.Add(connectionHash, primaryScope);
                scope = primaryScope;
            }
            else
            {
                scope = new ChildTransactionScope(primaryScope, transactionScopeFactory);
            }

            return(scope);
        }
Example #2
0
        /// <summary>
        /// Creates a new transaction scope associated with the specified connection.
        /// The scope can be nested inside another scope in which case the underlying
        /// db transaction is only committed once both the outer and inner transaction(s)
        /// have been committed. The returned ITransactionScope implements IDisposable
        /// and should be wrapped in a using statement.
        /// </summary>
        /// <param name="dbConnection">
        /// <para>
        /// The DbConnection instance to manage transactions for. Transaction scopes
        /// created by this instance only apply to a single DbConnection, so if you want
        /// the scope to span additional data access mechanism then they must share the
        /// same connection.
        /// </para>
        /// <para>
        /// You can use the ICofoundryDbConnectionManager to get a reference to the shared
        /// connection directly.
        /// </para>
        /// </param>
        /// <returns>ITransactionScope, which is IDisposable and must be disposed.</returns>
        public ITransactionScope Create(DbConnection dbConnection)
        {
            ITransactionScope scope;
            var connectionHash = dbConnection.GetHashCode();
            var primaryScope   = _primaryTransactionScopes.GetOrDefault(connectionHash);

            if (primaryScope == null)
            {
                primaryScope = new PrimaryTransactionScope(this);
                _primaryTransactionScopes.Add(connectionHash, primaryScope);
                scope = primaryScope;
            }
            else
            {
                scope = new ChildTransactionScope(primaryScope);
            }

            return(scope);
        }