Adhesive framework series-Configuration Service Module usage

Source: Internet
Author: User

In the previous articleArticleThe Configuration Service Module is divided into the server and client. As a user, you only need to care about some interfaces of the client. Let's take a look at the definitions of these interfaces.

 Public   Interface Iconfigservice {T getconfigitemvalue <t> ( String Catename, t defval); t getconfigitemvalue <t> ( String Catename, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> ( String Catename, String Itemname, t defval); t getconfigitemvalue <t> ( String Catename, String Itemname, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> (String Catename, String Subcatename, String Itemname, t defval); t getconfigitemvalue <t> ( String Catename, String Subcatename, String Itemname, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> ( String [] Pathitemnames, t defval); t getconfigitemvalue <t> ( String [] Pathitemnames, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> ( Bool Global, String Catename, t defval); t getconfigitemvalue <t> ( Bool Global, String Catename, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> ( Bool Global, String Catename, String Itemname, t defval); t getconfigitemvalue <t> ( Bool Global, String Catename, String Itemname, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> ( Bool Global, String Catename,String Subcatename, String Itemname, t defval); t getconfigitemvalue <t> ( Bool Global, String Catename, String Subcatename, String Itemname, t defval, configitemvalueupdatecallback callback); t getconfigitemvalue <t> ( Bool Global, String [] Pathitemnames, t defval); t getconfigitemvalue <t> ( Bool Global, String [] Pathitemnames, t defval, configitemvalueupdatecallback callback );}

Although there are many methods for overloading, there are two types:Global(True by default ).GlobalParameter.GlobalThe parameter indicates whether the configuration is global. Configuration can be divided into two categories based on the application scope: global configuration and private configuration.ProgramIt is used within the specified range and belongs to the Application name. There are two advantages for this division: first, sometimes, the specific settings of the same configuration vary depending on the application; second, the data volume during synchronization configuration is reduced, during synchronization, you only need to care about the global configuration and the configuration of your own application. The configuration is hierarchical. For example, a configuration under a certain module under a business can beCatenameThe parameter specifies the name of the configuration node at the first level.SubcatenameThe parameter specifies the name of the configuration node at the second level.ItemnameThe parameter specifies the name of the configuration node at the third level. The configuration module supports unlimited levels. If you want to access configuration nodes above Layer 3, you can usePathitemnamesParameter overload method, specify the path to access the configuration node. Each method has a generic parameter.TThis parameter indicates the configured data type. The following table lists the data types supported by the configuration module:

 

DefvalThe parameter indicates the default value of the configuration node. If the value of the configuration node is null (not configured), you can specify this parameter to return a default value. If the configuration node does not exist, it is automatically created and the value of the configuration node is initialized Based on the specified default value.CallbackThe parameter indicates the method name to be called back when the configuration is updated. You can obtain the updated configuration again in the callback method. This is a passive mode. Another active mode is to obtain the latest configuration every time.

The following are some examples of calls:

1. Basic Data Type Configuration

1.1. Set the Global Path to tradingconfig -- searchconfig -- enableneweditionsearch (without callback ):

 
_ Enableneweditionsearch = _ Configservice. getconfigitemvalue ("Tradingconfig","Searchconfig","Enableneweditionsearch",False); Console. writeline (_ enableneweditionsearch );

Output result:

 

Web Background:

 

Modify the value of the enableneweditionsearch node to true:

Client output result:

 

1.2. Set the Global Path to tradingconfig -- searchconfig -- enableneweditionsearch (with callback ):

 Class Program { Private   Static   Bool _ Enableneweditionsearch = False ; Private   Static Iconfigservice _ Configservice = Null ; Static   Void Main ( String [] ARGs) {adhesiveframework. Start (); _ Configservice = localservicelocator. getservice <iconfigservice> (); _ enableneweditionsearch = _ Configservice. getconfigitemvalue ( "Tradingconfig" , "Searchconfig" , "Enableneweditionsearch" , False , Configitemvalueupdatecallback); console. writeline (_ enableneweditionsearch); console. writeline ( "Press any key to continue ..." ); Console. readkey (); adhesiveframework. End (); console. readkey ();} Public   Static   Void Configitemvalueupdatecallback (configitemvalueupdatearguments ARGs) {_ enableneweditionsearch = _ Configservice. getconfigitemvalue ( "Tradingconfig" , "Searchconfig" , "Enableneweditionsearch" , False , Configitemvalueupdatecallback); console. writeline ( String . Format ( "Configuration item value changed, latest value: {0 }" , _ Enableneweditionsearch ));}}

Change the value of the configuration node to false in the Web Background, and the output result of the client is:

2. List Type Configuration

 List  string  games = _ Configservice. getconfigitemvalue ( "tradingconfig" ,  "searchconfig" ,  "games" ,  New  List  string  >{< SPAN class = "str"> "World of Warcraft ",  "Adventure Island" }, configitemvalueupdatecallback );  foreach  (VAR game  in  games) {console. writeline (game) ;}

Output result:

In the preceding example, a list named games is obtained and a list containing two elements is specified as the default value. When you obtain the node for the first time, a node named games is automatically created, and two subnodes named games_0 and games_1 are automatically created based on the specified default value. The initial value of the node is from the default value. As shown in the Web Management Background:

 

Add an element to the list in the Web Background:

Obtain the list named games again. The output result of the client is:

 

3. dictionary Type Configuration

 
Dictionary <String,Bool> Switchs = _ Configservice. getconfigitemvalue ("Tradingconfig","Searchconfig","Switchs",NewDictionary <String,Bool> {{"Equipment",True},{"Card",False}}, Configitemvalueupdatecallback );Foreach(VAR deInSwitchs) {console. writeline ("Item type: {0}, whether to enable this service: {1 }", De. Key, De. Value );}

Output result:

In the preceding example, the obtained name isSwitchsA boolean dictionary, and a dictionary containing two dictionary items is specified as the default value. The name is automatically createdSwitchsAnd automatically creates a node namedEquipmentAndCardThe dictionary key is used as the node name. The initial value of the node comes from the default value. As shown in the Web Management Background:

 

Next, add a dictionary item to the dictionary in the Web Background:

 

Output result:

 

4. Custom Object Type Configuration

Let's first look at a custom object:

[Configentity (friendlyname = "Storage context configuration" )] Public   Class Storagecontextconfigurationentity {[configitem (friendlyname = "Storage context set" )] Public Dictionary < String , Storagecontextconfigurationitem> storagecontexts {Get; Set ;}} Public   Class Storagecontextconfigurationitem {[configitem (friendlyname ="Storage context name" )] Public   String Name {Get; set;} [configitem (friendlyname = "Data provider name" )] Public   String Providername {Get; set;} [configitem (friendlyname = "Database connection string" )] Public   String Connectionstring {Get; Set ;}}

A custom object can contain a series of fields or attributes. These fields or attributes can be basic data types, dictionaries, lists, or user-defined entities. The hierarchy is unrestricted. A custom object has two restrictions: First, it cannot be defined recursively, that is, the field or attribute type of the custom object cannot be the same as its own type. Second, custom entities cannot define their own constructors. When the client obtains the object type configuration for the first time, it will automatically create the object structure in the background based on the object definition and use the default value for initialization. You can useConfigentityattributeAttributes andConfigitemattributeAttributeFriendlynameAndDescriptionField.

Call example:

_ Defaultconfig = New Storagecontextconfigurationentity {storagecontexts = New Dictionary < String , Storagecontextconfigurationitem >{{ constants. defaultcontextname, New Storagecontextconfigurationitem {name = constants. defaultcontextname, providername = "System. Data. sqlclient" , Connectionstring = "Server =.; database = adhesive; user id = sa; Password = dldna9r + ifkkhxvwszylhw =; trusted_connection = false; persist Security info = true" , }}}; _ Storagecontextconfig = _ Configservice. getconfigitemvalue ( True , "Storagecontextconfigurationentity" , _ Defaconfig config );

 

Let's take a look at the web management background:

Level 1 Configuration:

 

Level 2 Configuration:

 

Level 3 Configuration:

 

Level 4 Configuration:

We can see that the structure of the custom object is automatically created in the background and the default value is set.

 

Next we will add a storage context to the storage context collection:

 

 

We can see that,StoragecontextsA new name namedOrdercontextStorage context. The storage context is an object class, which contains three attributes.StoragecontextsThe set has a default value.DefaultcontextAnd its structure is automatically created during initialization. In this wayOrdercontextdeWhen the object structure is knownDefaultcontextStructure,Then, modify the value of the corresponding field as needed..This is also the benefit of setting the initial value. If no initial value is set, you must manually create an object structure when you add an element to the list or dictionary where the element type is entity. We recommend that you set an initial value to reduce the input workload.

well, the usage of the Configuration Service Module is introduced here. The implementation principles will be detailed in the next article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.