diff --git a/docs/en/Configuration.md b/docs/en/Configuration.md new file mode 100644 index 0000000000..6e638f7814 --- /dev/null +++ b/docs/en/Configuration.md @@ -0,0 +1,3 @@ +# Configuration + +ASP.NET Core has an flexible and extensible key-value based configuration system. In fact, the configuration system is a part of Microsoft.Extensions libraries and it is independent from ASP.NET Core. That means it can be used in any type of application. See [Microsoft's documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/) to learn the configuration infrastructure. ABP framework is 100% compatible with the configuration system. \ No newline at end of file diff --git a/docs/en/CurrentUser.md b/docs/en/CurrentUser.md new file mode 100644 index 0000000000..bd45a0fcc6 --- /dev/null +++ b/docs/en/CurrentUser.md @@ -0,0 +1,3 @@ +# Current User + +TODO! \ No newline at end of file diff --git a/docs/en/Modules/Setting-Management.md b/docs/en/Modules/Setting-Management.md new file mode 100644 index 0000000000..97e8c2f3e1 --- /dev/null +++ b/docs/en/Modules/Setting-Management.md @@ -0,0 +1,3 @@ +# Setting Management Module + +TODO \ No newline at end of file diff --git a/docs/en/Options.md b/docs/en/Options.md new file mode 100644 index 0000000000..cead1aea61 --- /dev/null +++ b/docs/en/Options.md @@ -0,0 +1,4 @@ +# Options + +TODO! + diff --git a/docs/en/Settings.md b/docs/en/Settings.md index 767955a9d2..6fc0b707a4 100644 --- a/docs/en/Settings.md +++ b/docs/en/Settings.md @@ -1,3 +1,153 @@ # Settings -TODO! \ No newline at end of file +[Configuration system](Configuration.md) is a good way to configure the application on startup. In addition to the configurations, ABP provides another way to set and get some application settings. + +A setting is a name-value pair stored in a dynamic data source, generally in a database. Setting system is extensible and there are pre-built provides for a user, a tenant, global and default. + +## Defining Settings + +A setting must be defined before its use. ABP was designed to be [modular](Module-Development-Basics.md), so different modules can have different settings. A module must create a class derived from the `SettingDefinitionProvider` in order to define its settings. An example setting definition provider is shown below: + +````csharp +public class EmailSettingProvider : SettingDefinitionProvider +{ + public override void Define(ISettingDefinitionContext context) + { + context.Add( + new SettingDefinition("Smtp.Host", "127.0.0.1"), + new SettingDefinition("Smtp.Port", "25"), + new SettingDefinition("Smtp.UserName"), + new SettingDefinition("Smtp.Password", isEncrypted: true), + new SettingDefinition("Smtp.EnableSsl", "false") + ); + } +} +```` + +ABP automatically discovers this class and registers the setting definitions. + +### SettingDefinition + +`SettingDefinition` class has the following properties: + +* **Name**: Unique name of the setting in the application. This is **the only mandatory property**. Used to get/set the value of this setting in the application code (It's a good idea to define a const string for a setting name instead of using a magic string). +* **DefaultValue**: A setting may have a default value. +* **DisplayName**: A localizable string that can be used to show the setting name on the UI. +* **Description**: A localizable string that can be used to show the setting description on the UI. +* **IsVisibleToClients**: A boolean value indicates that whether this setting value is available in the client side or not. Default value is false to prevent accidently publishing an internal critical setting value. +* **IsInherited**: A boolean value indicates that whether this setting value is inherited from other providers or not. Default value is true and fallbacks to the next provider if the setting value was not set for the requested provider (see the setting value providers section for more). +* **IsEncrypted**: A boolean value indicates that whether this setting value should be encrypted on save and decrypted on read. It makes possible to secure the setting value in the database. +* **Providers**: Can be used to restrict providers available for a particular setting (see the setting value providers section for more). +* **Properties**: A name/value collection to set custom properties about this setting those can be used later in the application code. + +## ISettingProvider + +`ISettingProvider` is used to get the value of a setting or get the values of all the settings. Example usages: + +````csharp +public class MyService +{ + private readonly ISettingProvider _settingProvider; + + //Inject ISettingProvider in the constructor + public MyService(ISettingProvider settingProvider) + { + _settingProvider = settingProvider; + } + + public async Task FooAsync() + { + //Get a string value. + string userName = await _settingProvider.GetOrNullAsync("Smtp.UserName"); + + //Get a bool value and fallback to the default value (false) if not set. + bool enableSsl = await _settingProvider.GetAsync("Smtp.EnableSsl"); + + //Get a bool value and fallback to the provided default value (true) if not set. + bool enableSsl = await _settingProvider.GetAsync( + "Smtp.EnableSsl", defaultValue: true); + + //Get a bool value with the IsTrueAsync shortcut extension method + bool enableSsl = await _settingProvider.IsTrueAsync("Smtp.EnableSsl"); + + //Get an int value or the default value (0) if not set + int port = (await _settingProvider.GetAsync("Smtp.Port")); + + //Get an int value or null if not provided + int? port = (await _settingProvider.GetOrNullAsync("Smtp.Port"))?.To(); + } +} +```` + +> `ISettingProvider` is a very common service and some base classes (like `IApplicationService`) already property-inject it. You can directly use the `SettingProvider` in such cases. + +## Setting Value Providers + +Setting system is extensible, you can extend it by defining setting value providers to get setting values from any source and based on any condition. + +`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: + +* `DefaultValueSettingValueProvider`: Gets the value from the default value of the setting definition, if set (see the SettingDefinition section above). +* `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). + +> Setting fallback system works from bottom (user) to top (default). + +Global, Tenant and User setting value providers use the `ISettingStore` to read the value from the data source (see the section below). + +### Custom Setting Value Providers + +If you need to extend the setting system, you can define a class derived from the `SettingValueProvider` class. Example: + +````csharp +public class CustomSettingValueProvider : SettingValueProvider +{ + public override string Name => "Custom"; + + public CustomSettingValueProvider(ISettingStore settingStore) + : base(settingStore) + { + } + + public override Task GetOrNullAsync(SettingDefinition setting) + { + /* Return the setting value or null + Use the SettingStore or another data source */ + } +} +```` + +> Alternatively, you can implement the `ISettingValueProvider` interface. Remember to register it to the [dependency injection](Dependency-Injection.md) in this case. + +Every provider should have a unique Name (which is "Custom" here). Built-in providers use the given names: + +* `DefaultValueSettingValueProvider`: "**D**". +* `GlobalSettingValueProvider`: "**G**". +* `TenantSettingValueProvider`: "**T**". +* `UserSettingValueProvider`: "**U**". + +One-letter names were preferred to reduce the data size in the database (provider name is repeated in each row). + +Once you define a custom setting value provider, you need to explicitly register it to the `AbpSettingOptions`: + +````csharp +Configure(options => +{ + options.ValueProviders.Add(); +}); +```` + +This example adds it as the last item, so it will be the first value provider used by the `ISettingProvider`. You could add it to another index in the `options.ValueProviders` list. + +### ISettingStore + +While a setting value provider is free to use any source to get the setting value, the`ISettingStore` service is the default source of the setting values. Global, Tenant and User setting value providers use it. + +## Setting Management Module + +The core setting system is pretty independent and doesn't make any assumption about how you manage (change) the setting values. Even the default `ISettingStore` implementation is the `NullSettingStore` which returns null for all setting values. + +The setting management module completes it (and implements `ISettingStore`) by managing setting values in a database. See the [Setting Management Module document](Modules/Setting-Management.md) for more. \ No newline at end of file diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index eb8b3865c1..c454d0278e 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -56,6 +56,10 @@ { "text": "Fundamentals", "items": [ + { + "text": "Configuration", + "path": "Configuration.md" + }, { "text": "Dependency Injection", "path": "Dependency-Injection.md", @@ -93,7 +97,8 @@ "text": "Auditing" }, { - "text": "Setting Management" + "text": "Settings", + "path": "Settings.md" } ] }, @@ -116,6 +121,10 @@ { "text": "Services", "items": [ + { + "text": "Current User", + "path": "CurrentUser.md" + }, { "text": "Object to object mapping", "path": "Object-To-Object-Mapping.md"