【前言】
早就聽說七層登錄這個名詞了,今天自己終於也到這個項目了,在沒動手做之前,我認爲七層登錄是一個項目,機房重構是後一個項目的...不要笑...做完之後悶了將近一天才明白過來,原來這已經是機房重構了,這是機房重構的第一步—登錄(沒接觸之前先做的登錄)。並且明白了其中的一些套路。這裏就不詳細說了,先來總結一下七層登錄吧。
【內容】
1.七層介紹
(1)用戶界面層(UI):是用戶所能看的到的界面,用來接收和顯示用戶的請求,將參數傳遞給外觀層。
(2)外觀層(Facade):運用外觀層是爲了降低U層和B層之間的耦合,U層和B層之間的聯繫只需要通過Façade層的接口,U層無需知道B層內部有哪些方法。外觀層接收U層傳來的數據,然後調用B層的方法對信息進行驗證。
(3)業務邏輯層(BLL):進行邏輯判斷和計算。
(4)工廠層(Factory):通過配置文件和抽象工廠我們可以實現不更改代碼,換一下配置文件中的value值就可以更換數據庫了。Factory還需要完成的工作就是定義一個接口調用接口層,實現BLL層和DAL層之間的數據傳遞。
(5)接口層(IDAL):接口層是用於定義一個統一的接口,解除B層和D層的耦合。
(6)數據訪問層(DAL):實現接口層定義的接口。
(7)實體層(Entity):和三層中的實體層一樣,主要是用來各層中傳遞數據。
—SqlHelper:將D層中需要重複使用的連接數據庫代碼抽象到一個層裏面,減少代碼冗餘。
2.七層架構關係圖
3.七層架構引用關係
層 |
引用 |
UI層 |
Façade層、Entity層 |
Façade層 |
BLL層、Entity層 |
BLL層 |
Factory層、IDAL層、Entity層 |
Factory層 |
IDAL層 |
IDAL層 |
Entity層 |
DAL層 |
IDAL層、Entity層 |
Entity層 |
/ |
4.代碼部分
(1)配置文件
<appSettings>
<add key="DB" value ="DAL"/>
<add key="connStr" value="Server=FRJ;Database=Login7; User ID=sa;Password=123"/>
</appSettings>
(2)UI層
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "")
{
MessageBox.Show("請輸入你的用戶名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (txtPassword.Text == "")
{
MessageBox.Show("請輸入你的密碼", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//實例化一個外觀
Facade.LoginFacade facade = new Facade.LoginFacade();
//實例化一個用戶
Entity.UserInfo user = new Entity.UserInfo();
//接收信息
user.UserName = txtUserName.Text;
user.Password = txtPassword.Text;
//調用外觀方法,返回給user
Boolean flag = false;
Facade.LoginFacade flogin = new Facade.LoginFacade();
flag = flogin.SelectUser(user);
//判斷是否登錄成功
if (flag != false)
{
MessageBox.Show("登錄成功");
this.Hide();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
//加判斷權限(學生、操作員、管理員)
}
else
{
MessageBox.Show("用戶名或密碼不正確");
}
}
(3)外觀層(Facade)
public class LoginFacade
{
public Boolean SelectUser(Entity.UserInfo user)
{
bool flag;
BLL.LoginBLL userBLL = new BLL.LoginBLL();
flag = userBLL.UserBLL(user);
return flag;
}
}
(4)業務邏輯層(DLL)
//命名空間
using System.Data;
namespace BLL
{
public class LoginBLL
{
//判斷用戶是否存在
public bool UserBLL(Entity.UserInfo user)
{
//實例化工廠
Factory.LoginFactory fact = new Factory.LoginFactory();
//調用工廠方法創建接口
IDAL.LoginIDAL idal = fact.CreateUser();
//接收D層的返回值
DataTable table = idal.SelectUser(user);
bool flag;
//返回數據表類型,如果行數=0,說明沒有符合該賬號密碼的用戶
if (table.Rows.Count == 0)
{
flag = false;
}
else
{
flag = true;
}
return flag;
}
}
}
(5)工廠層(Factory)
//命名空間
using System.Reflection;
using System.Configuration;
namespace Factory
{
public class LoginFactory
{
//獲取配置文件
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
//應用反射來獲取DAL層操作
public IDAL.LoginIDAL CreateUser()
{
string ClassName = StrDB + "." + "LoginDAL"; //程序集+類名
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
}
(6)接口層(IDAL)
//命名空間
using System.Data;
namespace IDAL
{
//接口層
public interface LoginIDAL
{
//放置接口函數,判斷要登錄的用戶名是否在數據表中存在
DataTable SelectUser(Entity.UserInfo user);
}
}
(7)數據訪問層(DAL)
SqlHelper
//命名空間
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
public class SqlHelper
{
//定義數據庫連接操作,指定在數據庫上操作的類型,定義數據庫讀取操作
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
//數據庫連接
public SqlHelper()
{
string connStr = ConfigurationManager.AppSettings["connStr"];
conn = new SqlConnection(connStr);
}
private SqlConnection GetConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
//執行不帶參數的數據庫操作或存儲過程
public int ExcuteNonQuery(string cmdText, CommandType ct)
{
int res;
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
return res;
}
//執行帶參數的數據庫操作或存儲過程
public int ExcuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdText, GetConn()))
{
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
//執行不帶參數的SQL查詢語句或存儲過程
public DataTable ExecuteQuery(string cmdText, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
//執行帶參數的SQL查詢語句或存儲過程
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
}
}
LoginDAL
//命名空間
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using IDAL;
using Entity;
namespace DAL
{
public class LoginDAL : IDAL.LoginIDAL
{
public DataTable SelectUser(Entity.UserInfo user)
{
//實例化數據操作類,進行數據查詢,並獲取返回值
SqlHelper sqlHelper = new SqlHelper();
SqlParameter[] sqlparams = { new SqlParameter("@UserName",user.UserName),
new SqlParameter("@Password",user.Password)};
string sql = @"SELECT * FROM [Users] WHERE UserName =@UserName AND Password = @Password";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlparams, CommandType.Text);
return table;
}
}
}
(8)實體層(Entity)
public class UserInfo
{
//定義用戶ID屬性
public int UserID { get; set; }
//定義用戶名UserName屬性
public string UserName { get; set; }
//定義密碼Password屬性
public string Password { get; set; }
}
總結:
只有做了纔有發言權。不要給自己貼標籤說自己不會,不做肯定不會。無論是機房重構還是別的什麼項目,經過適當的思考之後就要動手去做,不然就會一拖再拖,越來越反感。給自己加油!希望在接下來的機房重構中越走越順利,可以學到更多的技術性知識!