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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
雷峰网
雷峰网
P
Proofpoint News Feed
IT之家
IT之家
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
T
The Blog of Author Tim Ferriss
月光博客
月光博客
V
Visual Studio Blog
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
T
Troy Hunt's Blog
Project Zero
Project Zero
U
Unit 42
T
Tor Project blog
Scott Helme
Scott Helme
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
InfoQ
Cloudbric
Cloudbric
P
Proofpoint News Feed
The Cloudflare Blog
H
Heimdal Security Blog
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
Attack and Defense Labs
Attack and Defense Labs
有赞技术团队
有赞技术团队
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
博客园 - 【当耐特】
Security Latest
Security Latest
The Register - Security
The Register - Security
F
Fortinet All Blogs
I
Intezer
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
NISL@THU
NISL@THU
T
Tenable Blog

博客园 - 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>