Resolved #2159: Create Configuration setting value provider.

pull/2165/head
Halil İbrahim Kalkan 6 years ago
parent d18229db8c
commit 913d46b78b

@ -73,9 +73,10 @@ Setting values are cached using the [distributed cache](../Caching.md) system. A
## Setting Management Providers
Setting Management module is extensible, just like the [setting system](../Settings.md). You can extend it by defining setting management providers. There are four pre-built setting management providers registered by the order below:
Setting Management module is extensible, just like the [setting system](../Settings.md). You can extend it by defining setting management providers. There are 5 pre-built setting management providers registered by the order below:
* `DefaultValueSettingManagementProvider`: Gets the value from the default value of the setting definition. It can not set the default value since default values are hard-coded on the setting definition.
* `ConfigurationSettingManagementProvider`: Gets the value from the [IConfiguration service](Configuration.md). It can not set the configuration value because it is not possible to change the configuration values on runtime.
* `GlobalSettingManagementProvider`: Gets or sets the global (system-wide) value for a setting.
* `TenantSettingManagementProvider`: Gets or sets the setting value for a tenant.
* `UserSettingManagementProvider`: Gets the setting value for a user.

@ -106,9 +106,10 @@ Setting system is extensible, you can extend it by defining setting value provid
`ISettingProvider` uses the setting value providers to obtain a setting value. It fallbacks to the next value provider if a value provider can not get the setting value.
There are four pre-built setting value providers registered by the order below:
There are 5 pre-built setting value providers registered by the order below:
* `DefaultValueSettingValueProvider`: Gets the value from the default value of the setting definition, if set (see the SettingDefinition section above).
* `ConfigurationSettingValueProvider`: Gets the value from the [IConfiguration service](Configuration.md).
* `GlobalSettingValueProvider`: Gets the global (system-wide) value for a setting, if set.
* `TenantSettingValueProvider`: Gets the setting value for the current tenant, if set (see the [multi-tenancy](Multi-Tenancy.md) document).
* `UserSettingValueProvider`: Gets the setting value for the current user, if set (see the [current user](CurrentUser.md) document).

@ -25,6 +25,7 @@ namespace Volo.Abp.Settings
Configure<AbpSettingOptions>(options =>
{
options.ValueProviders.Add<DefaultValueSettingValueProvider>();
options.ValueProviders.Add<ConfigurationSettingValueProvider>();
options.ValueProviders.Add<GlobalSettingValueProvider>();
options.ValueProviders.Add<TenantSettingValueProvider>();
options.ValueProviders.Add<UserSettingValueProvider>();

@ -0,0 +1,27 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Settings
{
public class ConfigurationSettingValueProvider : ISettingValueProvider, ITransientDependency
{
public const string ConfigurationNamePrefix = "Settings:";
public const string ProviderName = "C";
public string Name => ProviderName;
protected IConfiguration Configuration { get; }
public ConfigurationSettingValueProvider(IConfiguration configuration)
{
Configuration = configuration;
}
public virtual Task<string> GetOrNullAsync(SettingDefinition setting)
{
return Task.FromResult(Configuration[ConfigurationNamePrefix + setting.Name]);
}
}
}

@ -18,6 +18,7 @@ namespace Volo.Abp.SettingManagement
Configure<SettingManagementOptions>(options =>
{
options.Providers.Add<DefaultValueSettingManagementProvider>();
options.Providers.Add<ConfigurationSettingManagementProvider>();
options.Providers.Add<GlobalSettingManagementProvider>();
options.Providers.Add<TenantSettingManagementProvider>();
options.Providers.Add<UserSettingManagementProvider>();

@ -0,0 +1,34 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Settings;
namespace Volo.Abp.SettingManagement
{
public class ConfigurationSettingManagementProvider : ISettingManagementProvider, ITransientDependency
{
public string Name => ConfigurationSettingValueProvider.ProviderName;
protected IConfiguration Configuration { get; }
public ConfigurationSettingManagementProvider(IConfiguration configuration)
{
Configuration = configuration;
}
public Task<string> GetOrNullAsync(SettingDefinition setting, string providerKey)
{
return Task.FromResult(Configuration[ConfigurationSettingValueProvider.ConfigurationNamePrefix + setting.Name]);
}
public Task SetAsync(SettingDefinition setting, string value, string providerKey)
{
throw new AbpException($"Can not set a setting value to the application configuration.");
}
public Task ClearAsync(SettingDefinition setting, string providerKey)
{
throw new AbpException($"Can not set a setting value to the application configuration.");
}
}
}

@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Settings;
namespace Volo.Abp.SettingManagement
{
public static class ConfigurationValueSettingManagerExtensions
{
public static Task<string> GetOrNullConfigurationAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true)
{
return settingManager.GetOrNullAsync(name, ConfigurationSettingValueProvider.ProviderName, null, fallback);
}
public static Task<List<SettingValue>> GetAllConfigurationAsync(this ISettingManager settingManager, bool fallback = true)
{
return settingManager.GetAllAsync(ConfigurationSettingValueProvider.ProviderName, null, fallback);
}
}
}

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Settings;
using Xunit;

Loading…
Cancel
Save