Share communityserver (3)-provider

Source: Internet
Author: User

Provider implementation

Provider is the second embodiment of configuration-light programming. We all know that hierarchical design is very important. We also know that the maximum flexibility should be achieved on the basis of maintaining structural stability. The so-called irrelevant nature of a specific environment. For example, some functions can be abstracted and independently analyzed during design. For example, we need to make the database irrelevant. We need to design all the data access to the database for independent design, and extract the corresponding functional abstraction through analysis. Then we can use the actual database, design appropriate data drivers for specific databases so that different data drivers can be switched flexibly through configuration files. The provider is defined in csconfiguration. CS. This class is only an entity class that stores providers. It is used to describe specific feature providers. Data is analyzed from xmnode, mainly including name (provider name) and extensiontype (extension type) type (type string). In addition, it can adapt to more settings. The provider also adds a value pair named attributes to collect and obtain the value through the key name. Providers in Cs are obtained using the static instance method. In the static function named instance, the provider proceeds from the string to a class instance, which of course is benefited from the reflection function of. net. We will analyze the details from an instance: Analyze commondataprovider. CS ...... # Region instance Private Static commondataprovider _ defaultinstance = NULL; static commondataprovider () {createdefacommcommonprovider (); // The static constructor is constructed before any static method of the class is called for the first time }...... Private Static void createdefacommcommonprovider () {csconfiguration Config = csconfiguration. getconfig (); // obtain the global configuration class. In the previous article, we analyzed provider sqlforumsprovider = (provider) config. providers [commondataprovidername]; // gets the provider named "commondataprovider" from the provider set (commondataprovidername is a data member of the private string type, note that the sqlforumsprovider type is an object information class that describes logical components and is not a class that actually provides functions. It seems that the product manual only describes the product, not the product. The specific provider instance here is a product manual, and the lower _ defaultinstance is the product that actually provides the function _ defaultinstance = dataproviders. createinstance (sqlforumsprovider) as commondataprovider; // create a function instance by reading the provider (product manual) information and performing a reflection activity.} # Endregion ...... We need to note that commondataprovider is an abstract class (public abstract class commondataprovider) here. It must be abstracted by specific means. For data-type providers, CS uses a sealed class dataproviders to instantiate createinstance. The details are as follows: public static object createinstance (provider dataprovider) {string connectionstring = NULL; // dataprovider. attributes ["connectionstring"]; string databaseowner = NULL; // dataprovider. attributes ["databaseowner"]; getdatastoreparameter S (dataprovider, out connectionstring, out databaseowner); // get the type = type. getType (dataprovider. type); // obtain the type object newobject = NULL of the class instance through reflection; If (type! = NULL) // type {newobject = activator. createinstance (type, new object [] {databaseowner, connectionstring}); // obtain the instance through reflection} If (newobject = NULL) // if we can not create an instance, throw an exception providerexception (dataprovider. name); Return newobject;} basically, the provider mode uses. net reflection function. the XML node description in congfi creates the final instance by using the reflection capability. Components in other provider configuration nodes are implemented in a similar way. Dataproviders is a tool that turns the provider description into a specific functional component instance. Generally, functions that can be set using the provider mode in the communityservercomponents project are described using the abstract class, and then described in other projects (for example, the data access provider is a separate project) but it inherits the capabilities of this type of instance. The functions of all providers are designed in the provider directory of communityservercomponents. applicationkeyprovider provides the function of converting URL query strings to application keys applicationkey. To change the ing rules of applicationkey, You can override and modify classes, modify comuunityserv. config. Chinesetokenizeprovider provides an interface for Chinese word segmentation, in which the search requires word segmentation, while many independent algorithms for word segmentation can be replaced by a dedicated algorithm through provider settings. The class is an abstract class. The method you want to implement is actually only one public abstract string [] chinesetokenize (string input); that is, the input string, the string Word Array commondataprovider is basically provided for analysis based on the word segmentation algorithm. This type is quite large and is roughly grouped according to the database table entity table, basically, all database object operations can be defined here. By inheriting this class, you can implement specific versions for different databases. If the access-oriented version is implemented, the class should be inherited and implemented, and the communityserver should be modified. the relevant provider configuration section of config provides definitions of email queue operations, which must be overwritten by queueemail dequeueemail deletequeuedemail and other operations. Emailtemplateprovider provides the provider base class for mail template processing. It mainly defines the template operation as gettemplate and provides basic email template functions to facilitate post-class implementation. Gettemplate requires that you use a special template to obtain the specific body of the email after performing the template operation. Extensionmodule defines the definition of the CS plug-in template, which is replaced by the provider mode. For example, if you want a CMS module, you only need to implement the specific methods init and processrequest to implement plug-in replacement. Searchprovider provides the definition of the replaceable function for searching in the system. Urlrewriteprovider provides the URL rewriting function. What should we do if we need to flexibly process a specific function? Let's analyze the word segmentation provider. First, we need to create an abstract class to define the basic functional requirements of providers, such as chinesetokenizer. CS defines the word segmentation function. The input string returns the phrase public abstract string [] chinesetokenize (string input). Then, we integrate this class to implement its abstract class function. Specifically, we can add an external class chinesetokenizer: chinesetokenizeprovider {public chinesetokenizer () {}# Region member public override string [] chinesetokenize (string input) {try {somewordseg. wordseg dictseg = new wordseg (); Return dictseg. segment (input); // word segmentation} catch (exception) {// exception} return input. split ("". tochararray () ;}# endregionsomewordseg is a word segmentation component that implements Chinese segmentation. Word. The word segmentation of chinesetokenizeprovider is actually provided by this component. After implementing the specific provider function (place the specific implementation class in the searchbarrel project), we need to register the provider, specifically in the communityserver. add a node in config, named <Add name = "chinesetokenizeprovider" type = "communityserver. components. searchbarrel. chinesetokenizeprovider, communityserver. components. searchbarrel "/> communityserver. components. searchbarrel. chinesetokenizeprovider is the class name, communityserver. components. searchbarrel is the name of a collection. In this way, you can know that such a word splitting component exists globally, but you do not need to know the specific implementation. Then, when you need the word splitting function, perform the following steps to obtain the word splitting function :.... String [] words = chinesetokenizeprovider. instance (). chinesetokenize (searchterms); the instance finally instantiates the provider according to the following logic :... Csconfiguration Config = csconfiguration. getconfig (); provider chineseproviders = (provider) config. providers ["chinesetokenizeprovider"]; _ defaultinstance = activator. createinstance (type. getType (chineseproviders. type) as chinesetokenizeprovider ;... The following is a simple summary: 1. Abstract An abstract class that can be set up according to the functional requirements of the provider, and define the instance function in the abstract base class. A specific provider instance can be obtained through the instance function, this is the so-called contract programming. Contract programming is not necessarily implemented through interfaces. 2. Implement a specific implementation class to implement specific functions. 3. register the provider project on the communityserver. Config 4. The instance method of the abstract class is used to instantiate a specific component where the provider is needed, achieving componentization and customized component collaboration. Such a design will rationally define the classification boundary, and combine flexibility and practicality. That is to say, "reconfiguration, light programming"

 

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.