using System.Linq; using System.Transactions; using Abp.Dependency; namespace Abp.Domain.Uow { /// /// Unit of work manager. /// internal class UnitOfWorkManager : IUnitOfWorkManager, ITransientDependency { private readonly IIocResolver _iocResolver; private readonly ICurrentUnitOfWorkProvider _currentUnitOfWorkProvider; private readonly IUnitOfWorkDefaultOptions _defaultOptions; public IActiveUnitOfWork Current { get { return _currentUnitOfWorkProvider.Current; } } public UnitOfWorkManager( IIocResolver iocResolver, ICurrentUnitOfWorkProvider currentUnitOfWorkProvider, IUnitOfWorkDefaultOptions defaultOptions) { _iocResolver = iocResolver; _currentUnitOfWorkProvider = currentUnitOfWorkProvider; _defaultOptions = defaultOptions; } public IUnitOfWorkCompleteHandle Begin() { return Begin(new UnitOfWorkOptions()); } public IUnitOfWorkCompleteHandle Begin(TransactionScopeOption scope) { return Begin(new UnitOfWorkOptions { Scope = scope }); } public IUnitOfWorkCompleteHandle Begin(UnitOfWorkOptions options) { options.FillDefaultsForNonProvidedOptions(_defaultOptions); var outerUow = _currentUnitOfWorkProvider.Current; if (options.Scope == TransactionScopeOption.Required && outerUow != null) { return new InnerUnitOfWorkCompleteHandle(); } var uow = _iocResolver.Resolve(); uow.Completed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; }; uow.Failed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; }; uow.Disposed += (sender, args) => { _iocResolver.Release(uow); }; //Inherit filters from outer UOW if (outerUow != null) { options.FillOuterUowFiltersForNonProvidedOptions(outerUow.Filters.ToList()); } uow.Begin(options); //Inherit tenant from outer UOW if (outerUow != null) { uow.SetTenantId(outerUow.GetTenantId(), false); } _currentUnitOfWorkProvider.Current = uow; return uow; } } }