当前位置:网站首页>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
边栏推荐
猜你喜欢

Summary of the relationship among GPU, CUDA and cudnn
![抓包整理————tcp 协议[八]](/img/ae/4957a997af725a1bf3f61cb24fc717.png)
抓包整理————tcp 协议[八]

分享两个实用的shell脚本

Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)

运行报错:找不到或无法加载主类 com.xxx.Application

Use kettle to copy records to and get records from results

积极参与中学机器人竞赛的意义

云呐|如何管理好公司的固定资产,固定资产管理怎么做

力扣-70.爬楼梯

QT 64 bit static version display gif
随机推荐
User interface and im expression (IM 5.6)
nacos基础(5):nacos配置入门
《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
2022 love analysis · panoramic report of industrial Internet manufacturers
Sigmoid function of activation function
The listing of saiweidian Technology Innovation Board broke: a decrease of 26% and the market value of the company was 4.4 billion
Share two practical shell scripts
Blog post navigation (real-time update)
laravel 永远返回 JSON 响应
Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5
PyTorch 神经网络训练器
Laravel admin form validation
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
Who said you should know PS? This open-source artifact can also be pulled in batch, and the effect is outstanding!
远程访问家里的树莓派(上)
C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
MQ is easy to use in laravel
[Web 每日一练] 八色拼图(float)
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
PSCP 基本使用