public Customer GetByKey(int customerID, IDbTransactionScope transactionScope = null) { using (var db = _dbFactory.CreateDomainEFContext(transactionScope)) { return(db.Customer.AsNoTracking().SingleOrDefault(x => x.CustomerID == customerID)); } }
public IEnumerable <Customer> GetAll(IDbTransactionScope transactionScope = null) { using (var db = _dbFactory.CreateDomainEFContext(transactionScope)) { return(db.Customer.AsNoTracking().ToList()); } }
public void DeleteAll(IDbTransactionScope transactionScope = null) { using (var db = _dbFactory.CreateDomainConnectionScope(transactionScope)) { db.Connection.Execute("DELETE FROM dbo.Customer"); } }
/// <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)); } }
/// <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)); } }
/// <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()); } }
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()); }
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); } }
/// <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()); } }
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); }
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); }
public DomainDbConnectionScope CreateDomainConnectionScope(IDbTransactionScope transactionScope = null) { return(CreateConnectionScope <DomainDbConnectionScope>(transactionScope)); }
public IQueryable <Customer> Query(Expression <Func <Customer, bool> > wherePredicate, IDbTransactionScope transaction = null) { return(Query(transaction).Where(wherePredicate)); }
public IQueryable <Customer> QueryByKey(int customerID, IDbTransactionScope transaction = null) { return(Query(transaction).Where(x => x.CustomerID == customerID)); }
public bool Exists(int customerID, IDbTransactionScope transaction = null) { return(Query(transaction).Where(x => x.CustomerID == customerID).Select(x => true).FirstOrDefault()); }
/// <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)); }