小编自从跟着骨头(bone)学习机房重构按表走后深入研究一下为什么我们一定要按照表走,其实我们如何走都可以走的通,或者说代码上可能会有不会太大的变化,但是我想说的是这个是思想是逻辑,学习计算机最重要的就是学习逻辑,因为一门语言是我们优秀程序员的体现,如同是士兵长一样无法再升了,但是逻辑才是我们的重中之重,也可以说是发财之路。
好处 |
我们根本的目的是要找到数据库的数据,所以我们以终为始,从数据库开始找,并且我们写自己写程序的时候也大概从D层开始写的,这样我们按照在每个表中写方法不会重复,并且知道在哪个表中,唯一要求就是我们的前端需要知道我们的需要数据在哪个表中,并且我们的方法名也会有表名称(比如:D层中DALSelectUser_UserID 表示D层中的查询User表按照UserID字段查整行记录,如果有其他的条件会在后面添加在添加一个字段:_XXX,其他的层会修改前面的DAL改为其他层)
B层中由于是逻辑转换,所以从B层可以调用D层N次,但是从B层出去后要一一对应F层, 因为外观是将所有的B层方法进行打包总和。
接下来请看行走顺序流程:以注册为例(执行顺序:U-F-B-Factory-I—D) |
U层
需要添加到用户登录表中(UserInfo)、学生信息表中(Student)、充值记录中(Recharge),并且判断用户和学生表中是否存在这个值;
首先我们先判断是否存在就需要调用外观层(Facade)中的学生表里面的:FacadeSelectStudent AND FacadeSelectUser两个不同表里的不同方法(因为我们知道调取哪个表,并且在方法名中已经写出来了,所以非常的简单),其次我们需要分别调用:FacadeAddUser AND FacadeAddStduent AND FacadeAddReCharge 方法即可:
//判断信息完整度
if (txtUserID.Text == "" || txtStudentName.Text == "" || txtSex.Text == "" || txtDepartment.Text == "" || txtGrade.Text == "" || txtSpecialty.Text == "")
{
MessageBox.Show("请输入完整信息");
return;
}
//实例化UserInfo和student,并添加到实体中。
Entity.UserInfo UserInfo = new Entity.UserInfo();
UserInfo.UserID = txtUserID.Text.Trim();
UserInfo.UserName = txtStudentName.Text.Trim();
UserInfo.PassWord = "1";
UserInfo.Level = "学生";
UserInfo.Head = AddHead;
Entity.Student Student = new Entity.Student();
Student.studentNo=txtUserID.Text.Trim();
Student.studentName = txtStudentName.Text.Trim();
Student.sex = txtSex.Text.Trim();
Student.department = txtDepartment.Text.Trim();
Student.grade = txtGrade.Text.Trim();
Student.specialty = txtSpecialty.Text.Trim();
Student.balance = 20;
Student.Head = AddHead;
Student.state = true;
Student.IsCheck = "未结账";
//实例化外观
Facade.UserFacade FRegister = new Facade.UserFacade();
Facade.StudentFacade FStudent = new Facade.StudentFacade();
Boolean flagUser = false;
Boolean flagStudetn = false;
flagUser = FRegister.FacadeSelectUser(UserInfo);
flagStudetn = FStudent.FacadeSelectStudent(Student);
//实例化充值 实体和外观。
Entity.ReCharge Recharge = new Entity.ReCharge();
Recharge.UserID = txtUserID.Text.Trim();
Recharge.RechCash = 20;
Recharge.RechDate = DateTime.Now.ToString("yyyy-mm-dd");
Recharge.RechTime = DateTime.Now.ToString("hh:mm:ss");
Recharge.Head = AddHead;
Recharge.State = "注册";
Recharge.IsCheck = "未结账";
Facade.ReChargeFacade Frecharge = new Facade.ReChargeFacade();
Boolean flagAdd = Frecharge.FacadeAddReCharge(Recharge);
if (flagUser==false || flagStudetn ==false)
{
MessageBox.Show("该账号已有,请重新输入账号");
txtUserID.Text = "";
}
else
{
flagUser = FRegister.FacadeAddUser(UserInfo);
flagStudetn = FStudent.FacadeAddStudent(Student);
MessageBox.Show("用户ID:"+txtUserID.Text +"添加成功");
}
F层:需要去三个表中进行调取:
///
/// 用于查询UserID值
///
///
///
/// 返回值类型:Boolean
///
public Boolean FacadeSelectUser(Entity.UserInfo UserInfo)
{
//调用B层方法,然后将U层数据传入B层或者返回信息到U层
bool flag;
UserBLL selectUser = new UserBLL();
flag = selectUser.BLLSelectUser(UserInfo);
return flag;
}
///
/// 用于添加User所有值
///
///
///
/// 返回值类型:Boolean
///
public Boolean FacadeAddUser(Entity.UserInfo UserInfo)
{
bool flag;
UserBLL AddUser = new UserBLL();
flag = AddUser.BLLAddUser(UserInfo);
return flag;
}
Student和Recharge同理,不在书写。
接下来是B层:
///
/// 用于添加: UserID、UserName/Password/Level/head
///
///
///
/// 返回:Boolean
///
public Boolean BLLAddUser(UserInfo UserInfo)
{
Factory.UserFactory factAdd = new Factory.UserFactory();
IDAL.IUser idalAdd = factAdd.CreateUser();
DataTable tableAdd = idalAdd.DALAddUser (UserInfo); //后面调取的IUser里方法
bool flag;
if (tableAdd.Rows.Count !=0)
{
flag = false; //成功
}
else
{
flag = true;
}
return flag;
}
///
/// 用于查询UserID
///
///
/// 返回Boolean
public Boolean BLLSelectUser(Entity.UserInfo UserInfo)
{
Factory.UserFactory factBLLInquUser = new Factory.UserFactory();
IDAL.IUser idalBLLInquUser = factBLLInquUser.CreateUser();
DataTable tableBLlInquUser = idalBLLInquUser.DALSelectUser (UserInfo);
Boolean flagInquUSer;
if (tableBLlInquUser.Rows.Count == 0)
{
flagInquUSer = true;
}
else
{
flagInquUSer = false;//成功
}
return flagInquUSer;
}
student和recharge省略
D层: 同上,其他层不在书写
///
/// 注册 用于添加: UserID、UserName/Password/Level/head
///
///
///
/// 返回DataTalbe类型
///
public DataTable DALAddUser(Entity.UserInfo UserInfo)
{
SQLHelper SQLHealperAdd = new SQLHelper();
SqlParameter[] SQLParams ={
new SqlParameter("@UserID",UserInfo.UserID),
new SqlParameter("@UserName",UserInfo.UserName ),
new SqlParameter("@PassWord",UserInfo.PassWord),
new SqlParameter("@Level",UserInfo.Level),
new SqlParameter("@Head",UserInfo.Head)};
string SQL = "insert into UserInfo (UserID,UserName,PassWord,Level,Head) values(@UserID,@UserName,@PassWord,@level,@Head)";
DataTable dt = SQLHealperAdd.ExecuteQuery(SQL, SQLParams, CommandType.Text);
return dt;
}
///
/// 注册 用于查询(全部):UserID
///
///
///
/// 返回DataTable类型
///
public DataTable DALSelectUser(Entity.UserInfo UserInfo)
{
SQLHelper SQLHealperSelect = new SQLHelper();
SqlParameter[] ParmsSelect ={
new SqlParameter("@UserID",UserInfo.UserID)};
string SqlSelect = "Select * from UserInfo Where UserID=@UserID";
DataTable dtSelect = SQLHealperSelect.ExecuteQuery(SqlSelect, ParmsSelect, CommandType.Text);
return dtSelect;
}
**
备注 |
因为是傻瓜式编程,因为有方法名,所以我们不用去想去哪写,并且不会 因为每层都有20+个 表去烦恼,留给更多的时间去优化。从U层出来就知道去哪所以不但从减少了代码的冗余更带来的书写的便利。
感谢阅读,期待我们共同进步。。。。