使用什么-我的体系结构中的SqlTransaction或TransactionScope [英] What to use - SqlTransaction or TransactionScope in my architecture

查看:45
本文介绍了使用什么-我的体系结构中的SqlTransaction或TransactionScope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用包含以下内容的3层建筑SqlHelper-> DAL(数据访问层)-> BAL-> UI

We are using 3 layered architechture which contains SqlHelper -> DAL(Data access layer) -> BAL -> UI

DAL中的任何类都可以像调用任何BAL一样调用另一个BAL或DAL一样调用另一个DAL.

Any class within a DAL can call another DAL same way any BAL can call another BAL or DAL of its own.

例如

class Customer_DAL { display_CusDal(); }
class Customer_BAL { display_CusBal(); }   

class Product_DAL { display_ProDal(); }
class Product_BAL { display_ProBal(); }


display_CusDal()
{
 //call display_ProDal()
 //Do some work
}

display_CusDal 函数应作为事务运行,这意味着实习生意味着在此函数内进行的任何插入都应与事务对象相关联.

display_CusDal function should run as a transaction which intern means any insertion made within this function should be associated with transaction object.

由于display_CusDal可以调用display_ProDal,后者可能会或可能不会在另一个事务中的表中插入数据,因此我需要在事务中处理这些问题.

Since display_CusDal can call display_ProDal which might or might not be inserting data in table in another transaction, so I need to handle these in transactions.

我应该使用哪种方法.

推荐答案

SqlTransactions仅适用于SQL,并且要求您显式添加它们的用法.TransactionScope的优点是可以有更多的参与方加入交易.因此,不仅是SqlTransactions,而且还有其他类型的事务.使用事务作用域时,还会删除很多代码.

SqlTransactions only work with SQL and require you to add usage of them explicitly. The advantage of the TransactionScope is that more parties can join the transaction. So not only SqlTransactions, but also transactions from other types. Also a lot of code is taken away when using the transaction scope.

另一方面,使用事务范围可以引入很多奇怪的行为.在模糊的情况下,资料会升级为MSDTC.

On the other hand, using transaction scope can introduce quite some wierd behavior. Stuff gets escalated to MSDTC in vague situations.

根据您的情况,事务范围听起来像是最佳选择.通过这种方式,很容易将在 Customer_DAL Product_DAL 上执行的多个动作或在同一DAL上的多个动作合并到一个事务中.

For your situation the transaction scope sounds like the best option. Via this way it is very easy to join multiple actions executed on the Customer_DAL and Product_DAL, or multiple actions on the same DAL, in a single transaction.

  • 易于理解.
  • 明确
  • 您拥有完全的控制权.
  • 需要更多代码.
  • 想参加更多聚会的人.
  • 更难参与其他交易.
  • 将事务绑定到SQL(因此在切换到其他持久存储时不提供支持)
  • 易于使用.
  • 使代码保持简单,您无需担心 Customer_DAL Product_DAL 混合.
  • 开箱即用的出色交易管理.使用多个数据库时包括升级到MSDTC.
  • 不清楚何时会升级.

这篇关于使用什么-我的体系结构中的SqlTransaction或TransactionScope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆