理解ASP.NET中的三層結構

來源:互聯網
上載者:User

理解ASP.NET中的三層結構

  我們用三層結構主要是使項目結構更清楚,分工更明確,有利於後期的維護和升級.

  三層結構包含:展示層(USL),商務邏輯層(BLL),資料訪問層(DAL)

  1:資料訪問層:主要是對未經處理資料(資料庫或者文字檔等存放資料的形式)的操作層,而不

     是指未經處理資料,也就是說,是對資料的操作,而不是資料庫,具體為商務邏輯層或展示層提供資料服務.

  2:商務邏輯層:主要是針對具體的問題的操作,也可以理解成對資料層的操作,對資料業務邏

     輯處理,如果說資料層是積木,那邏輯層就是對這些積木的搭建。

  3:展示層:主要表示WEB方式,也可以表示成WINFORM方式,

       如果邏輯層相當強大和完善,無論表現層如何定義和更改,邏輯層都能完善地提供服務。
飛鴿傳書-http://www.freeeim.com/
具體的區分方法

  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,代表資料庫實體,一般是和資料庫一一對應的

view sourceprint?01 using System; 

02 using System.Collections.Generic; 

03 using System.Text; 

04   

05 namespace DBEntity 

06 { 

07     public class UserInfo 

08     { 

09         private int _id; 

10         private string _userName; 

11         private string _passWord; 

12   

13         public int Id 

14         { 

15             get { return _id; } 

16             set { _id = value; } 

17         } 

18   

19         public string UserName 

20         { 

21             get { return _userName; } 

22             set { _userName = value; } 

23         } 

24          

25         public string PassWord 

26         { 

27             get { return _passWord; } 

28             set { _passWord = value; } 

29         } 

30     } 

31 }

DAL裡添加UserDAL.cs

view sourceprint?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   

08 namespace DAL 

09 { 

10     public class UserDAL 

11     { 

12         private string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); 

13         public UserInfo Login(string userName, string passWord) 

14         { 

15             UserInfo info = new UserInfo(); 

16             string strSql = "select id,userName,passWord from Users where userName=@userName and passWord=@passWord"; 

17             SqlConnection conn = new SqlConnection(ConnectionString); 

18             conn.Open(); 

19             SqlCommand com = new SqlCommand(); 

20             com.CommandType = CommandType.Text; 

21             com.CommandText = strSql; 

22             com.Connection = conn; 

23             com.Parameters.AddWithValue("@userName", userName); 

24             com.Parameters.AddWithValue("@passWord", passWord); 

25             SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection); 

26             if (dr.Read()) 

27             { 

28                 info.Id = Convert.ToInt32(dr["id"]); 

29                 info.UserName = dr["userName"].ToString(); 

30                 info.PassWord = dr["passWord"].ToString(); 

31                 return info; 

32             } 

33             else

34             { 

35                 return null; 

36             } 

37         } 

38     } 

39 }

BLL裡添加UserBLL.cs

view sourceprint?01 using System; 

02 using System.Collections.Generic; 

03 using System.Text; 

04 using DBEntity; 

05 using DAL; 

06   

07 namespace BLL 

08 { 

09     public class UserBLL 

10     { 

11         UserDAL dal = new UserDAL(); 

12         public UserInfo Login(string userName, string passWord) 

13         { 

14             return dal.Login(userName, passWord); 

15         } 

16     } 

17 }

Web裡Login.aspx對應的後台代碼

view sourceprint?01 using System; 

02 using BLL; 

03 using DBEntity; 

04   

05 public partial class _Default : System.Web.UI.Page  

06 { 

07     protected void Page_Load(object sender, EventArgs e) 

08     { 

09   

10     } 

11     protected void Button1_Click(object sender, EventArgs e) 

12     { 

13         UserBLL data = new UserBLL(); 

14         UserInfo info = new UserInfo(); 

15         info = data.Login(TextBox1.Text, TextBox2.Text); 

16         if (info != null) 

17         { 

18             //登陸成功 

19             Response.Write("<script>alert(OK!)</script>"); 

20         } 

21         else

22         {  

23             //登陸失敗 

24             Response.Write("<script>alert(ERROR!)</script>"); 

25         } 

26     } 

27 }

至此,簡單的三層架構使用者登陸完成了!

水平有限,寫的不好大家來指點!

下次寫下三層架構的擴充:工廠模型

附源碼下載:ThreeModelSolution.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.