【机房重构】-登录小魔头

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qiqibei666/article/details/81591370

前言:

  通过三层的学习,对于分层有了那么一丢丢的认识,了解到为什么要使用分层,基于对三层的认识,看一下七层又是何方神圣;


1、What ?

  三层为B层,D层,U层,七层在三层的基础上增加了实体层Entity,接口层IDAL,SQLHelper层,工厂层Factory,外观层Facade,增加的这些层又有哪些作用呢?详见下表☟☟☟

  • 各层展示-构建过程
实现 作用
Entity 业务实体层,实现业务实体
IDAL 实现接口,放置接口函数,用以解决B层与D层耦合
DAL 接口中方法的实现,并接收sqlHelper
sqlHelper 具体执行数据的增删改查
配置文件 为DAL提供程序集
Factory 接收配置文件数据,并返回程序集指定类的实例
BLL 接收D层返回值,进行逻辑判断,实现接口方法
Facade 外观层-调用B层,得到B层执行结果返回值
UI 实例化外观层,接收返回值,实现登录

2、How ?

  • 各个层之间是怎么联系的呢?直接上图吧:

这里写图片描述

3、代码展示:

  • UI层–采集用户输入的信息和操作,展示给客户
using System.Windows.Forms;

namespace UI
{
    public partial class frmLogin : Form
    {
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //判断输入是否为空
            if (txtUserID.Text.Trim() == "")
            {
                MessageBox.Show("用户名不能为空,请输入", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (txtPassWord.Text == "")
            {
                MessageBox.Show("密码不能为空,请输入", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            //调用实体层Entity
            Entity.UserInfo user = new Entity.UserInfo();
            //实体赋值,传参
            user.userID = txtUserID.Text;
            user.passWord = txtPassWord.Text;

            Boolean flag = false;

            //实例化外观,将U层信息传入外观层,外观传入B层判断
            Facade.LoginFacade FLogin = new Facade.LoginFacade();

            //调用外观的方法,返回给user,
            flag = FLogin.SelectUser(user);

            if (flag != false)
            {
                this.Hide();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                DataTable level = FLogin.SelectLevel(user);
                if (level.Rows[0][3].ToString() == "学生")
                {
                    frmStudent frmStudent = new frmStudent();
                    frmStudent.userID = txtUserID.Text;
                    frmStudent.Show();
                }
                else if (level.Rows[0][3].ToString() == "教师")
                {
                    frmTeacher frmTeacher = new frmTeacher();
                    frmTeacher.Show();
                }
                else
                {
                    frmAdmin frmAdmin = new frmAdmin();
                    frmAdmin.Show();
                }                
            }
            else
            {
                MessageBox.Show("您输入的用户名或密码错误,请重新输入");
            }            
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            //彻底的退出方式,强制退出
            System.Environment.Exit(0);
        }
    }
}
  • 外观层–运用外观模式,连接U层与B层,避免两层之间的直接联系,解耦合;
using System.Data;

namespace Facade
{
    public class LoginFacade
    {
        public Boolean SelectUser(Entity.UserInfo user)
        {
            bool flag;
            //调用B层,得到B层处理结果返回值,U层实例化外观层接受返回值
            BLL.LoginBLL userBLL = new BLL.LoginBLL();
            flag = userBLL.UserBLL(user);
            return flag;
        }

        //判断登录权限
        public DataTable SelectLevel(Entity.UserInfo level1)
        {
            BLL.LoginBLL levelBLL = new BLL.LoginBLL();

            DataTable level = levelBLL.LevelBll(level1);

            //返回DataTable level
            return level;
        }
  • BLL—B层逻辑层,接受工厂的数据,引用接口与D层联系,接受D层数据进行逻辑判断,实现接口方法;
namespace BLL
{
    public class LoginBLL
    {
        public Boolean UserBLL(Entity.UserInfo UserInfo)
        {
            // 实例化工厂
            Factory.LoginFactory fact = new Factory.LoginFactory();

            //调用工厂方法创建接口
            IDAL.ILoginIDAL idal = fact.CreateUser();

            //接受D层的返回值(DataTable-中间变量,用于存储D层查询到的数据)
            //调用接口方法:selectUser
            DataTable table = idal.selectUser(UserInfo);

            //返回的DataTable类型,如果他的行数等于0,说明没有符合该账号密码的用户
            bool flag;
            if (table.Rows.Count == 0)
            { flag = false; }
            else
            {
                flag = true;
            }
            return flag;
        }

        public DataTable LevelBll(Entity.UserInfo UserInfo)
        {
            // 实例化工厂
            Factory.LoginFactory fact = new Factory.LoginFactory();

            //调用工厂方法创建接口
            IDAL.ILoginIDAL idal = fact.CreateUser();

            //接受D层返回值
            DataTable level = idal.selectUser(UserInfo);

            return level;
        }
    }
  • Factory—工厂层,使用了抽象工厂+反射+配置文件,接受配置文件数据,方便数据库的更换;
using System.Configuration;//配置文件的引用
using System.Reflection;//反射的应用

namespace Factory
{
    public class LoginFactory
    {
        //接受来自配置文件的数据
        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];

        //返回配置文件程序集指定类的实例
        public IDAL.ILoginIDAL CreateUser()
        {
            string className = StrDB + "." + "LoginDAL"; //DAL层类名
            return (IDAL.ILoginIDAL)Assembly.Load(StrDB).CreateInstance(className) ; //反射加工厂的应用
        }
    }
}
  • 配置文件—连接数据库,为DAL提供程序集
<appSettings>
    //此处MOMO为本地服务器的名称,一般是我们电脑的名称;
    <add key ="ConnStr" value="server=MOMO; database=Restructure;user ID = sa ; pwd=1"/>    
    <!-- 配置信息是为了提供DAL的程序集-->
    <add key ="DB" value="DAL" />
  </appSettings>
  • SqlHelper—执行具体数据的增删改查,降低各层的耦合性;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    //执行数据增删改查
    public class SQLHelper
    {
        private SqlConnection conn = null;

        //表示要对数据库执行的一个T-SQL语句或存储过程
        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 ExecuteNonQuery(string cmdText, CommandType ct)
        {
            int res;
            try
            {
                cmd = new SqlCommand(cmdText, Getconn());
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return res;
        }

        public int ExecuteNonQuery(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;
        }

        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;
        }

        /// <summary>
        /// 执行带参数的查询SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">查询SQL语句或存储过程</param>
        /// <param name="paras">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        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;
        }
    }
}
  • DAL—D层,与三层中的作用一致,与数据打交道,提供基本的数据范文,将业务逻辑处理交给B层进行;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    //实现接口中的方法,并接收sqlHelper执行结果返回值
    public class LoginDAL:IDAL.ILoginIDAL
    {
        //实现接口中的方法
        public DataTable selectUser(Entity.UserInfo UserInfo)
        {
            //实例化SQLHelper对象
            SQLHelper sqlHelper = new SQLHelper();

            //实例化参数数组
            SqlParameter[] sqlParams = { new SqlParameter("@userID", UserInfo.userID),
                                         new SqlParameter("@passWord", UserInfo.passWord)};

            string sql = @"SELECT * FROM T_User WHERE userID=@userID AND passWord=@passWord";

            //调用sqlHelper中的DataTable()方法执行查询,并接收返回值
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }
    }
}
  • 接口层—与外观层起到的作用一致,用于在B层与D层之间解耦合
using System.Data;

namespace IDAL
{
    //实现接口,放置接口函数,解决B层D层耦合
    public interface ILoginIDAL
    {
        DataTable selectUser(Entity.UserInfo Userinfo);        
    }
}
  • Entity—实体层,对字段进行封装

小结:

  篇幅不宜过长,常见错误下一博客见,每一次项目的开始比较坎坷的,正所谓万事开头难嘛,站在巨人的肩膀上,更好的认识学习,而不是较死劲,转牛角尖,革命才刚刚开始,前辈还需加油!

猜你喜欢

转载自blog.csdn.net/qiqibei666/article/details/81591370
今日推荐