Skip to content

Commit 3990813

Browse files
committed
改造拦截器 增加事物拦截器
1 parent d621409 commit 3990813

File tree

13 files changed

+104
-46
lines changed

13 files changed

+104
-46
lines changed

ant.mgr/DBModels/DBModels/DbModel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
<ItemGroup>
28-
<PackageReference Include="AntData.Core.Mysql" Version="1.4.0" />
28+
<PackageReference Include="AntData.Core.Mysql" Version="1.4.1" />
2929
</ItemGroup>
3030

3131
<ItemGroup>

ant.mgr/Infrastructure/Infrastructure/CodeGen/CrudTemplete/Respository.tpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ using System.Collections.Generic;
1414
using System.Linq;
1515
using System.Threading.Tasks;
1616
using ViewModels.Reuqest;
17-
17+
using Autofac.Aspect;
1818

1919
namespace Repository
2020
{
21-
[Component(Interceptor = typeof(AsyncTimeoutInterceptor))]
21+
[Component(typeof(I{{ModelClassName}}Respository))]
22+
[Aspect(InterceptorType.Interface)]
2223
public class {{ModelClassName}}Respository : BaseRepository<{{ModelClassName}}>, I{{ModelClassName}}Respository
2324
{
2425

ant.mgr/Infrastructure/Infrastructure/Infrastructure.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="DotLiquid" Version="2.0.298" />
18-
<PackageReference Include="HtmlAgilityPack" Version="1.11.4" />
19-
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
20-
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.1.1" />
21-
<PackageReference Include="NLog" Version="4.5.11" />
17+
<PackageReference Include="DotLiquid" Version="2.0.314" />
18+
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
19+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
20+
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
21+
<PackageReference Include="NLog" Version="4.6.7" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

ant.mgr/Mapping/Mapping/Mapping.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="AutoMapper" Version="5.1.1" />
12+
<PackageReference Include="AutoMapper" Version="9.0.0" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

ant.mgr/Repository/Repository/AdminRepository/AccountRespository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections.Generic;
1212
using System.Linq;
1313
using System.Threading.Tasks;
14+
using Autofac.Aspect;
1415
using DbModel;
1516
using Repository.Interceptors;
1617
using ViewModels.Reuqest;
@@ -20,7 +21,8 @@ namespace Repository
2021
/// <summary>
2122
/// 系统用户
2223
/// </summary>
23-
[Component(Interceptor = typeof(AsyncTimeoutInterceptor))]
24+
[Component(typeof(IAccountRespository))]
25+
[Aspect(InterceptorType.Interface)]
2426
public class AccountRespository : BaseRepository<SystemUsers>, IAccountRespository
2527
{
2628

ant.mgr/Repository/Repository/AdminRepository/CommonRespository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.IO;
1414
using System.Linq;
1515
using System.Reflection;
16+
using Autofac.Aspect;
1617
using Repository.Interceptors;
1718
using ViewModels.Reuqest;
1819

@@ -21,7 +22,8 @@ namespace Repository
2122
/// <summary>
2223
/// 公共处理
2324
/// </summary>
24-
[Component(Interceptor = typeof(AsyncTimeoutInterceptor))]
25+
[Component(typeof(ICommonRespository))]
26+
[Aspect(InterceptorType.Interface)]
2527
public class CommonRespository : BaseRepository, ICommonRespository
2628
{
2729

ant.mgr/Repository/Repository/AdminRepository/MenuRespository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections.Concurrent;
1313
using System.Collections.Generic;
1414
using System.Linq;
15+
using Autofac.Aspect;
1516
using DbModel;
1617
using Repository.Interceptors;
1718
using ViewModels.Reuqest;
@@ -21,7 +22,8 @@ namespace Repository
2122
/// <summary>
2223
/// 菜单处理
2324
/// </summary>
24-
[Component(Interceptor = typeof(AsyncTimeoutInterceptor))]
25+
[Component(typeof(IMenuRespository))]
26+
[Aspect(InterceptorType.Interface)]
2527
public class MenuRespository : BaseRepository<SystemMenu>, IMenuRespository
2628
{
2729

ant.mgr/Repository/Repository/AdminRepository/RoleRespository.cs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Linq;
1212
using System.Threading.Tasks;
13+
using Autofac.Aspect;
1314
using DbModel;
1415
using Repository.Interceptors;
1516
using ViewModels.Reuqest;
@@ -19,7 +20,8 @@ namespace Repository
1920
/// <summary>
2021
/// 角色权限管理
2122
/// </summary>
22-
[Component(Interceptor = typeof(AsyncTimeoutInterceptor))]
23+
[Component(typeof(IRoleRespository))]
24+
[Aspect(InterceptorType.Interface)]
2325
public class RoleRespository : BaseRepository<SystemRole>, IRoleRespository
2426
{
2527
/// <summary>
@@ -141,6 +143,7 @@ public async Task<Tuple<long, List<SystemRole>>> GetList(RoleVm model, Token use
141143
/// </summary>
142144
/// <param name="role"></param>
143145
/// <returns></returns>
146+
[EnableTransactionScope]
144147
public async Task<string> AddRole(AddRoleVm role, Token user)
145148
{
146149
if (role == null)
@@ -200,32 +203,26 @@ public async Task<string> AddRole(AddRoleVm role, Token user)
200203
}
201204
else
202205
{
203-
var update = false;
204-
this.DB.UseTransaction(con =>
205-
{
206-
//更新角色
207-
var updateResult = con.Tables.SystemRole.Where(r => r.Tid.Equals(role.Tid))
208-
.Set(r => r.DataChangeLastTime, DateTime.Now)
209-
.Set(r => r.RoleName, systemRole.RoleName)
210-
.Set(r => r.Description, systemRole.Description)
211-
.Set(r => r.MenuRights, systemRole.MenuRights)
212-
.Set(r => r.ActionList, systemRole.ActionList)
213-
.Update() > 0;
214-
215-
if (!updateResult)
216-
{
217-
return false;
218-
}
219-
220-
//更新所有角色下的用户菜单权限
221-
con.Tables.SystemUsers.Where(r => r.RoleTid.Equals(role.Tid))
222-
.Set(r => r.MenuRights, systemRole.MenuRights)
223-
.Set(r => r.DataChangeLastTime, DateTime.Now)
224-
.Update();
225-
226-
update = true;
227-
return true;
228-
});
206+
//更新角色
207+
var updateResult = this.Entity.Where(r => r.Tid.Equals(role.Tid))
208+
.Set(r => r.DataChangeLastTime, DateTime.Now)
209+
.Set(r => r.RoleName, systemRole.RoleName)
210+
.Set(r => r.Description, systemRole.Description)
211+
.Set(r => r.MenuRights, systemRole.MenuRights)
212+
.Set(r => r.ActionList, systemRole.ActionList)
213+
.Update() > 0;
214+
215+
if (!updateResult)
216+
{
217+
return Tip.UpdateError;
218+
}
219+
220+
//更新所有角色下的用户菜单权限
221+
var update = Entitys.SystemUsers.Where(r => r.RoleTid.Equals(role.Tid))
222+
.Set(r => r.MenuRights, systemRole.MenuRights)
223+
.Set(r => r.DataChangeLastTime, DateTime.Now)
224+
.Update() > 0;
225+
229226

230227
if (!update)
231228
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Transactions;
2+
using Autofac;
3+
using Autofac.Annotation;
4+
using Autofac.Aspect;
5+
using Castle.DynamicProxy;
6+
7+
namespace Repository.Interceptors
8+
{
9+
using System;
10+
using System.Threading.Tasks;
11+
12+
13+
/// <summary>
14+
/// 事物
15+
/// </summary>
16+
public class EnableTransactionScope : PointcutAttribute
17+
{
18+
/// <summary>
19+
/// 无参数返回拦截器
20+
/// </summary>
21+
/// <param name="context"></param>
22+
/// <param name="invocation"></param>
23+
/// <param name="proceed"></param>
24+
/// <returns></returns>
25+
public override async Task InterceptAsync(IComponentContext context, IInvocation invocation, Func<IInvocation, Task> proceed)
26+
{
27+
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
28+
{
29+
await proceed(invocation);
30+
31+
scope.Complete();
32+
}
33+
}
34+
35+
/// <summary>
36+
/// 有参数返回拦截器
37+
/// </summary>
38+
/// <typeparam name="TResult"></typeparam>
39+
/// <param name="context"></param>
40+
/// <param name="invocation"></param>
41+
/// <param name="proceed"></param>
42+
/// <returns></returns>
43+
public override async Task<TResult> InterceptAsync<TResult>(IComponentContext context, IInvocation invocation, Func<IInvocation, Task<TResult>> proceed)
44+
{
45+
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
46+
{
47+
var r = await proceed(invocation);
48+
49+
scope.Complete();
50+
51+
return r;
52+
}
53+
}
54+
}
55+
}

ant.mgr/Repository/Repository/Repository.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Autofac.Annotation" Version="2.0.2" />
12+
<PackageReference Include="Autofac.Annotation" Version="2.0.6" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

ant.mgr/ServicesModel/ServicesModel/ServicesModel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
<ItemGroup>
15-
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
15+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

ant.mgr/mgr.core/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6666

6767
#region AntORM
6868
//文档:https://github.com/yuzd/AntData.ORM
69-
AntData.ORM.Common.Configuration.UseDBConfig(Configuration);
70-
AntData.ORM.Common.Configuration.Linq.AllowMultipleQuery = true;
69+
app.UseAntData();
7170
#endregion
7271

7372
#region NLOG

ant.mgr/mgr.core/ant.mgr.core.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6-
<Version>2.2</Version>
6+
<Version>2.3</Version>
77
</PropertyGroup>
88

99
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -13,11 +13,11 @@
1313

1414

1515
<ItemGroup>
16-
<PackageReference Include="Autofac.Annotation" Version="2.0.2" />
16+
<PackageReference Include="Autofac.Annotation" Version="2.0.6" />
1717
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.0" />
1818
<PackageReference Include="Microsoft.AspNetCore.App" />
1919
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
20-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
20+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
2121
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
2222
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" />
2323
<PackageReference Include="NLog.MailKit" Version="3.1.0" />

0 commit comments

Comments
 (0)