使用Dapper哪个交易更好:BEGIN TRAN或TransactionScope? [英] Which transaction is better with Dapper: BEGIN TRAN or TransactionScope?

查看:387
本文介绍了使用Dapper哪个交易更好:BEGIN TRAN或TransactionScope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Dapper,我想知道哪种模式更好。

I've just started using Dapper and I was wondering which pattern for transactions would be better.

当我编写SQL时,我更喜欢使用以下脚本中已存在的事务:

When I write SQL I prefer to use the transaction already in the script with:

BEGIN TRAN

-- insert, update etc.

COMMIT

因为它更容易测试,但也有问题与dapper点网
的交易,现在使用.net交易我不确定我应该实际使用哪一个。

because it's easier to test it but there is also the question transaction with dapper dot net where .net transactions are used so now I'm not sure which one I should actually use.

这两种方法相对于另一种方法是否有缺点/缺点?

Does either method have any dis/advantages over the other?

推荐答案

实际上,这与Dapper无关。请参考答案。

Actually, this has nothing to do with Dapper. Refer this answer.

TransactionScope connection.BeginTransaction 或存储过程中的事务决定不在Dapper的范围内。 Dapper只是在ADO.NET的 DBConnection 对象上公开了一些扩展方法,这些扩展方法将查询的输出映射到对象。其余的取决于您。

TransactionScope or connection.BeginTransaction or "Transaction in stored procedure" decision is outside the scope of Dapper. Dapper simply exposes few extension methods on DBConnection object of ADO.NET those map the output of queries to object. Rest is up to you.


  1. TransactionScope 通常用于分布式交易;跨不同数据库的事务可能在不同系统上。这需要在操作系统和SQL Server上进行一些配置,如果没有这些配置将无法正常工作。如果所有查询都针对数据库的单个实例,则不建议这样做。

    如@ImrePühvel的注释中所述:对于单个数据库,当您需要在事务中包含不在您的代码中的代码时,这可能很有用。控制。对于单个数据库,它也不需要特殊的配置。

  1. TransactionScope is generally used for distributed transactions; transaction spanning different databases may be on different system. This needs some configurations on operating system and SQL Server without which this will not work. This is not recommended if all your queries are against single instance of database.
    As @ImrePühvel noted in comments: With single database this may be useful when you need to include the code in transaction that is not under your control. With single database, it does not need special configurations either.

connection.BeginTransaction 是ADO.NET对单个数据库执行事务处理(在C#,VB.NET等中)的语法。

connection.BeginTransaction is ADO.NET syntax to implement transaction (in C#, VB.NET etc.) against single database. This does not work across multiple databases.

存储过程中的事务是针对存储过程中的单个数据库而不是在应用程序代码中实现的。

"Transaction in stored procedure" is implemented against single database in Stored Procedure instead of doing this in application code.

嵌套交易或以上类型的组合是另一个需要讨论的话题;

Nested transactions or combination of above types is another topic for discussion; try searching net for this.

我个人认为是使用UnitOfWork更好地处理交易。我已经在上面的链接中提到了细节。它在内部使用 connection.BeginTransaction

My personal opinion is to use UnitOfWork to handle transactions in better way. I have already mentioned the details in the link above. It uses connection.BeginTransaction internally.

这篇关于使用Dapper哪个交易更好:BEGIN TRAN或TransactionScope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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