本系列的第四篇文章我给大家做了关于BlogEngine.Net全局配置的分析。在这篇文章里我将会对BlogEngine.Net中比较经典的部分DataStore做一个简单的分析,这个DataStore主要完成了BlogEngine.Net三大扩展特性(Extension,Widget,Theme)的数据存储部分,它提供了一个扩展特性统一的存储模型。DataStore是建立在Provider存储(本系列第三篇文章中介绍过)的上层,本文研究的内容会和后面讲到的扩展部分的文章有一定联系,希望大家继续关注。
BlogEngine.Net三大扩展特性大多数都是由开发者完成的,也就是说大多数都是后加入到BlogEngine.Net中的,所以这些存储必须单独完成,而且对于模型的统一性很重要。既然是由开发者开发,那么这些标准就显得格外重要,这样开发出的部分才可以与系统中的部分很好的集成。这里所说的DataStore就是指BlogEngine.Core.DataStore空间下的类型。
BlogEngine.Net中的DataStore如何设计的
先看一幅继承图吧:
从上图我们可以看到ExtensionSettings和WidgetSettings是SettingBase的直接子类。ExtensionSettings主要是为Extension而对SettingBase的实现,WidgetSettings是为Widget而对SettingBase的实现,对于Theme整个BlogEngine.Net中目前没有涉及到。
ExtensionSettingsBehavior,StringDictionaryBehavior,XMLDocumentBehavior这三个类都实现了ISettingsBehavior接口,而ISettingsBehavior只有加载配置和保存配置两个方法声明。此外ExtensionType是一个枚举用来表示扩展的类型。
SettingBase也有加载和保存配置的两个方法,此外SettingBase中还有一个对于ISettingsBehavior的引用,而SettingBase的加载和保存内部也是通过ISettingsBehavior的保存和加载完成的,这好像是一个桥模式。由以上分析我们可以知道对于Extension的数据加载和保存是通过ExtensionSettings完成的,具体实现是由ExtensionSettingsBehavior来做,ExtensionSettingsBehavior的内部使用BlogService调用Provider完成。对于Widget的数据的加载和保存是通过WidgetSettings完成的,具体实现由StringDictionaryBehavior来做,StringDictionaryBehavior的内部也是使用BlogService调用Provider完成,注意SerializableStringDictionary继承自StringDictionary并实现了IXmlSerializable来完成字典对象的序列化和反序列化。
对于Extension的存储的具体信息通过代码我们可以看到是一个Object,这个Object的格式是由ExtensionManager(这个在后续的相关文章也会讲到)来确定的,也主要是为ExtensionManager服务的。
<?xml version="1.0" encoding="utf-8"?>
<ManagedExtension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="Smilies">
<Version>1.3</Version>
<Description>Converts ASCII smilies into real smilies in the comments</Description>
<Author>BlogEngine.NET</Author>
<AdminPage />
<Enabled>true</Enabled>
<ShowSettings>true</ShowSettings>
</ManagedExtension>
WidgetSettings的存储的具体信息实际上是一个字典,字典项代表具体信息。
< SerializableStringDictionary >
< SerializableStringDictionary >
< DictionaryEntry Key ="content" Value ="Something about the author " />
</ SerializableStringDictionary >
</ SerializableStringDictionary >
XMLDocumentBehavior主要是为了配置WidgetZone而设计的,它实际上是一个Widget的集合的配置,在页面加载时会读取这些配置来决定怎样显示相应的Widget。
除此之外还需要我们注意的一个地方就是配置对象在XML和数据库的存储表示,实际上它们内部的信息都是一段XML,相应的对于这两种的数据访问的结果处理也是不一致的(我觉得这个地方设计的不是很好,因为这一层似乎对于Provider有些依赖关系代码,参见XMLDocumentBehavior.cs,不过还好毕竟是实现依赖于实现嘛,可能也没想出更好的办法)。
客户端的使用
Extension(App_Code\ExtensionManager\Manager.cs):
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
StringDictionary(widgets\TextBox\edit.ascx.cs):
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
XMLDocument(App_Code\Controls\WidgetZone.cs):
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
对于客户端的使用部分涉及到了Extension,Widget部分等,这些内容会在后续的文章中进行详细说明。
总结
1.桥模式的使用
2.统一的配置模型
3.对于XML存储和数据库存储的处理方式不同
面向对象的设计原则真是太经典了
上一篇:BlogEngine.Net架构与源代码分析系列part7:Web2.0特性——Pingback&Trackback
下一篇:BlogEngine.Net架构与源代码分析系列part9:开发扩展(上)——Extension与管理上的实现
【来源】: http://thriving-country.cnblogs.com/
本文版权归作者和博客园共同所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。