diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs
index a8983df0..31931267 100644
--- a/Bootstrap.DataAccess/RoleHelper.cs
+++ b/Bootstrap.DataAccess/RoleHelper.cs
@@ -1,113 +1,137 @@
-using Longbow;
-using Longbow.Caching;
-using Longbow.Caching.Configuration;
-using Longbow.ExceptionManagement;
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Data.Common;
-using System.Globalization;
-using System.Linq;
-
-namespace Bootstrap.DataAccess
-{
- public class RoleHelper
- {
- private const string RoleDataKey = "RoleData-CodeRoleHelper";
- ///
- /// 查询所有角色
- ///
- ///
- ///
- public static IEnumerable
RetrieveRole(string tId = null)
- {
- string sql = "select * from Roles";
- var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key =>
- {
- List roles = new List();
- DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
- try
- {
- using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
- {
- while (reader.Read())
- {
- roles.Add(new Role()
- {
- ID = (int)reader[0],
- RoleName = LgbConvert.ReadValue(reader[1], string.Empty),
- Description = LgbConvert.ReadValue(reader[2], string.Empty)
- });
- }
- }
- }
- catch (Exception ex) { ExceptionManager.Publish(ex); }
- return roles;
- }, CacheSection.RetrieveDescByKey(RoleDataKey));
- return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
- }
- ///
- /// 删除角色表
- ///
- ///
- public static bool DeleteRole(string IDs)
- {
- bool ret = false;
- if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret;
- try
- {
- string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles where ID in ({0})", IDs);
- using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
- {
- DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
- ClearCache();
- ret = true;
- }
- }
- catch (Exception ex)
- {
- ExceptionManager.Publish(ex);
- }
- return ret;
- }
-
- ///
- /// 保存新建/更新的角色信息
- ///
- ///
- ///
- public static bool SaveRole(Role p)
- {
- if (p == null) throw new ArgumentNullException("p");
- bool ret = false;
- if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50);
- if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
- string sql = p.ID == 0 ?
- "Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
- "Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID";
- try
- {
- using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
- {
- cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
- cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName, ParameterDirection.Input));
- cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
- DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
- }
- ret = true;
- ClearCache();
- }
- catch (DbException ex)
- {
- ExceptionManager.Publish(ex);
- }
- return ret;
- }
-
- // 更新缓存
- private static void ClearCache()
- {
- CacheManager.Clear(key => key.Contains("RoleData-"));
- }
- }
-}
+using Longbow;
+using Longbow.Caching;
+using Longbow.Caching.Configuration;
+using Longbow.ExceptionManagement;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Globalization;
+using System.Linq;
+
+namespace Bootstrap.DataAccess
+{
+ public class RoleHelper
+ {
+ private const string RoleDataKey = "RoleData-CodeRoleHelper";
+ ///
+ /// 查询所有角色
+ ///
+ ///
+ ///
+ public static IEnumerable RetrieveRoles(string tId = null)
+ {
+ string sql = "select * from Roles";
+ var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key =>
+ {
+ List roles = new List();
+ DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
+ try
+ {
+ using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
+ {
+ while (reader.Read())
+ {
+ roles.Add(new Role()
+ {
+ ID = (int)reader[0],
+ RoleName = LgbConvert.ReadValue(reader[1], string.Empty),
+ Description = LgbConvert.ReadValue(reader[2], string.Empty)
+ });
+ }
+ }
+ }
+ catch (Exception ex) { ExceptionManager.Publish(ex); }
+ return roles;
+ }, CacheSection.RetrieveDescByKey(RoleDataKey));
+ return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
+ }
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static bool SaveRolesByUserId(int id, string value)
+ {
+ //UNDONE: 编写通过用户ID保存当前授权角色的方法
+ return true;
+ }
+
+ ///
+ ///
+ ///
+ ///
+ public static IEnumerable RetrieveRolesByUserId()
+ {
+ //UNDONE: 编写通过用户ID获取所有角色的方法
+ return new List() {
+ new Role() { ID = 1, RoleName = "TestRole1", Description = "测试角色1" },
+ new Role() { ID = 2, RoleName = "TestRole2", Description = "测试角色2" }
+ };
+ }
+ ///
+ /// 删除角色表
+ ///
+ ///
+ public static bool DeleteRole(string IDs)
+ {
+ bool ret = false;
+ if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret;
+ try
+ {
+ string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles where ID in ({0})", IDs);
+ using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
+ {
+ DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
+ ClearCache();
+ ret = true;
+ }
+ }
+ catch (Exception ex)
+ {
+ ExceptionManager.Publish(ex);
+ }
+ return ret;
+ }
+
+ ///
+ /// 保存新建/更新的角色信息
+ ///
+ ///
+ ///
+ public static bool SaveRole(Role p)
+ {
+ if (p == null) throw new ArgumentNullException("p");
+ bool ret = false;
+ if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50);
+ if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
+ string sql = p.ID == 0 ?
+ "Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
+ "Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID";
+ try
+ {
+ using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
+ {
+ cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
+ cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName, ParameterDirection.Input));
+ cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
+ DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
+ }
+ ret = true;
+ ClearCache();
+ }
+ catch (DbException ex)
+ {
+ ExceptionManager.Publish(ex);
+ }
+ return ret;
+ }
+
+ // 更新缓存
+ private static void ClearCache()
+ {
+ CacheManager.Clear(key => key.Contains("RoleData-"));
+ }
+ }
+}
\ No newline at end of file
diff --git a/Bootstrap.DataAccessTests/RoleHelperTests.cs b/Bootstrap.DataAccessTests/RoleHelperTests.cs
index 7d114c54..50230fdf 100644
--- a/Bootstrap.DataAccessTests/RoleHelperTests.cs
+++ b/Bootstrap.DataAccessTests/RoleHelperTests.cs
@@ -1,49 +1,49 @@
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using System.Linq;
-
-namespace Bootstrap.DataAccess.Tests
-{
- [TestClass()]
- public class RoleTests
- {
- [TestMethod()]
- public void SaveRoleTest()
- {
- Role role1 = new Role()
- {
- RoleName = "管理员",
- Description = "可以读写所有内容"
- };
- var result1 = RoleHelper.SaveRole(role1);
- Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句");
- Role role2 = new Role()
- {
- ID = 1,
- RoleName = "管理员",
- Description = "读写所有内容"
- };
- var result2 = RoleHelper.SaveRole(role2);
- Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句");
- }
- [TestMethod()]
- public void RetrieveRoleTest()
- {
- var result = RoleHelper.RetrieveRole("1");
- Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
- result = RoleHelper.RetrieveRole();
- Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
- }
- [TestMethod()]
- public void DeleteRoleTest()
- {
- RoleHelper.SaveRole(new Role()
- {
- ID = 0,
- RoleName = "RoleUnitTest",
- Description = string.Empty
- });
- var role = RoleHelper.RetrieveRole().FirstOrDefault(r => r.RoleName == "RoleUnitTest");
- Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败");
- }
- }
-}
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Linq;
+
+namespace Bootstrap.DataAccess.Tests
+{
+ [TestClass()]
+ public class RoleTests
+ {
+ [TestMethod()]
+ public void SaveRoleTest()
+ {
+ Role role1 = new Role()
+ {
+ RoleName = "管理员",
+ Description = "可以读写所有内容"
+ };
+ var result1 = RoleHelper.SaveRole(role1);
+ Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句");
+ Role role2 = new Role()
+ {
+ ID = 1,
+ RoleName = "管理员",
+ Description = "读写所有内容"
+ };
+ var result2 = RoleHelper.SaveRole(role2);
+ Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句");
+ }
+ [TestMethod()]
+ public void RetrieveRoleTest()
+ {
+ var result = RoleHelper.RetrieveRoles("1");
+ Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
+ result = RoleHelper.RetrieveRoles();
+ Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
+ }
+ [TestMethod()]
+ public void DeleteRoleTest()
+ {
+ RoleHelper.SaveRole(new Role()
+ {
+ ID = 0,
+ RoleName = "RoleUnitTest",
+ Description = string.Empty
+ });
+ var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest");
+ Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败");
+ }
+ }
+}