Beispiel #1
0
 public void BeginReserved(string reservationName, IUnitOfWorkOptions unitOfWorkOptions)
 {
     if (!TryBeginReserved(reservationName, unitOfWorkOptions))
     {
         throw new XpressException($"Could not find a reserved unit of work with reservation name: {reservationName}");
     }
 }
Beispiel #2
0
 public virtual void Initialize(IUnitOfWorkOptions unitOfWorkOptions)
 {
     Check.NotNull(unitOfWorkOptions, nameof(unitOfWorkOptions));
     if (UnitOfWorkOptions != null)
     {
         throw new XpressException("This unit of work is already initialized before!");
     }
     else
     {
         IsReserved        = false;
         UnitOfWorkOptions = unitOfWorkOptions.Clone();
         if (UnitOfWorkOptions.IsTransactional)
         {
             if (_dbContextProvider.DbContextTransaction == null)
             {
                 var isoLationLevel = UnitOfWorkOptions.IsolationLevel;
                 _dbContextProvider.DbContextTransaction = _dbContextProvider.GetDbContext().Database.BeginTransaction(isoLationLevel);
             }
             else
             {
                 var dbTransaction = _dbContextProvider.DbContextTransaction.GetDbTransaction();
                 _dbContextProvider.GetDbContext().Database.UseTransaction(dbTransaction);
             }
         }
     }
 }
Beispiel #3
0
        private void ProcceesAvailableTransactionsAsync(IInvocation invocation, IUnitOfWorkOptions unitOfWorkOptions)
        {
            this._unitOfWorkAsync.BeginTransaction(unitOfWorkOptions);
            try
            {
                invocation.Proceed();
            }
            catch
            {
                _unitOfWorkAsync.Dispose();
                throw;
            }

            if (invocation.Method.ReturnType == typeof(Task))
            {
                invocation.ReturnValue = PublicAsyncHelper.AwaitTaskWithPostActionAndFinally(
                    (Task)invocation.ReturnValue,
                    async() => await _unitOfWorkAsync.CommitAsync(),
                    exception => _unitOfWorkAsync.Dispose()
                    );
            }
            else //Task<TResult>
            {
                invocation.ReturnValue = PublicAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
                    invocation.Method.ReturnType.GenericTypeArguments[0],
                    invocation.ReturnValue,
                    async() => await _unitOfWorkAsync.CommitAsync(),
                    exception => _unitOfWorkAsync.Dispose()
                    );
            }
        }
Beispiel #4
0
 private void ProccessUnitOfWork(IInvocation invocation, IUnitOfWorkOptions unitOfWorkOptions)
 {
     if (AsyncHelper.IsAsyncMethod(invocation.Method))
     {
         ProcceesAvailableTransactionsAsync(invocation, unitOfWorkOptions);
     }
     else
     {
         ProcceesAvailableTransactions(invocation, unitOfWorkOptions);
     }
 }
Beispiel #5
0
 private void ProcceesAvailableTransactions(IInvocation invocation, IUnitOfWorkOptions unitOfWorkOptions)
 {
     this._unitOfWorkAsync.BeginTransaction(unitOfWorkOptions);
     try
     {
         invocation.Proceed();
     }
     catch
     {
         _unitOfWorkAsync.Dispose();
         throw;
     }
     this._unitOfWorkAsync.Commit();
 }
Beispiel #6
0
 public async Task BeginTransactionAsync(IUnitOfWorkOptions unitOfWorkOptions = default)
 {
     if (unitOfWorkOptions == default)
     {
         _transaction = await((DbContext)_dataContext).Database.BeginTransactionAsync();
     }
     else
     {
         if (unitOfWorkOptions.Timeout.HasValue)
         {
             ((DbContext)_dataContext).Database.SetCommandTimeout(unitOfWorkOptions.Timeout.Value);
         }
         _transaction = unitOfWorkOptions.IsolationLevel.HasValue ? await((DbContext)_dataContext).Database.BeginTransactionAsync(unitOfWorkOptions.IsolationLevel.Value)
                                                                  : await((DbContext)_dataContext).Database.BeginTransactionAsync();
     }
 }
Beispiel #7
0
        public virtual void BeginTransaction(IUnitOfWorkOptions unitOfWorkOptions)
        {
            _dbContextAddedEvent += context =>
            {
                if (unitOfWorkOptions.IsolationLevel == null)
                {
                    unitOfWorkOptions.IsolationLevel = System.Data.IsolationLevel.ReadCommitted;
                }

                UnitOfWorkOptions = unitOfWorkOptions;
                if (UnitOfWorkOptions.IsolationLevel != null)
                {
                    var tran = context.Database.BeginTransaction(UnitOfWorkOptions.IsolationLevel.Value);
                    Transactions.Add(context.Name, tran);
                }
            };
        }
Beispiel #8
0
        public IUnitOfWork Begin(IUnitOfWorkOptions unitOfWorkOptions, bool requiresNew = false)
        {
            Check.NotNull(unitOfWorkOptions, nameof(unitOfWorkOptions));

            var currentUow = Current;

            if (currentUow != null && !requiresNew)
            {
                return(new ChildUnitOfWork(currentUow));
            }
            else
            {
                var unitOfWork = CreateNewUnitOfWork();
                unitOfWork.Initialize(unitOfWorkOptions);
                return(unitOfWork);
            }
        }
Beispiel #9
0
        public void BeginTransaction(IUnitOfWorkOptions unitOfWorkOptions = default)
        {
            if (unitOfWorkOptions == default)
            {
                _transaction = ((DbContext)_dataContext).Database.BeginTransaction();
            }
            else
            {
                if (unitOfWorkOptions.Timeout.HasValue)
                {
                    ((DbContext)_dataContext).Database.SetCommandTimeout(unitOfWorkOptions.Timeout.Value);
                }

                _transaction = unitOfWorkOptions.IsolationLevel.HasValue ? ((DbContext)_dataContext).Database.BeginTransaction(unitOfWorkOptions.IsolationLevel.Value)
                                                                         : ((DbContext)_dataContext).Database.BeginTransaction();
            }
        }
Beispiel #10
0
        public void FillDefaultsForNonProvidedOptions(IUnitOfWorkOptions defaultOptions)
        {
            if (!Timeout.HasValue && defaultOptions.Timeout.HasValue)
            {
                Timeout = defaultOptions.Timeout.Value;
            }

            if (!IsolationLevel.HasValue && defaultOptions.IsolationLevel.HasValue)
            {
                IsolationLevel = defaultOptions.IsolationLevel.Value;
            }

            if (!IsLazyLoadEnabled.HasValue)
            {
                IsLazyLoadEnabled = defaultOptions.IsLazyLoadEnabled;
            }
        }
Beispiel #11
0
        public bool TryBeginReserved(string reservationName, IUnitOfWorkOptions unitOfWorkOptions)
        {
            Check.NotNull(reservationName, nameof(reservationName));

            //Find reserved unit of work starting from current and going to outers
            var ambientUnitOfWork = _unitOfWorkAccessor.UnitOfWork;

            while (ambientUnitOfWork != null && !ambientUnitOfWork.IsReservedFor(reservationName))
            {
                ambientUnitOfWork = ambientUnitOfWork.Outer;
            }

            if (ambientUnitOfWork == null)
            {
                return(false);
            }
            else
            {
                ambientUnitOfWork.Initialize(unitOfWorkOptions);
                return(true);
            }
        }
Beispiel #12
0
 public void BeginTransaction(IUnitOfWorkOptions unitOfWorkOptions)
 {
     throw new NotImplementedException();
 }
Beispiel #13
0
 public UnitOfWorkBase(IUnitOfWorkOptions unitOfWorkOptions)
 {
     _filterStatus = new ConcurrentDictionary <string, bool>(unitOfWorkOptions.GetAllFilters());
 }
Beispiel #14
0
 public void BeginTransaction(IUnitOfWorkOptions unitOfWorkOptions = default) =>
 BeginTransactionAsync(unitOfWorkOptions).ConfigureAwait(false)
 .GetAwaiter()
 .GetResult();
Beispiel #15
0
 public Task BeginTransactionAsync(IUnitOfWorkOptions unitOfWorkOptions)
 {
     throw new NotImplementedException();
 }
 public EnableUnitOfWorkAttribute()
 {
     UnitOfWorkOptions = new UnitOfWorkOptions();
 }
Beispiel #17
0
 public void Initialize(IUnitOfWorkOptions unitOfWorkOptions)
 {
     _parent.Initialize(unitOfWorkOptions);
 }
Beispiel #18
0
 public UowActionFilter(IUnitOfWorkManager unitOfWorkManager, DefaultUnitOfWorkOptions defaultUnitOfWorkOptions)
 {
     _unitOfWorkManager        = unitOfWorkManager;
     _defaultUnitOfWorkOptions = defaultUnitOfWorkOptions.Clone();
 }
Beispiel #19
0
 public UnitOfWorkInterceptor(DefaultUnitOfWorkOptions unitOfWorkOptions, IUnitOfWorkManager unitOfWorkManager)
 {
     _defaultUnitOfWorkOptions = unitOfWorkOptions.Clone();
     _unitOfWorkManager        = unitOfWorkManager;
 }