工作流学习过程-事务

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

账户活动

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>

 

posted on 2010-11-02 15:42  gotolovo  阅读(250)  评论(0编辑  收藏  举报