示例#1
0
 public Customer GetByKey(int customerID, IDbTransactionScope transactionScope = null)
 {
     using (var db = _dbFactory.CreateDomainEFContext(transactionScope))
     {
         return(db.Customer.AsNoTracking().SingleOrDefault(x => x.CustomerID == customerID));
     }
 }
示例#2
0
 public IEnumerable <Customer> GetAll(IDbTransactionScope transactionScope = null)
 {
     using (var db = _dbFactory.CreateDomainEFContext(transactionScope))
     {
         return(db.Customer.AsNoTracking().ToList());
     }
 }
示例#3
0
 public void DeleteAll(IDbTransactionScope transactionScope = null)
 {
     using (var db = _dbFactory.CreateDomainConnectionScope(transactionScope))
     {
         db.Connection.Execute("DELETE FROM dbo.Customer");
     }
 }
示例#4
0
 /// <summary>
 ///     Create an empty or nested <see cref="IDbTransactionScope"/> with provided <paramref name="isolationLevel"/>.
 /// </summary>
 /// <param name="transactionScope">If null a new <see cref="IDbTransactionScope"/> is returned; otherwise nested.</param>
 /// <param name="isolationLevel"></param>
 /// <exception cref="DbTransactionScopeException">
 ///     If the provided IsolationLevel is different than the provided <paramref name="transactionScope"/>'s IsolationLevel.
 /// </exception>
 public IDbTransactionScope CreateTransactionScope(IDbTransactionScope transactionScope, System.Transactions.IsolationLevel isolationLevel)
 {
     if (transactionScope == null)
     {
         return(new DbTransactionScope(isolationLevel));
     }
     else
     {
         return(transactionScope.BeginNested(isolationLevel));
     }
 }
示例#5
0
 /// <inheritdoc />
 public IDbTransactionScope BeginTransactionScope(System.Transactions.IsolationLevel isolationLevel, int timeoutInSeconds = 30)
 {
     if (CurrentTransactionScope == null || CurrentTransactionScope.Completed)
     {
         return(CurrentTransactionScope = new DbTransactionScope(this, isolationLevel, timeoutInSeconds));
     }
     else
     {
         return(CurrentTransactionScope.BeginNested(isolationLevel));
     }
 }
示例#6
0
 /// <summary>
 ///     Create an empty or nested <see cref="IDbTransactionScope"/> with <see cref="DbScopeConfig.DefaultTransactionIsolationLevel"/> when creating empty,
 ///     otherwise for nested the provided <paramref name="transactionScope"/>'s IsolationLevel is inherited.
 /// </summary>
 /// <param name="transactionScope">
 ///     If null a new <see cref="IDbTransactionScope"/> is returned; otherwise nested.
 /// </param>
 public IDbTransactionScope CreateTransactionScope(IDbTransactionScope transactionScope)
 {
     if (transactionScope == null)
     {
         return(new DbTransactionScope(DbScopeConfig.DefaultTransactionIsolationLevel));
     }
     else
     {
         return(transactionScope.BeginNested());
     }
 }
示例#7
0
        public IQueryable <Customer> Query(IDbTransactionScope transaction = null)
        {
            var db = transaction == null ? _efContext : _dbFactory.CreateDomainEFContext(transaction, disposeWithTransaction: true);

            // Alternative to one-liner:
            // var db = _efContext;
            // if (transaction != null)
            // {
            //    db = _dbFactory.CreateDomainEFContext(transaction, disposeWithTransaction: true);
            // }

            return(db.Customer.AsNoTracking());
        }
示例#8
0
 public EF.DomainEFContext CreateDomainEFContext(IDbTransactionScope transactionScope, bool disposeWithTransaction = false)
 {
     if (transactionScope == null)
     {
         return(CreateDomainEFContext());
     }
     else
     {
         var ctx = new EF.DomainEFContext(CreateDomainConnectionScope(transactionScope), contextOwnsConnectionScope: false);
         if (disposeWithTransaction)
         {
             transactionScope.AddDisposable(ctx);
         }
         return(ctx);
     }
 }
示例#9
0
        /// <summary>
        ///     Create new instance of <paramref name="dbConnectionScopeType"/> using a registered factory or a parameterless constructor.
        ///     If <paramref name="transaction"/> is provided then a nested <see cref="DbConnectionScope"/> is returned.
        /// </summary>
        /// <param name="dbConnectionScopeType"></param>
        /// <param name="transaction"></param>
        /// <exception cref="ArgumentNullException">
        ///     If <paramref name="dbConnectionScopeType"/> is null.
        /// </exception>
        /// <exception cref="MissingMethodException">
        ///     If no factory have been registered for <paramref name="dbConnectionScopeType"/> and it has no parameterless constructor.
        /// </exception>
        public DbConnectionScope CreateConnectionScope(Type dbConnectionScopeType, IDbTransactionScope transaction = null)
        {
            if (dbConnectionScopeType == null)
            {
                throw new ArgumentNullException(nameof(dbConnectionScopeType));
            }

            if (transaction == null)
            {
                return(DbScopeConfig.CreateConnectionScope(dbConnectionScopeType));
            }
            else
            {
                return(transaction.GetConnectionScope(dbConnectionScopeType).BeginNested());
            }
        }
示例#10
0
        public int Create(Customer customer, IDbTransactionScope transactionScope = null)
        {
            // The following should be in a transaction regardless if the consumer provides a DbTransactionScope.
            using (var tx = _dbFactory.CreateTransactionScope(transactionScope))
                using (var db = _dbFactory.CreateDomainEFContext(tx))
                {
                    db.Customer.Add(customer);
                    db.SaveChanges();

                    db.Database.ExecuteSqlCommand("INSERT INTO dbo.Log (Type, Value) VALUES (@p0, @p1)", "CustomerCreated", $"CustomerID: {customer.CustomerID}");
                    tx.Commit();
                }

            // Do something outside the provided transaction. (just as an example)
            using (var cs = _dbFactory.CreateDomainConnectionScope())
            {
                //cs.Connection.Execute("");
            }

            return(customer.CustomerID);
        }
示例#11
0
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing)
         {
             // Only dispose if current scope is "root".
             if (_isRootScope == true)
             {
                 if (CurrentTransactionScope != null)
                 {
                     CurrentTransactionScope.Dispose();
                     CurrentTransactionScope = null;
                 }
                 if (Connection != null)
                 {
                     CloseConnection();
                     Connection?.Dispose();
                 }
             }
         }
         _disposed = true;
     }
 }
 public void Add(IDbTransactionScope transactionScope)
 {
     transactionScopeList.Add(transactionScope);
 }
示例#13
0
 public DomainDbConnectionScope CreateDomainConnectionScope(IDbTransactionScope transactionScope = null)
 {
     return(CreateConnectionScope <DomainDbConnectionScope>(transactionScope));
 }
示例#14
0
 public IQueryable <Customer> Query(Expression <Func <Customer, bool> > wherePredicate, IDbTransactionScope transaction = null)
 {
     return(Query(transaction).Where(wherePredicate));
 }
示例#15
0
 public IQueryable <Customer> QueryByKey(int customerID, IDbTransactionScope transaction = null)
 {
     return(Query(transaction).Where(x => x.CustomerID == customerID));
 }
示例#16
0
 public bool Exists(int customerID, IDbTransactionScope transaction = null)
 {
     return(Query(transaction).Where(x => x.CustomerID == customerID).Select(x => true).FirstOrDefault());
 }
示例#17
0
 /// <summary>
 ///     Create new instance of <typeparamref name="TDbConnectionScope" /> using a registered factory or a parameterless constructor.
 ///     If <paramref name="transaction"/> is provided then a nested <typeparamref name="TDbConnectionScope" /> is returned.
 /// </summary>
 /// <typeparam name="TDbConnectionScope"></typeparam>
 /// <param name="transaction"></param>
 /// <exception cref="MissingMethodException">
 ///     If no factory have been registered for <typeparamref name="TDbConnectionScope"/> and it has no parameterless constructor
 /// </exception>
 public TDbConnectionScope CreateConnectionScope <TDbConnectionScope>(IDbTransactionScope transaction = null) where TDbConnectionScope : DbConnectionScope
 {
     return((TDbConnectionScope)CreateConnectionScope(typeof(TDbConnectionScope), transaction));
 }