概述
1.三层架构
通常意义上的三层架构就是将整个业务应用划分为:表现层(UI)、业务逻辑层(BLL)、数据访问层(DAL)。
区分层次的目的即为了“高内聚,低耦合”的思想。(通俗地讲高内聚就是关系紧密联系,耦合就是每一层只处理本层的任务)
表现层(UI):通俗讲就是展现给用户的界面,即用户在使用一个系统的时候的所见所得。
业务逻辑层(BLL):针对具体问题的操作,也可以说是对数据层的操作,对数据业务逻辑处理。
数据访问层(DAL):该层所做事务直接操作数据库,针对数据的增添、删除、修改、更新、查找等每层之间是一种垂直的关系。
2.我要举一个栗子
要实现的结果样式如下:
利用三层架构写的格式以及代码
DAL层(TruckTeamServer.cs)代码如下:
namespace DAL
{
public
class TruckTeamServer
{
//显示到GridView中
public DataTable getTruck()
{
string sql = @"SELECT [TeamID] ,[TeamName] ,[Leader] ,[Reamark] ,[CheckInTime]
,[IsDelete] ,[AlterTime] FROM
[TruckTeam]";
DataTable table = DAL.DBHelper.ExecQuery(sql);
return table;
}
//执行查询操作
public DataTable getTruckTeam(string TeamName, string Leader)
{
string sql = "SELECT [TeamID] ,[TeamName],[Leader]
,[Reamark] ,[CheckInTime] ,[IsDelete]
,[AlterTime] FROM [TruckTeam]";
List<SqlParameter> paramList = new List<SqlParameter>();
List<string> whereList = new List<string>();
if (TeamName != "-1")
{
//sql += "and @TeamName=TeamName";
whereList.Add("TeamName=@TeamName");
paramList.Add(new SqlParameter("@TeamName", TeamName));
}
if (!string.IsNullOrWhiteSpace(Leader))
{
//sql += "and @Leader=Leader";
whereList.Add("Leader=@Leader");
paramList.Add(new SqlParameter("@Leader", Leader));
}
if (whereList.Count > 0)
{
sql = sql + " where " + string.Join(" and ", whereList.ToArray());
}
DataTable dt = DAL.DBHelper.ExecQuery(sql, paramList.ToArray());
return dt;
}
//车队名字下拉框
public DataTable getDteamName()
{
string sql = "SELECT [TeamName] FROM [TruckTeam]";
DataTable table = DAL.DBHelper.ExecQuery(sql);
return table;
}
}
}
BLL层(TruckTeamManage.cs)代码如下:
namespace BLL
{
public class TruckTeamManage
{
//显示到GridView中
public DataTable getTruck()
{
TruckTeamServer table = new TruckTeamServer();
return table.getTruck();
}
//执行查询操作
public DataTable getTruckTeam(string teamName,string leader)
{
TruckTeamServer table = new TruckTeamServer();
return table.getTruckTeam(teamName, leader);
}
//车队名字下拉框
public DataTable getDteamName()
{
TruckTeamServer table = new TruckTeamServer();
return table.getDteamName();
}
}
}
UI层 代码如下:
namespace LoginUI
{
public partial class TruckTeamQuery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BinDList();
BinDteamName();
}
}
//显示到GridView中
public void BinDList() {
TruckTeamManage table = new TruckTeamManage();
GVTruckTeam.DataSource = table.getTruck();
GVTruckTeam.DataBind();
}
//车队名字下拉框
public void BinDteamName() {
TruckTeamManage table = new TruckTeamManage();
TeamNameList.DataSource = table.getDteamName();
TeamNameList.DataTextField = "TeamName";
TeamNameList.DataValueField = "TeamName";
TeamNameList.DataBind();
TeamNameList.Items.Insert(0, new ListItem("请选择", "-1"));
}
//执行查询操作
protected void TeamMan_Click(object sender, EventArgs e)
{
string teamName = TeamNameList.SelectedValue;
string leader = TxtTeamMan.Text;
TruckTeamManage TruckTeamBll = new TruckTeamManage();
GVTruckTeam.DataSource = TruckTeamBll.getTruckTeam(teamName, leader);
GVTruckTeam.DataBind();
}
}
}
利用单层架构写的格式以及代码
因为是单层所以只有一页代码:
namespace LoginUI
{
public partial class TruckTeamQuery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BinDList();
BinDteamName();
}
}
//显示到GridView中
public void BinDList() {
string sql = @"SELECT [TeamID] ,[TeamName] ,[Leader] ,[Reamark] ,[CheckInTime]
,[IsDelete] ,[AlterTime] FROM
[TruckTeam]";
DataTable table = DAL.DBHelper.ExecQuery(sql);
GVTruckTeam.DataSource = table;
GVTruckTeam.DataBind();
}
//车队名字的下拉框
public void BinDteamName() {
string sql = "SELECT [TeamName] FROM [TruckTeam]";
DataTable table = DAL.DBHelper.ExecQuery(sql);
TeamNameList.DataSource = table;
TeamNameList.DataTextField = "TeamName";
TeamNameList.DataValueField = "TeamName";
TeamNameList.DataBind();
TeamNameList.Items.Insert(0, new ListItem("请选择", "-1"));
}
//执行查询
protected void TeamMan_Click(object sender, EventArgs e)
{
string sql = "SELECT [TeamID] ,[TeamName],[Leader]
,[Reamark] ,[CheckInTime] ,[IsDelete]
,[AlterTime] FROM [TruckTeam]";
List<SqlParameter> paramList = new List<SqlParameter>();
List<string> whereList = new List<string>();
string teamName = TeamNameList.SelectedValue;
string leader = TxtTeamMan.Text;
if (teamName != "-1")
{
//sql += "and @TeamName=TeamName";
whereList.Add("TeamName=@TeamName");
paramList.Add(new SqlParameter("@TeamName", teamName));
}
if (!string.IsNullOrWhiteSpace(leader))
{
//sql += "and @Leader=Leader";
whereList.Add("Leader=@Leader");
paramList.Add(new SqlParameter("@Leader", leader));
}
if (whereList.Count > 0)
{
sql = sql + " where " + string.Join(" and ", whereList.ToArray());
}
string str = string.Join(" and ", whereList.ToArray());
DataTable dt = DAL.DBHelper.ExecQuery(sql, paramList.ToArray());
GVTruckTeam.DataSource = dt;
GVTruckTeam.DataBind();
}
}
}
小结:
三层结构是N层结构的一种,一般来说,层次之间是向下依赖的,下层代码未确定其接口(契约)前,上层代码是无法开
发的,下层代码接口(契约)的变化将使上层的代码一起变化。
优点: 分工明确,条理清晰,易于调试,而且具有可扩展性。
缺点: 增加成本。
最后
以上就是背后时光为你收集整理的ASP.NET网站开发--单层架构与三层架构区别,以及转换小结:的全部内容,希望文章能够帮你解决ASP.NET网站开发--单层架构与三层架构区别,以及转换小结:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复