如果大家从官方下载2.0正式版的DLL程序集中会从里面找到discuz.config这个DLL文件。通过
reflector 反射加载这个程序集后,后从中找到一些从类名称上非常相似且有一定规律(格式)的类。
它们是:
全局配置类
GeneralConfigFileManager.cs
GeneralConfigInfo.cs
GeneralConfigs.cs
Emial配置类
EmailConfigFileManager.cs
EmailConfigInfo.cs
EmailConfigs.cs
基本配置类
BaseConfigFileManager.cs
BaseConfigInfo.cs
BaseConfigInfoCollection.cs
BaseConfigs.cs
相册配置类
AlbumConfigFileManager.cs
AlbumConfigInfo.cs
AlbumConfigs.cs
聚合配置类
AggregationConfig.cs
AggregationConfigFileManager.cs
AggregationConfigInfo.cs
空间开通配置类
SpaceActiveConfigs.cs
SpaceActiveConfigFileManager.cs
SpaceActiveConfigInfo.cs.cs
可以看到,这些类基本上是以三个一组,共六组。而这六组类中的...FileManager.cs与Defau-
ltConfigFileManager.cs的关系如下图所示:
从类图上看,DefaultConfigFileManager.cs是基类,而GeneralConfigFileManager.cs,Emai-
lConfigFileManager.cs,BaseConfigFileManager.cs,AlbumConfigFileManager.cs,Aggregatio-
nConfigFileManager.cs, SpaceActiveConfigFileManager.cs分别派生自DefaultConfigFileManager。
而DefaultConfigFileManager本身也定义了几个属性和方法以便于子类调用或重写(如SaveConfig)。
而DefaultConfigFileManager的定义如下:
如果您一直关注我们的这个产品,就会从中发现在1.0正式版时,这些类(或实现这些功能的类)
还是零星分面在discuz.forum和discuz.forumpage(2.0正式版改名为discuz.web.dll)等几个DLL文件
中。后来因为考虑软件架构和出于统一配置管理的需求,产品小组将这些类重构到了discuz.config,
从此这些类有了一个“新家”。
为了便于开发和统一接口调用,我们又创建了两个接口类,就是IConfigFileManager.cs,ICon-
figInfo.cs,它们的声明如下(相关说明见注释):
IConfigFileManager.cs
IConfigInfo.cs
大家可以看到,其中的 IConfigInfo接口没有任何属性和方法,是一个“空接口”,这主要是为
了提供统一的向上转型需要。当然,如果以后有需要还是会添加一些方法和属性的。
为了减少篇幅,下面只对其中的 GeneralConfig进行相应介绍,一是因为它是核心的配置类,有
关它的使用在项目中不胜枚举。另外它也是这个项目中比较标准的“宝贝”,从一开始,只要有新的
功能往往都会对这个配置类有所“关照”。下面就是通过反射后得到的代码,相关的说明见注释即可:
论坛全局配置管理类(GeneralConfigFileManager):
论坛全局配置描述类(GeneralConfigInfo):
论坛全局配置类(GeneralConfigs):
通过对general系列配置类的介绍,再看其它的配置类会很清楚了。这里就不再多做解释了,相信2.0开源后
大家看到源码和注释后会有所感悟的。
另外就是之前因为用到了“序列化”,所以可能会出现所谓的“内存碎片”的问题,详见下面链接:
而微软官方所推荐的方式如下(http://support.microsoft.com/kb/886385/en-us):
1. Create one instance of the XmlSerializer class, and put that instance in the cache by using
the caching APIs. For example, for a .dll file that is named HighSchool, the following code
caches one instance of the XmlSerializer class:
2 extraTypes, root, "http://www.microsoft.com");
3 Cache["HighSchoolSerializer"] = mySerializer
2. Use the instance of the XmlSerializer class that you put in the cache instead of creating a
new XmlSerializer object every time.
Use the following XmlSerializer class constructors. These class constructors cache the
assemblies:
In the .NET Framework version 1.0
2
In the .NET Framework version 1.1
2 public XmlSerializer(Type type, string defaultNamespace);
3
3. Declare the XmlSerializer object to be a static member of a class.
(这种方式是我们项目中所采用的方案)
如果大家对这个话题感兴趣,恰恰园子里就有朋友做这方面的探索,链接如下:
http://www.cnblogs.com/lixiong/archive/2007/10/26/938430.html
好了,今天的内容到此就要结束了。希望大家能够支持和关注我们这个本土开源项目。祝大家工作顺利,
生活幸福,谢谢大家:)
关键字:discuz,discuzNT,discuz.config,config,XmlSerializer,序列化,配置类,代震军,daizhj