class0512三层
2014-06-28 19:12 fanhongshuo 阅读(153) 评论(0) 收藏 举报登录三层流程
1、编写模型类(Model):
class Seat
{
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
代表DAL层返回的数据,因为DAL层不能返回DataTable等ado.net相关的对象。
2、编写DAL层:
namespace 呼叫中心.DAL
{
class SeatDAL
{
public Seat GetByUserName(string username)
{
DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Seats where UserName=@UserName",
new SqlParameter("@UserName", username));
if (dt.Rows.Count <= 0)
{
return null;
}
else if (dt.Rows.Count == 1)
{
DataRow row = dt.Rows[0];
Seat seat = new Seat();
seat.Id = (int)row["Id"];
seat.UserName = (string)row["UserName"];
seat.Password = (string)row["Password"];
return seat;
}
else
{
throw new Exception("存在多个同名用户");
}
}
}
}
DAL层一定不能返回DataTable,如果是行数据,则以Seat返回。DAL层中不应该出现逻辑判断。
3、编写BLL层:
namespace 呼叫中心.BLL
{
class SeatBLL
{
public bool ValidateUser(string username,
string password)
{
Seat seat = new SeatDAL().GetByUserName(username);
if (seat == null)
{
return false;
}
else
{
string usermd5 = CommonHelper.GetMD5(password);
return usermd5 == seat.Password;
//if (usermd5 == seat.Password)
//{
// return true;
//}
//else
//{
// return false;
//}
}
}
}
}
BLL就是对DAL中的数据是用业务逻辑进行封装、二次加工。BLL提供UI层访问的一个方法。BLL中不能出现MessageBox、TextBox等界面相关的类。
4、UI层调用BLL层中的代码,收集界面上用户的输入传递给BLL,获得BLL层返回值,把返回结果以合适的形式显示给用户。
private void btnLogin_Click(object sender, EventArgs e)
{
SeatBLL bll = new SeatBLL();
if (bll.ValidateUser(txtUserName.Text, txtPwd.Text))
{
MessageBox.Show("ok");
}
else
{
MessageBox.Show("error");
}
}
todo:ValidateUser返回一个枚举值,表示是用户名不存在还是密码错误还是登陆正确。(OK、UserNameNotFound、PasswordError)
不要把“恭喜,修改成功”由BLL层返回,因为显示什么问题是UI层决定的
浙公网安备 33010602011771号