Mediar.Framework—业务的实现3 (控制UI控件的可视和可编辑属性、验证、以及一对一,一对多,多对多关系)

1、控制UI控件的可视和可编辑属性

它的实现原理是在对象初使化的时候,通过方法InitializePropertyState 把需要隐藏或者要设为只读的控件设为它的状态类型。这里有个状态类型的玫举BizObjectPropertyStateType 它分别有ReadOnly, Hidden, Editable.系统会把所有具有状态的域收集到一个Hashtable 中。

    

protected override void InitializePropertyState()

       
{

           
base.InitializePropertyState();

 

           
this.OnBizObjectPropertyStateEvent(new BizObjectPropertyStateEventArgs(BizObjectPropertyStateType.ReadOnly, "Notes"));

           
this.OnBizObjectPropertyStateEvent(new BizObjectPropertyStateEventArgs(BizObjectPropertyStateType.Hidden, "WebSiteURL"));

       }


 

protected virtual void OnBizObjectPropertyStateEvent(BizObjectPropertyStateEventArgs e)

         
{

              
this.mPropertyState[e.Property()] = e;

              
if (BizObjectPropertyStateEvent != null

              
{

                   BizObjectPropertyStateEvent(
this, e);

              }


         }


 

UI中,UI初使化时,会相应的查测控件的属性状态,并设置所有的状态。根据对象的属性状态就可以控制UI控件的隐藏,或者只读。

    

 private void SetControlEditModes(BizObject forThisBizObject)

         
{

            BizObjectPropertyStateEventArgs[] PropertyStates;

              PropertyStates 
= forThisBizObject.GetPropertyState();

              
foreach (BizObjectPropertyStateEventArgs PropertyState in PropertyStates) 

              
{    SetControlEditMode(forThisBizObject, PropertyState.Property().ToString(), PropertyState.Type());

              }


         }


 

    
protected void SetControlEditMode(BizObject forThisBizObject, Control theControl, BizObjectPropertyStateType editMode)

         
{

              
bool enableControl=false;

              
if (editMode == BizObjectPropertyStateType.Editable) 

              
{

                   theControl.Visible 
= true;

                   
if (((bool)(mEnableEditControls[forThisBizObject]))) 

                   
{

                       enableControl 
= true;

                   }
 

                   
else 

                   
{

                       enableControl 
= false;

                   }


              }
 

              
else if (editMode == BizObjectPropertyStateType.Hidden) 

              
{

                   theControl.Visible 
= false;

                   enableControl 
= false;

              }
 

              
else if (editMode == BizObjectPropertyStateType.ReadOnly) 

              
{

                   theControl.Visible 
= true;

                   enableControl 
= false;

              }


              

           EnableControl(theControl, enableControl);

                   

              

         }


 

2 对于用户在UI中输入的数据的验证是放在对象实体中。对应的方法是 Validate.当验证与条件不符合时,把这个错误寄存到一个ArrayList中,并引发相关事件。在UI中进行捕获,并进行相关的处理。下面几个方法从名字上面可以看出实际的意义。

  

  public override bool Validate()

        
{

            
bool valid;

            valid 
= base.Validate();    

 

            
if (mFirstName[AttributeValueVersion.Current].Length <= 0)

            
{

                valid 
= false;

                
this.OnBizObjectErrorEvent(new BizObjectErrorEventArgs(BizObjectErrorType.Invalid, "FirstName is requared!""FirstName"));

            }
           

            
return valid;

        }


 

3一对一,一对多

 当然一个对象实体存在 一个或者多个 一对一、一对多时,对象实体中就需要手对声明。一对一声明为DataHolder,一对多声明为CollectionDataHolder.

      

 PhoneNumberCollectionDataHolder mPhoneNumbers;

        ContactAssociationCollectionDataHolder mContactAssociations;

 

    

        
private AddressDataHolder mAddress;

        
protected void InstantiateObjects2()

        
{

            mPhoneNumbers 
= new PhoneNumberCollectionDataHolder(this);

            mPhoneNumbers.BizObjectCollectionInstantiated 
+= new EventHandler<BizObjectCollectionInstantiatedEventArgs>(BizObjectCollectionInstantiatedHandler);

            mContactAssociations 
= new ContactAssociationCollectionDataHolder(this);

            mContactAssociations.BizObjectCollectionInstantiated 
+= new EventHandler<BizObjectCollectionInstantiatedEventArgs>(BizObjectCollectionInstantiatedHandler);

     

 

            mAddress 
= new AddressDataHolder();

            mAddress.BizObjectInstantiated 
+= new BizObjectInstantiatedEventHandler(BizObjectInstantiatedHandler);

        }


 

        
protected override void GetChildrenDataObjects(DataSet ds)

        
{

          

            
if (this.mPhoneNumbers.IsInstantiated)

            
{

                mPhoneNumbers.PhoneNumberCollection.GetDataObjectDataTable(ds);

            }


 

            
if (this.mContactAssociations.IsInstantiated)

            
{                mContactAssociations.ContactAssociationCollection.GetDataObjectDataTable(ds);

            }


 

            
if (this.mAddress.IsInstantiated)

            
{

                mAddress.Address.GetDataObjectRow(ds);

            }


        }


 

        
protected override void RefreshChildren(DataSet ds)

        
{

          
if (this.mPhoneNumbers.IsInstantiated)

            
{

                mPhoneNumbers.PhoneNumberCollection.Refresh(ds);

            }


 

            
if (this.mContactAssociations.IsInstantiated)

            
{

                mContactAssociations.ContactAssociationCollection.Refresh(ds);

            }


 

            
if (this.mAddress.IsInstantiated)

            
{

                mAddress.Address.Refresh(ds);

            }


        }


 

        
protected override void CancelEditChildren()

        
{

           
if (this.mPhoneNumbers.IsInstantiated)

            
{

                mPhoneNumbers.PhoneNumberCollection.CancelEdit();

            }


            
if (this.mContactAssociations.IsInstantiated)

            
{

                mContactAssociations.ContactAssociationCollection.CancelEdit();

            }


            
if (this.mAddress.IsInstantiated)

            
{

                mAddress.Address.CancelEdit();

            }


 

        }


 

        
protected override void CloneChildren(BizObject bobj)

        
{        

            Contact contactBobj 
= bobj as Contact;

            
if (this.mPhoneNumbers.IsInstantiated)

            
{

                contactBobj.mPhoneNumbers.PhoneNumberCollection 
= mPhoneNumbers.PhoneNumberCollection.Clone(trueas PhoneNumberCollection;

            }


            
if (this.mContactAssociations.IsInstantiated)

            
{

                contactBobj.mContactAssociations.ContactAssociationCollection 
= mContactAssociations.ContactAssociationCollection.Clone(trueas ContactAssociationCollection;

            }


 

 

            
if (this.mAddress.IsInstantiated)

            
{

                contactBobj.mAddress.Address 
= mAddress.Address.Clone(trueas Address;

            }


        }



4 多对多的处理

多对多一般都有一个中间表。对应的就有一个多对多的实体对象。一个多对多的实体对象包括了两个实体对象。Demo GetContactAssociations 是获取的中间对象实体。

 

public ContactAssociationCollection GetContactAssociations()

        
{

            
return mContactAssociations.ContactAssociationCollection;

        }

 

 在一个对象中创建中间的对象实体方法如下。

 

public ContactAssociation CreateContactAssociation(Customer CustomerBO)

       
{

           ContactAssociation ca
=null;

           
if (CustomerBO != null)

           
{

               ca 
= ContactAssociationFactory.GetInstance().CreateBizObject() as ContactAssociation;

               ca.Contact 
= this;

               ca.Customer 
= CustomerBO;

           }


 

           
return ca;

 

       }


 

 

对于多对多在UI中也要加入相应的处理方法:

private void lnkCreate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        
{

            Customer CustomerBO;

            CustomerBO 
= CustomerFactory.GetInstance().CreateBizObject() as Customer;

 

            CustomerEdit.ShowOnce(CustomerBO);

            CustomerBO.BizObjectUpdatedEvent 
+= new EventHandler<BizObjectUpdatedEventArgs>(CustomerBO_BizObjectUpdatedEvent);

 

 

        }


 

        
void CustomerBO_BizObjectUpdatedEvent(object sender, BizObjectUpdatedEventArgs e)

        
{

            
if (sender is Customer)

            
{

                Customer CustomerBO 
= sender as Customer;

                CustomerBO.BizObjectUpdatedEvent 
-= new EventHandler<BizObjectUpdatedEventArgs>(CustomerBO_BizObjectUpdatedEvent);

                
if (CustomerBO.IsPersisted && CustomerBO.IsDirty)

                
{

                    AddCustomerToGrid(CustomerBO);

                }


            }


        }


 

        
private void AddCustomerToGrid(Customer CustomerBO)

        
{

            ContactAssociation ca;

            ca 
= (this.BizObject as Contact).CreateContactAssociation(CustomerBO);

 

            (
this.BizObject as Contact).GetContactAssociations().Add(ca);

 

        }

posted @ 2006-09-29 09:46  瑞德船长  阅读(1687)  评论(0编辑  收藏  举报