Exemple #1
0
        public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null)
        {
            if (isolationLevel.HasValue && joiningOption == DbContextScopeOption.JoinExisting)
                throw new ArgumentException("Cannot join an ambient DbContextScope when an explicit database transaction is required. When requiring explicit database transactions to be used (i.e. when the 'isolationLevel' parameter is set), you must not also ask to join the ambient context (i.e. the 'joinAmbient' parameter must be set to false).");

            _disposed = false;
            _completed = false;
            _readOnly = readOnly;

            _parentScope = GetAmbientScope();
            if (_parentScope != null && joiningOption == DbContextScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !this._readOnly)
                {
                    throw new InvalidOperationException("Cannot nest a read/write DbContextScope within a read-only DbContextScope.");
                }

                _nested = true;
                _dbContexts = _parentScope._dbContexts;
            }
            else
            {
                _nested = false;
                _dbContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory);
            }

            SetAmbientScope(this);
        }
Exemple #2
0
        public void Test01_Attempt_To_Get_DbContext_That_Was_Never_Registered_Before()
        {
            var dbContextCollection = new DbContextCollection(dbContextFactory: FakeObjectFactory.CreateDbContextFactory());
            var dbContext           = dbContextCollection.Get <FakeDbContext>();

            dbContext.Should().NotBeNull();
        }
Exemple #3
0
        public UnitOfWorkTransactionScope(UnitOfWorkScopeOption joiningOption, bool readOnly, IsolationLevel?isolationLevel, IDbContextFactoryProducerSingleton dbContextFactory = null, IAmbientDbContextLocator contextLocator = null, IGenericRepositoryFactory repositoryFactory = null, IDomainEvents domainEvents = null, CancellationToken cancellationToken = default(CancellationToken))
            : base(contextLocator : contextLocator, repositoryFactory : repositoryFactory, cancellationToken : cancellationToken)
        {
            if (isolationLevel.HasValue && joiningOption == UnitOfWorkScopeOption.JoinExisting)
            {
                throw new ArgumentException("Cannot join an ambient UnitOfWorkScope when an explicit database transaction is required. When requiring explicit database transactions to be used (i.e. when the 'isolationLevel' parameter is set), you must not also ask to join the ambient context (i.e. the 'joinAmbient' parameter must be set to false).");
            }

            _disposed  = false;
            _completed = false;
            _readOnly  = readOnly;

            _parentScope = GetAmbientScope();
            if (_parentScope != null && joiningOption == UnitOfWorkScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !this._readOnly)
                {
                    throw new InvalidOperationException("Cannot nest a read/write UnitOfWorkScope within a read-only UnitOfWorkScope.");
                }

                _nested     = true;
                _dbContexts = _parentScope._dbContexts;
            }
            else
            {
                _nested     = false;
                _dbContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory, domainEvents);
            }

            SetAmbientScope(this);
        }
        public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null)
        {
            if (isolationLevel.HasValue && joiningOption == DbContextScopeOption.JoinExisting)
            {
                throw new ArgumentException("Cannot join an ambient DbContextScope when an explicit database transaction is required. When requiring explicit database transactions to be used (i.e. when the 'isolationLevel' parameter is set), you must not also ask to join the ambient context (i.e. the 'joinAmbient' parameter must be set to false).");
            }

            _disposed  = false;
            _completed = false;
            _readOnly  = readOnly;

            _parentScope = GetAmbientScope();
            if (_parentScope != null && joiningOption == DbContextScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !this._readOnly)
                {
                    throw new InvalidOperationException("Cannot nest a read/write DbContextScope within a read-only DbContextScope.");
                }

                _nested     = true;
                _dbContexts = _parentScope._dbContexts;
            }
            else
            {
                _nested     = false;
                _dbContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory);
            }

            SetAmbientScope(this);
        }
Exemple #5
0
        public void Test03_Attempt_To_Get_DbContext_That_Was_Registered_Before_Without_Supplying_Factory()
        {
            var dbContextCollection    = new DbContextCollection();
            var dbContext              = dbContextCollection.Get <FakeDbContext>();
            var dbContextSecondAttempt = dbContextCollection.Get <FakeDbContext>();

            dbContextSecondAttempt.InstanceId.Should().Be(dbContext.InstanceId);
        }
Exemple #6
0
        public void Test02_Attempt_To_Get_DbContext_That_Was_Registered_Before()
        {
            var dbContextCollection    = new DbContextCollection(dbContextFactory: FakeObjectFactory.CreateDbContextFactory());
            var dbContext              = dbContextCollection.Get <FakeDbContext>();
            var dbContextSecondAttempt = dbContextCollection.Get <FakeDbContext>();

            dbContextSecondAttempt.InstanceId.Should().Be(dbContext.InstanceId);
        }
Exemple #7
0
        public void Test05_Attempt_To_Commit_Changes_On_One_DbContext()
        {
            var dbContextCollection = new DbContextCollection();
            var dbContext           = dbContextCollection.Get <FakeDbContext>();
            var commitedChanges     = dbContextCollection.Commit();

            dbContext.Should().NotBeNull();
            commitedChanges.Should().Be(2);
        }
Exemple #8
0
        public void Test06_Attempt_To_Commit_Changes_For_ReadOnly_DbContext()
        {
            var dbContextCollection = new DbContextCollection(true);
            var dbContext           = dbContextCollection.Get <FakeDbContext>();
            var commitedChanges     = dbContextCollection.Commit();

            dbContext.Should().NotBeNull();
            commitedChanges.Should().Be(0);
        }
Exemple #9
0
        public void Test04_Attempt_To_Commit_Changes_On_Two_DbContexts()
        {
            var dbContextCollection = new DbContextCollection();
            var dbContext1          = dbContextCollection.Get <FakeDbContext>();
            var dbContext2          = dbContextCollection.Get <AnotherFakeDbContext>();
            var commitedChanges     = dbContextCollection.Commit();

            dbContext1.Should().NotBeNull();
            dbContext2.Should().NotBeNull();
            dbContext1.InstanceId.Should().NotBe(dbContext2.InstanceId);
            commitedChanges.Should().Be(3);
        }
Exemple #10
0
        public void Should_Instantiate_One_DbContext_Using_Several_Interfaces()
        {
            var factory    = new DbContextFactory();
            var collection = new DbContextCollection(false, null, factory);

            var dbContext1 = collection.Get <TestDbContext>();
            var dbContext2 = collection.Get <ITestDbContext1>();
            var dbContext3 = collection.Get <ITestDbContext2>();

            Assert.Same(dbContext1, dbContext2);
            Assert.Same(dbContext1, dbContext3);
            Assert.True(collection.InitializedDbContexts.GetDbContexts().ToArray().Length == 1);
        }
Exemple #11
0
        /// <summary>
        /// Responsible for creating a new dbContext with given <see cref="DbContextOption.Mode"/>
        /// </summary>
        /// <typeparam name="T">dbContext of type {T} to be created.</typeparam>
        /// <param name="mode"><see cref="DbContextOption.Mode"/> to create dbContext with.</param>
        /// <param name="isolationLevel"><see cref="IsolationLevel"/> to create dbContext with.</param>
        /// <param name="dbTransaction">create a dbContext with external transaction</param>
        /// <param name="sqlConnection">create a dbContext with existing sql connection</param>
        /// <returns>true if new dbContext created else false.</returns>
        internal bool CreateNewDbContextIfNotExists <T>(DbContextOption.Mode mode, IsolationLevel?isolationLevel, DbTransaction dbTransaction, DbConnection sqlConnection) where T : DbContext, IAmbientDbContext, new()
        {
            if (DbContextCollection.GetDbContextByType <T>() != null)
            {
                return(false);
            }
            //Creating a new DbContext Type
            T currentDbContext;

            if (dbTransaction != null && sqlConnection != null)
            {
                currentDbContext = Activator.CreateInstance(typeof(T), sqlConnection, false) as T;
            }
            else
            {
                currentDbContext = Activator.CreateInstance(typeof(T)) as T;
            }

            if (currentDbContext != null)
            {
                currentDbContext.Mode = mode;

                if (mode == DbContextOption.Mode.Read)
                {
                    currentDbContext.Configuration.AutoDetectChangesEnabled = false;
                }
                if (dbTransaction != null)
                {
                    currentDbContext.Database.UseTransaction(dbTransaction);
                    DbContextCollection.Add(currentDbContext, null);
                }
                else
                {
                    if (isolationLevel.HasValue)
                    {
                        IsolationLevel = isolationLevel.Value;
                        var transaction = currentDbContext.Database.BeginTransaction(isolationLevel.Value);
                        DbContextCollection.Add(currentDbContext, transaction);
                    }
                    else
                    {
                        var transaction = currentDbContext.Database.BeginTransaction();
                        DbContextCollection.Add(currentDbContext, transaction);
                    }
                }
            }
            return(true);
        }
Exemple #12
0
 /// <summary>
 /// Commit all dbContext transactions.
 /// </summary>
 internal void SaveAndCommit()
 {
     DbContextCollection.SaveAndCommitChanges();
 }
Exemple #13
0
 /// <summary>
 /// Save all dirty object in the dbcontext but the transaction is not committed yet.
 /// </summary>
 internal Task SaveAsync(CancellationToken cancellationToken)
 {
     return(DbContextCollection.SaveChangesAsync(cancellationToken));
 }
Exemple #14
0
 internal T GetDbContextByType <T>() where T : DbContext, IAmbientDbContext, new()
 {
     return(DbContextCollection.GetDbContextByType <T>());
 }
Exemple #15
0
 /// <summary>
 /// Dispose all dbContexts.
 /// </summary>
 public void Dispose()
 {
     DbContextCollection.Dispose();
 }
Exemple #16
0
 /// <summary>
 /// Rollback all dbContext transactions.
 /// </summary>
 public void Rollback()
 {
     DbContextCollection.Rollback();
 }
Exemple #17
0
 /// <summary>
 /// Commit all dbContext transactions.
 /// </summary>
 /// <param name="cancellationToken"><see cref="CancellationToken"/></param>
 internal async Task SaveAndCommitAsync(CancellationToken cancellationToken)
 {
     await DbContextCollection.SaveAndCommitChangesAsync(cancellationToken);
 }
Exemple #18
0
 /// <summary>
 /// Save all dirty object in the dbcontext but the transaction is not committed yet.
 /// </summary>
 internal void Save()
 {
     DbContextCollection.SaveChanges();
 }
Exemple #19
0
 /// <summary>
 /// Commit all transaction.
 /// </summary>
 internal void Commit()
 {
     DbContextCollection.Commit();
 }