WinForm应用程序框架设计之WinAction(数据操作)
我们来先看看WinAction的有关数据操作的流程:(包括更新数据操作和取消更新的操作)
具体的参考代码:
更新数据操作:
/// <summary>
/// 实体更新时执行
/// </summary>
/// <param name="entity">当前操作的实体</param>
/// <param name="caller">修改对象(窗体)</param>
/// <param name="sender">修改对象(窗体)的按钮</param>
/// <param name="e">唤出窗体的事件参数</param>
public virtual void SaveEntity(object entity, object caller, object sender, EventArgs e)
{
try
{
object mResult = useBll.Update((T)entity);
//卸载修改窗体的方法
if (caller is IEditDataForm)
UnloadEditForm(caller as IEditDataForm);
//隐藏执行该方法的窗体(修改窗体)
if (caller is Form)
(caller as Form).Hide();
if (AfterUpdateEntity != null)
AfterUpdateEntity(entity, caller, this, e);
}
catch (Exception ex)
{
/*
if (entity is Entity)
{
if (!(entity as Entity).IsNewEntity)
{//出错时把数据实体的数据进行恢复
if (caller is IBindEntityAware)
(caller as IBindEntityAware).RecoverEntity(entity);
}
}*/
if (AfterUpdateEntityError != null)
AfterUpdateEntityError(entity, caller, this, e);
else
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 更新实体对象列表时执行
/// </summary>
/// <param name="entity"></param>
/// <param name="caller">修改对象(窗体)</param>
/// <param name="sender">修改对象(窗体)的按钮</param>
/// <param name="e">唤出窗体的事件参数</param>
public virtual void UpdateEntitys(object entity, object caller, object sender, EventArgs e)
{
try
{
if (caller is IDataListEdit)
{
IEnumerable deletedList = (caller as IDataListEdit).GetDeletedDataList();
foreach (object delData in deletedList)
useBll.Delete(TransformToEntity(delData));
IEnumerable updateList = (caller as IDataListEdit).GetCurrentDataList();
foreach (object curData in updateList)
useBll.Update(TransformToEntity(curData));
}
//卸载修改窗体的方法
if (caller is IEditDataForm)
UnloadEditForm(caller as IEditDataForm);
if (caller is Form)
(caller as Form).Hide();
if (AfterUpdateEntity != null)
AfterUpdateEntity(entity, caller, this, e);
}
catch (Exception ex)
{
if (AfterUpdateEntityError != null)
AfterUpdateEntityError(entity, caller, this, e);
else
throw new Exception(ex.Message, ex);
}
}
/// 实体更新时执行
/// </summary>
/// <param name="entity">当前操作的实体</param>
/// <param name="caller">修改对象(窗体)</param>
/// <param name="sender">修改对象(窗体)的按钮</param>
/// <param name="e">唤出窗体的事件参数</param>
public virtual void SaveEntity(object entity, object caller, object sender, EventArgs e)
{
try
{
object mResult = useBll.Update((T)entity);
//卸载修改窗体的方法
if (caller is IEditDataForm)
UnloadEditForm(caller as IEditDataForm);
//隐藏执行该方法的窗体(修改窗体)
if (caller is Form)
(caller as Form).Hide();
if (AfterUpdateEntity != null)
AfterUpdateEntity(entity, caller, this, e);
}
catch (Exception ex)
{
/*
if (entity is Entity)
{
if (!(entity as Entity).IsNewEntity)
{//出错时把数据实体的数据进行恢复
if (caller is IBindEntityAware)
(caller as IBindEntityAware).RecoverEntity(entity);
}
}*/
if (AfterUpdateEntityError != null)
AfterUpdateEntityError(entity, caller, this, e);
else
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 更新实体对象列表时执行
/// </summary>
/// <param name="entity"></param>
/// <param name="caller">修改对象(窗体)</param>
/// <param name="sender">修改对象(窗体)的按钮</param>
/// <param name="e">唤出窗体的事件参数</param>
public virtual void UpdateEntitys(object entity, object caller, object sender, EventArgs e)
{
try
{
if (caller is IDataListEdit)
{
IEnumerable deletedList = (caller as IDataListEdit).GetDeletedDataList();
foreach (object delData in deletedList)
useBll.Delete(TransformToEntity(delData));
IEnumerable updateList = (caller as IDataListEdit).GetCurrentDataList();
foreach (object curData in updateList)
useBll.Update(TransformToEntity(curData));
}
//卸载修改窗体的方法
if (caller is IEditDataForm)
UnloadEditForm(caller as IEditDataForm);
if (caller is Form)
(caller as Form).Hide();
if (AfterUpdateEntity != null)
AfterUpdateEntity(entity, caller, this, e);
}
catch (Exception ex)
{
if (AfterUpdateEntityError != null)
AfterUpdateEntityError(entity, caller, this, e);
else
throw new Exception(ex.Message, ex);
}
}
注意,有两种更新的方式:一种是单个数据实体更新,另外一个就是批量更新。
取消操作:
/// <summary>
/// editform执行的取消实体更新时执行
/// </summary>
/// <param name="entity">当前操作的实体</param>
/// <param name="caller">修改对象(窗体)</param>
/// <param name="sender">修改对象(窗体)的按钮</param>
/// <param name="e">唤出窗体的事件参数</param>
public virtual void CancelUpdateEntity(object entity, object caller, object sender, EventArgs e)
{
//editform执行的取消更新过程
if (AfterUpdateEntityCancel != null)
AfterUpdateEntityCancel(entity, caller, this, e);
if (caller is IEditDataForm)
UnloadEditForm(caller as IEditDataForm);
if (caller is Form)
(caller as Form).Hide();
}
/// editform执行的取消实体更新时执行
/// </summary>
/// <param name="entity">当前操作的实体</param>
/// <param name="caller">修改对象(窗体)</param>
/// <param name="sender">修改对象(窗体)的按钮</param>
/// <param name="e">唤出窗体的事件参数</param>
public virtual void CancelUpdateEntity(object entity, object caller, object sender, EventArgs e)
{
//editform执行的取消更新过程
if (AfterUpdateEntityCancel != null)
AfterUpdateEntityCancel(entity, caller, this, e);
if (caller is IEditDataForm)
UnloadEditForm(caller as IEditDataForm);
if (caller is Form)
(caller as Form).Hide();
}
原创作品出自努力偷懒,转载请说明文章出处:http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/