前面两边的代码就是一套初步的工具包架构,基本底层通用,可以移植到任意项目实现类似的需求。接下来,再在我们特定的项目几微助手里面再实现一套基于自己项目的基类,根据项目需求抽象一下项目内的常用方法。理论上,这一步的抽象,如果移植到其他项目,应该也是有一定的通用性。
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /// <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的数据文件。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /// <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数据表是有两个的。一个是模块整体的配置,一个是模块里面单个对象的设置。这个类就是管理前者。
这个就是管理模块单项配置的基类。