From 2ef69da9e1fc808356a683f2f50e9188f042ce02 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Thu, 2 Jun 2022 10:57:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=20GroupService?= =?UTF-8?q?=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/GroupService.cs | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs index bd850888..bcccf0f9 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs @@ -17,26 +17,34 @@ class GroupService : IGroup private const string GroupServiceGetGroupsByRoleIdCacheKey = "GroupService-GetGroupsByRoleId"; - private IDatabase Database { get; } + private IDBManager DBManager { get; } /// /// /// /// - public GroupService(IDatabase db) => Database = db; + public GroupService(IDBManager db) => DBManager = db; /// /// /// /// - public List GetAll() => CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => Database.Fetch()); + public List GetAll() => CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => + { + using var db = DBManager.Create(); + return db.Fetch(); + }); /// /// /// /// /// - public List GetGroupsByUserId(string? userId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByUserIdCacheKey}-{userId}", entry => Database.Fetch("select GroupID from UserGroup where UserID = @0", userId)); + public List GetGroupsByUserId(string? userId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByUserIdCacheKey}-{userId}", entry => + { + using var db = DBManager.Create(); + return db.Fetch("select GroupID from UserGroup where UserID = @0", userId); + }); /// /// @@ -47,17 +55,18 @@ class GroupService : IGroup public bool SaveGroupsByUserId(string? userId, IEnumerable groupIds) { var ret = false; + using var db = DBManager.Create(); try { - Database.BeginTransaction(); - Database.Execute("delete from UserGroup where UserID = @0", userId); - Database.InsertBatch("UserGroup", groupIds.Select(g => new { GroupID = g, UserID = userId })); - Database.CompleteTransaction(); + db.BeginTransaction(); + db.Execute("delete from UserGroup where UserID = @0", userId); + db.InsertBatch("UserGroup", groupIds.Select(g => new { GroupID = g, UserID = userId })); + db.CompleteTransaction(); ret = true; } catch (Exception) { - Database.AbortTransaction(); + db.AbortTransaction(); throw; } if (ret) @@ -72,7 +81,11 @@ class GroupService : IGroup /// /// /// - public List GetGroupsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByRoleIdCacheKey}-{roleId}", entry => Database.Fetch("select GroupID from RoleGroup where RoleID = @0", roleId)); + public List GetGroupsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByRoleIdCacheKey}-{roleId}", entry => + { + using var db = DBManager.Create(); + return db.Fetch("select GroupID from RoleGroup where RoleID = @0", roleId); + }); /// /// @@ -83,17 +96,18 @@ class GroupService : IGroup public bool SaveGroupsByRoleId(string? roleId, IEnumerable groupIds) { var ret = false; + using var db = DBManager.Create(); try { - Database.BeginTransaction(); - Database.Execute("delete from RoleGroup where RoleID = @0", roleId); - Database.InsertBatch("RoleGroup", groupIds.Select(g => new { GroupID = g, RoleID = roleId })); - Database.CompleteTransaction(); + db.BeginTransaction(); + db.Execute("delete from RoleGroup where RoleID = @0", roleId); + db.InsertBatch("RoleGroup", groupIds.Select(g => new { GroupID = g, RoleID = roleId })); + db.CompleteTransaction(); ret = true; } catch (Exception) { - Database.AbortTransaction(); + db.AbortTransaction(); throw; }