Click here to go to the ABP series articles General Catalogue
DDD-based Modern ASP.--ABP Series 9, ABP Setup Management
The ABP is "ASP. Boilerplate Project (ASP. NET Template project) "for short.
ABP's official website :http://www.aspnetboilerplate.com
ABP's Open source project on GitHub : https://github.com/aspnetboilerplate
This article is provided by Shandong -li Wei translation
Introduced
Each application needs to store some settings and use those settings somewhere in the application. The ABP Framework provides a robust infrastructure that allows us to store/retrieve application, tenant, and user-level configurations on either the server or client settings.
Settings are typically stored in a database (or another source), represented by a structure that corresponds to a name-value (Name-value) string. We can convert non-string values into string values to store them.
Note: About the Isettingstore interface
The Isettingstore interface must be implemented in order to use settings management. You can implement it in your own way, and you can refer to the complete implementation of the Module-zero project.
Define Settings
You must first define the settings before using them. The ABP framework is modular in design, so different modules can have different settings. In order to define the module's own settings, each module should create a derived class that inherits from Settingprovider. An example of a setup provider is as follows:
Public classmysettingprovider:settingprovider{ Public OverrideIenumerable<settingdefinition>getsettingdefinitions (Settingdefinitionprovidercontext context) {return New[] { NewSettingdefinition ("smtpserveraddress", "127.0.0.1" ), NewSettingdefinition ("Passiveuserscannotlogin", "true", Scopes:SettingScopes.Application|settingscopes.tenant),NewSettingdefinition ("sitecolorpreference", "Red", Scopes:SettingScopes.User, isvisibletoclients:true ) }; }}
The Getsettingdefinitions method returns the Settingdefinition object. The constructor for the Settingdefinition class has the following parameters:
- Name (required): Must have a system-wide unique name. The better way is to define a string constant to set name.
- Default value: Sets a defaults. This value can be null or an empty string.
- Scopes: Defines the scope of the setting (see below).
- Display name: A localizable string that will be used to display the name of the setting in the UI later.
- Description: A localizable string that is used to display a description of the settings later in the UI.
- Group: Can be used to set up groups. This is only UI usage and is not used for settings management.
- isvisibletoclients: Set to True will make the settings available on the client.
After creating the provisioning provider (Settingprovider), we should register our module in the pre-initialize (Preintialize) method:
Configuration.settings.providers.add<mysettingprovider> ();
The provisioning provider automatically registers dependency injection. Therefore, the settings provider can inject any dependency, such as a repository, to generate some other source of the settings definition.
Set Range
There are three set ranges (or levels) defined in the Settingscopes enumeration:
- Application: Application scope settings are used for user/tenant independent settings. For example, we can define a setting called "smtpserveraddress" that gets the IP address of the server when an e-mail message is sent. If this setting has a single value (not based on user changes), then we can define it as an application scope.
- Tenant: If the application is multi-tenancy, we can define tenant-specific settings.
- User: We can store/get the set value for each user by using the settings of the users range.
The Settingscopes enumeration has the Flags property, so we can define a setting with multiple scopes.
The set range is hierarchical. For example, if we define a setting range of "Application | Tenant | User "and attempts to get the value of the current setting;
- We get the value of a specific user if it defines (overrides) the username.
- If not, we get a specific tenant value if it defines (Overrides) Tenant.
- If not, we get the value of the app if it defines application.
- If not, we get the default value.
The default value can be null or an empty string. If you can, we recommend that you provide a default value for your settings.
Get Set Value
After defining the settings, we can obtain its current value on the server and the client.
(1) server-side (server side)
The Isettingmanager is used to perform the setup operation. We can inject and use it from anywhere in the application. Isettingmanager defines a number of ways to get set values.
The most common method is Getsettingvalue (or getsettingvalueasync is called asynchronously). It returns the values that are currently set based on the default values, application, tenant, and user settings range (as described in the paragraph before setting the scope). Example:
// Getting A Boolean value (Async call) var await settingmanager.getsettingvalueasync<bool> ("passiveuserscannotlogin" ); // Getting A String value (Sync call) var value2 = Settingmanager.getsettingvalue ("smtpserveraddress");
The getsettingvalue has a generic and asynchronous version, as shown above. There is also a way to get a list of settings for a particular tenant or user or for all set values.
Because Isettingmanager is widely used, some specific base classes (such as Applicationservice, DomainService, and Abpcontroller) have a property named Settingmanager. If we inherit from these classes, we don't have to inject it explicitly.
(2) Client
If you set Isvisibletoclients to True when you define a setting, you can use JavaScript on the client to get its current value. The Abp.setting namespace defines the required functions and objects. Example:
var currentcolor = abp.setting. Get ("sitecolorpreference");
There are also methods such as GetInt and Getboolean. You can use the Abp.setting.values object to get all the values. Note that if you change the settings on the server side, the client will not know about this change unless you refresh the page or reload the page in some way or manually update it via code.
Change settings
Isettingmanager defines changesettingforapplicationasync,changesettingfortenantasync , and The Changesettingforuserasync method (as well as the synchronous version) changes the settings for the application, tenant, and user respectively.
About caching
Caching is managed on server-side settings, so we should not use the repository or database update statements directly to change the value of the setting.
I hope that more domestic architects will be able to focus on the ABP project, and perhaps it will help you, perhaps with your participation, this project can develop better.
Welcome to add ABP Architecture Design Exchange QQ Group: 134710707
Click here to go to the ABP series articles General Catalogue
ABP (modern ASP. NET template Development Framework) Series 9, ABP Setup Management