From e9c5bd2c08789bb08e2ad138cface6e888fb807c Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Thu, 2 Jun 2022 10:57:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20Role=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/RoleService.cs | 64 +++++++++++++------ 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs index fa549f1a..1a2d10e3 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs @@ -19,25 +19,44 @@ class RoleService : IRole private const string RoleServiceGetRolesByMenuIdCacheKey = "RoleService-GetRolesByMenusId"; - private IDatabase Database { get; } + private IDBManager DBManager { get; } /// /// /// /// - public RoleService(IDatabase db) => Database = db; + public RoleService(IDBManager db) + { + DBManager = db; + } /// /// /// /// - public List GetAll() => CacheManager.GetOrAdd(RoleServiceGetAllCacheKey, entry => CacheManager.GetOrAdd(RoleServiceGetAllCacheKey, entry => Database.Fetch())); + public List GetAll() => CacheManager.GetOrAdd(RoleServiceGetAllCacheKey, entry => CacheManager.GetOrAdd(RoleServiceGetAllCacheKey, entry => + { + using var db = DBManager.Create(); + return db.Fetch(); + })); - public List GetRolesByGroupId(string? groupId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByGroupIdCacheKey}-{groupId}", entry => Database.Fetch("select RoleID from RoleGroup where GroupID = @0", groupId)); + public List GetRolesByGroupId(string? groupId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByGroupIdCacheKey}-{groupId}", entry => + { + using var db = DBManager.Create(); + return db.Fetch("select RoleID from RoleGroup where GroupID = @0", groupId); + }); - public List GetRolesByUserId(string? userId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByUserIdCacheKey}-{userId}", entry => Database.Fetch("select RoleID from UserRole where UserID = @0", userId)); + public List GetRolesByUserId(string? userId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByUserIdCacheKey}-{userId}", entry => + { + using var db = DBManager.Create(); + return db.Fetch("select RoleID from UserRole where UserID = @0", userId); + }); - public List GetRolesByMenuId(string? menuId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByMenuIdCacheKey}-{menuId}", entry => Database.Fetch("select RoleID from NavigationRole where NavigationID = @0", menuId)); + public List GetRolesByMenuId(string? menuId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByMenuIdCacheKey}-{menuId}", entry => + { + using var db = DBManager.Create(); + return db.Fetch("select RoleID from NavigationRole where NavigationID = @0", menuId); + }); /// /// @@ -48,17 +67,18 @@ class RoleService : IRole public bool SaveRolesByGroupId(string? groupId, IEnumerable roleIds) { var ret = false; + using var db = DBManager.Create(); try { - Database.BeginTransaction(); - Database.Execute("delete from RoleGroup where GroupID = @0", groupId); - Database.InsertBatch("RoleGroup", roleIds.Select(g => new { RoleID = g, GroupID = groupId })); - Database.CompleteTransaction(); + db.BeginTransaction(); + db.Execute("delete from RoleGroup where GroupID = @0", groupId); + db.InsertBatch("RoleGroup", roleIds.Select(g => new { RoleID = g, GroupID = groupId })); + db.CompleteTransaction(); ret = true; } catch (Exception) { - Database.AbortTransaction(); + db.AbortTransaction(); throw; } if (ret) @@ -77,17 +97,18 @@ class RoleService : IRole public bool SaveRolesByUserId(string? userId, IEnumerable roleIds) { var ret = false; + using var db = DBManager.Create(); try { - Database.BeginTransaction(); - Database.Execute("delete from UserRole where UserID = @0", userId); - Database.InsertBatch("UserRole", roleIds.Select(g => new { RoleID = g, UserID = userId })); - Database.CompleteTransaction(); + db.BeginTransaction(); + db.Execute("delete from UserRole where UserID = @0", userId); + db.InsertBatch("UserRole", roleIds.Select(g => new { RoleID = g, UserID = userId })); + db.CompleteTransaction(); ret = true; } catch (Exception) { - Database.AbortTransaction(); + db.AbortTransaction(); throw; } if (ret) @@ -100,17 +121,18 @@ class RoleService : IRole public bool SaveRolesByMenuId(string? menuId, IEnumerable roleIds) { var ret = false; + using var db = DBManager.Create(); try { - Database.BeginTransaction(); - Database.Execute("delete from NavigationRole where NavigationID = @0", menuId); - Database.InsertBatch("NavigationRole", roleIds.Select(g => new { RoleID = g, NavigationID = menuId })); - Database.CompleteTransaction(); + db.BeginTransaction(); + db.Execute("delete from NavigationRole where NavigationID = @0", menuId); + db.InsertBatch("NavigationRole", roleIds.Select(g => new { RoleID = g, NavigationID = menuId })); + db.CompleteTransaction(); ret = true; } catch (Exception) { - Database.AbortTransaction(); + db.AbortTransaction(); throw; } if (ret)