Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Makes the provided 'UnitOfWorkScope' available as the the ambient scope via the CallContext.
        /// </summary>
        internal static void SetAmbientScope(UnitOfWorkTransactionScope newAmbientScope)
        {
            if (newAmbientScope == null)
            {
                throw new ArgumentNullException("newAmbientScope");
            }

            var current = CallContext.LogicalGetData(AmbientUnitOfWorkScopeKey) as InstanceIdentifier;

            if (current == newAmbientScope._instanceIdentifier)
            {
                return;
            }

            // Store the new scope's instance identifier in the CallContext, making it the ambient scope
            CallContext.LogicalSetData(AmbientUnitOfWorkScopeKey, newAmbientScope._instanceIdentifier);

            // Keep track of this instance (or do nothing if we're already tracking it)
            UnitOfWorkScopeInstances.GetValue(newAmbientScope._instanceIdentifier, key => newAmbientScope);
        }
 public UnitOfWorkReadOnlyScope(UnitOfWorkScopeOption joiningOption, IsolationLevel?isolationLevel, IDbContextFactoryProducerSingleton dbContextFactory = null, IAmbientDbContextLocator contextLocator = null, IGenericRepositoryFactory repositoryFactory = null)
     : base(contextLocator : contextLocator, repositoryFactory : repositoryFactory)
 {
     _internalScope = new UnitOfWorkTransactionScope(joiningOption : joiningOption, readOnly : true, isolationLevel : isolationLevel, dbContextFactory : dbContextFactory, contextLocator : contextLocator, repositoryFactory : repositoryFactory);
 }