常见代码的编写规范(三)---对象的赋值与保存
2005-06-30 08:04
369 查看
常见界面代码的编写规范(三)---对象的赋值与保存
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
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
相关文章推荐
- 常见代码的编写规范(三)---对象的赋值与保存
- 常见代码的编写规范(四)---对象的保存与更新
- 常见代码的编写规范(四)---对象的保存与更新
- 常见代码编写规范(二)--详细数据的读与显示
- 常见代码的编写规范(一)---列表数据的显示
- 编写高质量代码改善C#程序的157个建议——建议53:必要时应将不再使用的对象引用赋值为null
- 常见代码编写规范(二)--详细数据的读与显示
- 常见代码编写规范(二)--详细数据的读与显示
- 常见代码的编写规范(一)---列表数据的显示
- 常见代码的编写规范(一)---列表数据的显示
- 编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范。
- .net代码编写规范
- Scala代码编写中常见的十大陷阱
- Scala代码编写中常见的十大陷阱
- Effective Objective-C 2.0 编写高质量iOS与OS X代码 对象等同性
- Java代码规范和一些常见问题
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少分配率, 最重要的规则,缩短对象的生命周期,减少对象层次的深度,减少对象之间的引用,避免钉住对象(Pinning)
- 8种常见的Java不规范代码
- [转自MSDN]根据FxCop整理的.NET代码编写规范
- Java编码规范,让你的代码赏心悦目--对象和数据结构