三層架構,通常意義上的三層架構就是將整個業務應用划分為:表現層(UI)、業務邏輯層(BLL)、數據訪問層(DAL)。區分層次的目的即為了“高內聚,低耦合”的思想。
一、英文拓展:
三層架構(3-Tier ASrchitecture)
表現層UI(User Interface)
業務邏輯層BLL(Business Logic Layer)
數據訪問層DAL(Data Access Layer)
二、各層作用解析:
1、DAL作用:
1)從數據源加載數據Select
2)向數據源寫入數據Insert/Update
3)從數據源刪除數據Delete
2、UI的作用:
1)向用戶展現特定業務數據。
2)采集用戶的輸入信息和操作。
3)特定的數據顯示給用戶
原則:用戶至上,界面簡潔明了
3、BLL的作用:
1)從DAL中獲取數據,供UI顯示用。
2)從UI中獲取用戶指令和數據,執行業務邏輯。
3)從UI中獲取用戶指令和數據,通過DAL寫入數據源。
BLL的職責機制:
UI——BLL——UI
UI——BLL——DAL——BLL——UI
4、數據模型的引入:
為了避免三層之間的互相引用,所以出現Model,用於傳輸數據的,業務數據模型
三、系統登陸實例,步驟:
1、新建數據庫
(名稱)LoginDemo,包含兩張表:
新建表Users
其中,設定ID為主鍵,自增長。
新建表Scores
其中,設定ID為主鍵,自增長。
2、編碼階段:
解決方案名稱:LoginSolution
位置:LoginDemo
1)DAL數據訪問層:
新建項目名稱:LoginDAL
默認命名空間:Login.DAL
添加類:UserDAO,ScoreDAO,DbUtil
引用:LoginModel
namespace Login.DAL { class DbUtil { //sever機器名,Database數據庫名, public static string ConnString = @"Server=192.168.**.**;Database=LoginDemo;User ID=sa;Password=123456"; } } //每成功登陸一次用戶,增加10點積分。 public class ScoreDAO { public void UpdateScore(string userName, int value) { using (SqlConnection conn = new SqlConnection(DbUtil.ConnString)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @"INSERT INTO SCORES(UserName,Score) Values (@UserName,@Score)"; cmd.Parameters.Add(new SqlParameter("@UserName", userName)); cmd.Parameters.Add(new SqlParameter("@Score", value)); conn.Open(); cmd.ExecuteNonQuery(); } } } public class UserDAO { //根據userName和password返回一個布爾值。 public Login.Model.UserInfo SelectUser(string userName, string password) { { //有了using以后,connection就可以自動關閉 了 SqlConnection conn=new SqlConnection (DbUtil .ConnString ); { SqlCommand cmd=conn.CreateCommand (); cmd.CommandText=@"SELECT ID,UserName,Password,Email FROM USERS WHERE UserName=@UserName AND Password=@Password"; cmd.CommandType=CommandType .Text; cmd.Parameters.Add(new SqlParameter ("@UserName",userName)); cmd.Parameters.Add(new SqlParameter ("@Password",password)); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); //設置user的默認值為null Login.Model .UserInfo user=null; while (reader.Read()) { if (user==null ) { //如果user是null的話,則延遲加載 user=new Login .Model .UserInfo (); } user.ID=reader.GetInt32(0); user.UserName=reader.GetString(1); user.Password=reader.GetString(2);//not suggestion //如果Email不是null的話,才可以去讀。 if (!reader.IsDBNull(3)) { user.Email=reader.GetString(3); } } return user; } } } }
2)UI表示層:
添加新項目,Windows窗體應用程序。
名稱:LoginUI ,設置為啟動項目
默認命名空間:Login.UI
引用:LoginBLL,LoginModel
登陸:btnLogin
用戶名:(Name):txtUserName
密碼: (Name):txtPassword; PasswordChar:*
窗體: Text:系統登陸; MaximizeBox:False; MinimizeBox:False; FormBorderStyle:FixedSingle
namespace LoginUI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { ////通常,直接使用DAO呼叫數據庫。 //IDbConnection conn = new SqlConnection("c...."); //IDbCommand cmd = conn.CreateCommand(); //cmd.CommandText = "Select UserName From USERS WHERE ....."; //cmd.ExecuteReader(); //利用三層架構,需要引用下一層的 string userName = txtUserName.Text.Trim(); string password = txtPassword.Text; Login.BLL.LoginManager mgr = new Login.BLL.LoginManager(); Login.Model .UserInfo user= mgr.UserLogin(userName, password); MessageBox.Show("登陸用戶:"+user.UserName ); } } }
3)BLL業務邏輯層:
添加新項目;
名稱:LoginBLL
默認命名空間:Login.BLL
添加新類:LoginManager/LoginService服務
引用:LoginDAL,LoginModel
namespace Login.BLL { public class LoginManager { public Login.Model.UserInfo UserLogin(string userName, string password) { //throw new NotImplementedException(); //呼叫數據源,獲取相應數據 Login.DAL.UserDAO uDao = new Login.DAL.UserDAO(); Login.Model.UserInfo user = uDao.SelectUser(userName, password); if (user != null)//login successful { //如果登陸成功,則增加10點積分。 Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO(); sDao.UpdateScore(userName, 10); return user; } else { throw new Exception("登陸失敗。"); } } } }
4)Modle數據模型:
添加新建項目:
名稱:LoginModel
默認命名空間:Login.Model
添加類:UserInfo
Model數據模型:是獨立於其余層次的,不知道其余層次的信息,其余層次都會引用Model。介於UI和BLL,此處表示我們想要返回的數據為USER對象。
namespace Login.Model { public class UserInfo { public int ID { get; set; } public string UserName { get; set; } public string Password { get; set; } public string Email { get; set; } } }