Cuyahoga Study on user configuration data processing

Source: Internet
Author: User

Although the configurationblock introduced by MS is very easy to use, Cuyahoga does not use the MS block. It uses its own encoding to read configuration data. The following is a typical configuration: < Configsections >
< Section Name = "Cuyahogasettings" Type = "Cuyahoga. Core. util. cuyahogasectionhandler, Cuyahoga. Core"   />
</ Configsections >
< Cuyahogasettings >
< Add Setting = "Templatedir" Value = "~ /Templates"   />
< Add Setting = "Imagedir" Value = "~ /Userfiles/image"   />
< Add Setting = "Superuser" Value = "Admin"   />
< Add Setting = "Administratorrole" Value = "Administrator"   />
< Add Setting = "Smtpserver" Value = "Localhost"   />
< Add Setting = "Searchindexdir" Value = "~ /Index"   />
< Add Setting = "Instantindexing" Value = "True"   />
<! -- True/false -->
</ Cuyahogasettings >

The above is a Web application Program The configuration section is defined in configsections. A Configuration section named cuyahogasettings is defined here. The configuration information of this section is determined by the Assembly Cuyahoga. the class Cuyahoga defined in core. corel. util. cuyahogasectionhandler class for processing
The subsequent <cuyahogasettings> configuration is a typical dictionary configuration information.
The following is the source code of cuyahogasectionhandler. Public   Class Cuyahogasectionhandler: namevaluesectionhandler
{
Protected   Override   String Keyattributename
{
Get   {Return "Setting";}
}

Protected   Override   String Valueattributename
{< br> Get { return base . valueattributename ;}
}
}

This class is derived from namevaluesectionhandler. configuration namespace, you will find that it has two attributes defined as virtual: keyattributename and valueattributename. In the cuyahogasectionhandler class, you need to redefine these two attributes because in the cuyahogasettings configuration section, replace the key attribute name that we usually use with setting. That is to say, if the key/value pair in the receivetings type is used as always in the configuration, there is no need to write this custom configuration processing class, but the namevaluesectionhandler class can be directly used to process configuration reading (you can see that a small attribute name has been changed, so much need to be written.Code. How can this problem be solved)
The purpose of overwriting is to return the key attribute name setting in the get accessor of the keyattributename attribute.

Here is an issue:
The configurationsettings object provides us with the deleetaluattribute, which is a namevaluecollection. Therefore, we can use the deleettings [Key] method to access the configuration information of the deleettings section.
In essence, even if it is not the configuration content of the appsettings section, you can also define any configuration section times as follows:

< Configsections >
< Section Name = "Cuyahogasettings" Type = "System. configuration. namevaluesectionhandler, system"   />
</ Configsections >
< Cuyahogasettings >
< Add Key = "Templatedir" Value = "~ /Templates"   />
< Add Key = "Imagedir" Value = "~ /Userfiles/image"   />
< Add Key = "Superuser" Value = "Admin"   />
< Add Key = "Administratorrole" Value = "Administrator"   />
< Add Key = "Smtpserver" Value = "Localhost"   />
< Add Key = "Searchindexdir" Value = "~ /Index"   />
< Add Key = "Instantindexing" Value = "True"   />
<! -- True/false -->
</ Cuyahogasettings >

Use the above configuration to specify to use the default namevaluesectionhandler class to process the configuration of the cuyahogasettings section.
In the code, we can get a configuration as follows: Namevaluecollection cuyahogaconfig = (Namevaluecollection) (configurationsettings. getconfig ( " Cuyahogasettings " ));
// The getconfig static method returns an object. You can convert it to namevaluecollection. In addition, you need to introduce
// Using system. collection. Specialized
Response. Write (cuyahogaconfig [ " Templatedir " ]);

There is also a Config class with only one static method getconfiguration (). This method is actually a call to configurationsettings. getconfig. Therefore, the process is the same as described earlier, so you don't need to talk about it.

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.