For blog configuration, blogengine is operated by a global configuration class blogsettings. For blogsettings, it adopts the singleton design.
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 }
Use a key-value pair to store the set of Singleton set for the blog, and then obtain the specific blog settings based on the current blog instance (return blog. currentinstance according to the current blog ID)
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 }
The above constructor is private. I think it is needed for the singleton mode. First, blogservice. loadsettings (); load the configuration, and then assign the attribute to the current instance. At this point, the Initialization Configuration is complete.