1:数据访问层:主要是对原始数据(数据库或者文本文件等存放数据的形式)的操作层,而不
是指原始数据,也就是说,是对数据的操作,而不是数据库,具体为业务逻辑层或表示层提供数据服务.
2:业务逻辑层:主要是针对具体的问题的操作,也可以理解成对数据层的操作,对数据业务逻
辑处理,如果说数据层是积木,那逻辑层就是对这些积木的搭建。
3:表示层:主要表示WEB方式,也可以表示成WINFORM方式,
如果逻辑层相当强大和完善,无论表现层如何定义和更改,逻辑层都能完善地提供服务。
具体的区分方法
1:数据访问层:主要看你的数据层里面有没有包含逻辑处理,实际上他的各个函数主要完成
各个对数据文件的操作。而不必管其他操作。
2:业务逻辑层:主要负责对数据层的操作。也就是说把一些数据层的操作进行组合。
3:表示层:主要对用户的请求接受,以及数据的返回,为客户端提供应用程序的访问。
三层结构说明
完善的三层结构的要求是:修改表现层而不用修改逻辑层,修改逻辑层而不用修改数据层
.否则你的应用是不是多层结构,或者说是层结构的划分和组织上是不是有问题就很难说.
不同的应用有不同的理解,这是一个概念的问题.
流程图
部署三层结构
1:新建一空白解决方案
2:在此解决方案上添加>>新建项目>>类库 取名DBEntity(数据库实体)
3:在此解决方案上添加>>新建项目>>类库 取名DAL(数据访问层)
4:在次解决方案上添加>>新建项目>>类库 取名BLL(业务逻辑层)
5:在次解决方案上添加>>新建网站>>ASP.NET网站 取名WebSite(表示层,WinForm项目的话添加一Window应用程序)
6:DAL,BLL, WebSite分别添加对数据库实体DBEntity的引用
7:BLL添加对对DAL的引用,WebSite添加对BLL的引用

下面用一用户登陆演示项目
DBEntity添加UserInfo.cs,代表数据库实体,一般是和数据库一一对应的
01 |
using System; |
02 |
using System.Collections.Generic; |
03 |
using System.Text; |
04 |
namespace DBEntity |
05 |
{ |
06 |
public class UserInfo |
07 |
{ |
08 |
private int _id; |
09 |
private string _userName; |
10 |
private string _passWord; |
11 |
public int Id |
12 |
{ |
13 |
get { return _id; } |
14 |
set { _id = value; } |
15 |
} |
16 |
public string UserName |
17 |
{ |
18 |
get { return _userName; } |
19 |
set { _userName = value; } |
20 |
} |
21 |
public string PassWord |
22 |
{ |
23 |
get { return _passWord; } |
24 |
set { _passWord = value; } |
25 |
} |
26 |
} |
27 |
} |
DAL里添加UserDAL.cs
01 |
using System; |
02 |
using System.Data; |
03 |
using System.Data.SqlClient; |
04 |
using System.Configuration; |
05 |
using System.Collections.Generic; |
06 |
using DBEntity; |
07 |
namespace DAL |
08 |
{ |
09 |
public class UserDAL |
10 |
{ |
11 |
private string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); |
12 |
public UserInfo Login(string userName, string passWord) |
13 |
{ |
14 |
UserInfo info = new UserInfo(); |
15 |
string strSql = "select id,userName,passWord from Users where userName=@userName and passWord=@passWord"; |
16 |
SqlConnection conn = new SqlConnection(ConnectionString); |
17 |
conn.Open(); |
18 |
SqlCommand com = new SqlCommand(); |
19 |
com.CommandType = CommandType.Text; |
20 |
com.CommandText = strSql; |
21 |
com.Connection = conn; |
22 |
com.Parameters.AddWithValue("@userName", userName); |
23 |
com.Parameters.AddWithValue("@passWord", passWord); |
24 |
SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection); |
25 |
if (dr.Read()) |
26 |
{ |
27 |
info.Id = Convert.ToInt32(dr["id"]); |
28 |
info.UserName = dr["userName"].ToString(); |
29 |
info.PassWord = dr["passWord"].ToString(); |
30 |
return info; |
31 |
} |
32 |
else |
33 |
{ |
34 |
return null; |
35 |
} |
36 |
} |
37 |
} |
38 |
} |
BLL里添加UserBLL.cs
01 |
using System; |
02 |
using System.Collections.Generic; |
03 |
using System.Text; |
04 |
using DBEntity; |
05 |
using DAL; |
06 |
namespace BLL |
07 |
{ |
08 |
public class UserBLL |
09 |
{ |
10 |
UserDAL dal = new UserDAL(); |
11 |
public UserInfo Login(string userName, string passWord) |
12 |
{ |
13 |
return dal.Login(userName, passWord); |
14 |
} |
15 |
} |
16 |
} |
Web里Login.aspx对应的后台代码
01 |
using System; |
02 |
using BLL; |
03 |
using DBEntity; |
04 |
public partial class _Default : System.Web.UI.Page |
05 |
{ |
06 |
protected void Page_Load(object sender, EventArgs e) |
07 |
{ |
08 |
} |
09 |
protected void Button1_Click(object sender, EventArgs e) |
10 |
{ |
11 |
UserBLL data = new UserBLL(); |
12 |
UserInfo info = new UserInfo(); |
13 |
info = data.Login(TextBox1.Text, TextBox2.Text); |
14 |
if (info != null) |
15 |
{ |
16 |
//登陆成功 |
17 |
Response.Write("<script>alert(OK!)</script>"); |
18 |
} |
19 |
else |
20 |
{ |
21 |
//登陆失败 |
22 |
Response.Write("<script>alert(ERROR!)</script>"); |
23 |
} |
24 |
} |
25 |
} |
至此,简单的三层架构用户登陆完成了!
留言