zoukankan      html  css  js  c++  java
  • BlogEngine.netBlogSettings

    对于博客配置,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();加载配置,然后赋值给当前实例的属性,至此初始化配置完毕。



  • 相关阅读:
    【自然框架】 之 资源角色——列表过滤方案(思路篇)
    【自然框架】 之 主从表的添加、修改
    今天你进步了吗?
    【自然框架】通用权限的视频演示(一):添加角色,权限到功能节点和按钮
    【杂谈】您是以什么姿态来参与讨论、回帖的?
    【自然框架】之通用权限:用PowerDesigner重新设计了一下数据库,有ER图和表关系图
    【自然框架】 页面里的父类—— 改进和想法、解释
    页面里的父类——BaseUI源代码下载(2009.10.15更新)
    【自然框架】 页面里的父类——把共用的东东都交给父类,让子类专注于其他。
    【自然框架】之通用权限:数据库设计的几种使用方式
  • 原文地址:https://www.cnblogs.com/whosedream/p/2258774.html
Copyright © 2011-2022 走看看