Skip to content

Commit

Permalink
Resolved #2159: Create Configuration setting value provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Nov 13, 2019
1 parent d18229d commit 913d46b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/en/Modules/Setting-Management.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion docs/en/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
Configure<AbpSettingOptions>(options =>
{
options.ValueProviders.Add<DefaultValueSettingValueProvider>();
options.ValueProviders.Add<ConfigurationSettingValueProvider>();
options.ValueProviders.Add<GlobalSettingValueProvider>();
options.ValueProviders.Add<TenantSettingValueProvider>();
options.ValueProviders.Add<UserSettingValueProvider>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
Configure<SettingManagementOptions>(options =>
{
options.Providers.Add<DefaultValueSettingManagementProvider>();
options.Providers.Add<ConfigurationSettingManagementProvider>();
options.Providers.Add<GlobalSettingManagementProvider>();
options.Providers.Add<TenantSettingManagementProvider>();
options.Providers.Add<UserSettingManagementProvider>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 913d46b

Please sign in to comment.