2.4 [Enterprise Library] add application code

Source: Internet
Author: User
Tags configuration settings
Copyright (c) 2007 Dorian Deng. Signature Sharing is not commercially available.
Reprinted please indicate the source: http://blog.csdn.net/doriandeng/archive/2007/09/20/1793768.aspx

 

Enterprise Library contains applicationsProgramBlock source code. You can include the source code in the application or compile the Application Block and use the compiled assembly. For details about how to compile an Application Block, see compile an Application Block. Before using the Enterprise Library assembly in an application, you must add it to the corresponding application block assembly andCommonAssembly reference. Write an applicationCodeNote the protocols and object creation modes of the Enterprise Library namespace.

 

Reference an Enterprise Library assembly

Before building an application with a mixed Enterprise Library Application Block, you must add,CommonAndObjectbuilderAssembly reference. This is to prepare applications for using the Enterprise Library. Be careful when selecting an assembly, whether to use Microsoft strong-naming assembly, non-strong-naming assembly, or a customized Enterprise Library Assembly set.

Prepare applications
    1. Add a reference to the application block assembly. In Visual Studio, right-click the project node in the solution browser, and then clickAdd reference, ClickBrowseTag, and then find the location of the application block assembly (scriptCopyassemblies. batYou can copy all application blocksBinSubdirectory), select the assembly, and then clickOKAdd reference. For example, to reference the cache application block assembly, browseBinSubdirectory, select assemblyMicrosoft. Practices. enterpriselibrary. caching. dllAnd then clickOK.

    2. Use the same process to add an Enterprise LibraryCommonAssemblyMicrosoft. Practices. enterpriselibrary. Common. dllAnd pairObjectbuilderAssemblyMicrosoft. Practices. objectbuilder. dll.

Enterprise Library namespace

All application blocks comply with the same namespace conventions. All code of the Enterprise Library is included in the root namespace.Microsoft. Practices. enterpriselibrary.

All application blocks are included in their own namespaces under this root. For example, use the Security Application BlockMicrosoft. Practices. enterpriselibrary. SecurityNamespace.

Complex application blocks may contain sub-namespaces, but it is only necessary to improve readability. For example, use the SQL server section of the Data Access Application BlockMicrosoft. Practices. enterpriselibrary. Data. SQLNamespace.

Add an element from the application block at the top of the source code fileUsingStatement (C #) orImportStatement (Visual Basic. net ). For example, if you want to use an element from the cache application block without a fully precise element reference, use the following at the top of the source file:UsingStatement (C #) orImportStatement (Visual Basic. net ).

Example 2.1. C #

Using Microsoft. Practices. enterpriselibrary. caching; using Microsoft. Practices. enterpriselibrary. caching. expirations;


Example 2.2. Visual Basic. net

 
Imports Microsoft. Practices. enterpriselibrary. cachingimports Microsoft. Practices. enterpriselibrary. caching. expirations


Note:

For visual basic projects, the project designer's reference page manages referenced and imported namespaces. To access the reference page, select a project node in the solution browser, and thenProjectClickAttributeWhen the project designer appears, selectReferenceLabel.

Create an Application Block object

The Enterprise Library kernel supports object creation. configuration information is required for object creation. Figure 1 shows how these objects are built using the Enterprise Library kernel.

Figure 2.1. the application block factory uses the Enterprise Library Kernel



Use static factory Method

The Enterprise Library application block contains static factory methods that can be used to create Application Block objects. These static methods implement the factory mode to instantiate the provider. This means that if applications use these static factory methods, they do not need any information about the specific implementation of the provider during creation. The following code shows that the application uses data to access the Application BlockDatabasefactoryClass to createDatabaseObject.

Example 2.3. C #

 
Database DB = databasefactory. createdatabase ("northwind ");


Example 2.4. Visual Basic. net

 
Dim dB as database = databasefactory. createdatabase ("northwind ")


DatabaseA class is an abstract class and provides a common interface for the implementation of the provider. Static MethodCreatedatabaseReturns an object of a specific implementation type (for example,SqldatabaseObject .) The type of the returned object is determined by the application configuration information.

The static method uses the configuration information from the default configuration source. You can use the configuration console to define the default configuration source for the application configuration. If no configuration source is defined, the static factory method uses the system configuration source. This means that the configuration information must be saved in the application configuration file (App. config or web. config ).

The configuration console saves information for the configuration source to the application configuration file. The following XML shows a configuration section in the application configuration file, which defines two configuration sources.

 
<Enterpriselibrary. configurationsource selectedsource = "systemsource"> <sources> <Add name = "filesource" type = "Microsoft. practices. enterpriselibrary. common. configuration. fileconfigurationsource, Microsoft. practices. enterpriselibrary. common "filepath =" test.exe. config "/> <Add name =" systemsource "type =" Microsoft. practices. enterpriselibrary. common. configuration. systemconfigurationsource, Microsoft. practices. enterpriselibrary. common "/> </sources> </enterpriselibrary. configurationsource>
Precautions

Here are some additional points about the configuration Source:

    • The static factory method uses the application configuration file to determine the default configuration source, which means that there must be an application configuration file when using the static factory method.

    • If the application configuration file does not contain <Enterpriselibrary. configurationsource>, Enterprise Library uses the system configuration source. This means that the application block configuration must be saved to the application configuration file.

    • All static factory methods use the default configuration source, which means that the configuration information of all application block objects built using the static factory method is saved in the same location.

Use the provider factory Method

A static factory method creates an appropriate configuration source object and then constructs a provider factory object. You can also use the provider factory object in the application code.

Use the provider factory to create the provider
    1. Create a configuration source object.

    2. Create the provider factory object and pass the configuration source object.

    3. Use the provider factory object to create the provider.

The following sections describe these steps in detail.

Create configuration Source

AvailableConfigurationsourcefactoryClass to create a configuration source object.CreateThe method accepts a string parameter that identifies the configuration source name. This configuration source must be defined in the application configuration file (if the configuration source is not defined, an exception is triggered ). This means thatConfigurationsourcefactoryYou must have an application configuration file. The following code illustrates how to useConfigurationsourcefactoryCreate a configuration source.

Example 2.5. C #

 
Iconfigurationsource source = configurationsourcefactory. Create ("filesource ");


Example 2.6. Visual Basic. net

 
Dim filesource as iconfigurationsource = configurationsourcefactory. Create ("filesource ")


You can also directly create a configuration source object. The configuration source constructor accepts the information required by the configuration source. The following code describes how to directly create a configuration source.

Example 2.7. C #

 
Fileconfigurationsource filesource = new fileconfigurationsource (@ "productapplication. config"); systemconfigurationsource systemsource = new systemconfigurationsource ();


Example 2.8. Visual Basic. net

 
Dim filesource as fileconfigurationsource = new fileconfigurationsource ("productapplication. config") dim systemsource as systemconfigurationsource = new systemconfigurationsource ()


Note:

Enterprise Library containsSqlconfigurationsourceConfiguration example of the source provider, which reads configuration settings from the SQL database using the Data Access Application Block. The Section used by this provider must be derived fromSerializableconfigurationsectionClass.

Configuration Modification notification

All configuration source classes are implementedIconfigurationsourceInterface, which allows the application code to subscribe to configuration change notifications. In the Enterprise Library, only log application blocks are registered to receive notifications of configuration changes.

Create a provider Factory

The constructor of the provider factory accepts a configuration source object. The provider factory object uses this configuration source to read configuration information for the provider object.

Example 2.9. C #

 
Systemconfigurationsource systemsource = new systemconfigurationsource (); databaseproviderfactory factory = new databaseproviderfactory (systemsource );


Example 2.10. Visual Basic. net

 
Dim systemsource as systemconfigurationsource = new systemconfigurationsource () dim factory as databaseproviderfactory = new databaseproviderfactory (systemsource)



Use the provider factory to create the provider

You can use a single provider factory to construct multiple providers for implementation. You can also use multiple provider factory objects to create Application Block objects from multiple configuration sources.

The following code uses different configuration source objects to create twoDatabaseproviderfactoryObject,DatabaseproviderfactoryObjectFactory1Slave FileProductapplication. configRead configuration information,DatabaseproviderfactoryObjectFactory2Read the configuration information from the application file.

Example 2.11. C #

Fileconfigurationsource filesource = new fileconfigurationsource (@ "productapplication. config "); databaseproviderfactory factory1 = new databaseproviderfactory (filesource); database db1 = logical (); systemconfigurationsource systemsource = new systemconfigurationsource (); databaseproviderfactory factory2 = new databaseproviderfactory (systemsource ); database DB2 = factory2.create ("northwind ");


Example 2.12. Visual Basic. net

Dim filesource as fileconfigurationsource = new fileconfigurationsource ("productapplication. config ") dim factory1 as databaseproviderfactory = new databaseproviderfactory (filesource) dim db1 as database = partition () dim systemsource as systemconfigurationsource = new systemconfigurationsource () dim factory2 as partition = new databaseproviderfactory (systemsource) dim DB2 as database = factory2.create ("northwind ")



Usage tips

Here are some key points about the provider Factory:

    • A provider factory is bound to a single configuration source. The configuration source of the provider factory object cannot be changed after construction, which means that the configuration settings of each provider object are read from the same configuration source by the instance.

    • The following application block provider factory always returns the same named provider instance:

      • Cachemanagerfactory

      • Securitycacheproviderfactory

      • Exceptionpolicyfactory

Directly construct the Application Block object

Sometimes the configuration information of the application scenario does not exist in a configuration source. For example, the system dynamically creates configuration information from user input data. In these cases, you can directly construct the Application Block object and pass necessary configuration information to the constructor.

The factory method returns a common provider interface. When constructing an Application Block object, you must use the appropriate constructor parameters to construct a specific provider Implementation type and pass necessary parameters to the constructor. Parameters can be created by a factory or constructor. The following code constructs a Data Access Application Block:SqldatabaseObject.

Example 2.13. C #

 
Sqldatabase DB = new sqldatabase (@ "Server = (local) \ sqlexpress; database = entlibquickstarts; Integrated Security = true ");


Example 2.14. Visual Basic. net

 
Dim dB as sqldatabase = new sqldatabase ("Server = (local) \ sqlexpress; database = entlibquickstarts; Integrated Security = true ")


Note:

the Enterprise Library contains the code that makes the measurement available. If you create an application or block object directly, the measurement does not work for those objects. However, in most cases, you can bind a suitable measurement listener to the application provider. For more information about metrics and providers, see metrics.

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.