当前位置:网站首页>Castle. Dynamic proxy implements transaction unit control
Castle. Dynamic proxy implements transaction unit control
2022-04-23 11:42:00 【Control life】
using System;
using System.Collections.Generic;
using System.Text;
namespace Initial.Extensions
{
/// <summary>
/// The unit of work
/// Only for property marking
/// </summary>
public class UnitOfWorkAttribute : Attribute
{
}
}
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using DataCommon.UnitOfWork;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace Initial
{
public class UnitOfWorkIInterceptor : IInterceptor
{
#region necessary
private readonly ILogger _logger;
private readonly IUnitOfWork _unitOfWork;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly HttpContext _httpContext;
#endregion
public UnitOfWorkIInterceptor(
ILogger logger,
IUnitOfWork unitOfWork,
IHttpContextAccessor httpContextAccessor
)
{
_logger = logger;
_unitOfWork = unitOfWork;
_httpContextAccessor = httpContextAccessor;
_httpContext = httpContextAccessor.HttpContext;
}
public void Intercept(IInvocation invocation)
{
MethodInfo methodInfo = invocation.MethodInvocationTarget;
if (methodInfo == null)
methodInfo = invocation.Method;
UnitOfWorkAttribute transaction = methodInfo.GetCustomAttributes<UnitOfWorkAttribute>(true).FirstOrDefault();
// If it's marked [UnitOfWork], And not in transaction nesting .
if (transaction != null)
{
try
{
// Open transaction
_unitOfWork.BeginTransaction();
invocation.Proceed();
// Commit transaction
var result = ((Task<bool>)invocation.ReturnValue).Result;
_unitOfWork.Commit();
}
catch (Exception ex)
{
// Roll back
_unitOfWork.Rollback();
_logger.Info($"[{(_httpContextAccessor.HttpContext ?? _httpContext)?.TraceIdentifier}]" +
$"_{methodInfo.Name} Perform abnormal {ex.ToString()}");
throw new BusinessException(" Submit exception ");
}
}
else
{
// If there is no sign [UnitOfWork], Direct execution method
invocation.Proceed();
}
}
}
}
using Castle.Core;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repository
{
[Interceptor(typeof(UnitOfWorkIInterceptor))]
public class TemplateRepository : ITemplateRepository
{
#region necessary
private readonly ILogger _logger;
private readonly IDBInstance _IDBInstance;
#endregion
public TemplateRepository(
ILogger logger,
IDBInstance IDBInstance
)
{
_logger = logger;
_IDBInstance = IDBInstance;
}
#region Specification for addition, deletion, modification and query of single table
#endregion
#region Multi meter operation
[UnitOfWork]
public async Task<bool> AddTepWithLogAsync(MSE_RPT_TEMPLATE mSE_RPT_TEMPLATE,
MSE_RPT_TEMPLATE_LOG mSE_RPT_TEMPLATE_LOG)
{
await _IDBInstance.DBOperation.InsertAsync(mSE_RPT_TEMPLATE);
await _IDBInstance.DBOperation.InsertAsync(mSE_RPT_TEMPLATE_LOG);
return true;
}
[UnitOfWork]
public async Task<bool> UpdateTepWithLogAsync(MSE_RPT_TEMPLATE mSE_RPT_TEMPLATE,
MSE_RPT_TEMPLATE_LOG mSE_RPT_TEMPLATE_LOG)
{
await _IDBInstance.DBOperation.UpdateAsync(mSE_RPT_TEMPLATE);
await _IDBInstance.DBOperation.InsertAsync(mSE_RPT_TEMPLATE_LOG);
return true;
}
#endregion
}
}
1、 The first code block is mainly for marking the methods that need transactions
2、 The second code block mainly controls the transaction ;
版权声明
本文为[Control life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231131472894.html
边栏推荐
- MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
- Laravel增加自定义助手函数
- How Im expressions work (5.3)
- IM 体系结构:CPU架构:SIMD向量处理(IM-2.3)
- C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
- nacos基础(8):登录管理
- 1.Electron开发环境搭建
- ImportError: libX11.so.6: cannot open shared object file: No such file or directory
- Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
- 远程访问家里的树莓派(上)
猜你喜欢
随机推荐
golang之筆試題&面試題01
How does QT turn qwigdet into qdialog
MySQL 的主从复制配置
第四章 为IM 启用填充对象之强制填充In-Memory对象:教程(IM 4.7)
Pytorch neural network trainer
Chapter 4 specifies the attribute of the inmemory column on the no inmemory table for im enabled filling objects: examples (Part IV of im-4.4)
kettle复制记录到结果和从结果获取记录使用
解读机器人创造出来的艺术
数据库如何填充IM表达式(IM 5.4)
Laravel admin time range selector daterange default value problem
Laravel always returns JSON response
第五章 使用In-Memory表达式优化查询(IM 5.1)
The fourth chapter is about enabling and disabling the im column storage of table space for im enabled filling objects (IM 4.5)
Im architecture: CPU architecture: SIMD vector processing (im-2.3)
Yunna | how to manage the company's fixed assets and how to manage fixed assets
云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
laravel 永远返回 JSON 响应
Analyze the rules for the use of robots with good performance
Siri gave the most embarrassing social death moment of the year
Docker MySQL master-slave backup