public IUnitOfWorkCompleteHandle Begin(UnitOfWorkOptions options) { options.FillDefaultsForNonProvidedOptions(_defaultOptions); var outerUow = _currentUnitOfWorkProvider.Current; if (options.Scope == TransactionScopeOption.Required && outerUow != null) { return(new InnerUnitOfWorkCompleteHandle()); } var uow = _serviceProvider.GetService <IUnitOfWork>(); uow.Completed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; }; uow.Failed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; }; uow.Disposed += (sender, args) => { // _serviceProvider.Release(uow); }; //Inherit filters from outer UOW if (outerUow != null) { options.FillOuterUowFiltersForNonProvidedOptions(outerUow.Filters.ToList()); } uow.Begin(options); _currentUnitOfWorkProvider.Current = uow; return(uow); }
public static T PerformAsyncUow <T>(this IUnitOfWorkManager unitOfWorkManager, Func <T> action, UnitOfWorkOptions options = null) { var uow = unitOfWorkManager.Begin(options ?? DefaultUoWOptions); object returnValue; try { returnValue = action(); } catch { uow.Dispose(); throw; } if (typeof(T) == typeof(Task)) { returnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)returnValue, async() => await uow.CompleteAsync(), exception => uow.Dispose()); } else //Task<TResult> { returnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( typeof(T).GenericTypeArguments[0], returnValue, async() => await uow.CompleteAsync(), exception => uow.Dispose()); } return((T)returnValue); }
public static void PerformSyncUow(this IUnitOfWorkManager unitOfWorkManager, Action action, UnitOfWorkOptions options = null) { using (var uow = unitOfWorkManager.Begin(options ?? DefaultUoWOptions)) { action(); uow.Complete(); } }