现在的位置: 首页 > 综合 > 正文

常见代码的编写规范(三)—对象的赋值与保存

2012年10月09日 ⁄ 综合 ⁄ 共 2486字 ⁄ 字号 评论关闭

常见界面代码的编写规范(三)---对象的赋值与保存
2004年12月9日

 


 

3.对象的赋值与保存
    对象的赋值与保存,遵循三个步骤,1.数据的检验,2.属性的赋值,3.保存,属性赋值时,首先要保证一条语句赋一个属性的值,如果存在转换就要使用函数进行转换,以保证这一点。然后,对于一个对象中存在与别的对象的关系,该对象也需要保存时,要使用函数,单独保存该对象,不要混杂在一起。例子如下:
1.主函数
 private void bt_save_Click(object sender, System.EventArgs e)
  {
       if(!VerifyData()) return;

       SavePerson();

       Response.Redirect("WFRFDList.aspx");
  }
2.数据校验
private bool VerifyData()
  {
   string sMsg = "";
   if(tb_no.Text.Trim() =="")
   {
    sMsg = " 必须设置员工工号";
   }

   if(this.tbCardNo.Text.Trim()=="")
   {
    sMsg+= " 必须设置员工卡卡号 ";
   }

   if(this.tb_name.Text.Trim()=="")
    sMsg+= " 员工姓名不能为空 ";

   if(ddDepartment.SelectedValue ==null || ddDepartment.SelectedValue =="")
    sMsg+= " 就职部门不能为空 ";

   if(ddPostion.SelectedValue ==null || ddPostion.SelectedValue =="")
    sMsg+= " 职务不能为空 ";

   if(ddPost.SelectedValue ==null || ddPost.SelectedValue =="")
    sMsg+= " 岗位不能为空 ";

   if(this.ddPostionLevel .SelectedValue ==null || ddPostionLevel.SelectedValue =="")
    sMsg+= " 岗位水平不能为空 ";
   if(tb_salary.Text =="")
    sMsg+= "薪水不能为空";

   if(sMsg.Length >0)
   {
    WebSiteHelper.ShowAlertMessage(sMsg);
    return false;
   }
   return true;
  }

3.属性的赋值

    3a.一个对象自身的保存
private int SavePerson()
  {
   CPerson person= GetPerson();
   person.Name = this.tb_name.Text ;
   person.EnglishName = this.tbEnglishName .Text;
   SaveDuty( person.ReportForDuty ,person);
   person.State = 5;   
   person.Save();
   CHelper.FlushSession();
   return 0;
  }

    3b.关联对象的保存
 private CReportForDuty SaveDuty(CReportForDuty report,CPerson person)
  {
   if(report==null)
    report=  new CReportForDuty();
   
   report.StartTime=this.sy_start.DateValue;
   report.TrainingMothes=System.Int16.Parse(this.ddMonth.SelectedItem.Value);
   report.ProbationershipSalary=GetProbationershipSalary();
   report.Person=person;
   report.State = 1;
   report.EndTime =report.StartTime.AddMonths (report.TrainingMothes);
   report.RFDTime = DateTime.Now ;

   report.Employee = this.SaveEmployee (report.Employee ,person);

   report.ProbationershipAccession = this.SaveAccession(report.ProbationershipAccession,report.Employee);
   report.Employee.CurrentAccession = report.ProbationershipAccession;
   report.ProbationershipPost =this.SaveAppointPost(report.ProbationershipPost,report.Employee );
   report.Employee .CurrentPost = report.ProbationershipPost;
   report.Employee .Save();

   report.Save();   
   return report;
  }

    3c.数据的转换
 private decimal GetProbationershipSalary()
  {
   try
   {
    return Decimal.Parse(this.tb_salary.Text);
   }
   catch
   {
    return 0.0M;
   }
  }
4.保存
    由于后台使用E/R Mapping ,这里Save只是简单的调用Save()方法就可以了。

发表于 2004年12月09日 8:28 AM

抱歉!评论已关闭.