So far, the modules we use are all in the same configuration file, which is obviously not flexible. We hope that each functional module can be reflected independently in a configuration file, this is not only easy to manage, easy to expand, but also easy to read and classify. of course, the Enterprise Library considers this problem and provides solutions. This chapter describes how to use the configuration application block to separate modules into independent configuration files.
As in the past, we first raised our requirements and explained them with examples. This is more vivid:
1) create a project that uses the data access module and log processing module of the enterprise database.
2) The configurations of the two modules are saved in different configuration files (data. config and log. config)
3) InProgramCall these two modules
1. Use vs 2008 to create a console application. Then runEntlibconfig.exe, SelectBlocksMenu, clickAdd data settings.And setConnection stringAttribute, you can set a connection string at will, which is only used for testing. After configuration, save itData. config, Save to your console program directory:
2. Open anotherEntlibconfig.exe. SelectBlocksMenu, clickAdd logging settings.The settings are as follows. For details about the Log Module, refer to my previousArticle. After configuration, save itLog. configAnd save it to your console program directory.
3. Open anotherEntlibconfig.exe. SelectBlocksMenu, clickAdd configuration settings.ClickSources+ Button in the upper right corner of the Panel-Add sources-Add file-based configuration Source. Set the createdFile-based configuration SourceOfNameThe property isData configurationsource.File PathSet the property to the previous one.Data. configFile:
4. ClickRedirected sections+ Button in the upper right corner of the Panel-Add redirected sections. SetConfiguration SourceIsData configuration Source:
5. As in step 3 and 4, we can configure the log module, as shown in the following figure:
6. Well, save the configuration file as app. config and save it to the console program directory created earlier. At this time, your program Directory should have the following three files:
7. Create a new console applicationApp. configAdd to the program and add the required DLL file:
Add reference:
Using system. configuration;
Using Microsoft. Practices. enterpriselibrary. Common. configuration;
Using Microsoft. Practices. enterpriselibrary. Data;
Using Microsoft. Practices. enterpriselibrary. Common. configuration. manageability;
Using Microsoft. Practices. enterpriselibrary. Data. configuration;
Using Microsoft. Practices. enterpriselibrary. logging;
8. test:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
UsingSystem. configuration;
UsingMicrosoft. Practices. enterpriselibrary. Common. configuration;
UsingMicrosoft. Practices. enterpriselibrary. Data;
UsingMicrosoft. Practices. enterpriselibrary. Common. configuration. manageability;
UsingMicrosoft. Practices. enterpriselibrary. Data. configuration;
UsingMicrosoft. Practices. enterpriselibrary. logging;
Namespace Test
{
Class Program
{
Static Void Main ( String [] ARGs)
{
// Get settings from the data. config configuration file
Databaseproviderfactory dbfactory =
New Databaseproviderfactory (getfileconfigurationsource ( " Data configuration Source " ));
Database DB=Dbfactory. Create ("Connection string");
Console. writeline (db. connectionstring );
// from log. config configuration file acquisition Settings
logwriter LW = New logwriterfactory (getfileconfigurationsource ( " log configuration source " )). create ();
LW. write ( " 123 " );
}
private static fileconfigurationsource getfileconfigurationsource ( string sourcename)
{< br> // obtain the app. config
Configuration config = configurationmanager. openexeconfiguration (configurationuserlevel. none);
//Get Resource Node Set
Configurationsourcesection Section=
(Configurationsourcesection) config. getsection (configurationsourcesection. sectionname );
//Obtain the resource configuration node of the redirection configuration file
Fileconfigurationsourceelement ELEM=
(Fileconfigurationsourceelement) section. Sources. Get (sourcename );
//Get redirection configuration file resources
Fileconfigurationsource filesource=NewFileconfigurationsource (ELEM. filepath );
ReturnFilesource;
}
}
}
9. Running result:
Open rolling. log and you can see the following content:
So far, we have solved all the requirements mentioned above. We have created the configuration of the database module and the Log Module in data. config and log. config file, and use the app. config is used as a dictionary to query the subordination between each module and the configuration file, which greatly reduces the coupling between each module and simplifies management. now, the series of Microsoft Enterprise Library tutorials have come to an end. you have to prepare for the exam in 6 or 7 months. After the exam, I will try other series of tutorials. Please pay attention to it. Haha