爱摄叽歪

爱生活爱叽歪,一个不务正业的人!

基于SQLite+EF6实现一套自己的Key-Value存储管理工具包(3)

前面两边的代码就是一套初步的工具包架构,基本底层通用,可以移植到任意项目实现类似的需求。接下来,再在我们特定的项目几微助手里面再实现一套基于自己项目的基类,根据项目需求抽象一下项目内的常用方法。理论上,这一步的抽象,如果一直到其他项目,应该也是有一定的通用性。

 
 /// <summary>
    /// 站点设置的基类,基类主要提供站点扩展设置的数据库连接等操作
    /// </summary>
    public abstract class BaseSiteExtensions:BaseExtensions
    {
        public Guid SiteUnid { get; set; }
        private MiniSiteDataExtensionsEntities _dataContext = null;
        /// <summary>
        /// 数据操作实例
        /// </summary>
        public virtual MiniSiteDataExtensionsEntities DataContext
        {
            get
            {
                if (this._dataContext == null)
                    this._dataContext = MiniSiteDataExtensionsEntities.GetContext(this.SiteUnid);
                return this._dataContext;
            }
            set
            {
                this._dataContext = value;
            }
        }


        /// <summary>
        /// 站点扩展设置初始化
        /// </summary>
        /// <param name="siteunid"></param>
        public BaseSiteExtensions(Guid siteunid):base()
        {
            this.SiteUnid = siteunid;
        }

        /// <summary>
        /// 打开一个数据连接
        /// </summary>
        public virtual void OpenConnection()
        {
            if (this.DataContext.Database.Connection.State == ConnectionState.Closed)
                this.DataContext.Database.Connection.Open();
        }

        /// <summary>
        /// 关闭数据库联机并销毁对象
        /// </summary>
        public virtual void CloseConnection()
        {
            if (this.DataContext.Database.Connection.State != ConnectionState.Closed)
                this.DataContext.Database.Connection.Close();
            this.DataContext.Dispose();
            this.DataContext = null;
        }

    }

在几微助手里面,有站点的概念,这里是为站点的扩展配置,做一套管理基类。用于管理站点的Key-Value数据库。这里用到的EF数据库实例就是上篇里面的MiniSiteDataExtensionsEntities(这里也可以用其它的,在表结构相同的情况下,通过继承后在实现自己的数据库操作类,根据自己的需求来管理EF的数据文件。)

/// <summary>
    /// 站点的扩展设置基类
    /// </summary>
    public class BaseSiteSettingExtensions:BaseSiteExtensions
    {
        public  event ItemSaved OnItemSaved;
        public  event AllItemsSaved OnAllItemsSaved;

        public virtual SettingEditorDictionary EditorItems { get; set; }

        protected string TypeName { get; set; }

        /// <summary>
        /// 根据指定类型初始化站点扩展设置
        /// </summary>
        /// <param name="siteunid"></param>
        /// <param name="typename"></param>
        public BaseSiteSettingExtensions(Guid siteunid,string typename):base(siteunid)
        {
            this.TypeName = typename;
        }

        /// <summary>
        /// 从数据库读取键值,将指定key的设置以泛型方式获取对象的实例
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">配置的键</param>
        /// <returns>返回配置对象的实例</returns>
        public virtual T GetValue<T>(string key)
        {
            T result= default(T);
            string res=this.GetValue(key);
            if (!string.IsNullOrWhiteSpace(res))
                result = JsonConvert.DeserializeObject<T>(res);
            return result;
        }
        /// <summary>
        /// 获取指定key的扩展设置值
        /// </summary>
        /// <param name="key">设置的键</param>
        /// <returns>返回值的字符串形式</returns>
        public virtual string GetValue(string key)
        {
                return this.GetValue(this.TypeName, key);
        }

        /// <summary>
        /// 根据key获取指定类型的扩展设置值
        /// </summary>
        /// <param name="typename">设置类型的枚举</param>
        /// <param name="key">设置的键</param>
        /// <returns>返回值的字符串形式</returns>
        public virtual string GetValue(string typename,string key)
        {
            string result = null;
            if (this.DataContext.SettingExtensions.Count(m => m.TypeName == typename.ToString() && m.Key == key.ToLower()) > 0)
            {
                SettingExtension setting = this.DataContext.SettingExtensions.Single(m => m.TypeName == typename.ToString() && m.Key == key.ToLower());
                result = setting.Value;
            }
            this.CloseConnection();
            return result;

        }

        /// <summary>
        /// 保存指定键的项
        /// </summary>
        /// <param name="key">需要保存的配置键</param>
        public virtual void SaveItem(string key)
        {
            this.SaveItem(this.TypeName, key, this.Items[key.ToLower()]);
        }

        /// <summary>
        /// 更新或者添加指定键值的单个配置
        /// </summary>
        /// <param name="key">需要保存的配置键</param>
        /// <param name="value"></param>
        public virtual void SaveItem(string key, string value)
        {
            this.SaveItem(this.TypeName, key, value);
        }

        /// <summary>
        /// 保存设置项
        /// </summary>
        /// <param name="item">设置项实</param>
        public virtual void SaveItem(SettingItem item)
        {
            this.SaveItem(this.TypeName, item.Key, item.Value);
        }

        /// <summary>
        /// 保存指定设置类型的设置项目
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="item"></param>
        public virtual void SaveItem(string typename, SettingItem item)
        {
            this.SaveItem(typename, item.Key, item.Value);
        }

        /// <summary>
        /// 跟新或添加设置
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public virtual void SaveItem(string typename, string key, string value)
        {
            if (this.DataContext.SettingExtensions.Count(m => m.TypeName == typename.ToString() && m.Key == key.ToLower()) > 0)
            {
                SettingExtension setting = this.DataContext.SettingExtensions.AsEnumerable().Single(m => m.TypeName == typename.ToString() && m.Key == key.ToLower());
                setting.Value = value;
            }
            else
            {
                SettingExtension setting = new SettingExtension();
                setting.SettingID = Guid.NewGuid();
                setting.TypeName = typename.ToString();
                setting.Key = key.ToLower();
                setting.Value = String.IsNullOrEmpty(value)?"":value;
                this.DataContext.SettingExtensions.Add(setting);
            }
            this.DataContext.SaveChanges();
            this.CloseConnection();
            this.SetItem(key, value);

            OnItemSaved?.Invoke(this, key);
        }



        /// <summary>
        /// 跟新或者新建扩展设置
        /// </summary>
        public virtual void SaveAllItems()
        {
            this.SaveAllItems(this.Items);
        }

        /// <summary>
        /// 用自定的配置字典对象批量更新站点设
        /// </summary>
        /// <param name="items"></param>
        public virtual void SaveAllItems(SettingDictionary items)
        {
            this.SaveAllItems(this.TypeName, items);
        }

        /// <summary>
        /// 批量添加或更新设置
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="items"></param>
        public virtual void SaveAllItems(string typename, SettingDictionary items)
        {
            foreach (SettingItem kv in items)
            {
                try
                {
                    if (this.DataContext.SettingExtensions.Count(m => m.TypeName == typename.ToString() && m.Key == kv.Key.ToLower()) > 0)
                    {
                        SettingExtension setting = this.DataContext.SettingExtensions.Single(m => m.TypeName == typename.ToString() && m.Key == kv.Key.ToLower());
                        if (setting.Value == kv.Value)
                        {
                            continue;
                        }
                        else
                        {
                            setting.Value = kv.Value;
                        }
                    }
                    else
                    {
                        SettingExtension setting = new SettingExtension();
                        setting.SettingID = Guid.NewGuid();
                        setting.TypeName = typename.ToString();
                        setting.Key = kv.Key.ToLower();
                        setting.Value = String.IsNullOrEmpty(kv.Value) ? "" : kv.Value;
                        this.DataContext.SettingExtensions.Add(setting);
                    }
                }
                catch(Exception e)
                {
                    SiteLog.Debug(kv.Key,kv.Value);
                }
            }
            this.DataContext.SaveChanges();
            this.CloseConnection();

            OnAllItemsSaved?.Invoke(this);
        }

        /// <summary>
        /// 读取数据,并通过读取的键值对填充配置的Items属性
        /// </summary>
        public virtual void GetItems()
        {
            this.GetItems(this.TypeName);
        }

        /// <summary>
        /// 根据string枚举属性读取数据,并通过读取的键值对填充配置的Items属性
        /// </summary>
        /// <param name="typename"></param>
        public virtual void GetItems(string typename)
        {
            List<SettingExtension> list = this.GetSettingExtensions(typename);
            if (list != null)
            {
                foreach (SettingExtension setting in list)
                {
                    if (this.Items.ContainsKey(setting.Key.ToLower()) && !String.IsNullOrEmpty(setting.Value))
                    {
                        this.Items[setting.Key.ToLower()] = setting.Value;
                    }
                }
            }
        }

        /// <summary>
        /// 获取配置扩展的实例对象列表
        /// </summary>
        /// <returns>返回特定类型的配置对象列表</returns>
        public virtual List<SettingExtension> GetSettingExtensions()
        {
            return this.GetSettingExtensions(this.TypeName);
        }

        /// <summary>
        /// 获取指定类型的扩展信息列表
        /// </summary>
        /// <param name="typename">配置类型枚举</param>
        /// <returns>返回指定类型的配置对象列表</returns>
        public virtual List<SettingExtension> GetSettingExtensions(string typename)
        {
            List<SettingExtension> result = null;
            if (this.DataContext.SettingExtensions.Count(m => m.TypeName == typename.ToString()) > 0)
            {
                result = this.DataContext.SettingExtensions.Where(m => m.TypeName == typename.ToString()).ToList();
            }
            this.CloseConnection();
            return result;
        }

        /// <summary>
        /// 解析表单提交的设置,并根据表单提交数据对设置重新修改赋值
        /// </summary>
        /// <param name="form">前端UI提交的表单对象</param>
        public virtual void ParseFormSettingValues(FormCollection form)
        {
            foreach (SettingItem item in this.Items)
            {
                if (this.EditorItems.ContainsKey(item.Key))
                {
                    switch (this.EditorItems[item.Key].TagName.ToLower())
                    {
                        case "bool":
                            if (form[item.Key] == null)
                                this.SetItem<bool>(item.Key, false);
                            else
                                this.SetItem<bool>(item.Key, bool.Parse(form[item.Key]));
                            break;
                        default:
                            this.SetItem(item.Key, form[item.Key]);
                            break;
                    }
                }
                else
                {
                    if (form[item.Key] == null)
                        continue;
                    else
                        this.SetItem(item.Key, form[item.Key]);
                }
            }
        }


    }

BaseSiteSettingExtensions这里是实现一套基于集合的扩展类。第一篇讲的数据表里面,我们的Key-Value数据表是有两个的。一个是模块整体的配置,一个是模块里面单个对象的设置。这个类就是管理前者。

 
 public class BaseSiteModuleItemExtensions: BaseSiteExtensions
    {
        public  event ItemSaved OnItemSaved;
        public  event AllItemsSaved OnAllItemsSaved;


        protected string TypeName { get; set; }
        public Guid ItemID { get; set; }

        public BaseSiteModuleItemExtensions(Guid siteunid, Guid itemid,string typename):base(siteunid)
        {
            this.ItemID = itemid;
            this.TypeName = typename;
        }



         /// <summary>
        /// 从数据库读取键值,将指定key的设置以泛型方式获取对象的实例
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">配置的键</param>
        /// <returns>返回配置对象的实例</returns>
        public virtual T GetValue<T>(string key)
        {
            T result;
            string res=this.GetValue(key);
            result = JsonConvert.DeserializeObject<T>(res);
            return result;
        }
        /// <summary>
        /// 获取指定key的扩展设置值
        /// </summary>
        /// <param name="key">设置的键</param>
        /// <returns>返回值的字符串形式</returns>
        public virtual string GetValue(string key)
        {
                return this.GetValue(this.TypeName, key);
        }

        /// <summary>
        /// 根据key获取指定类型的扩展设置值
        /// </summary>
        /// <param name="typename">设置类型的枚举</param>
        /// <param name="key">设置的键</param>
        /// <returns>返回值的字符串形式</returns>
        public virtual string GetValue(string typename,string key)
        {
            string result = null;
            if (this.DataContext.ModuleItemSettiingExtensions.Count(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString() && m.Key == key.ToLower()) > 0)
            {
                ModuleItemSettiingExtension setting = this.DataContext.ModuleItemSettiingExtensions.Single(m =>m.ModuleItemID==this.ItemID && m.ModuleType == typename.ToString() && m.Key == key.ToLower());
                result = setting.Value;
            }
            this.CloseConnection();
            return result;

        }

        /// <summary>
        /// 保存指定键的项
        /// </summary>
        /// <param name="key">需要保存的配置键</param>
        public virtual void SaveItem(string key)
        {
            this.SaveItem(this.TypeName, key, this.Items[key.ToLower()]);
        }

        /// <summary>
        /// 更新或者添加指定键值的单个配置
        /// </summary>
        /// <param name="key">需要保存的配置键</param>
        /// <param name="value"></param>
        public virtual void SaveItem(string key, string value)
        {
            this.SaveItem(this.TypeName, key, value);
        }

        /// <summary>
        /// 保存指定的设置项
        /// </summary>
        /// <param name="item"></param>
        public virtual void SaveItem(SettingItem item)
        {
            this.SaveItem(this.TypeName, item.Key, item.Value);
        }

        /// <summary>
        /// 保存指定模块的配置项
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="item"></param>
        public virtual void SaveItem(string typename, SettingItem item)
        {
            this.SaveItem(typename, item.Key, item.Value);
        }

        /// <summary>
        /// 更新或添加指定模块类型的单个配置
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public virtual void SaveItem(string typename, string key, string value)
        {
            if (this.DataContext.ModuleItemSettiingExtensions.Count(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString() && m.Key == key.ToLower()) > 0)
            {
                ModuleItemSettiingExtension setting = this.DataContext.ModuleItemSettiingExtensions.Single(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString() && m.Key == key.ToLower());
                setting.Value = value;
            }
            else
            {
                ModuleItemSettiingExtension setting = new ModuleItemSettiingExtension();
                setting.SettingID = Guid.NewGuid();
                setting.ModuleItemID = this.ItemID;
                setting.ModuleType = typename.ToString();
                setting.Key = key.ToLower();
                setting.Value = value;
                this.DataContext.ModuleItemSettiingExtensions.Add(setting);
            }
            this.DataContext.SaveChanges();
            this.CloseConnection();
            this.SetItem(key, value);

            OnItemSaved?.Invoke(this, key);
        }

        /// <summary>
        /// 更新或者新建扩展设置
        /// </summary>
        public virtual void SaveAllItems()
        {
            this.SaveAllItems(this.Items);
        }


        /// <summary>
        /// 用指定的配置字典批量更新或者添加模块项配置
        /// </summary>
        /// <param name="items"></param>
        public virtual void SaveAllItems(SettingDictionary items)
        {
            this.SaveAllItems(this.TypeName, items);
        }

        /// <summary>
        /// 批量添加或更新模块项目设置
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="items"></param>
        public virtual void SaveAllItems(string typename, SettingDictionary items)
        {
            foreach (SettingItem kv in items)
            {
                if (this.DataContext.ModuleItemSettiingExtensions.Count(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString() && m.Key == kv.Key) > 0)
                {
                    ModuleItemSettiingExtension setting = this.DataContext.ModuleItemSettiingExtensions.Single(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString() && m.Key == kv.Key);
                    if (setting.Value == kv.Value)
                    {
                        continue;
                    }
                    else
                    {
                        setting.Value = kv.Value.ToLower();
                    }
                }
                else
                {
                    ModuleItemSettiingExtension setting = new ModuleItemSettiingExtension();
                    setting.SettingID = Guid.NewGuid();
                    setting.ModuleItemID = this.ItemID;
                    setting.ModuleType = typename.ToString();
                    setting.Key = kv.Key.ToLower();
                    setting.Value = kv.Value;
                    this.DataContext.ModuleItemSettiingExtensions.Add(setting);
                }
            }
            this.DataContext.SaveChanges();
            this.CloseConnection();
            OnAllItemsSaved?.Invoke(this);
        }

        public virtual void DeleteItemSettings()
        {
            this.DeleteDataByItemID(this.ItemID);
        }

        /// <summary>
        /// 删除一个模块项目关联是所有配置信息
        /// </summary>
        /// <param name="itemID">关联项目ID</param>
        public virtual void DeleteDataByItemID(Guid itemID)
        {
            if (this.DataContext.ModuleItemSettiingExtensions.Count(m => m.ModuleItemID == itemID) > 0)
            {
                this.DataContext.ModuleItemSettiingExtensions.RemoveRange(
                    this.DataContext.ModuleItemSettiingExtensions.Where(m=>m.ModuleItemID==itemID)
                    );
                this.DataContext.SaveChanges();
            }
            this.CloseConnection();
        }

        /// <summary>
        /// 读取数据,并通过读取的键值对填充配置的Items属性
        /// </summary>
        public virtual void GetItems()
        {
            this.GetItems(this.TypeName);
        }

        /// <summary>
        /// 根据string枚举属性读取数据,并通过读取的键值对填充配置的Items属性
        /// </summary>
        /// <param name="typename"></param>
        public virtual void GetItems(string typename)
        {
            List<ModuleItemSettiingExtension> list = this.GetModuleItemSettiingExtensions(typename);
            if (list != null)
            {
                foreach (ModuleItemSettiingExtension setting in list)
                {
                    if (this.Items.ContainsKey(setting.Key) && !String.IsNullOrEmpty(setting.Value))
                    {
                        this.Items[setting.Key.ToLower()] = setting.Value;
                    }
                }
            }
        }

        /// <summary>
        /// 获取模块单项配置扩展的实例对象列表
        /// </summary>
        /// <returns>返回特定模块类型的配置对象列表</returns>
        public virtual List<ModuleItemSettiingExtension> GetModuleItemSettiingExtensions()
        {
            return this.GetModuleItemSettiingExtensions(this.TypeName);
        }

        /// <summary>
        /// 获取指定模块单项类型的扩展信息列表
        /// </summary>
        /// <param name="typename">配置类型枚举</param>
        /// <returns>返回指定模块类型的配置对象列表</returns>
        public virtual List<ModuleItemSettiingExtension> GetModuleItemSettiingExtensions(string typename)
        {
            List<ModuleItemSettiingExtension> result = null;
            if (this.DataContext.ModuleItemSettiingExtensions.Count(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString()) > 0)
            {
                result = this.DataContext.ModuleItemSettiingExtensions.Where(m => m.ModuleItemID == this.ItemID && m.ModuleType == typename.ToString()).ToList();
            }
            this.CloseConnection();
            return result;
        }
    }

这个就是管理模块单项配置的基类

不允许评论