我们来先看看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>
        
/// 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/

posted @ 2011-09-09 11:29 努力偷懒 阅读(386) 评论(0) 编辑

我们来先看看WinAction的删除数据的流程:

具体的参考代码:

 

        public virtual void DeleteEntity(object entity, object caller, object sender, EventArgs e)
        {
            
if (caller is IDataListView)
            {
                IDataListView listForm 
= caller as IDataListView;
                
if (null == listForm.DataList || (listForm.DataList is ICollection && (listForm.DataList as ICollection).Count == 0))
                    
return;
            }
            
if (MessageBox.Show("确实要删除当前记录?""提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                
return;
            
//判断当前删除的是否是新增的记录。(如果是新增的那条,其id为-1)
            if (entity is Entity && (entity as Entity).IsNewEntity)
            {
                Form eForm 
= CallEditForm(caller);
                
if (eForm.Visible)
                    eForm.Close();
                
return;
            }
            useBll.Delete((T)entity);
            
if (updateFreshByDB && caller is IDataListView && (caller as IDataListView).RefreshEntityList != null)
                (caller 
as IDataListView).RefreshEntityList((caller as IDataListView).SearchEntity, caller, sender, e);
        }

 

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/
posted @ 2011-09-09 11:23 努力偷懒 阅读(79) 评论(0) 编辑

我们来先看看WinAction的修改数据的流程:

具体的参考代码:

 

        public virtual void EditEntityHandler(object entity, object caller, object sender, EventArgs e)
        {
            
if (caller is IDataListView)
            {
                IDataListView listForm 
= caller as IDataListView;
                
if (null == listForm.DataList || (listForm.DataList is ICollection && (listForm.DataList as ICollection).Count == 0))
                    
return;
            }

            Form eForm 
= CallEditForm(caller);
            
if (eForm is IEditDataForm)
            {
                IEditDataForm mEditForm 
= eForm as IEditDataForm;
                
if (eForm.Visible)
                {
                    
if (mEditForm.Entity is Entity && (mEditForm.Entity as Entity).IsNewEntity)//新增时进行修改
                    {
                        
if (caller is IDataControlActionView)
                        {
                            
//修改最后新增的记录
                            if ((entity as Entity).IsNewEntity)
                            {
                                CommonFunctions.SetFormTop(eForm);
                                
return;
                            }
                        }
                        
if (caller is IDataControlActionView && caller is IDataListView && (caller as IDataListView).DataList is IList)
                            (caller 
as IDataControlActionView).RemoveLastNewRow();
                        
if (caller is IDataControlActionView)//已经执行过修改
                            UnloadEditEntityEvent(caller as IDataControlActionView);
                    }
                    
else
                    {
                        
if (mEditForm is IBindEntityAware)
                            (mEditForm 
as IBindEntityAware).BindEntity(entity);
                        CommonFunctions.SetFormTop(eForm);
                        
return;
                    }
                }
                LoadEditForm(EditForm);
            }
            
if (eForm is IBindEntityAware)
                (eForm 
as IBindEntityAware).BindEntity(entity);
            
if (caller is IDataControlActionView)
                LoadEditEntityEvent(caller 
as IDataControlActionView);
            
if (eForm.Visible)
                CommonFunctions.SetFormTop(eForm);
            
else
                eForm.Show();
        }

 

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/
posted @ 2011-09-09 11:21 努力偷懒 阅读(78) 评论(0) 编辑

我们来先看看WinAction的新增数据的流程:

源码参考:

 

        public virtual void NewEntityHandler(object entity, object caller, object sender, EventArgs e)
        {
            
//判断编辑窗体是否已打开
            Form eForm = CallEditForm(caller);
            
if (eForm is IEditDataForm)
            {
                IEditDataForm mEditForm 
= eForm as IEditDataForm;
                
if (eForm.Visible)
                {
                    
if (mEditForm.Entity is Entity && (mEditForm.Entity as Entity).IsNewEntity)//新增时再次进行新增
                    {
                        CommonFunctions.SetFormTop(eForm);
                        
return;
                    }
                    
if (caller is IDataControlActionView)//修改时进行新增
                        UnloadEditEntityEvent(caller as IDataControlActionView);
                }
                LoadEditForm(EditForm);
            }

            T mEntity 
= GetNewEntity();
            
if (mEntity is Entity)
                (mEntity 
as Entity).IsNewEntity = true;
            
if (eForm is IBindEntityAware)
                (eForm 
as IBindEntityAware).BindEntity(mEntity);
            
if (caller is IDataListView && (caller as IDataListView).DataList is IList)
                ((caller 
as IDataListView).DataList as IList).Add(mEntity);

            
if (caller is IDataControlActionView)
                LoadEditEntityEvent(caller 
as IDataControlActionView);

            
if (eForm.Visible)
                CommonFunctions.SetFormTop(eForm);
            
else
                eForm.Show();
        }

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/

posted @ 2011-09-09 11:20 努力偷懒 阅读(269) 评论(0) 编辑

我们来先看看WinAction的显示列表窗体的流程:


具体代码参考:

        public void InitListForm(Form listForm, object entity)
        {
            
if (listForm is IDataListView)
            {
                IDataListView mForm 
= listForm as IDataListView;
                IList
<T> dataList;
                
if (null==entity)
                    dataList 
= useBll.SelectAll<T>();
                
else
                    dataList 
= useBll.QueryForList<T>(entity);
                mForm.setDataList(TransformToDataListObject(dataList));
                
if (null == mForm.RefreshEntityList)
                    mForm.RefreshEntityList 
= RefreshEntity;
                
//初始化搜索对象
                if (entity != null)
                    mForm.SearchEntity 
= entity;
            }
            
//
            if (listForm is IDataListGridView && !(listForm as IDataListGridView).InitColumn)
            {
                (listForm 
as IDataListGridView).InitColumns(useBll.GetModelPropertys(), useBll.GetTableColumns(), pdm.FindTableByTableName(useBll.GetTableName()));
                
if (listForm is IDataControlActionView)
                {
                    IDataControlActionView dForm 
= listForm as IDataControlActionView;
                    dForm.DeleteEntity 
= DeleteEntity;
                    dForm.EditEntity 
= EditEntityHandler;
                    dForm.NewEntity 
= NewEntityHandler;
                    dForm.AfterUpdateEntity
= UpdateEditCallerData;
                    dForm.AfterUpdateEntityCancel 
= CancelEditCallerData;
                    dForm.AfterUpdateEntityError 
= ErrorEditCallerData;
                    dForm.ClosingListForm 
= ClosingListFormHandler;
                }
            }
        }

 

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/

posted @ 2011-09-09 11:13 努力偷懒 阅读(163) 评论(0) 编辑

在步入正题前,我们来先聊一些题外的话,魔兽世界估计很多程序员都听过,可能玩过的人也很多,我虽然没怎么玩,但我也接触过,我们来说下战场,要想打赢,必须有一个人去指挥战场,如果没人指挥,这战场十有八九会输掉,我相信,很多人打过战场的人都深有体会的。没玩过魔兽,没打过战场?没关系,拿真实的战役来说吧,三国时期的赤壁之战,这个够经典了吧,如果连这个都没听过,下面的,不用看了。为何刘备他们能打赢?那是因为有一个很了得的军师:诸葛亮做指挥。在开战前,军师们,当然不只诸葛亮是军师,曹操他们那边也有不少。军师会先分析研究地形、气候、人,以及模拟猜测敌方会使用何种计谋等,然后再根据自己自身的条件去作出不同的人员安排等等。可知,要胜利,必须要有一个站在高处,纵观所有一切可以掌握的资源,去作出合理安排的指挥者。

做软件设计何尝不是呢?这就是为何我要设计WinAction的原因,这里WinAction就是这个站在一定的层面上去设计的,是由它来安排所有的资源去运行。这样做有以下的好处:

 

  1. 各功能模块相对独立,避免不同功能模块之间的引用混乱(做产品开发的人或者是十分注重可重用性的人,对这个的理解应该会比较深入,因为很多时候,你要复用一个功能模块,你就得去引用某个dll文件,如果这个dll里面的代码不是相对独立的,同时又引用了别的功能模块的,甚至有的引用到了别的项目的dll文件,那是多么的悲哀),因此,我们最好就是在其中一个层面上去引用你需要的模块。而不要因为方便,而无任何规律地盲目引用,我们这里,就是让WinAction可引用所有项目中需要用到的资源。
  2. 类似一种MVC的框架模式,WinAction调用BLL去获取需要的数据实体,然后把数据实体的数据推送到展示层使用,因此,WinAction就充当了Controler的角色了。
  3. 方便问题分析。目前大部分业务系统都是围绕着数据库进行开发的应用,因此,查问题就可以分开来查,在WinAction中检查页面通知要执行的操作,在执行这个操作的时候,检查相关的参数是否正确,如果是不正确,跟踪检查页面逻辑。如果正确,检查WinAction中所调用的操作流程的逻辑。如果是数据库操作的,可以直接检查执行当前执行的SQL语句有没有问题。
  4. 使用接口编程的方式,页面允许日后的重载,客户自己封装。只需满足接口需要,就可以进行各窗体的灵活替换。
我们将在日后的章节来作更深入的介绍。

 

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/

posted @ 2011-09-09 11:05 努力偷懒 阅读(354) 评论(0) 编辑