惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
博客园 - 司徒正美
A
About on SuperTechFans
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
H
Help Net Security
量子位
IT之家
IT之家

博客园 - gotolovo

导出csv 导出excel 正则学习电话匹配 logmanager des 加密 解密 - gotolovo c# 发送邮件 工作流学习过程-状态机 蛋疼的viewstate 工作流学习过程-持久化服务 工作流学习过程-使用关联 工作流学习过程-本地服务之事件处理 工作流学习过程-本地服务之调用方法 工作流学习过程-验证活动 工作流学习过程-自定义活动 工作流学习过程-开篇 基本的几个排序算法 关于sql中时间的格式转换 数据行变列 请编程遍历页面上所有TextBox控件并给它赋值为空
工作流学习过程-事务
gotolovo · 2010-11-02 · via 博客园 - gotolovo

使用事务时务必有持久化前提

账户活动

public partial class AccountAdjustmentActivity: Activity
{
public AccountAdjustmentActivity()
{
InitializeComponent();
}
public static DependencyProperty AmountProperty = DependencyProperty.Register("Amount", typeof(decimal), typeof(AccountAdjustmentActivity));

[DescriptionAttribute(

"Amount")]
[CategoryAttribute(
"Amount Category")]
[BrowsableAttribute(
true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public decimal Amount
{
get
{
return ((decimal)(base.GetValue(AccountAdjustmentActivity.AmountProperty)));
}
set
{
base.SetValue(AccountAdjustmentActivity.AmountProperty, value);
}
}
public static DependencyProperty AccountIdProperty = DependencyProperty.Register("AccountId", typeof(string), typeof(AccountAdjustmentActivity));

[DescriptionAttribute(

"AccountId")]
[CategoryAttribute(
"AccountId Category")]
[BrowsableAttribute(
true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string AccountId
{
get
{
return ((string)(base.GetValue(AccountAdjustmentActivity.AccountIdProperty)));
}
set
{
base.SetValue(AccountAdjustmentActivity.AccountIdProperty, value);
}
}
public static DependencyProperty IsCreditProperty = DependencyProperty.Register("IsCredit", typeof(bool), typeof(AccountAdjustmentActivity));

[DescriptionAttribute(

"IsCredit")]
[CategoryAttribute(
"IsCredit Category")]
[BrowsableAttribute(
true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public bool IsCredit
{
get
{
return ((bool)(base.GetValue(AccountAdjustmentActivity.IsCreditProperty)));
}
set
{
base.SetValue(AccountAdjustmentActivity.IsCreditProperty, value);
}
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
using ( SqlConnection connection = new SqlConnection(
ConfigurationManager.ConnectionStrings
[
"ProWorkflow"].ConnectionString))
{
connection.Open();
if (!IsCredit)
{
Decimal currentBal
= GetCurrentBalance(connection, AccountId);
if (currentBal < Amount)
{
throw new ArgumentException("余额不足,无法处理");
}
}

UpdateBalance(connection, AccountId, Amount, IsCredit);
connection.Close();
}

return base.Execute(executionContext);
}
private decimal GetCurrentBalance(SqlConnection connection, string accountId)
{
Decimal balance
= 0;
String sql
= @"select balance from account where accountId = @AccountId";

SqlCommand command

= new SqlCommand(sql);
SqlParameter p
= new SqlParameter("@AccountId", accountId);
command.Parameters.Add(p);
command.Connection
= connection;

Object result

= command.ExecuteScalar();
if (result != null)
{
balance
= (Decimal)result;
}
return balance;
}
private void UpdateBalance(SqlConnection connection, string accountId, decimal amount, bool isCredit)
{
String sql;
if (isCredit)
{
sql
=
@"update account set balance = balance + @Amount where accountId = @AccountId";
}
else
{
sql
=
@"update account set balance = balance - @Amount where accountId = @AccountId";
}

SqlCommand command

= new SqlCommand(sql);
SqlParameter p
= new SqlParameter("@AccountId", accountId);
command.Parameters.Add(p);
p
= new SqlParameter("@Amount", amount);
command.Parameters.Add(p);
command.Connection
= connection;
command.ExecuteNonQuery();
}
}

交易工作流

public partial class AccountTransferWorkflow : SequentialWorkflowActivity
{
public AccountTransferWorkflow()
{
InitializeComponent();
}
public static DependencyProperty AmountProperty = DependencyProperty.Register("Amount", typeof(decimal), typeof(AccountTransferWorkflow));

[DescriptionAttribute(

"Amount")]
[CategoryAttribute(
"Amount Category")]
[BrowsableAttribute(
true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public decimal Amount
{
get
{
return ((decimal)(base.GetValue(AccountTransferWorkflow.AmountProperty)));
}
set
{
base.SetValue(AccountTransferWorkflow.AmountProperty, value);
}
}
public static DependencyProperty FromAccountIdProperty = DependencyProperty.Register("FromAccountId", typeof(string), typeof(AccountTransferWorkflow));

[DescriptionAttribute(

"FromAccountId")]
[CategoryAttribute(
"FromAccountId Category")]
[BrowsableAttribute(
true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string FromAccountId
{
get
{
return ((string)(base.GetValue(AccountTransferWorkflow.FromAccountIdProperty)));
}
set
{
base.SetValue(AccountTransferWorkflow.FromAccountIdProperty, value);
}
}
public static DependencyProperty ToAccountIdProperty = DependencyProperty.Register("ToAccountId", typeof(string), typeof(AccountTransferWorkflow));

[DescriptionAttribute(

"ToAccountId")]
[CategoryAttribute(
"ToAccountId Category")]
[BrowsableAttribute(
true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string ToAccountId
{
get
{
return ((string)(base.GetValue(AccountTransferWorkflow.ToAccountIdProperty)));
}
set
{
base.SetValue(AccountTransferWorkflow.ToAccountIdProperty, value);
}
}

}

配置文件

<connectionStrings>
<add name="WorkflowPersistence" connectionString=
"Integrated Security=SSPI;Initial Catalog=c6ps;
Data Source=.;Integrated Security=SSPI"
/>
<add name="ProWorkflow" connectionString="Integrated Security=SSPI;Initial Catalog=ProWorkflow;
Data Source=.;Integrated Security=SSPI"
/>
</connectionStrings>