| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Immutable;
- using System.Linq;
- using System.Threading.Tasks;
- using Abp.Extensions;
- using Abp.Runtime.Session;
- using Abp.Utils.Etc;
- using Castle.Core;
- namespace Abp.Domain.Uow
- {
- /// <summary>
- /// Base for all Unit Of Work classes.
- /// </summary>
- public abstract class UnitOfWorkBase : IUnitOfWork
- {
- public string Id { get; }
- [DoNotWire]
- public IUnitOfWork Outer { get; set; }
- /// <inheritdoc/>
- public event EventHandler Completed;
- /// <inheritdoc/>
- public event EventHandler<UnitOfWorkFailedEventArgs> Failed;
- /// <inheritdoc/>
- public event EventHandler Disposed;
- /// <inheritdoc/>
- public UnitOfWorkOptions Options { get; private set; }
- /// <inheritdoc/>
- public IReadOnlyList<DataFilterConfiguration> Filters
- {
- get { return _filters.ToImmutableList(); }
- }
- private readonly List<DataFilterConfiguration> _filters;
- public Dictionary<string, object> Items { get; set; }
- /// <summary>
- /// Gets default UOW options.
- /// </summary>
- protected IUnitOfWorkDefaultOptions DefaultOptions { get; }
- /// <summary>
- /// Gets the connection string resolver.
- /// </summary>
- protected IConnectionStringResolver ConnectionStringResolver { get; }
- /// <summary>
- /// Gets a value indicates that this unit of work is disposed or not.
- /// </summary>
- public bool IsDisposed { get; private set; }
- /// <summary>
- /// Reference to current ABP session.
- /// </summary>
- public IAbpSession AbpSession { protected get; set; }
- protected IUnitOfWorkFilterExecuter FilterExecuter { get; }
- /// <summary>
- /// Is <see cref="Begin"/> method called before?
- /// </summary>
- private bool _isBeginCalledBefore;
- /// <summary>
- /// Is <see cref="Complete"/> method called before?
- /// </summary>
- private bool _isCompleteCalledBefore;
- /// <summary>
- /// Is this unit of work successfully completed.
- /// </summary>
- private bool _succeed;
- /// <summary>
- /// A reference to the exception if this unit of work failed.
- /// </summary>
- private Exception _exception;
- private int? _tenantId;
- /// <summary>
- /// Constructor.
- /// </summary>
- protected UnitOfWorkBase(
- IConnectionStringResolver connectionStringResolver,
- IUnitOfWorkDefaultOptions defaultOptions,
- IUnitOfWorkFilterExecuter filterExecuter)
- {
- FilterExecuter = filterExecuter;
- DefaultOptions = defaultOptions;
- ConnectionStringResolver = connectionStringResolver;
- Id = Guid.NewGuid().ToString("N");
- _filters = defaultOptions.Filters.ToList();
- AbpSession = NullAbpSession.Instance;
- Items = new Dictionary<string, object>();
- }
- /// <inheritdoc/>
- public void Begin(UnitOfWorkOptions options)
- {
- Check.NotNull(options, nameof(options));
- PreventMultipleBegin();
- Options = options; //TODO: Do not set options like that, instead make a copy?
- SetFilters(options.FilterOverrides);
- SetTenantId(AbpSession.TenantId, false);
- BeginUow();
- }
- /// <inheritdoc/>
- public abstract void SaveChanges();
- /// <inheritdoc/>
- public abstract Task SaveChangesAsync();
- /// <inheritdoc/>
- public IDisposable DisableFilter(params string[] filterNames)
- {
- //TODO: Check if filters exists?
- var disabledFilters = new List<string>();
- foreach (var filterName in filterNames)
- {
- var filterIndex = GetFilterIndex(filterName);
- if (_filters[filterIndex].IsEnabled)
- {
- disabledFilters.Add(filterName);
- _filters[filterIndex] = new DataFilterConfiguration(_filters[filterIndex], false);
- }
- }
- disabledFilters.ForEach(ApplyDisableFilter);
- return new DisposeAction(() => EnableFilter(disabledFilters.ToArray()));
- }
- /// <inheritdoc/>
- public IDisposable EnableFilter(params string[] filterNames)
- {
- //TODO: Check if filters exists?
- var enabledFilters = new List<string>();
- foreach (var filterName in filterNames)
- {
- var filterIndex = GetFilterIndex(filterName);
- if (!_filters[filterIndex].IsEnabled)
- {
- enabledFilters.Add(filterName);
- _filters[filterIndex] = new DataFilterConfiguration(_filters[filterIndex], true);
- }
- }
- enabledFilters.ForEach(ApplyEnableFilter);
- return new DisposeAction(() => DisableFilter(enabledFilters.ToArray()));
- }
- /// <inheritdoc/>
- public bool IsFilterEnabled(string filterName)
- {
- return GetFilter(filterName).IsEnabled;
- }
- /// <inheritdoc/>
- public IDisposable SetFilterParameter(string filterName, string parameterName, object value)
- {
- var filterIndex = GetFilterIndex(filterName);
- var newfilter = new DataFilterConfiguration(_filters[filterIndex]);
- //Store old value
- object oldValue = null;
- var hasOldValue = newfilter.FilterParameters.ContainsKey(parameterName);
- if (hasOldValue)
- {
- oldValue = newfilter.FilterParameters[parameterName];
- }
- newfilter.FilterParameters[parameterName] = value;
- _filters[filterIndex] = newfilter;
- ApplyFilterParameterValue(filterName, parameterName, value);
- return new DisposeAction(() =>
- {
- //Restore old value
- if (hasOldValue)
- {
- SetFilterParameter(filterName, parameterName, oldValue);
- }
- });
- }
- public virtual IDisposable SetTenantId(int? tenantId)
- {
- return SetTenantId(tenantId, true);
- }
- public virtual IDisposable SetTenantId(int? tenantId, bool switchMustHaveTenantEnableDisable)
- {
- var oldTenantId = _tenantId;
- _tenantId = tenantId;
- IDisposable mustHaveTenantEnableChange;
- if (switchMustHaveTenantEnableDisable)
- {
- mustHaveTenantEnableChange = tenantId == null
- ? DisableFilter(AbpDataFilters.MustHaveTenant)
- : EnableFilter(AbpDataFilters.MustHaveTenant);
- }
- else
- {
- mustHaveTenantEnableChange = NullDisposable.Instance;
- }
- var mayHaveTenantChange = SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenantId);
- var mustHaveTenantChange = SetFilterParameter(AbpDataFilters.MustHaveTenant, AbpDataFilters.Parameters.TenantId, tenantId ?? 0);
- return new DisposeAction(() =>
- {
- mayHaveTenantChange.Dispose();
- mustHaveTenantChange.Dispose();
- mustHaveTenantEnableChange.Dispose();
- _tenantId = oldTenantId;
- });
- }
- public int? GetTenantId()
- {
- return _tenantId;
- }
- /// <inheritdoc/>
- public void Complete()
- {
- PreventMultipleComplete();
- try
- {
- CompleteUow();
- _succeed = true;
- OnCompleted();
- }
- catch (Exception ex)
- {
- _exception = ex;
- throw;
- }
- }
- /// <inheritdoc/>
- public async Task CompleteAsync()
- {
- PreventMultipleComplete();
- try
- {
- await CompleteUowAsync();
- _succeed = true;
- OnCompleted();
- }
- catch (Exception ex)
- {
- _exception = ex;
- throw;
- }
- }
- /// <inheritdoc/>
- public void Dispose()
- {
- if (!_isBeginCalledBefore || IsDisposed)
- {
- return;
- }
- IsDisposed = true;
- if (!_succeed)
- {
- OnFailed(_exception);
- }
- DisposeUow();
- OnDisposed();
- }
- /// <summary>
- /// Can be implemented by derived classes to start UOW.
- /// </summary>
- protected virtual void BeginUow()
- {
-
- }
- /// <summary>
- /// Should be implemented by derived classes to complete UOW.
- /// </summary>
- protected abstract void CompleteUow();
- /// <summary>
- /// Should be implemented by derived classes to complete UOW.
- /// </summary>
- protected abstract Task CompleteUowAsync();
- /// <summary>
- /// Should be implemented by derived classes to dispose UOW.
- /// </summary>
- protected abstract void DisposeUow();
- protected virtual void ApplyDisableFilter(string filterName)
- {
- FilterExecuter.ApplyDisableFilter(this, filterName);
- }
- protected virtual void ApplyEnableFilter(string filterName)
- {
- FilterExecuter.ApplyEnableFilter(this, filterName);
- }
- protected virtual void ApplyFilterParameterValue(string filterName, string parameterName, object value)
- {
- FilterExecuter.ApplyFilterParameterValue(this, filterName, parameterName, value);
- }
- protected virtual string ResolveConnectionString(ConnectionStringResolveArgs args)
- {
- return ConnectionStringResolver.GetNameOrConnectionString(args);
- }
- /// <summary>
- /// Called to trigger <see cref="Completed"/> event.
- /// </summary>
- protected virtual void OnCompleted()
- {
- Completed.InvokeSafely(this);
- }
- /// <summary>
- /// Called to trigger <see cref="Failed"/> event.
- /// </summary>
- /// <param name="exception">Exception that cause failure</param>
- protected virtual void OnFailed(Exception exception)
- {
- Failed.InvokeSafely(this, new UnitOfWorkFailedEventArgs(exception));
- }
- /// <summary>
- /// Called to trigger <see cref="Disposed"/> event.
- /// </summary>
- protected virtual void OnDisposed()
- {
- Disposed.InvokeSafely(this);
- }
- private void PreventMultipleBegin()
- {
- if (_isBeginCalledBefore)
- {
- throw new AbpException("This unit of work has started before. Can not call Start method more than once.");
- }
- _isBeginCalledBefore = true;
- }
- private void PreventMultipleComplete()
- {
- if (_isCompleteCalledBefore)
- {
- throw new AbpException("Complete is called before!");
- }
- _isCompleteCalledBefore = true;
- }
- private void SetFilters(List<DataFilterConfiguration> filterOverrides)
- {
- for (var i = 0; i < _filters.Count; i++)
- {
- var filterOverride = filterOverrides.FirstOrDefault(f => f.FilterName == _filters[i].FilterName);
- if (filterOverride != null)
- {
- _filters[i] = filterOverride;
- }
- }
- if (AbpSession.TenantId == null)
- {
- ChangeFilterIsEnabledIfNotOverrided(filterOverrides, AbpDataFilters.MustHaveTenant, false);
- }
- }
- private void ChangeFilterIsEnabledIfNotOverrided(List<DataFilterConfiguration> filterOverrides, string filterName, bool isEnabled)
- {
- if (filterOverrides.Any(f => f.FilterName == filterName))
- {
- return;
- }
- var index = _filters.FindIndex(f => f.FilterName == filterName);
- if (index < 0)
- {
- return;
- }
- if (_filters[index].IsEnabled == isEnabled)
- {
- return;
- }
- _filters[index] = new DataFilterConfiguration(filterName, isEnabled);
- }
- private DataFilterConfiguration GetFilter(string filterName)
- {
- var filter = _filters.FirstOrDefault(f => f.FilterName == filterName);
- if (filter == null)
- {
- throw new AbpException("Unknown filter name: " + filterName + ". Be sure this filter is registered before.");
- }
- return filter;
- }
- private int GetFilterIndex(string filterName)
- {
- var filterIndex = _filters.FindIndex(f => f.FilterName == filterName);
- if (filterIndex < 0)
- {
- throw new AbpException("Unknown filter name: " + filterName + ". Be sure this filter is registered before.");
- }
- return filterIndex;
- }
- public override string ToString()
- {
- return $"[UnitOfWork {Id}]";
- }
- }
- }
|