public abstract class BaseForm : Form
{
protected IStatusUpdateStrategy statusUpdateStrategy;
protected ILockStrategy lockStrategy;
protected IHoldStrategy holdStrategy;
protected SqlConnection connection;
public BaseForm(IStatusUpdateStrategy statusStrategy, ILockStrategy lockStrategy, IHoldStrategy holdStrategy, SqlConnection connection)
{
this.statusUpdateStrategy = statusStrategy;
this.lockStrategy = lockStrategy;
this.holdStrategy = holdStrategy;
this.connection = connection;
lockStrategy.LockData(GetTrayCode());
}
protected void RegisterData(string trayCode, FinancialData data)
{
using (var transaction = connection.BeginTransaction())
{
try
{
SaveFinancialData(trayCode, data, transaction);
statusUpdateStrategy.UpdateStatus(trayCode, GetCurrentUserName(), transaction);
lockStrategy.UnlockData(trayCode, transaction);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
}
protected abstract void SaveFinancialData(string trayCode, FinancialData data, SqlTransaction transaction);
protected virtual string GetTrayCode()
{
return "TRAY001";
}
protected string GetCurrentUserName()
{
return "UserA";
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
lockStrategy.UnlockData(GetTrayCode());
}
}