Browse Source

Use `DisposeAction` with parameter.

pull/14453/head
maliming 4 years ago
parent
commit
d9b9137598
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 5
      framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs
  2. 11
      framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs
  3. 2
      framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs
  4. 5
      framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs
  5. 4
      framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs
  6. 1
      framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs
  7. 8
      framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs
  8. 13
      framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs
  9. 5
      framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.cs

5
framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs

@ -60,10 +60,11 @@ public static class AbpCrossCuttingConcerns
public static IDisposable Applying(object obj, params string[] concerns)
{
AddApplied(obj, concerns);
return new DisposeAction(() =>
return new DisposeAction<ValueTuple<object, string[]>>(static (state) =>
{
var (obj, concerns) = state;
RemoveApplied(obj, concerns);
});
}, (obj, concerns));
}
public static string[] GetApplieds(object obj)

11
framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs

@ -31,19 +31,20 @@ public class DisposeAction : IDisposable
/// <summary>
/// This class can be used to provide an action when
/// Dispose method is called.
/// <typeparam name="T">The type of the parameter of the action.</typeparam>
/// <typeparam name="T">The type of the parameter of the action.</typeparam>
/// </summary>
public class DisposeAction<T> : IDisposable
{
private readonly Action<T> _action;
[CanBeNull] private readonly T _parameter;
[CanBeNull]
private readonly T _parameter;
/// <summary>
/// Creates a new <see cref="DisposeAction"/> object.
/// </summary>
/// <param name="action">Action to be executed when this object is disposed.</param>
/// <param name="parameter">The parameter of the action.</param>
public DisposeAction(Action<T> action, T parameter)
{
Check.NotNull(action, nameof(action));
@ -51,9 +52,9 @@ public class DisposeAction<T> : IDisposable
_action = action;
_parameter = parameter;
}
public void Dispose()
{
_action(_parameter);
}
}

2
framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs

@ -83,6 +83,6 @@ public static class DirectoryHelper
Directory.SetCurrentDirectory(targetDirectory);
return new DisposeAction(() => { Directory.SetCurrentDirectory(currentDirectory); });
return new DisposeAction<string>(Directory.SetCurrentDirectory, currentDirectory);
}
}

5
framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs

@ -29,11 +29,12 @@ public static class CultureHelper
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = uiCulture ?? culture;
return new DisposeAction(() =>
return new DisposeAction<ValueTuple<CultureInfo, CultureInfo>>(static (state) =>
{
var (currentCulture, currentUiCulture) = state;
CultureInfo.CurrentCulture = currentCulture;
CultureInfo.CurrentUICulture = currentUiCulture;
});
}, (currentCulture, currentUiCulture));
}
public static bool IsRtl => CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;

4
framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs

@ -80,9 +80,9 @@ public static class SemaphoreSlimExtensions
private static IDisposable GetDispose(this SemaphoreSlim semaphoreSlim)
{
return new DisposeAction(() =>
return new DisposeAction<SemaphoreSlim>(static (semaphoreSlim) =>
{
semaphoreSlim.Release();
});
}, semaphoreSlim);
}
}

1
framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs

@ -27,6 +27,7 @@ public class CurrentTenant : ICurrentTenant, ITransientDependency
{
var parentScope = _currentTenantAccessor.Current;
_currentTenantAccessor.Current = new BasicTenantInfo(tenantId, name);
return new DisposeAction<ValueTuple<ICurrentTenantAccessor, BasicTenantInfo>>(static (state) =>
{
var (currentTenantAccessor, parentScope) = state;

8
framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs

@ -21,9 +21,11 @@ public abstract class CurrentPrincipalAccessorBase : ICurrentPrincipalAccessor
{
var parent = Principal;
_currentPrincipal.Value = principal;
return new DisposeAction(() =>
return new DisposeAction<ValueTuple<AsyncLocal<ClaimsPrincipal>, ClaimsPrincipal>>(static (state) =>
{
_currentPrincipal.Value = parent;
});
var (currentPrincipal, parent) = state;
currentPrincipal.Value = parent;
}, (_currentPrincipal, parent));
}
}

13
framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs

@ -46,18 +46,21 @@ public class AmbientDataContextAmbientScopeProvider<T> : IAmbientScopeProvider<T
_dataContext.SetData(contextKey, item.Id);
return new DisposeAction(() =>
return new DisposeAction<ValueTuple<ConcurrentDictionary<string, ScopeItem>, ScopeItem, IAmbientDataContext, string>>(static (state) =>
{
ScopeDictionary.TryRemove(item.Id, out item);
var (scopeDictionary, item, dataContext, contextKey) = state;
scopeDictionary.TryRemove(item.Id, out item);
if (item.Outer == null)
{
_dataContext.SetData(contextKey, null);
dataContext.SetData(contextKey, null);
return;
}
_dataContext.SetData(contextKey, item.Outer.Id);
});
dataContext.SetData(contextKey, item.Outer.Id);
}, (ScopeDictionary, item, _dataContext, contextKey));
}
private ScopeItem GetCurrentItem(string contextKey)

5
framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.cs

@ -9,9 +9,10 @@ public static class AsyncLocalSimpleScopeExtensions
{
var previousValue = asyncLocal.Value;
asyncLocal.Value = value;
return new DisposeAction(() =>
return new DisposeAction<ValueTuple<AsyncLocal<T>, T>>(static (state) =>
{
var (asyncLocal, previousValue) = state;
asyncLocal.Value = previousValue;
});
}, (asyncLocal, previousValue));
}
}

Loading…
Cancel
Save