当前位置:网站首页>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
边栏推荐
- PCB的注意事项
- IFLYTEK's revenue in 2021 was 18.3 billion yuan: a year-on-year increase of 41% and a net profit of 1.556 billion yuan
- 一文详解头部位姿估计【收藏好文】
- 第四章 为物化视图启用和禁用IM列存储(IM 4.6)
- 力扣-1137.第N个泰波那契数
- Understanding of MQ
- Compress the curl library into a sending string of utf8 and send it with curl library
- Design and practice of the smallest short website system in the whole network
- How imeu is associated with imcu (IM 5.5)
- AcWing 1874. Moo encryption (enumeration, hash)
猜你喜欢

Analyze the rules for the use of robots with good performance

Share two practical shell scripts
![Study notes of C [8] SQL [1]](/img/b3/cb684f3ee14cc4e313be217320d5d6.png)
Study notes of C [8] SQL [1]

Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients

rebbitMQ的简单搭建

Yunna | how to manage the company's fixed assets and how to manage fixed assets

laravel编写Console脚本

分享两个实用的shell脚本

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

运行报错:找不到或无法加载主类 com.xxx.Application
随机推荐
Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
激活函数之sigmoid函数
第四章 为IM 启用填充对象之在NO INMEMORY表上指定INMEMORY列属性:示例(IM-4.4 第四部分)
Golang Pen & interview 01
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Summary of the relationship among GPU, CUDA and cudnn
rebbitMQ的简单搭建
The way to change children's programming structure
Analyze the rules for the use of robots with good performance
汇编语言 运行环境设置等教程链接整理
MQ的了解
激活函数之阶跃函数
laravel编写Console脚本
MQ is easy to use in laravel
解析幼儿教育中steam教育的融合
Laravel adds custom helper functions
How the database fills in IM expressions (IM 5.4)
The fourth chapter is the enable and disable columns of IM enabled fill objects (Part III of im-4.3)
Nacos Foundation (9): Nacos configuration management from single architecture to microservices
Optimize connections using connection groups (IM 6)