Enterprise Library Step By Step系列(一):配置应用程序块——入门篇
Posted on 2006-02-15 14:57 Piccolo Goo 阅读(456) 评论(0) 编辑 收藏 举报写在前面:
最近准备写Enterprise Library Step By Step的系列文章,对于每一个应用程序块,我都会用入门篇,进阶篇,剖析篇三篇文章去写。 在入门篇里会详细介绍应用程序块的使用步骤,主要是针对新手入门的;进阶篇会介绍一些应用程序块的更深的应用及扩展机制;剖析篇会去分析应用程序块的底层设计和类设计的一些内容。所有的观点都纯属个人理解,有不当之处请大家多多指教。
一.配置应用程序块概述:
几乎每一个应用程序都需要一些配置信息,这些信息可以是简单的数据库连接信息,或者复杂的多块多层次用户信息。如何以及在哪里存储应用程序配置数据是程序员经常面临的问题。
.为应用系统提供了一个通用的配置管理解决方案,可方便灵活地从各种存储中读写配置信息配置信息
.为读写配置信息提供了简单的接口
.支持不同的配置存储
.支持配置信息的加密
.支持配置文件的动态更新
.支持复杂的配置对象
Config Config Block Block 的使用场景:
.应用系统需要在运行时读写复杂的配置信息
.应用系统需要在配置文件中保存敏感数据(比如密码)
.设计应用系统时能帮助开发人员进行正确的配置读写
.应用系统部署后系统管理员能有一个好的工具修改XM 配置文件
二.使用步骤:
假设已经新建好的项目中,并且已经存在Web.config或App.config配置文件。
第一步:建立自定义配置数据类:
该类根据项目的实际情况建立,需要注意的是该类必须是可序列化的(XMLSerializer),并且可以任意复杂
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
43
![](/Images/OutliningIndicators/None.gif)
第二步:用Enterprise Library Configuration配置应用程序:
1.运行Enterprise Library Configuration 工具,选择File | Open Application 打开App.config文件
2.右击Application并选择New | Configuration Application Block,创建一个配置应用程序块
3.右击 Configuration Application Block 并选择 New | Configuration Section,创建一个配置区,注意对于每一个配置区都要设置一个Storage Provider 和一个Transformer。
4.重命名configuration section为EditorSettings
5.右击EditorSettings 并选择New | XML File Storage Provider,设置它的Storage Provider
6.在FileName输入框中输入EditorSettings.config,路径相对于App.config
7.在Enterprise Library Configuration 工具里面选择New | XML Serializer Transformer。指定它的Transformer
8.选择File | Save All命令保存全部
9.此时在VS中选择项目|显示所有文件,就会发现在项目中多出了一个名为EditorSettings.config的配置文件
10.在项目中选择 属性|生成事件|生成后事件命令行 输入如下内容:
![](/Images/OutliningIndicators/None.gif)
第三步:用代码访问配置数据:
1.在项目中添加如下两个引用
- Microsoft.Practices.EnterpriseLibrary.Common.dll,
- Microsoft.Practices.EnterpriseLibrary.Configuration.dll.
2.并在.CS代码中添加:
![](/Images/OutliningIndicators/None.gif)
3.写配置信息:注意在写配置信息时是整个配置区被覆盖(无合并),另外,元配置文件必须包含该配置区的定义。
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
4.读配置信息:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
入门篇就到这里了,下面我会尽快写配置应用程序块的进阶篇及剖析篇的:)