private void PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
 {
     using (var uow = _unitOfWorkManager.Begin(options))
     {
         invocation.Proceed();
         uow.Complete();
     }
 }
 private void PerformUow(IInvocation invocation, UnitOfWorkOptions options)
 {
     if (AsyncHelper.IsAsyncMethod(invocation.Method))
     {
         PerformAsyncUow(invocation, options);
     }
     else
     {
         PerformSyncUow(invocation, options);
     }
 }
        /// <inheritdoc />
        public void Begin(UnitOfWorkOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            PreventMultipleBegin();
            Options = options; //TODO: Do not set options like that, instead make a copy?

            BeginUow();
        }
        public IUnitOfWorkCompleteHandle Begin(UnitOfWorkOptions options)
        {
            options.FillDefaultsForNonProvidedOptions(_defaultOptions);

            if (options.Scope == TransactionScopeOption.Required && _currentUnitOfWorkProvider.Current != null)
            {
                return(new InnerUnitOfWorkCompleteHandle());
            }

            var uow = _iocResolver.Resolve <IUnitOfWork>();

            uow.Completed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; };

            uow.Failed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; };

            uow.Disposed += (sender, args) => { _iocResolver.Release(uow); };

            uow.Begin(options);

            _currentUnitOfWorkProvider.Current = uow;

            return(uow);
        }
        public IUnitOfWorkCompleteHandle Begin(UnitOfWorkOptions options)
        {
            options.FillDefaultsForNonProvidedOptions(_defaultOptions);

            if (options.Scope == TransactionScopeOption.Required && _currentUnitOfWorkProvider.Current != null)
            {
                return new InnerUnitOfWorkCompleteHandle();
            }

            var uow = _iocResolver.Resolve<IUnitOfWork>();

            uow.Completed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; };

            uow.Failed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; };

            uow.Disposed += (sender, args) => { _iocResolver.Release(uow); };

            uow.Begin(options);

            _currentUnitOfWorkProvider.Current = uow;

            return uow;
        }
        private void PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options)
        {
            var uow = _unitOfWorkManager.Begin(options);

            invocation.Proceed();

            if (invocation.Method.ReturnType == typeof (Task))
            {
                invocation.ReturnValue = ExtendedAsyncHelper.AwaitTaskWithPostActionAndFinally(
                    (Task) invocation.ReturnValue,
                    async () => await uow.CompleteAsync(),
                    exception => uow.Dispose()
                    );
            }
            else //Task<TResult>
            {
                invocation.ReturnValue = ExtendedAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
                    invocation.Method.ReturnType.GenericTypeArguments[0],
                    invocation.ReturnValue,
                    async () => await uow.CompleteAsync(),
                    exception => uow.Dispose()
                    );
            }
        }
        private void PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options)
        {
            var uow = _unitOfWorkManager.Begin(options);

            invocation.Proceed();

            if (invocation.Method.ReturnType == typeof(Task))
            {
                invocation.ReturnValue = ExtendedAsyncHelper.AwaitTaskWithPostActionAndFinally(
                    (Task)invocation.ReturnValue,
                    async() => await uow.CompleteAsync(),
                    exception => uow.Dispose()
                    );
            }
            else //Task<TResult>
            {
                invocation.ReturnValue = ExtendedAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
                    invocation.Method.ReturnType.GenericTypeArguments[0],
                    invocation.ReturnValue,
                    async() => await uow.CompleteAsync(),
                    exception => uow.Dispose()
                    );
            }
        }