ABP's Setting

Introduction

Each application needs to store some settings, and use those settings somewhere in your application. ABP provides a powerful infrastructure for storage / retrieval application server and client are available, tenants and user level.

Setting is the name typically stored in a database (or other data sources) - A string value pairs. We can store it to a string by converting non-string value.

About ISettingStore

 To use the system settings, you must implement ISettingStore interface. Although we can implement it in their own way, but it is completely implemented in the Module Zero project. If you do not realize, from the application's configuration file (web.config or app.config) read the settings. However, these settings can not be changed. Also it does not work to limit the scope.

Custom Settings

Settings must be defined before use. ABP is designed to be modular, so different modules may have different settings. Module must be created that derives from SettingProvider class, in order to define its settings. E.g:

public class MySettingProvider : SettingProvider
{
    public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
    {
        return new[]
                {
                    new SettingDefinition(
                        "SmtpServerAddress",
                        "127.0.0.1"
                        ),

                    new SettingDefinition(
                        "PassiveUsersCanNotLogin",
                        "true",
                        scopes: SettingScopes.Application | SettingScopes.Tenant
                        ),

                    new SettingDefinition(
                        "SiteColorPreference",
                        "red",
                        scopes: SettingScopes.User,
                        clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
                        )

                };
    }
}

 

GetSettingDefinitions method returns a SettingDefinition target class has SettingDefinition parameters in its constructor:

  • Name Name (required): setting name must be unique system-wide. Try using a constant string instead of a string changes frequently.
  • Default value: must have a default value, the default value may be null or empty string.
  • Scopes: the need to define the scope of the setting.
  • Display Name Display name: a localized string, it can be used to set the display name in the UI later.
  • Description Description: a localized string, it may be used to describe the UI display settings later.
  • Group Group: Group set may be used. This is only for the UI, is not used to set management.
  • ClientVisibilityProvider: it can be used to determine whether the client is provided.
  • isInherited: This setting is used to determine whether inherited by the tenants and users (see Setting range section).
  • customData: it can be used to define settings for the custom data set.

After creating the setting provider, it must be registered in PreIntialize method of the module:

Configuration.Settings.Providers.Add<MySettingProvider>();

Injection set is automatically registered by the provider dependent. Set provider can inject any dependencies (e.g., store), to use other sources to build defined settings.

Range setting (Setting scope)

Defines three setting range (or level) in SettingScopes enum enumeration

  • Application (Application): the application range is provided independently of the user / tenant set. For example, we can define the IP address called "SmtpServerAddress" settings to get the server when sending e-mail. If the setting is only one value (not change according to the user), then we can define it as the application range.
  • Tenant (Tenant): If the application is multi-tenant, we can define settings for specific tenants.
  • User: We can use a range set to store user / get the value specific to each user's settings.

Flags settingscope enum has a property, we can define a plurality of ranges having provided.

By default, the setting range is hierarchical (unless you inherit is set to false). For example, if we define a range set for the "Application | Tenant | User", and trying to get the current value of this setting:

• If user-defined (covering) the value of the user-specific, we will get the value.

• If not, if it is defined as a tenant (coverage), we will get a tenant-specific values.

• The application value If not, we will get a defined.

• If not, we will get the default value.

The default value may be null or empty string. It proposed to provide default values ​​for the set where possible.

Server (Server-side)

Inserting Manager

ISettingManager for performing the setting operation. We can inject and use it anywhere in the application. ISettingManager defines a number of methods to get the value set.

The most common method is GetSettingValue (or asynchronous calls GetSettingValueAsync). It is based on the default value, applications, user settings, and returns the tenant set current value (as stated previously set range). E.g:

//Getting a boolean value (async call)
var value1 = await SettingManager.GetSettingValueAsync<bool>("PassiveUsersCanNotLogin");

//Getting a string value (sync call)
var value2 = SettingManager.GetSettingValue("SmtpServerAddress");

As indicated above, GetSettingValue and universal asynchronous versions, there are some ways to get a list of users or a set value of a particular tenant or all of the set values.

Since ISettingManager is widely used, some special base class (e.g. ApplicationService, DomainService and AbpController) of a property called SettingManager. If we derive from these classes, you do not need to explicitly inject it.

ISettingDefinitionManager

ISettingDefinitionManager may also be provided for acquiring the definition AppSettingProvider defined. We can also inject and use it anywhere in the application. Use ISettingDefinitionManager get definition name, default value, display name, and so on.

Client

Set ClientVisibilityProvider determining visibility attribute definitions set by the client. There are four ISettingClientVisibilityProvider achieved.

  • VisibleSettingClientVisibilityProvider: the setting defines the client visible.
  • HiddenSettingClientVisibilityProvider: the client hidden settings defined.
  • RequiresAuthenticationSettingClientVisibilityProvider: When a user logs in, the setting defines the client visible.
  • RequiresPermissionSettingClientVisibilityProvider: If the login user has a specific permission, the setting defines the client visible.

If ClientVisibilityProvider according to a set of defined set of clients is visible, you can use JavaScript to get its current value on the client.  abp.setting namespace defines the required functions and objects, such as :

var currentColor = abp.setting.get("SiteColorPreference");

There getInt and getBoolean method. You can use abp.setting.values object to get all the values . Note that if we change the settings on the server, the client can not know of the change, unless refresh the page, or in some way to reload or update the settings set by the code manually.

Change settings

ISettingManager defined ChangeSettingForApplicationAsync, ChangeSettingForTenantAsync and ChangeSettingForUserAsync methods (and synchronous versions), respectively, for the application, tenants and users to change settings.

About Cache

Settings Manager cache settings on the server side, so we should not use a repository or database update statements directly change the set value.

 

Guess you like

Origin www.cnblogs.com/yixuanhan/p/10949085.html