Mediar.Framework -- 业务的实现1(UI的绑定)


  Mediar.Framework -- 业务的实现1UI的绑定)

 

这所以说Mediar.Framework 是一款MVC架构,就是因为在业务上面,业务逻辑完全在model中实现。UI只做简单的绑定。(这里不是使用说明,而且设计的实现,所以部分代码是框架的代码)

 

1 下拉列表的绑定

下拉列表的值填充是通过FillDisplayLists 方法,系统是初使化时会自动的调用,并且会把业务对象实体作为参数传入方法。可以把获取集合的方法写在实体里,这里为了方便就直接从工厂获取。下拉列表PropertyList, DisplayProperty是为了在点击的时候的,使用多例显示,目前还不支持。

    

   protected override void FillDisplayLists (BizObject forThisBizObject)

        
{

              cboAddressType.SetDisplayedBizObjects(AddressTypeFactory.GetInstance().GetAll() 
as AddressTypeCollection);

            
//cboAddressType.PropertyList = "Name";

            
//cboAddressType. DisplayProperty = "Name";

   }

2 数据的绑定

UI中数据的显示无非是两种情况,一种是值,一种是集合。对于值的显示可以通过textbox,combo,checkbox,等等 ,集合是通过gridview 进行绑定。值的绑定要绑定实体中的域,集合是绑定实体中获取集合的方法,当然也可以写成属性。绑定是通过方法InitializeBinding。它的原理是把UI中的控件,和对应的实体的域注册到一个Hashtable中。UI在初使化时根据实体(每个form对需对应一个对象实体,而且要在打开窗体时就是要把对象实体传入)中的值,再赋值给UI中的控件。

  

protected override void InitializeBinding()

        
{

            
this.mPropertyToCtl.Add("Contact.DisplayID"new BoundControl(this.txtDisplayID, this.lbDisplayID));

            
this.mPropertyToCtl.Add("Contact.FirstName"new BoundControl(this.txtFirstName, this.lbFirstName));

            
this.mPropertyToCtl.Add("Contact.LastName"new BoundControl(this.txtLastName, this.lbLastName));

            
this.mPropertyToCtl.Add("Contact.GetPhoneNumbers"new BoundControl(this.dgPhoneNumber));

            
this.mPropertyToCtl.Add("Contact.GetContactAssociations"new BoundControl(this.dgCustomer));

      
base.InitializeBinding();

        }

绑定中之所以把label 也放入,是为了控制UI其在UI中的显示。绑定是通过 changed 事件给对象实体设置值。 缺点是每在TEXT中每输入一个字母,就会引发对changed事件,从而引发对象值改变,对象值改变时又会刷新,等等要进行一系列的操作。另一个不完美的地方就是从对象实体获取值勤的时候要用到反射。我试图使用DataBindings来实现,但是另一个问题又出现了:当我修改text 的时候,工具条上的save 按钮不可用,这时需要我击活另外一个可编辑对象时save 才可用。为大概是DataBindings 只有在击活别的对象时,它才执行EndEdit方法吧。最后衡量还是使用changed事件.

    

 protected override void SetControlvalues(BizObject forThisBizObject)
        
{
            
//string keyName;
            string propertyName;
            
string objectName;
            
string[] splits;
            
object propertyvalue;
            BoundControl bindToControls;
            Type boType 
= forThisBizObject.GetType();

            
if (forThisBizObject.GetType() != this.BizObject.GetType())
            
{
                
foreach (string keyName in this.mPropertyToCtl.Keys)
                
{
                    bindToControls 
= ((BoundControl)(this.mPropertyToCtl[keyName]));
                    splits 
= keyName.Split(new char[] '.' }2);
                    objectName 
= splits[0];
                    propertyName 
= splits[1];
                    
if ((boType.Name == objectName))
                    
{
                        propertyvalue 
= forThisBizObject.GetValue(propertyName);

                        SetEditControlvalue(bindToControls, propertyvalue);
                    }

                }

                
return;
            }

                
foreach (string keyName in this.mPropertyToCtl.Keys)
                
{
                    bindToControls 
= ((BoundControl)(this.mPropertyToCtl[keyName]));
                    splits 
= keyName.Split(new char[] '.' }2);
                    objectName 
= splits[0];
                    propertyName 
= splits[1];
                    
if ((boType.Name != objectName))
                    
{                   
                     
if ((this.mPropertyRelationshipName.ContainsKey(objectName)))
                        
{
                            objectName 
= ((string)(this.mPropertyRelationshipName[objectName]));
                        }

                        propertyName 
= objectName + "." + propertyName;  
                     
                    }

                    propertyvalue 
= forThisBizObject.GetValue(propertyName);
                    SetEditControlvalue(bindToControls, propertyvalue);
                }

        }



3 UI窗体的打开

 由于每个窗体对应一个对象实体,所以窗体已经不能随便的new 了,只能过一个 ShowOnce的方法。原形如下:

 

public static void ShowOnce(Contact ContactBO)

        
{

            
try

            
{

                BizObjectEdit.ShowOnce(ContactBO, 
typeof(ContactEdit), Program.mainForm);

            }


            
catch (Exception ee)

            
{

                
string e = ee.ToString();

            }


        }


public static BizObjectEdit ShowOnce(BizObject bizObjectToEdit, Type editFormType, Form MDIParent)

         
{

              BizObjectEdit EditForm;

              
if ((mForms.ContainsKey(bizObjectToEdit))) 

              
{

                   EditForm 
= ((BizObjectEdit)(mForms[bizObjectToEdit]));

                   
try 

                   
{

                       EditForm.MdiParent 
= MDIParent;

                       EditForm.Show();

                   }
 

                   
catch 

                   
{

                       EditForm 
= OpenNewEditForm(bizObjectToEdit, editFormType, MDIParent);

                   }


              }
 

              
else 

              
{

                   EditForm 
= OpenNewEditForm(bizObjectToEdit, editFormType, MDIParent);

              }


              
return EditForm;

         }


 

         
private static BizObjectEdit OpenNewEditForm(BizObject bizObjectToEdit, Type editFormType, Form MDIParent)

         
{

              BizObjectEdit NewForm;

              System.Reflection.ConstructorInfo MyConstructor;

              
object[] ConstructorParameters;

              Type[] TypeParameters;

              ConstructorParameters 
=new object[0] ;

              TypeParameters 
=new Type[0] ;             

MyConstructor 
= editFormType.GetConstructor(

                   BindingFlags.Instance 
| BindingFlags.Public, null,

                   CallingConventions.HasThis, TypeParameters, 
null);

              NewForm 
= ((BizObjectEdit)(MyConstructor.Invoke(ConstructorParameters)));

              NewForm.BizObject 
= bizObjectToEdit;

              NewForm.MdiParent 
= MDIParent;

              NewForm.Show();

              
return NewForm;

         }

 

通过上面的方法可以看出每个窗体与一个对象是对应的,而且会放在一个hashtable中。

 

4 UI中还有什么东东?

   UI中当然不止这些,还有一button对应的事件,一些对象实体中不能完全处理,必需在UI提供辅助的方法。还提供一些列如显示标题的接口InitializeForm等。整个UI的使用是比较简单的,而且许多代码都是可以通过代码工具生成.


相关:
     Mediar.Framework --一个MVC架构
     Mediar.Framework --对象mapping 
     Mediar.Framework--使用反射来声明Remoting wellknown服务
     Mediar.Framework -- 业务的实现1(UI的绑定)

posted @ 2006-09-25 11:09  瑞德船长  阅读(1739)  评论(1编辑  收藏  举报