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



  • 相关阅读:
    java总结1
    immutable
    iconfont
    实战build-react(三)+ style-components
    PHP判断字符串的包含
    win7 64位操作系统中 Oracle 11g 安装教程(图解)
    我在博客园写博客的原因
    面向对象程序设计-C++_课时17函数重载和默认参数
    面向对象程序设计-C++_课时16子类父类关系
    面向对象程序设计-C++_课时14对象组合_课时15继承
  • 原文地址:https://www.cnblogs.com/whosedream/p/2258774.html
Copyright © 2020-2023  润新知