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

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

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

    

None.gif protected   override   void  InitializePropertyState()
None.gif
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif {
InBlock.gif
InBlock.gif           
base.InitializePropertyState();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif           
this.OnBizObjectPropertyStateEvent(new BizObjectPropertyStateEventArgs(BizObjectPropertyStateType.ReadOnly, "Notes"));
InBlock.gif
InBlock.gif           
this.OnBizObjectPropertyStateEvent(new BizObjectPropertyStateEventArgs(BizObjectPropertyStateType.Hidden, "WebSiteURL"));
InBlock.gif
ExpandedBlockEnd.gif       }

None.gif
None.gif 
None.gif
None.gif
protected   virtual   void  OnBizObjectPropertyStateEvent(BizObjectPropertyStateEventArgs e)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif {
InBlock.gif
InBlock.gif              
this.mPropertyState[e.Property()] = e;
InBlock.gif
InBlock.gif              
if (BizObjectPropertyStateEvent != null
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   BizObjectPropertyStateEvent(
this, e);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedBlockEnd.gif         }

None.gif
None.gif

 

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

    

None.gif   private   void  SetControlEditModes(BizObject forThisBizObject)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif {
InBlock.gif
InBlock.gif            BizObjectPropertyStateEventArgs[] PropertyStates;
InBlock.gif
InBlock.gif              PropertyStates 
= forThisBizObject.GetPropertyState();
InBlock.gif
InBlock.gif              
foreach (BizObjectPropertyStateEventArgs PropertyState in PropertyStates) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{    SetControlEditMode(forThisBizObject, PropertyState.Property().ToString(), PropertyState.Type());
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedBlockEnd.gif         }

None.gif
None.gif 
None.gif
None.gif    
protected   void  SetControlEditMode(BizObject forThisBizObject, Control theControl, BizObjectPropertyStateType editMode)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif {
InBlock.gif
InBlock.gif              
bool enableControl=false;
InBlock.gif
InBlock.gif              
if (editMode == BizObjectPropertyStateType.Editable) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   theControl.Visible 
= true;
InBlock.gif
InBlock.gif                   
if (((bool)(mEnableEditControls[forThisBizObject]))) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                   
dot.gif{
InBlock.gif
InBlock.gif                       enableControl 
= true;
InBlock.gif
ExpandedSubBlockEnd.gif                   }
 
InBlock.gif
InBlock.gif                   
else 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                   
dot.gif{
InBlock.gif
InBlock.gif                       enableControl 
= false;
InBlock.gif
ExpandedSubBlockEnd.gif                   }

InBlock.gif
ExpandedSubBlockEnd.gif              }
 
InBlock.gif
InBlock.gif              
else if (editMode == BizObjectPropertyStateType.Hidden) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   theControl.Visible 
= false;
InBlock.gif
InBlock.gif                   enableControl 
= false;
InBlock.gif
ExpandedSubBlockEnd.gif              }
 
InBlock.gif
InBlock.gif              
else if (editMode == BizObjectPropertyStateType.ReadOnly) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   theControl.Visible 
= true;
InBlock.gif
InBlock.gif                   enableControl 
= false;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
InBlock.gif
InBlock.gif           EnableControl(theControl, enableControl);
InBlock.gif
InBlock.gif                   
InBlock.gif
InBlock.gif              
InBlock.gif
ExpandedBlockEnd.gif         }

None.gif
None.gif

 

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

  

None.gif    public   override   bool  Validate()
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
bool valid;
InBlock.gif
InBlock.gif            valid 
= base.Validate();    
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (mFirstName[AttributeValueVersion.Current].Length <= 0)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                valid 
= false;
InBlock.gif
InBlock.gif                
this.OnBizObjectErrorEvent(new BizObjectErrorEventArgs(BizObjectErrorType.Invalid, "FirstName is requared!""FirstName"));
InBlock.gif
ExpandedSubBlockEnd.gif            }
           
InBlock.gif
InBlock.gif            
return valid;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif

 

3一对一,一对多

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

      

None.gif  PhoneNumberCollectionDataHolder mPhoneNumbers;
None.gif
None.gif        ContactAssociationCollectionDataHolder mContactAssociations;
None.gif
None.gif 
None.gif
None.gif    
None.gif
None.gif        
private  AddressDataHolder mAddress;
None.gif
None.gif        
protected   void  InstantiateObjects2()
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            mPhoneNumbers 
= new PhoneNumberCollectionDataHolder(this);
InBlock.gif
InBlock.gif            mPhoneNumbers.BizObjectCollectionInstantiated 
+= new EventHandler<BizObjectCollectionInstantiatedEventArgs>(BizObjectCollectionInstantiatedHandler);
InBlock.gif
InBlock.gif            mContactAssociations 
= new ContactAssociationCollectionDataHolder(this);
InBlock.gif
InBlock.gif            mContactAssociations.BizObjectCollectionInstantiated 
+= new EventHandler<BizObjectCollectionInstantiatedEventArgs>(BizObjectCollectionInstantiatedHandler);
InBlock.gif
InBlock.gif     
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            mAddress 
= new AddressDataHolder();
InBlock.gif
InBlock.gif            mAddress.BizObjectInstantiated 
+= new BizObjectInstantiatedEventHandler(BizObjectInstantiatedHandler);
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif 
None.gif
None.gif        
protected   override   void  GetChildrenDataObjects(DataSet ds)
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif          
InBlock.gif
InBlock.gif            
if (this.mPhoneNumbers.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mPhoneNumbers.PhoneNumberCollection.GetDataObjectDataTable(ds);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (this.mContactAssociations.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                mContactAssociations.ContactAssociationCollection.GetDataObjectDataTable(ds);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (this.mAddress.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mAddress.Address.GetDataObjectRow(ds);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif 
None.gif
None.gif        
protected   override   void  RefreshChildren(DataSet ds)
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif          
if (this.mPhoneNumbers.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mPhoneNumbers.PhoneNumberCollection.Refresh(ds);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (this.mContactAssociations.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mContactAssociations.ContactAssociationCollection.Refresh(ds);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (this.mAddress.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mAddress.Address.Refresh(ds);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif 
None.gif
None.gif        
protected   override   void  CancelEditChildren()
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif           
if (this.mPhoneNumbers.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mPhoneNumbers.PhoneNumberCollection.CancelEdit();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (this.mContactAssociations.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mContactAssociations.ContactAssociationCollection.CancelEdit();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (this.mAddress.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                mAddress.Address.CancelEdit();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif 
None.gif
None.gif        
protected   override   void  CloneChildren(BizObject bobj)
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {        
InBlock.gif
InBlock.gif            Contact contactBobj 
= bobj as Contact;
InBlock.gif
InBlock.gif            
if (this.mPhoneNumbers.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                contactBobj.mPhoneNumbers.PhoneNumberCollection 
= mPhoneNumbers.PhoneNumberCollection.Clone(trueas PhoneNumberCollection;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (this.mContactAssociations.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                contactBobj.mContactAssociations.ContactAssociationCollection 
= mContactAssociations.ContactAssociationCollection.Clone(trueas ContactAssociationCollection;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (this.mAddress.IsInstantiated)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                contactBobj.mAddress.Address 
= mAddress.Address.Clone(trueas Address;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif


4 多对多的处理

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

 

None.gif public  ContactAssociationCollection GetContactAssociations()
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
return mContactAssociations.ContactAssociationCollection;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif

 

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

 

None.gif public  ContactAssociation CreateContactAssociation(Customer CustomerBO)
None.gif
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif {
InBlock.gif
InBlock.gif           ContactAssociation ca
=null;
InBlock.gif
InBlock.gif           
if (CustomerBO != null)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif
InBlock.gif               ca 
= ContactAssociationFactory.GetInstance().CreateBizObject() as ContactAssociation;
InBlock.gif
InBlock.gif               ca.Contact 
= this;
InBlock.gif
InBlock.gif               ca.Customer 
= CustomerBO;
InBlock.gif
ExpandedSubBlockEnd.gif           }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif           
return ca;
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif       }

None.gif
None.gif 
None.gif
None.gif 
None.gif
None.gif对于多对多在UI中也要加入相应的处理方法:
None.gif
None.gif
private   void  lnkCreate_LinkClicked( object  sender, LinkLabelLinkClickedEventArgs e)
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            Customer CustomerBO;
InBlock.gif
InBlock.gif            CustomerBO 
= CustomerFactory.GetInstance().CreateBizObject() as Customer;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            CustomerEdit.ShowOnce(CustomerBO);
InBlock.gif
InBlock.gif            CustomerBO.BizObjectUpdatedEvent 
+= new EventHandler<BizObjectUpdatedEventArgs>(CustomerBO_BizObjectUpdatedEvent);
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif 
None.gif
None.gif        
void  CustomerBO_BizObjectUpdatedEvent( object  sender, BizObjectUpdatedEventArgs e)
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
if (sender is Customer)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                Customer CustomerBO 
= sender as Customer;
InBlock.gif
InBlock.gif                CustomerBO.BizObjectUpdatedEvent 
-= new EventHandler<BizObjectUpdatedEventArgs>(CustomerBO_BizObjectUpdatedEvent);
InBlock.gif
InBlock.gif                
if (CustomerBO.IsPersisted && CustomerBO.IsDirty)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    AddCustomerToGrid(CustomerBO);
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif 
None.gif
None.gif        
private   void  AddCustomerToGrid(Customer CustomerBO)
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            ContactAssociation ca;
InBlock.gif
InBlock.gif            ca 
= (this.BizObject as Contact).CreateContactAssociation(CustomerBO);
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            (
this.BizObject as Contact).GetContactAssociations().Add(ca);
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif

转载于:https://www.cnblogs.com/mediar/archive/2006/09/29/517872.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值