| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System.Linq;
- using System.Transactions;
- using Abp.Dependency;
- namespace Abp.Domain.Uow
- {
- /// <summary>
- /// Unit of work manager.
- /// </summary>
- 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<IUnitOfWork>();
- 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;
- }
- }
- }
|