三层结构概述
- 多层结构的划分:物理/逻辑
- 两层/三层结构
- 物理上的三层:显示层/业务层/数据层
- 客户PC:应用服务器,数据库服务器
- 逻辑上的三层:UI/BLL+DAL/DB
原则
- DAL只提供基本的数据访问,不包含任何业务相关的逻辑处理。
- UI只负责显示和采集用户操作,不包含任何的业务相关的逻辑处理。
- BLL负责处理逻辑。通过获取UI传出来的操作指令,决定执行业务逻辑,在需要访问数据源的时候直接交给DAL处理。处理完成后,返回必要的数据给UI。
数据访问层(DAL)
DAL的作用
- 从数据源加载数据(Select)
- 向数据源写入数据(Insert/Update)
- 从数据源删除数据(Delete)
DAL中常用的技术
- ADO.NET+SQL语句
- O/R Mapping框架
- 访问SQL Server数据库时Linq to SQL
namespace Login.DAL
{
public class ScoreDAO
{
public void UpateScore(string userName, int value)
{
throw new NotImplementedException();
}
}
}
显示层(UI)
UI的作用
- 向用户展示特定业务数据
- 采集用户的输入信息和操作
UI设计的原则
- 用户至上,兼顾简洁
UI中常用的技术
- WindowsForm:Form、Control
- ASP.NET:aspx、ascx、master、html
private void btnLogin_Click(object sender, EventArgs e)
{
IDbConnection conn = new SqlConnection("c........");
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select UserName From USER WHERE....";
cmd.ExecuteReader();
}
业务逻辑层(BLL)
BLL的作用
- 从DAL中获取数据,以供UI显示用
- 从UI中获取用户指令和数据,执行业务逻辑
- 从UI中获取用户指令和数据,通过DAL写入数据源
BLL的职责机制
- UI—>BLL—>UI
- UI—>BLL—>DAL—>BLL—>UI
namespace Login.BLL
{
public class LoginService
{
public void Login(string userName, string password)
{
throw new NotImplementedException();
}
}
}