BlogEngine.net---BlogSettings

对于博客配置,BlogEngine由一个全局配置类BlogSettings来操作,关于BlogSettings,它采用了单例设计

复制代码
singleton
 1 ///<summary>
2 /// The blog settings singleton.
3 ///</summary>
4 ///<remarks>
5 /// This should be created immediately instead of lazyloaded. It'll reduce the number of null checks that occur
6 /// due to heavy reliance on calls to BlogSettings.Instance.
7 ///</remarks>
8 private static readonly Dictionary<Guid, BlogSettings> blogSettingsSingleton = new Dictionary<Guid, BlogSettings>();
9
10 ///<summary>
11 /// Gets the singleton instance of the <see cref = "BlogSettings" /> class.
12 ///</summary>
13 ///<value>A singleton instance of the <see cref = "BlogSettings" /> class.</value>
14 ///<remarks>
15 ///</remarks>
16 public static BlogSettings Instance
17 {
18 get
19 {
20 return GetInstanceSettings(Blog.CurrentInstance);
21 }
22 }
23
24 ///<summary>
25 /// Returns the settings for the requested blog instance.
26 ///</summary>
27 public static BlogSettings GetInstanceSettings(Blog blog)
28 {
29 BlogSettings blogSettings;
30
31 if (!blogSettingsSingleton.TryGetValue(blog.Id, out blogSettings))
32 {
33 lock (SyncRoot)
34 {
35 if (!blogSettingsSingleton.TryGetValue(blog.Id, out blogSettings))
36 {
37 // settings will be loaded in constructor.
38 blogSettings = new BlogSettings();
39
40 blogSettingsSingleton[blog.Id] = blogSettings;
41 }
42 }
43 }
44
45 return blogSettings;
46 }
复制代码

用一个键、值对集合存储博客设置的单例集合,然后获取特定的博客设置的时候,根据当前博客实例(根据当前博客的ID返回Blog.CurrentInstance)

复制代码
 1         ///<summary>
2 /// Prevents a default instance of the <see cref = "BlogSettings" /> class from being created.
3 /// Initializes a new instance of the <see cref = "BlogSettings" /> class.
4 ///</summary>
5 private BlogSettings()
6 {
7 this.Load();
8 }
9
10 ///<summary>
11 /// Initializes the singleton instance of the <see cref="BlogSettings"/> class.
12 ///</summary>
13 private void Load()
14 {
15
16 // ------------------------------------------------------------
17 // Enumerate through individual settings nodes
18 // ------------------------------------------------------------
19 var dic = BlogService.LoadSettings();
20 var settingsProps = GetSettingsTypePropertyDict();
21
22 foreach (System.Collections.DictionaryEntry entry in dic)
23 {
24 string name = (string)entry.Key;
25 System.Reflection.PropertyInfo property = null;
26
27 if (settingsProps.TryGetValue(name, out property))
28 {
29 // ------------------------------------------------------------
30 // Attempt to apply configured setting
31 // ------------------------------------------------------------
32 try
33 {
34 if (property.CanWrite)
35 {
36 string value = (string)entry.Value;
37 var propType = property.PropertyType;
38
39 if (propType.IsEnum)
40 {
41 property.SetValue(this, Enum.Parse(propType, value), null);
42 }
43 else
44 {
45 property.SetValue(this, Convert.ChangeType(value, propType, CultureInfo.CurrentCulture), null);
46 }
47 }
48 }
49 catch (Exception e)
50 {
51 Utils.Log(string.Format("Error loading blog settings: {0}", e.Message));
52 }
53 }
54
55 }
56
57 }
复制代码

上面的构造函数是私有的,我想应该是单例模式的需要吧。首先BlogService.LoadSettings();加载配置,然后赋值给当前实例的属性,至此初始化配置完毕。



posted @   一文钱  阅读(269)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示