当前位置:网站首页>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
边栏推荐
- Link sorting of tutorials such as assembly language running environment setting
- 少儿编程结构的改变之路
- Laravel增加自定义助手函数
- Share two practical shell scripts
- Summary of convolution layer and pooling layer
- Nacos Foundation (9): Nacos configuration management from single architecture to microservices
- 怎么进行固定资产盘点,资产盘点报告如何一键生成
- The way to change children's programming structure
- 零钱兑换II——【LeetCode】
- WIN10 启动后花屏
猜你喜欢

少儿编程结构的改变之路

Use kettle to copy records to and get records from results

激活函数之relu函数

2022 love analysis · panoramic report of industrial Internet manufacturers

Cognition and R & D technology of micro robot

nacos基础(5):nacos配置入门

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

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

The way to change children's programming structure

MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
随机推荐
《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
Laravel always returns JSON response
nacos基础(5):nacos配置入门
Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
RebbitMQ的初步了解
[Web 每日一练] 八色拼图(float)
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)
nacos基础(8):登录管理
Laravel admin time range selector daterange default value problem
Simple construction of rebbitmq
解读机器人创造出来的艺术
Master slave replication configuration of MySQL
怎么进行固定资产盘点,资产盘点报告如何一键生成
The fourth chapter is to enable the filling object of IM and enable ADO for im column storage (IM 4.8)
云呐|如何管理好公司的固定资产,固定资产管理怎么做
第四章 为IM 启用填充对象之启用和禁用表空间的IM列存储(IM 4.5)
Exploring the equipment and teaching of robot education
Database design of simple voting system
Golang Pen & interview 01
博客文章导航(实时更新)