Code-level analysis of enterprise database object Creation

Source: Internet
Author: User

I didn't intend to write this analysis, but when I analyzed the cache components, I found that all the components of the Enterprise Library were created in one way, which made me curious, so I decided to see how he created the correct object through the configuration file.

 

 

Here there are four important interfaces. In a word, the configurator parses the information in the source (iconfigurationsource) in a specific Parsing Method (ityperegistrationsprovider, it is finally released as an iservicelocator ).

 

1. Service Positioner

 

We can see from the figure that the so-called enterprise library service locator is actually an encapsulation of the dependency injection framework unity. It parses the interface and its implementation class matching through the getinstance <t> method.

 

Ii. Configurator

 

The Configurator is a process of completing type registration. As shown in the figure, registration information in the Unity container is registered through unitycontainerconfigurator.CodeIs:

 

Container. registertype (registrationentry. servicetype, registrationentry. implementationtype, registrationentry. Name, createlifetimemanager (registrationentry), getinjectionmembers (registrationentry ));

 

In essence, it registers the type with the unity container. After the registration is complete, you can provide services through unityservicelocator.

 

3. Configure the source

Our configuration information is generally written in the system configuration file, while the iconfigurationsource interface is responsible for reading the relevant information from the file. as you can see, the real implementation class is the systemconfigurationsource class. there is such a code in it:

Return Appdomain. currentdomain. setupinformation. configurationfile;

This also determines that the system can only read information from the default configuration file.

 

Iv. Resolution Method

 

As you can see, parsing is to analyze the configuration file into a ing table between interfaces and implementation classes. typeregistrationprovidersconfigurationsection, typeregistrationproviderelementcollection and typeregistrationproviderelement correspond to the configuration section in the configuration file, configuration element integration and configuration element, and blocksectionnames class defines the configuration section name used by the Enterprise Library. focus on the createtyperegistrationsproviderfactory method of the configurationbasedtyperegistrationsproviderlocators class. The Code is as follows:

 

1 Typeregistrationprovidersconfigurationsection section = configurationsource. getsection (typeregistrationprovidersconfigurationsection. sectionname) As Typeregistrationprovidersconfigurationsection;
2 If (Section = Null )
3 {
4 Section = New Typeregistrationprovidersconfigurationsection ();
5 }
6
7 Foreach (Typeregistrationproviderelement In Section. typeregistrationproviders)
8 {
9 If (! String . Isnullorempty (typeregistrationproviderelement. sectionname )&&
10 ! String . Isnullorempty (typeregistrationproviderelement. providertypename ))
11 {
12 Throw New Configurationerrorsexception (
13 String . Format ( " Type registration provider settings '{0}' cannot declare both sectionname and providertype attributes " ,
14 Typeregistrationproviderelement. Name ));
15 }
16 If (! String . Isnullorempty (typeregistrationproviderelement. sectionname ))
17 {
18 Yield Return New Configsectionlocator (typeregistrationproviderelement. sectionname, recyclingeventsource );
19 }
20 Else If (! String . Isnullorempty (typeregistrationproviderelement. providertypename ))
21 {
22 Yield Return New Typeloadinglocator (typeregistrationproviderelement. providertypename, recyclingeventsource );
23 }
24 }

In Row 4, a new typeregistrationprovidersconfigurationsection object is created. When the typeregistrationproviders attribute is returned, the default node set used by the library is created. The Code is as follows:

Public Typeregistrationproviderelementcollection ()
{
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. cachingtyperegistrationprovidername, sectionname = blocksectionnames. caching });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. cryptographytyperegistrationprovidername, sectionname = blocksectionnames. cryptography });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. exceptionhandlingtyperegistrationprovidername, sectionname = blocksectionnames. exceptionhandling });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. instrumentationtyperegistrationprovidername, sectionname = blocksectionnames. instrumentation });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. loggingtyperegistrationprovidername, sectionname = blocksectionnames. Logging });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. policyinjectiontyperegistrationprovidername, sectionname = blocksectionnames. policyinjection });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. securitytyperegistrationprovidername, sectionname = blocksectionnames. Security });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. dataaccesstyperegistrationprovidername, providertypename = blocksectionnames. dataregistrationproviderlocatortype });
Baseadd ( New Typeregistrationproviderelement () {name = typeregistrationprovidersconfigurationsection. validationtyperegistrationprovidername, providertypename = blocksectionnames. validationregistrationproviderlocatortype });
}

 

The following code reads the information of these nodes. Lines 18th and 22nd of the Code create different locators in different situations.

So far, I only know which configuration sections will be used by the enterprise database. For the specific analysis work, let's go back to the unitycontainerconfigurator class:

1 Protected Override Void Registerallcore (iconfigurationsource configurationsource, ityperegistrationsprovider rootprovider)
2 {
3 Enterwritelock ();
4 Try
5 {
6 Foreach ( VaR Registration In Rootprovider. getregistrations (configurationsource ))
7 {
8 Register (registration );
9 }
10 }
11 Finally
12 {
13 Exitwritelock ();
14 }
15 }

You can see that the ityperegistrationsprovider interface is complete, and the typeregistrationsprovider abstract class implements this interface. typeloadinglocator and configsectionlocator inherit this abstract class. Just read one:

Public Override Ienumerable <typeregistration> getregistrations (iconfigurationsource configurationsource)
{
Return Getregistrationsinternal (configurationsource, (p, CS) => P. getregistrations (CS ));
}

1 Private Ienumerable <typeregistration> getregistrationsinternal (iconfigurationsource configurationsource,
2 Func <ityperegistrationsprovider, iconfigurationsource, ienumerable <typeregistration> registrationsaccessor)
3 {
4 Ityperegistrationsprovider provider = Null ;
5 Configurationsection section = configurationsource. getsection (name );
6 If (Section! = Null )
7 {
8 Provider = Section As Ityperegistrationsprovider;
9 }
10
11 If (Provider! = Null )
12 {
13 Return Registrationsaccessor (provider, configurationsource );
14 }
15 Return Enumerable. Empty <typeregistration> ();
16 }

First, it gets the specified node from the source, and then converts it to ityperegistrationsprovider, and then calls its getregistrations method to obtain the real configuration information. for example, after obtaining the cache configuration using getsection, it is actually the cachemanagersettings class. It implements the ityperegistrationsprovider interface and calls its getregistrations method to parse the cache configuration.

Is it messy? I think so. I don't know why it's not just parsing a configuration file. Why is it so complicated? Isn't it possible to reflect the Nb and value of the enterprise database without turning a few corners ?! I personally think the most messy is the use of the ityperegistrationsprovider interface. the unitycontainerconfigurator class calls the ityperegistrationsprovider interface and actually calls the compositetyperegistrationsproviderlocator class. this class internally maintains a set of ityperegistrationsprovider interfaces. The getregistrations method is the same as the example. This set calls the respective getregistrations methods one by one. in fact, this set contains the typeloadinglocator class and configsectionlocator class, so the methods of these two classes are called. the two classes then obtain the source again, convert the obtained result to the ityperegistrationsprovider interface, and then call the converted getregistrations method. in this step, we can take Information! Is it so complicated ?!

 

ReferenceArticle:

Microsoft enterprise database 5.0 Study Notes

 

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.