PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

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