Browse Source
Use `DisposeAction` with parameter.
pull/14453/head
maliming
4 years ago
No known key found for this signature in database
GPG Key ID: 96224957E51C89E
9 changed files with
32 additions and
22 deletions
-
framework/src/Volo.Abp.Core/Volo/Abp/Aspects/AbpCrossCuttingConcerns.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/DisposeAction.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/Localization/CultureHelper.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs
-
framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/CurrentTenant.cs
-
framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/CurrentPrincipalAccessorBase.cs
-
framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AmbientDataContextAmbientScopeProvider.cs
-
framework/src/Volo.Abp.Threading/Volo/Abp/Threading/AsyncLocalSimpleScopeExtensions.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) |
|
|
|
|
|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -83,6 +83,6 @@ public static class DirectoryHelper |
|
|
|
|
|
|
|
Directory.SetCurrentDirectory(targetDirectory); |
|
|
|
|
|
|
|
return new DisposeAction(() => { Directory.SetCurrentDirectory(currentDirectory); }); |
|
|
|
return new DisposeAction<string>(Directory.SetCurrentDirectory, currentDirectory); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -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; |
|
|
|
|
|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -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; |
|
|
|
|
|
|
|
@ -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)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -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) |
|
|
|
|
|
|
|
@ -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)); |
|
|
|
} |
|
|
|
} |
|
|
|
|