首先我们看一下 ISettingManager 的默认实现 SettingManager。
public class SettingManager : ISettingManager, ISingletonDependency { public const string ApplicationSettingsCacheKey = "ApplicationSettings"; /// <summary> /// Reference to the current Session. /// </summary> public IAbpSession AbpSession { get; set; } /// <summary> /// Reference to the setting store. /// </summary> public ISettingStore SettingStore { get; set; } private readonly ISettingDefinitionManager _settingDefinitionManager; private readonly ITypedCache<string, Dictionary<string, SettingInfo>> _applicationSettingCache; private readonly ITypedCache<int, Dictionary<string, SettingInfo>> _tenantSettingCache; private readonly ITypedCache<string, Dictionary<string, SettingInfo>> _userSettingCache; /// <inheritdoc/> public SettingManager(ISettingDefinitionManager settingDefinitionManager, ICacheManager cacheManager) { _settingDefinitionManager = settingDefinitionManager; AbpSession = NullAbpSession.Instance; SettingStore = DefaultConfigSettingStore.Instance; _applicationSettingCache = cacheManager.GetApplicationSettingsCache(); _tenantSettingCache = cacheManager.GetTenantSettingsCache(); _userSettingCache = cacheManager.GetUserSettingsCache(); } }可以看到在这里面,他注入了 ISetingStore 与 ISettingDefinitionManager ,并且使用了三个 ITypedCache 来为这些设置进行一个缓存。
下面这个 GetSettingValueAsync() 方法则是获取一个指定名称的设置值。
public Task<string> GetSettingValueAsync(string name) { return GetSettingValueInternalAsync(name, AbpSession.TenantId, AbpSession.UserId); } private async Task<string> GetSettingValueInternalAsync(string name, int? tenantId = null, long? userId = null, bool fallbackToDefault = true) { // 获取指定 Name 的 SettingDefine var settingDefinition = _settingDefinitionManager.GetSettingDefinition(name); // 判断该设置项的使用范围是否为 User if (settingDefinition.Scopes.HasFlag(SettingScopes.User) && userId.HasValue) { var settingValue = await GetSettingValueForUserOrNullAsync(new UserIdentifier(tenantId, userId.Value), name); if (settingValue != null) { return settingValue.Value; } if (!fallbackToDefault) { return null; } if (!settingDefinition.IsInherited) { return settingDefinition.DefaultValue; } } // 判断该设置项的使用范围是否为 Tenant if (settingDefinition.Scopes.HasFlag(SettingScopes.Tenant) && tenantId.HasValue) { var settingValue = await GetSettingValueForTenantOrNullAsync(tenantId.Value, name); if (settingValue != null) { return settingValue.Value; } if (!fallbackToDefault) { return null; } if (!settingDefinition.IsInherited) { return settingDefinition.DefaultValue; } } // 判断该设置项的使用范围是否为 Application if (settingDefinition.Scopes.HasFlag(SettingScopes.Application)) { var settingValue = await GetSettingValueForApplicationOrNullAsync(name); if (settingValue != null) { return settingValue.Value; } if (!fallbackToDefault) { return null; } } // 如果都没有定义,则返回默认的设置值 return settingDefinition.DefaultValue; }这里又为每个判断内部封装了一个方法,这里以 GetSettingValueForApplicationOrNullAsync() 为例,转到其定义:
private async Task<SettingInfo> GetSettingValueForApplicationOrNullAsync(string name) { return (await GetApplicationSettingsAsync()).GetOrDefault(name); } private async Task<Dictionary<string, SettingInfo>> GetApplicationSettingsAsync() { // 从缓存当中获取设置信息,如果不存在,则执行其工厂方法 return await _applicationSettingCache.GetAsync(ApplicationSettingsCacheKey, async () => { var dictionary = new Dictionary<string, SettingInfo>(); // 从 ISettingStore 当中获取对应的 Value 值 var settingValues = await SettingStore.GetAllListAsync(null, null); foreach (var settingValue in settingValues) { dictionary[settingValue.Name] = settingValue; } return dictionary; }); } 1.2.3 ISettingDefinitionManager这个管理器作用最开始已经说明了,就是单纯的获取到用户注册到 Providers 里面的 SettingDefinition。
1.2.4 SettingProviderSettingProvider 用于开发人员配置自己的配置项,所有的设置提供者只需要继承自本类,实现其 GetSettingDefinitions 方法即可。
1.2.5 ISettingStore