PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/DataStore/StringDictionaryBehavior.cs

#
C# | 108 lines | 60 code | 15 blank | 33 comment | 5 complexity | 6ce92cc553dc6e79f9697c0c65f5f0e9 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.DataStore
  2. {
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Xml.Serialization;
  8. using BlogEngine.Core.Providers;
  9. /// <summary>
  10. /// The string dictionary behavior.
  11. /// </summary>
  12. internal class StringDictionaryBehavior : ISettingsBehavior
  13. {
  14. #region Constants and Fields
  15. /// <summary>
  16. /// The section.
  17. /// </summary>
  18. private static readonly BlogProviderSection Section =
  19. (BlogProviderSection)ConfigurationManager.GetSection("BlogEngine/blogProvider");
  20. #endregion
  21. #region Implemented Interfaces
  22. #region ISettingsBehavior
  23. /// <summary>
  24. /// Retreaves StringDictionary object from database or file system
  25. /// </summary>
  26. /// <param name="extensionType">
  27. /// Extension Type
  28. /// </param>
  29. /// <param name="extensionId">
  30. /// Extension Id
  31. /// </param>
  32. /// <returns>
  33. /// StringDictionary object as Stream
  34. /// </returns>
  35. public object GetSettings(ExtensionType extensionType, string extensionId)
  36. {
  37. SerializableStringDictionary ssd;
  38. var sd = new StringDictionary();
  39. var serializer = new XmlSerializer(typeof(SerializableStringDictionary));
  40. if (Section.DefaultProvider == "XmlBlogProvider")
  41. {
  42. var stm = (Stream)BlogService.LoadFromDataStore(extensionType, extensionId);
  43. if (stm != null)
  44. {
  45. ssd = (SerializableStringDictionary)serializer.Deserialize(stm);
  46. stm.Close();
  47. sd = ssd;
  48. }
  49. }
  50. else
  51. {
  52. var o = BlogService.LoadFromDataStore(extensionType, extensionId);
  53. if (!string.IsNullOrEmpty((string)o))
  54. {
  55. using (var reader = new StringReader((string)o))
  56. {
  57. ssd = (SerializableStringDictionary)serializer.Deserialize(reader);
  58. }
  59. sd = ssd;
  60. }
  61. }
  62. return sd;
  63. }
  64. /// <summary>
  65. /// Saves String Dictionary to Data Store
  66. /// </summary>
  67. /// <param name="extensionType">
  68. /// Extension Type
  69. /// </param>
  70. /// <param name="extensionId">
  71. /// Extension Id
  72. /// </param>
  73. /// <param name="settings">
  74. /// StringDictionary settings
  75. /// </param>
  76. /// <returns>
  77. /// The save settings.
  78. /// </returns>
  79. public bool SaveSettings(ExtensionType extensionType, string extensionId, object settings)
  80. {
  81. var sd = (StringDictionary)settings;
  82. var ssd = new SerializableStringDictionary();
  83. foreach (DictionaryEntry de in sd)
  84. {
  85. ssd.Add(de.Key.ToString(), de.Value.ToString());
  86. }
  87. BlogService.SaveToDataStore(extensionType, extensionId, ssd);
  88. return true;
  89. }
  90. #endregion
  91. #endregion
  92. }
  93. }