TransactionScope和Enterprise Libray 3.0 Data Access Application Block

TransactionScope和Enterprise Libray 3.0 Data Access Application Block,第1张

概述Enterprise Libray 3.0已经发布了,具体可参见TerryLee的 Enterprise Library 3.0 发布.下载了看看,有非常激动人心的更新.我只是看看Data Acces

Enterprise libray 3.0已经发布了,具体可参见TerryLee的 Enterprise Library 3.0 发布.下载了看看,有非常激动人心的更新.我只是看看Data Access Application Block代码,代码中有这个类TransactionScopeConnections,是个内部类,设计意图很明显就是使用数据库的事务模型.我觉得设计为内部类有点瑕疵,我的习惯是事务和提交在业务逻辑层. .NET 2.0的System.Transactions应该是一个更好的选择。就将Data Access Application Block的QuickStart例子代码:

/// <summary>
/// Transfers an amount between two accounts.
/// </summary>
/// <param name="transactionAmount">Amount to transfer.</param>
/// <param name="sourceAccount">Account to be credited.</param>
/// <param name="destinationAccount">Account to be debited.</param>
/// <returns>true if sucessful; otherwise false.</returns>
/// <remarks>Demonstrates executing multiple updates within the
/// context of a transaction.</remarks>
public bool Transfer(int transactionAmount,int sourceAccount,int destinationAccount)
{
bool result = false;

// Create the Database object,using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();

// Two operations,one to credit an account,and one to debit another
// account.
string sqlCommand = "Creditaccount"
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(creditCommand,"AccountID",DbType.Int32,sourceAccount);
db.AddInParameter(creditCommand,"Amount",transactionAmount);

sqlCommand = "Debitaccount"
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(debitCommand,destinationAccount);
db.AddInParameter(debitCommand,transactionAmount);

using (DbConnection connection = db.CreateConnection())
{
connection.open();
DbTransaction transaction = connection.BeginTransaction();

try
{
// Credit the first account
db.ExecuteNonquery(creditCommand,transaction);
// Debit the second account
db.ExecuteNonquery(debitCommand,transaction);

// Commit the transaction
transaction.Commit();

result = true;
}
catch
{
// Rollback transaction
transaction.Rollback();
}
connection.Close();

return result;
}
}

按照TransactionScope类进行改造,试验成功了,代码如下:

public bool Transfer(int transactionAmount,int destinationAccount)
{
bool result = false;
Database database = DatabaseFactory.CreateDatabase();

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
TestCommand1(database,transactionAmount,sourceAccount);
TestCommand2(database,destinationAccount);
scope.Complete();
result = true;
}
return result;

}

private voID TestCommand1(Database db,int transactionAmount,int sourceAccount)
{
string sqlCommand = "Creditaccount"
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(creditCommand,transactionAmount);

// Credit the first account
db.ExecuteNonquery(creditCommand);
}

private voID TestCommand2(Database db,int destinationAccount)
{
string sqlCommand = "Debitaccount"
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(debitCommand,transactionAmount);

// Debit the second account
db.ExecuteNonquery(debitCommand);
}

  DAAB  在一个事务中可以在一个数据库连接中检测到几个命令的执行,这样可以避免虽然一个数据库连接执行的几个命令而启用 分布式事务 。在企业类库2.0的DAAB常常启用了分布式事务,就凭这一点,使用企业类库2.0的同学们有必要升级到企业类库3.0。

Parameter discovery on Ms Access and sqlServer. using Microsoft Patterns and Practices DataBlock version 3.0 final
http://www.codeproject.com/useritems/Parameter_DiscoveryV292.asp

总结

以上是内存溢出为你收集整理的TransactionScope和Enterprise Libray 3.0 Data Access Application Block全部内容,希望文章能够帮你解决TransactionScope和Enterprise Libray 3.0 Data Access Application Block所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1151222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存