当前位置:网站首页>ASP.NET MVC 4中实现action的事务功能
ASP.NET MVC 4中实现action的事务功能
2022-08-11 05:25:00 【Three Big Stones】
有时候我们的业务逻辑可能涉及多个数据库操作,如下代码片段所示,执行方法Option时,具体执行了3个对应的数据库操作方法,如果Option1执行成功,Option2失败,不采用事务的情况,Option1的结果会影响到数据库,Option2的结果不会影响数据库,这样会出现数据不一致的问题,在针对Option方法增加了事务的情况下,所有的数据库操作,要么全部成功,要么全部失败:
public bool Option1()
{
数据库操作1...
}
public bool Option2()
{
数据库操作2...
}
public bool Option3()
{
数据库操作3...
}
public bool Option()
{
this.Option1();
this.Option2();
this.Option3();
...
}事务的具体实现主要使用了TransactionScope类,具体介绍可参考官方文档:使用事务范围实现隐式事务 - .NET Framework | Microsoft Docs
下面介绍如何在Asp.NET MVC项目中支持Action方法的事务功能。
1、项目中引入程序集System.Transactions
2、实现过滤器,定义自己的事务功能,用于拦截Action,注入事务功能。
/// <summary>
/// 事务拦截器,在action方法上增加该特性,可以自动增加事务功能,主要是利用了TransactionScope这个神器类
/// </summary>
public class TransactionScopeAttribute: ActionFilterAttribute
{
public TransactionScopeAttribute()
{
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
using (var txScope = new TransactionScope(TransactionScopeOption.Required))
{
try
{
//这里需要手动调用action方法,并且将执行结果赋值给filterContext,并且 return,否则该方法执行完后,目标action方法会重复执行
var result = filterContext.ActionDescriptor.Execute(filterContext.Controller.ControllerContext, filterContext.ActionParameters);
txScope.Complete();
filterContext.Result = (ContentResult)result;
return;
}
catch (Exception e)
{
throw e;
}
}
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
}3、在需要支持事务功能的action方法上增加特性TransactionScopeAttribute
/// <summary>
/// 批准操作
/// </summary>
/// <param name="keyValue">主键</param>
/// <returns></returns>
[TransactionScopeAttribute()]
public ActionResult PassAuthorizor(string keyValue, MES_ReleaseEntity entity)
{
if (string.IsNullOrEmpty(keyValue))
{
return Error($"传递参数keyValue不能为空!");
}
var rst = mes_releasebll.PassAuthorizor(keyValue, entity);
if (rst)
return Success("处理成功");
else
return Error("处理失败!");
}边栏推荐
- Safety helmet recognition - construction safety "regulator"
- The kernel communicates with user space through character devices
- The selection points you need to know about the helmet identification system
- Safety helmet recognition system
- Socket 网络协议 等
- 梅科尔工作室-深度学习第二讲 BP神经网络
- 梅科尔工作室-华为云ModelArts第二次培训
- 我心仪的数据集—目标检测为主
- Toolbar 和 DrawerLayout 滑动菜单
- 通过字符设备虚拟文件系统实现kernel和userspace数据交换(基于kernel 5.8测试通过)
猜你喜欢

恶劣天气 3D 目标检测数据集收集

KANO模型——确定需求优先级的神器

【调试记录1】提高MC3172浮点运算能力,IQmath库的获取与导入使用教程

360°大视野安全帽识别系统-深度学习智能视频分析

AIDL 简介以及使用

Maykel Studio - Django Web Application Framework + MySQL Database Third Training

基于ijkplayer 0.8.8编译的完整so. libijkffmpeg.so等,支持ssl h265, rm, rmvb

2022年最新安全帽佩戴识别系统

Joint 3D Instance Segmentation and Object Detection for Autonomous Driving

Pay “Attention” to Adverse Weather
随机推荐
微文案也能提升用户体验
CMT2380F32模块开发11-RTC例程
需求文档(PRD)撰写指南
产品版本号是如何确定的
Node-2.垃圾回收机制
关于安全帽识别系统,你需要知道的选择要点
SCNet: Semantic Consistency Networks for 3D Object Detection
如何快速转行做产品经理
Maykel Studio - Django Web Application Framework + MySQL Database Second Training
CMT2380F32模块开发3-GPIO例程
The selection points you need to know about the helmet identification system
梅科尔工作室-HarmonyOS应用开发第一次培训
Diagnostic Log and Trace——DLT 离线日志存储
Diagnostic Log and Trace——dlt的编译和安装
用正则验证文件名是否合法
Zhejiang University School of Software 2020 Guarantee Research Computer Real Question Practice
HTTP缓存机制详解
推出 Space Marketplace 测试版 | 新发布
RecycleView
弱监督语义分割CLIMS(CVPR2022)