feat: 设置管理添加通过前缀获取列表的方法。

main
NoahLan 6 months ago
parent dde7c73990
commit d7587dac5c

@ -8,9 +8,11 @@ namespace NPin.Framework.SettingManagement.Domain;
/// </summary> /// </summary>
public interface ISettingManager public interface ISettingManager
{ {
Task<string?> GetOrNullAsync([NotNull] string name, [NotNull] string providerName, string? providerKey, bool fallback = true); Task<string?> GetOrNullAsync(string name, string providerName, string? providerKey, bool fallback = true);
Task<List<SettingValue>> GetAllAsync([NotNull] string providerName, string? providerKey, bool fallback = true); Task<List<SettingValue>> GetAllAsync(string providerName, string? providerKey, bool fallback = true);
Task<Dictionary<string, SettingValue>> GetAllByPrefixAsync(string namePrefix, string providerName, string? providerKey, bool fallback = true);
Task SetAsync([NotNull] string name, string? value, [NotNull] string providerName, string? providerKey, bool forceToSet = false); Task SetAsync(string name, string? value, string providerName, string? providerKey, bool forceToSet = false);
} }

@ -15,6 +15,12 @@ public interface ISettingRepository: IBasicRepository<SettingEntity, Guid>
string providerName, string providerName,
string providerKey, string providerKey,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
Task<List<SettingEntity>> GetListByPrefixAsync(
string prefix,
string providerName,
string providerKey,
CancellationToken cancellationToken = default);
Task<List<SettingEntity>> GetListAsync( Task<List<SettingEntity>> GetListAsync(
string[] names, string[] names,

@ -20,6 +20,11 @@ public class ConfigurationSettingManagementProvider: ISettingManagementProvider,
return Task.FromResult(Configuration[ConfigurationSettingValueProvider.ConfigurationNamePrefix + setting.Name]); return Task.FromResult(Configuration[ConfigurationSettingValueProvider.ConfigurationNamePrefix + setting.Name]);
} }
public Task<List<SettingValue>> GetByPrefixAsync(string prefix, string? providerKey)
{
throw new AbpException($"Can not get a setting value by prefix to the application configuration.");
}
public Task SetAsync(SettingDefinition setting, string value, string providerKey) public Task SetAsync(SettingDefinition setting, string value, string providerKey)
{ {
throw new AbpException($"Can not set a setting value to the application configuration."); throw new AbpException($"Can not set a setting value to the application configuration.");

@ -12,6 +12,12 @@ public class DefaultValueSettingManagementProvider : ISettingManagementProvider,
return Task.FromResult(setting.DefaultValue); return Task.FromResult(setting.DefaultValue);
} }
public Task<List<SettingValue>> GetByPrefixAsync(string prefix, string? providerKey)
{
throw new AbpException(
$"Can not get a value by prefix of a setting.");
}
public Task SetAsync(SettingDefinition setting, string value, string providerKey) public Task SetAsync(SettingDefinition setting, string value, string providerKey)
{ {
throw new AbpException( throw new AbpException(

@ -13,7 +13,15 @@ public interface ISettingManagementProvider
/// <param name="setting"></param> /// <param name="setting"></param>
/// <param name="providerKey"></param> /// <param name="providerKey"></param>
/// <returns></returns> /// <returns></returns>
Task<string?> GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); Task<string?> GetOrNullAsync(SettingDefinition setting, string? providerKey);
/// <summary>
/// 通过前缀获取配置列表
/// </summary>
/// <param name="prefix"></param>
/// <param name="providerKey"></param>
/// <returns></returns>
Task<List<SettingValue>> GetByPrefixAsync(string prefix, string? providerKey);
/// <summary> /// <summary>
/// 设置配置数据 /// 设置配置数据
@ -22,7 +30,7 @@ public interface ISettingManagementProvider
/// <param name="value"></param> /// <param name="value"></param>
/// <param name="providerKey"></param> /// <param name="providerKey"></param>
/// <returns></returns> /// <returns></returns>
Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string providerKey); Task SetAsync(SettingDefinition setting, string value, string? providerKey);
/// <summary> /// <summary>
/// 清除配置项 /// 清除配置项
@ -30,5 +38,5 @@ public interface ISettingManagementProvider
/// <param name="setting"></param> /// <param name="setting"></param>
/// <param name="providerKey"></param> /// <param name="providerKey"></param>
/// <returns></returns> /// <returns></returns>
Task ClearAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); Task ClearAsync(SettingDefinition setting, string? providerKey);
} }

@ -18,17 +18,22 @@ public abstract class SettingManagementProvider : ISettingManagementProvider
SettingManagementStore = settingManagementStore; SettingManagementStore = settingManagementStore;
} }
public virtual async Task<string?> GetOrNullAsync(SettingDefinition setting, string providerKey) public virtual async Task<string?> GetOrNullAsync(SettingDefinition setting, string? providerKey)
{ {
return await SettingManagementStore.GetOrNullAsync(setting.Name, Name, NormalizeProviderKey(providerKey)); return await SettingManagementStore.GetOrNullAsync(setting.Name, Name, NormalizeProviderKey(providerKey));
} }
public virtual async Task SetAsync(SettingDefinition setting, string value, string providerKey) public virtual async Task<List<SettingValue>> GetByPrefixAsync(string prefix, string? providerKey)
{
return await SettingManagementStore.GetListByPrefixAsync(prefix, Name, NormalizeProviderKey(providerKey));
}
public virtual async Task SetAsync(SettingDefinition setting, string value, string? providerKey)
{ {
await SettingManagementStore.SetAsync(setting.Name, value, Name, NormalizeProviderKey(providerKey)); await SettingManagementStore.SetAsync(setting.Name, value, Name, NormalizeProviderKey(providerKey));
} }
public virtual async Task ClearAsync(SettingDefinition setting, string providerKey) public virtual async Task ClearAsync(SettingDefinition setting, string? providerKey)
{ {
await SettingManagementStore.DeleteAsync(setting.Name, Name, NormalizeProviderKey(providerKey)); await SettingManagementStore.DeleteAsync(setting.Name, Name, NormalizeProviderKey(providerKey));
} }

@ -34,7 +34,7 @@ public class SettingManager : ISettingManager, ISingletonDependency
protected virtual List<ISettingManagementProvider> CreateSettingManagementProviders() protected virtual List<ISettingManagementProvider> CreateSettingManagementProviders()
{ {
using var scope = ServiceProvider.CreateScope(); using var scope = ServiceProvider.CreateScope();
return Options.Providers return Options.Providers
.Select(p => (scope.ServiceProvider.GetRequiredService(p) as ISettingManagementProvider)!) .Select(p => (scope.ServiceProvider.GetRequiredService(p) as ISettingManagementProvider)!)
.ToList(); .ToList();
@ -112,6 +112,50 @@ public class SettingManager : ISettingManager, ISingletonDependency
return settingValues.Values.ToList(); return settingValues.Values.ToList();
} }
public async Task<Dictionary<string, SettingValue>> GetAllByPrefixAsync(string namePrefix, string providerName,
string? providerKey, bool fallback = false)
{
Check.NotNull(providerName, nameof(providerName));
var setting = await SettingDefinitionManager.GetAsync(namePrefix);
var providers = Enumerable.Reverse(Providers)
.SkipWhile(c => c.Name != providerName);
if (!fallback)
{
providers = providers.TakeWhile(c => c.Name == providerName);
}
var providerList = providers.Reverse().ToList();
if (!providerList.Any())
{
return new Dictionary<string, SettingValue>();
}
var settingValues = new Dictionary<string, SettingValue>();
foreach (var provider in providerList)
{
var list = await provider.GetByPrefixAsync(
setting.Name,
provider.Name == providerName ? providerKey : null
);
foreach (var sv in list)
{
if (setting.IsEncrypted)
{
sv.Value = SettingEncryptionService.Decrypt(setting, sv.Value);
}
if (sv.Value != null)
{
settingValues[sv.Name] = sv;
}
}
}
return settingValues;
}
public virtual async Task SetAsync(string name, string? value, string providerName, string? providerKey, public virtual async Task SetAsync(string name, string? value, string providerName, string? providerKey,
bool forceToSet = false) bool forceToSet = false)
{ {

@ -12,6 +12,8 @@ public interface ISettingManagementStore
Task<List<SettingValue>> GetListAsync(string providerName, string providerKey); Task<List<SettingValue>> GetListAsync(string providerName, string providerKey);
Task<List<SettingValue>> GetListAsync(string[] names, string providerName, string providerKey); Task<List<SettingValue>> GetListAsync(string[] names, string providerName, string providerKey);
Task<List<SettingValue>> GetListByPrefixAsync(string prefix, string providerName, string providerKey);
Task SetAsync(string name, string value, string providerName, string providerKey); Task SetAsync(string name, string value, string providerName, string providerKey);

@ -2,7 +2,6 @@
using NPin.Framework.SettingManagement.Domain.Entities; using NPin.Framework.SettingManagement.Domain.Entities;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Settings; using Volo.Abp.Settings;
using Volo.Abp.Uow; using Volo.Abp.Uow;
@ -56,6 +55,16 @@ public class SettingManagementStore : ISettingManagementStore, ITransientDepende
return result; return result;
} }
[UnitOfWork]
public virtual async Task<List<SettingValue>> GetListByPrefixAsync(string prefix, string providerName,
string providerKey)
{
Check.NotNullOrEmpty(prefix, nameof(prefix));
var settings = await SettingRepository.GetListByPrefixAsync(prefix, providerName, providerKey);
return settings.Select(s => new SettingValue(s.Name, s.Value)).ToList();
}
[UnitOfWork] [UnitOfWork]
public virtual async Task SetAsync(string name, string value, string providerName, string providerKey) public virtual async Task SetAsync(string name, string value, string providerName, string providerKey)
{ {

@ -29,6 +29,15 @@ public class SqlSugarCoreSettingRepository : SqlSugarRepository<SettingEntity, G
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
public virtual async Task<List<SettingEntity>> GetListByPrefixAsync(string prefix, string providerName, string providerKey,
CancellationToken cancellationToken = default)
{
return await DbQueryable
.Where(s => s.Name.StartsWith(prefix))
.Where(s => s.ProviderName == providerName && s.ProviderKey == providerKey)
.ToListAsync(cancellationToken);
}
public virtual async Task<List<SettingEntity>> GetListAsync(string[] names, string providerName, string providerKey, public virtual async Task<List<SettingEntity>> GetListAsync(string[] names, string providerName, string providerKey,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {

@ -0,0 +1 @@
np-dev.db
Loading…
Cancel
Save