三層架構之登陸

一:三層構架的基礎知識

在項目開發的過程當中,有時把整個項目分爲三層架構,其中包括:表示層(UI)、業務邏輯層(BLL)和數據訪
問層(DAL)。三層的做用分別以下:
 html

表示層:

爲用戶提供交互操做界面,這一點不管是對於Web仍是WinForm都是如此,就是用戶界面操做
。咱們網站展現給用戶看的界面。
 sql

業務邏輯層:

負責關鍵業務的處理和數據的傳遞。複雜的邏輯判斷和涉及到數據庫的數據驗證都須要在此
作出處理。根據傳入的值返回用戶想獲得的值,或者處理相關的邏輯。
  數據庫

數據訪問層:

見名知意,負責數據庫數據的訪問。主要爲業務邏輯層提供數據,根據傳入的值來操做數據
庫,增、刪、改或者其它。
  設計模式

如下我簡單介紹下一個用戶管理模塊:

 

  爲了整個項目的開發方便,咱們在項目中會建幾個類庫Common,BLL,DAL,Model。
爲了命名清晰,咱們能夠這樣命名這個三個工程(即在解決方案裏添加的類庫):
  架構

業務邏輯層(BusinessLogicLayer):BLL,命名空間默認設置爲BLL
數據訪問層(DataAccessLayer):DAL,命名空間默認設置爲DAL

 

工具類:如:Common,命名空間默認設置爲Common

 

  另外咱們爲了數據傳遞的方便,一般再添加一個類庫,這個類庫是貫穿於整個三層架構中的。即實體類。
一般命名爲Model,命名空間默認值設置爲:Models。其中封裝的每一個類都對應一個實體,一般就是數據庫
中的一個表。如數據庫中的用戶表(custom)封裝爲(custom),將表中的每一個字段都封裝成共有的屬
性。工具

這樣三層架構的搭建就基本完成了。這三層有着很是強的依賴關係:
  表示層 ← 業務邏輯層 ← 數據訪問層
  他們之間的數據傳遞是雙向的,而且一般藉助實體類傳遞數據。 網站

  那麼三層架構都有哪些優勢呢:

 

一、易於項目的修改和維護。

在項目的開發過程當中或者開發後的升級過程當中,甚至在項目的移植過程當中。
這種三層架構是很是方便的。好比項目從Web移植到Form,咱們只須要將表示層從新作一遍就能夠了。
其他兩層不用改動,只需添加到現有項目就能夠了。若是不採用這種架構,只是將代碼寫到表示層。那麼
全部的編碼幾乎都要從新來了。
 ui

二、易於擴展。

在功能的擴展上一樣如此,若有功能的添加只需把原有的類庫添加方法就可了
  this

三、易於代碼的重用。

這一點就不用解釋了。
 編碼

四、易於分工協做開

 

還能夠加個接口類庫Iinterface, 加入設計模式,使你的代碼靈活性更好,質量更高。

二:系統登陸的例子

a,總體

2014-04-13_1139512014-04-12_182305

b,OperationLogBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.DAL;

namespace HRMSys.BLL
{
    /// <summary>
    /// 插入操做記錄
    /// </summary>
    public class OperationLogBLL
    {
        OperationLogDAL dal = new OperationLogDAL();
        public void InsertOperationLog(Guid id, string name, string ActionDesc)
        {
            dal.InsertLog(id, name, ActionDesc);
        }
    }
}

c,OperatorBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.DAL;
using HRMSys.Model;
using Common;

namespace HRMSys.BLL.cs
{
    public class OperatorBLL
    {
        OperatorDAL dal=new OperatorDAL();
        /// <summary>
        /// 獲得指定name的operator的數據
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Operator getOperatorByName(string name)
        {
            Operator op=new Operator();
            op=dal.loginUser(name);
            return op;

        }
        /// <summary>
        /// 密碼錯誤超過三次,鎖定登陸者
        /// </summary>
        /// <param name="op"></param>
        public void updateOperator(Operator op)
        {
            dal.UpdateOperator(op);
        }
        /// <summary>
        /// 登陸
        /// </summary>
        /// <returns></returns>
        public bool login(string name,int errorTime,string pwd,out string message,out Operator op)
        {
            bool r = false;
            if (name.Length <= 0)
            {
                message = "請輸入用戶名";
                op = null;
                return r;
            }
            if (pwd.Length <= 0)
            {
                message = "請輸入用戶名";
                op = null;
                return r;
            }

            if (errorTime>=3)
            {
                message = "用戶已被鎖定";
                op = null;
                return r;

            }
            else
            {
                op = dal.loginUser(name);
                if (op == null)
                {
                   
                   
                    message = "用戶名不存在";
                }
                else
                {
                    pwd = CommonHelper.GetMD5(pwd + CommonHelper.getPasswordSalt());
                    if (op.Password != pwd)
                    {
                        
                        message = "密碼不正確";
                        op.IsLocked = true;
                        updateOperator(op);
                        return r;
                    }
                    else
                    {
                        if (op.IsLocked == true)
                            message = "用戶被鎖定";
                        else
                        {
                        r = true;
                        message = "登陸成功";
                            }

                    }
                }
                return r;
            }
           
 
        }
        
    }
}

d,OperationLogDAL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.Model;
using System.Data;
using System.Data.SqlClient;

namespace HRMSys.DAL
{
    public class OperationLogDAL
    {
       
        /// <summary>
        /// 插入一條操做記錄
        /// </summary>
        /// <param name="OperatorId"></param>
        /// <param name="ActionDesc"></param>
        
        public void InsertLog(Guid OperatorId, string loginUser, string ActionDesc)
        {
                                                         
            sqlhelper.ExecuteNon(@"insert into T_OperationLog(Id,OperatorId,MakeDate,ActionDesc,LoginUser)
            values(newid(),@OperatorId,getdate(),@ActionDesc,@LoginUser)", new SqlParameter("@ActionDesc", ActionDesc),
                                                                         new SqlParameter("@OperatorId", OperatorId),
                                                                        new SqlParameter("@LoginUser", loginUser));//這裏是values,不是value

        }
        
    }
}

e,OperatorDAL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace HRMSys.DAL
{
    public class OperatorDAL
    {
           

        /// <summary>
        /// 將表的形式轉換爲vs的形式,給對象
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private Operator ToOperator(DataRow row)
        {
            Operator op = new Operator();
            op.Id = (Guid)row["Id"];//不加就能夠檢查用戶名是否重複了
            op.UserName = (string)row["UserName"];
            op.Password = (string)row["Password"];
            op.IsLocked = (bool)row["IsLocked"];
            op.IsDelete=(bool)row["IsDelete"];
            op.RealName = (string)row["RealName"];
            return op;
        }
        /// <summary>
        ///查詢指定username的一條數據
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Operator loginUser(string name)
        {
               DataTable table = sqlhelper.datatable("select * from T_Operator where UserName=@UserName and IsDelete=0 and IsLocked=0",
                   new SqlParameter("@UserName", name));
               if (table.Rows.Count <= 0)
                   return null;

               else if (table.Rows.Count > 1)
                   throw new Exception("用戶名重複");
               else
               {
                   DataRow row = table.Rows[0];
                  

                   return ToOperator(row);
               }
        }
        /// <summary>
        /// 鎖定管理員更新管理員表
        /// </summary>
        /// <param name="op"></param>
        public void UpdateOperator(Operator op)
        {
            sqlhelper.ExecuteNon("update T_Operator set IsLocked=@IsLocked  where Id=@Id",
                new SqlParameter("@IsLocked", op.IsLocked),
                new SqlParameter("@id",op.Id));
 
        }
       
    }
}

f,login.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSys.Model;
using HRMSys.BLL;
using HRMSys.BLL.cs;

namespace HYMSys.UI
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }
        int errorTime = 0;
        string message;
        Operator op;
        OperationLogBLL logbll = new OperationLogBLL();
        OperatorBLL bll = new OperatorBLL();
        /// <summary>
        /// 登陸,並保存登陸操做記錄
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
             
            //返回是否登陸成功的bool類型,並將提示信息和管理員對象返回
          bool result=  bll.login(tb_name.Text, errorTime, tb_pwd.Text, out message, out op);
          if (result == false)
          {
              errorTime += 1;
              MessageBox.Show(message);
              
              return;
          }
          else
          {
              //插入一條操做記錄
              logbll.InsertOperationLog(op.Id, op.UserName, message);
              MessageBox.Show(message);

          }

        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

 

三:總結

這是一個很是簡單但很是典型的一個三層架構的小例子,但願能給像我同樣的初學者一些幫助。歡迎你們來積極的討論。

相關文章
相關標籤/搜索