mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 增加 using DataFilter.Disable、Enable 使用完成后恢复可用状态;#9
This commit is contained in:
		@@ -10,11 +10,29 @@ namespace FreeSql {
 | 
			
		||||
 | 
			
		||||
		IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp);
 | 
			
		||||
 | 
			
		||||
		IDataFilter<TEntity> Enable(params string[] filterName);
 | 
			
		||||
		IDataFilter<TEntity> EnableAll();
 | 
			
		||||
		/// <summary>
 | 
			
		||||
		/// 开启过滤器,若使用 using 则使用完后,恢复为原有状态
 | 
			
		||||
		/// </summary>
 | 
			
		||||
		/// <param name="filterName">过滤器名称</param>
 | 
			
		||||
		/// <returns></returns>
 | 
			
		||||
		IDisposable Enable(params string[] filterName);
 | 
			
		||||
		/// <summary>
 | 
			
		||||
		/// 开启所有过滤器,若使用 using 则使用完后,恢复为原有状态
 | 
			
		||||
		/// </summary>
 | 
			
		||||
		/// <returns></returns>
 | 
			
		||||
		IDisposable EnableAll();
 | 
			
		||||
 | 
			
		||||
		IDataFilter<TEntity> Disable(params string[] filterName);
 | 
			
		||||
		IDataFilter<TEntity> DisableAll();
 | 
			
		||||
		/// <summary>
 | 
			
		||||
		/// 禁用过滤器,若使用 using 则使用完后,恢复为原有状态
 | 
			
		||||
		/// </summary>
 | 
			
		||||
		/// <param name="filterName"></param>
 | 
			
		||||
		/// <returns></returns>
 | 
			
		||||
		IDisposable Disable(params string[] filterName);
 | 
			
		||||
		/// <summary>
 | 
			
		||||
		/// 禁用所有过滤器,若使用 using 则使用完后,恢复为原有状态
 | 
			
		||||
		/// </summary>
 | 
			
		||||
		/// <returns></returns>
 | 
			
		||||
		IDisposable DisableAll();
 | 
			
		||||
 | 
			
		||||
		bool IsEnabled(string filterName);
 | 
			
		||||
	}
 | 
			
		||||
@@ -40,34 +58,63 @@ namespace FreeSql {
 | 
			
		||||
			return this;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public IDataFilter<TEntity> Disable(params string[] filterName) {
 | 
			
		||||
			if (filterName == null || filterName.Any() == false) return this;
 | 
			
		||||
		public IDisposable Disable(params string[] filterName) {
 | 
			
		||||
			if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
 | 
			
		||||
 | 
			
		||||
			List<string> restore = new List<string>();
 | 
			
		||||
			foreach (var name in filterName) {
 | 
			
		||||
				if (_filters.TryGetValue(name, out var tryfi))
 | 
			
		||||
					tryfi.IsEnabled = false;
 | 
			
		||||
				if (_filters.TryGetValue(name, out var tryfi)) {
 | 
			
		||||
					if (tryfi.IsEnabled) {
 | 
			
		||||
						restore.Add(name);
 | 
			
		||||
						tryfi.IsEnabled = false;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			return this;
 | 
			
		||||
			return new UsingAny(() => this.Enable(restore.ToArray()));
 | 
			
		||||
		}
 | 
			
		||||
		public IDataFilter<TEntity> DisableAll() {
 | 
			
		||||
			foreach (var val in _filters.Values.ToArray())
 | 
			
		||||
				val.IsEnabled = false;
 | 
			
		||||
			return this;
 | 
			
		||||
		public IDisposable DisableAll() {
 | 
			
		||||
			List<string> restore = new List<string>();
 | 
			
		||||
			foreach (var val in _filters) {
 | 
			
		||||
				if (val.Value.IsEnabled) {
 | 
			
		||||
					restore.Add(val.Key);
 | 
			
		||||
					val.Value.IsEnabled = false;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			return new UsingAny(() => this.Enable(restore.ToArray()));
 | 
			
		||||
		}
 | 
			
		||||
		class UsingAny : IDisposable {
 | 
			
		||||
			Action _ondis;
 | 
			
		||||
			public UsingAny(Action ondis) {
 | 
			
		||||
				_ondis = ondis;
 | 
			
		||||
			}
 | 
			
		||||
			public void Dispose() {
 | 
			
		||||
				_ondis?.Invoke();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public IDataFilter<TEntity> Enable(params string[] filterName) {
 | 
			
		||||
			if (filterName == null || filterName.Any() == false) return this;
 | 
			
		||||
		public IDisposable Enable(params string[] filterName) {
 | 
			
		||||
			if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
 | 
			
		||||
 | 
			
		||||
			List<string> restore = new List<string>();
 | 
			
		||||
			foreach (var name in filterName) {
 | 
			
		||||
				if (_filters.TryGetValue(name, out var tryfi))
 | 
			
		||||
					tryfi.IsEnabled = true;
 | 
			
		||||
				if (_filters.TryGetValue(name, out var tryfi)) {
 | 
			
		||||
					if (tryfi.IsEnabled == false) {
 | 
			
		||||
						restore.Add(name);
 | 
			
		||||
						tryfi.IsEnabled = true;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			return this;
 | 
			
		||||
			return new UsingAny(() => this.Disable(restore.ToArray()));
 | 
			
		||||
		}
 | 
			
		||||
		public IDataFilter<TEntity> EnableAll() {
 | 
			
		||||
			foreach (var val in _filters.Values.ToArray())
 | 
			
		||||
				val.IsEnabled = true;
 | 
			
		||||
			return this;
 | 
			
		||||
		public IDisposable EnableAll() {
 | 
			
		||||
			List<string> restore = new List<string>();
 | 
			
		||||
			foreach (var val in _filters) {
 | 
			
		||||
				if (val.Value.IsEnabled == false) {
 | 
			
		||||
					restore.Add(val.Key);
 | 
			
		||||
					val.Value.IsEnabled = true;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			return new UsingAny(() => this.Disable(restore.ToArray()));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public bool IsEnabled(string filterName) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 | 
			
		||||
  <PropertyGroup>
 | 
			
		||||
    <TargetFramework>netstandard2.0</TargetFramework>
 | 
			
		||||
    <Version>0.3.13</Version>
 | 
			
		||||
    <Version>0.3.13.1</Version>
 | 
			
		||||
    <Authors>YeXiangQin</Authors>
 | 
			
		||||
    <Description>FreeSql 通用仓库层实现,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,读写分离、分表分库。</Description>
 | 
			
		||||
    <PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 | 
			
		||||
  <PropertyGroup>
 | 
			
		||||
    <TargetFramework>netstandard2.0</TargetFramework>
 | 
			
		||||
    <Version>0.3.13</Version>
 | 
			
		||||
    <Version>0.3.13.1</Version>
 | 
			
		||||
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
 | 
			
		||||
    <Authors>YeXiangQin</Authors>
 | 
			
		||||
    <Description>打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,读写分离、分表分库。</Description>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user