当前位置:网站首页>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("处理失败!");
}
边栏推荐
- KANO模型——确定需求优先级的神器
- stm32-WS2812 PWM+DMA(自己写库函数)
- LiDAR Snowfall Simulation for Robust 3D Object Detection
- Mei cole studios - fifth training DjangoWeb application framework + MySQL database
- CVPR2022——A VERSATILE MULTI-VIEW FRAMEWORK
- 通用的 kernel和 userspace Makefile
- 安全帽识别
- CVPR2020: Seeing Through Fog Without Seeing Fog
- 防盗链——防止其他页面通过url直接访问本站资源
- Safety helmet recognition system
猜你喜欢
音乐竞品分析:酷狗、QQ音乐、网易云、酷我、汽水音乐
stm32-WS2812 PWM+DMA(自己写库函数)
HTTP缓存机制详解
AI-based intelligent image recognition: 4 different industry applications
Mei cole studios - sixth DjangoWeb application framework + MySQL database training
解决jupyter中import torch出错问题
安全帽识别算法
LiDAR Snowfall Simulation for Robust 3D Object Detection
张小龙的微信公开课(2019年)
AI智能图像识别的工作原理及行业应用
随机推荐
The working principle and industry application of AI intelligent image recognition
Node-1.高性能服务器
HTTP缓存机制详解
智慧工地 安全帽识别系统
The selection points you need to know about the helmet identification system
arduino的esp32环境搭建(不需要翻墙,不需要离线安装)
OpenPCDet installs the latest version: spconv in one step
用正则验证文件名是否合法
梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第四次培训
Robust 3D Object Detection in Cold Weather Conditions
Maykle Studio - Second Training in HarmonyOS App Development
四大组件之一BroadCast(其一)
目标检测前言
产品经理人物推荐
Robust 3D Object Detection in Cold Weather Conditions
Realize data exchange between kernel and userspace through character device virtual file system (passed based on kernel 5.8 test)
安全帽识别系统-为安全生产保驾护航
物联网IOT 固件升级
通用的 kernel和 userspace Makefile
Mei cole studios - fifth training DjangoWeb application framework + MySQL database