常见代码的编写规范(三)---对象的赋值与保存
来源:互联网 发布:抽象工厂设计模式 java 编辑:程序博客网 时间:2024/05/17 22:32
常见代码的编写规范(三)---对象的赋值与保存
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()方法就可以了。
- 常见代码的编写规范(三)---对象的赋值与保存
- 常见代码的编写规范(四)---对象的保存与更新
- 常见代码编写规范(二)--详细数据的读与显示
- 常见代码编写规范(二)--详细数据的读与显示
- 常见的代码规范
- 常见代码的编写规范(一)---列表数据的显示
- 常见代码的编写规范(一)---列表数据的显示
- 对象的赋值与对象的复制
- JAVA代码编写程序的基本规范
- 编写代码时需要注意的规范
- 编写代码时需要注意的规范
- JAVA代码编写程序的基本规范
- FxCop整理的.NET代码编写规范
- 不错的代码编写规范模板
- Java开发代码的编写规范记要
- 自己整理的iOS代码编写规范
- 常见的Java不规范代码
- 常见的代码规范及逻辑
- 设计模式之Adapter(适配器)
- 设计模式之Bridge
- Toaday Is Thursday
- 设计模式之Flyweight(享元) FlyWeight模式
- J2SE5.0新特性之监控与管理
- 常见代码的编写规范(三)---对象的赋值与保存
- 你的伴侣会有“第四者”吗?
- 让Asp.NET的DataGrid可排序、可选择、可分页
- 安全理论介绍及其实践
- Eclipse3.0下配置JBoss3.2.5(与其它的有点不同)
- 唐僧100年后的真言[转贴]
- 利用CSS控制打印
- 详解 Visual C# 数据库编程
- C#数据库连接字符大全