常见代码编写规范(二)--详细数据的读与显示 2004年12月8日 2.详细数据的读与显示 详细数据的读与显示是我们碰到的最多代码之一,这种代码分成二个阶段来完成:1.取到对应的对象,2.显示数据到界面上。显示数据的时候要好好组织,对于一个类聚合多个类时,每个类都要分成一个函数进行处理,每个处理函数要基本保证一条语句处理一个Porperty,对于需要转换的属性,使用一个函数进行转换。代码例子如下: 1.取相应的对象 private CPerson GetPerson() { Guid GuidPerson=new Guid(this.m_pGuid.Text); CPerson person=CPerson .Load(GuidPerson); } 2.显示相应的对象 显示Person private void ShowPerson() { CPerson person=GetPerson() if(person==null) return; this.tb_name.Text=person.Name; ///当通过一个置值语句不能完成任务,或需要进行条件判断时,使用函数 SetGender(person.Gender);
this.tb_old.DateValue=person.BornDate; ShowContactInfo(person.ContactInfo); ShowApplyRecord(person.ApplyRecord); this.tb_worktime.DateValue = person.StartWorkDate; }
private void SetGender(bool bGender) { if(bGender) this.rbMale.Checked = true; else this.rbFemale .Checked = true; } ///显示其它对象,这里显示联系信息 private void ShowContactInfo(CContactInfo info) { if(info==null) return; this.tb_phone.Text=info.Phone; this.tbMobilePhone.Text = info.MobilePhoneNo ; this.tbEmail.Text = info.EMail ; } ///这里显示教育经历 private void ShowEducationHistory(CEducationHistory history) { if(history==null) return; this.tb_school.Text=history.School; this.tb_time.Value=history.StartDate.ToShortDateString(); this.tb_study.Text=history.Speciality; this.ddlEducationalLevel.SelectedValue = history.EducationalLevel ; this.ddlDegree.SelectedValue =history.Degree ; } private void InitApplyRecord(CApplyRecord apply) { if(apply==null) return;
this.tb_eng.SelectedValue =apply.EnglishLevel; this.tb_jan.SelectedValue = apply.JanpaneseLevel ; this.tb_other.SelectedValue = apply.OtherLanguageName ; this.tbOtherLanguageLevel.Text = apply.OtherLanguageLevel ;
this.tb_work1.Text=apply.ApplyJob1; this.tb_work2.Text=apply.ApplyJob2; this.tb_work3.Text=apply.ApplyJob3; this.applydate.Value=apply.ApplyDate.ToShortDateString();
InitEducationHistory(apply.EducationHistory); } 结论:显示数据要清晰易懂,责任明确,每个哪个函数负责取对象,哪个函数负责显示哪个对象的数据,显示数据是要保持一条语句显示一个Porperty,做不到的使用函数来进行处理。 
|