重构代码:移除Dict类,使用BootstrapDict类,减少代码重复

This commit is contained in:
Argo-Lenovo 2017-04-02 16:32:26 +08:00
parent fd7371b883
commit 13c2f7152f
7 changed files with 38 additions and 82 deletions

View File

@ -1,5 +1,6 @@
using Bootstrap.Admin.Models;
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Security.Principal;
using Longbow.Web.Mvc;
using Newtonsoft.Json.Linq;
@ -17,7 +18,7 @@ namespace Bootstrap.Admin.Controllers
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public QueryData<Dict> Get([FromUri]QueryDictOption value)
public QueryData<BootstrapDict> Get([FromUri]QueryDictOption value)
{
return value.RetrieveData();
}
@ -27,7 +28,7 @@ namespace Bootstrap.Admin.Controllers
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public Dict Get(int id)
public BootstrapDict Get(int id)
{
return DictHelper.RetrieveDicts().FirstOrDefault(t => t.Id == id);
}
@ -36,7 +37,7 @@ namespace Bootstrap.Admin.Controllers
/// </summary>
/// <param name="value"></param>
[HttpPost]
public bool Post([FromBody]Dict value)
public bool Post([FromBody]BootstrapDict value)
{
return DictHelper.SaveDict(value);
}
@ -46,9 +47,9 @@ namespace Bootstrap.Admin.Controllers
/// <returns></returns>
[HttpPost]
[AllowAnonymous]
public IEnumerable<Dict> Post(int id, [FromBody]JObject value)
public IEnumerable<BootstrapDict> Post(int id, [FromBody]JObject value)
{
IEnumerable<Dict> ret = new List<Dict>();
IEnumerable<BootstrapDict> ret = new List<BootstrapDict>();
dynamic json = value;
switch ((string)json.type)
{

View File

@ -1,4 +1,5 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Web.Mvc;
using System.Linq;
@ -22,7 +23,7 @@ namespace Bootstrap.Admin.Models
/// 字典表查询
/// </summary>
/// <returns></returns>
public QueryData<Dict> RetrieveData()
public QueryData<BootstrapDict> RetrieveData()
{
var data = DictHelper.RetrieveDicts();
if (!string.IsNullOrEmpty(Category))
@ -37,7 +38,7 @@ namespace Bootstrap.Admin.Models
{
data = data.Where(t => t.Define.ToString() == Define);
}
var ret = new QueryData<Dict>();
var ret = new QueryData<BootstrapDict>();
ret.total = data.Count();
// 通过option.Sort属性判断对那列进行排序
switch (Sort)

View File

@ -48,8 +48,11 @@
<add key="ExceptionHelper-RetrieveExceptions" interval="600" desc="程序异常数据缓存" />
<add key="MessageHelper-RetrieveMessages" interval="600" desc="站内消息数据缓存" />
<add key="TaskHelper-RetrieveTasks" interval="600" desc="所有任务数据缓存" />
<add key="BootstrapAdminPrincipal-RetrieveAllRoles" interval="600" desc="系统所有角色列表数据" />
<add key="BootstrapAdminPrincipal-RetrieveRolesByUrl" interval="600" desc="指定菜单的角色数据缓存" />
<add key="BootstrapAdminPrincipal-RetrieveRolesByUserName" interval="600" desc="指定用户名角色数据缓存" />
<add key="BootstrapAdminPrincipal-RetrieveAllGroups-BA" interval="600" desc="系统所有用户组列表数据" />
<add key="BootstrapAdminPrincipal-RetrieveGroupsByUserName" interval="600" desc="通过用户获得用户组数据" />
</cacheManager>

View File

@ -59,7 +59,6 @@
</Compile>
<Compile Include="CacheCleanUtility.cs" />
<Compile Include="DBAccessManager.cs" />
<Compile Include="Dict.cs" />
<Compile Include="DictHelper.cs" />
<Compile Include="ExceptionHelper.cs" />
<Compile Include="Exceptions.cs" />

View File

@ -1,4 +1,5 @@
using Longbow.Caching.Configuration;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
using System.Collections.Generic;
@ -79,16 +80,16 @@ namespace Bootstrap.DataAccess
cacheKeys.Add(ExceptionHelper.RetrieveExceptionsDataKey + "*");
}
var section = CacheListSection.GetSection();
section.Items.Where(item => item.Enabled).AsParallel().ForAll(ele =>
cacheKeys.AsParallel().ForAll(key => CacheManager.Clear(k => key.EndsWith("*") ? k.Contains(key.TrimEnd('*')) : k.Equals(key)));
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
var section = CacheListSection.GetSection();
section.Items.Where(item => item.Enabled).Skip(1).AsParallel().ForAll(ele =>
{
try
{
var client = new WebClient();
cacheKeys.ForEach(k => client.OpenRead(new Uri(string.Format(ele.Url, k))));
}
catch (Exception ex)
{

View File

@ -1,29 +0,0 @@
namespace Bootstrap.DataAccess
{
/// <summary>
/// 字典表实体
/// </summary>
public class Dict
{
/// <summary>
/// 字典主键 数据库自增
/// </summary>
public int Id { get; set; }
/// <summary>
/// 分类
/// </summary>
public string Category { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 代号
/// </summary>
public string Code { get; set; }
/// <summary>
/// 1表示系统使用0表示用户自定义 默认为1
/// </summary>
public int Define { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using Longbow.Caching;
using Bootstrap.Security;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
@ -15,39 +16,18 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class DictHelper
{
/// <summary>
///
/// </summary>
internal const string RetrieveDictsDataKey = "DictHelper-RetrieveDicts";
private const string RetrieveCategoryDataKey = "DictHelper-RetrieveDictsCategory";
/// <summary>
/// 查询所有字典信息
///
/// </summary>
/// <returns></returns>
public static IEnumerable<Dict> RetrieveDicts()
public static IEnumerable<BootstrapDict> RetrieveDicts()
{
return CacheManager.GetOrAdd(RetrieveDictsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveDictsDataKey), key =>
{
string sql = "select ID, Category, Name, Code, Define from Dicts";
List<Dict> dicts = new List<Dict>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
dicts.Add(new Dict()
{
Id = (int)reader[0],
Category = (string)reader[1],
Name = (string)reader[2],
Code = (string)reader[3],
Define = (int)reader[4]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return dicts;
}, CacheSection.RetrieveDescByKey(RetrieveDictsDataKey));
return BootstrapDict.RetrieveDicts();
}
/// <summary>
/// 删除字典中的数据
@ -80,7 +60,7 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveDict(Dict p)
public static bool SaveDict(BootstrapDict p)
{
bool ret = false;
if (p.Category.Length > 50) p.Category = p.Category.Substring(0, 50);
@ -129,7 +109,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", category));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheManager.Clear(key => key.Contains(RetrieveDictsDataKey));
CacheCleanUtility.ClearCache(dictIds: string.Empty);
ret = true;
}
catch (DbException ex)
@ -142,11 +122,11 @@ namespace Bootstrap.DataAccess
/// 获取字典分类名称
/// </summary>
/// <returns></returns>
public static IEnumerable<Dict> RetrieveCategories()
public static IEnumerable<BootstrapDict> RetrieveCategories()
{
return CacheManager.GetOrAdd(RetrieveCategoryDataKey, CacheSection.RetrieveIntervalByKey(RetrieveCategoryDataKey), key =>
{
var ret = new List<Dict>();
var ret = new List<BootstrapDict>();
string sql = "select distinct Category from Dicts";
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
@ -155,7 +135,7 @@ namespace Bootstrap.DataAccess
{
while (reader.Read())
{
ret.Add(new Dict() { Category = (string)reader[0] });
ret.Add(new BootstrapDict() { Category = (string)reader[0] });
}
}
}
@ -170,7 +150,7 @@ namespace Bootstrap.DataAccess
public static string RetrieveWebTitle()
{
var settings = RetrieveDicts();
return (settings.FirstOrDefault(d => d.Name == "网站标题" && d.Category == "网站设置" && d.Define == 0) ?? new Dict() { Code = "后台管理系统" }).Code;
return (settings.FirstOrDefault(d => d.Name == "网站标题" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "后台管理系统" }).Code;
}
/// <summary>
///
@ -179,13 +159,13 @@ namespace Bootstrap.DataAccess
public static string RetrieveWebFooter()
{
var settings = RetrieveDicts();
return (settings.FirstOrDefault(d => d.Name == "网站页脚" && d.Category == "网站设置" && d.Define == 0) ?? new Dict() { Code = "2016 © 通用后台管理系统" }).Code;
return (settings.FirstOrDefault(d => d.Name == "网站页脚" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "2016 © 通用后台管理系统" }).Code;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static IEnumerable<Dict> RetrieveWebCss()
public static IEnumerable<BootstrapDict> RetrieveWebCss()
{
var data = RetrieveDicts();
return data.Where(d => d.Category == "网站样式");
@ -194,7 +174,7 @@ namespace Bootstrap.DataAccess
///
/// </summary>
/// <returns></returns>
public static IEnumerable<Dict> RetrieveActiveCss()
public static IEnumerable<BootstrapDict> RetrieveActiveCss()
{
var data = RetrieveDicts();
return data.Where(d => d.Name == "使用样式" && d.Category == "当前样式" && d.Define == 0 && !d.Code.Equals("site.css", StringComparison.OrdinalIgnoreCase));
@ -203,10 +183,10 @@ namespace Bootstrap.DataAccess
/// 获取头像路径
/// </summary>
/// <returns></returns>
public static Dict RetrieveIconFolderPath()
public static BootstrapDict RetrieveIconFolderPath()
{
var data = RetrieveDicts();
return data.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == 0) ?? new Dict() { Code = "~/Content/images/uploader/" };
return data.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == 0) ?? new BootstrapDict() { Code = "~/Content/images/uploader/" };
}
/// <summary>
/// 获得默认的前台首页地址,默认为~/Home/Index
@ -215,7 +195,7 @@ namespace Bootstrap.DataAccess
public static string RetrieveHomeUrl()
{
var settings = RetrieveDicts();
return (settings.FirstOrDefault(d => d.Name == "前台首页" && d.Category == "网站设置" && d.Define == 0) ?? new Dict() { Code = "~/Home/Index" }).Code;
return (settings.FirstOrDefault(d => d.Name == "前台首页" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "~/Home/Index" }).Code;
}
/// <summary>
///