详解三层转七层:登录

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

背景

一开始借用别人的代码,敲完也是很多地方看不懂!不知道从什么地方下手!不懂三层到七层到底是怎么映射过去的!

后来就是多查,慢慢有大体的轮廓,逐个部分解决!

过程:

1.看整体,对于不懂的部分,先查个大概

2.把属于三层的部分分出来看,明白七层里面是怎么走的

3.搞明白工厂,外观是为什么加,怎么加上去的

外观模式:

工厂模式:

在七层中,一开始的初心的确是用抽象工厂+反射,因为不确定数据库的使用,所以需要写出个中工厂类

但后来实际分析,七层中用的并不是抽象工厂,而是简单工厂改进的抽象工厂

因为如果真的是抽象工厂,那为什么代码里没有IFactory的类呢?

其实它将抽象工厂类IFactory和具体的实现子类用一个单独的类来表示,也就是简单工厂,将原本抽象工厂中实例化不同的工厂的过程变成了在简单工厂中的选择

最后,而在简单工厂中有因为应用反射来代替简单工厂中的选择

所以,就不存在选择的部分,想用哪个数据库,直接改配置文件,实例的过程是依赖配置文件来实例化,修改配置文件,只需要该DAL里面具体实现接口的类

关于每一层具体的功能,其实对照着三层,就能够大致感觉出来

4.调试,解决运行过程中出现的问题

收获

敲七层看了很多博客,也回头了看自己的博客,最明显的感受,不管做什么都要一往无前!纯理论——转变——实际应用

一往无前,一直前进,前面没有东西能够阻挡,勇猛武威的前进
【反义词】畏葸不前,裹足不定,畏首畏尾                                                                         

越勇敢不怕,越是会发现 问题如此简单,多查,多反复,多实践总结

代码:

Entity层

namespace Entity
{
    public class UserInfo
    {
        
        private int? userid;      //定义字段 用户ID 字段
        private string userName;  //定义字段 用户名 字段
        private string password;  //定义字段 密码 字段
        private string level;     //定义字段 等级 字段
        

        public int? Userid { get => userid; set => userid = value; }        
        public string UserName { get => userName; set => userName = value; }
        public string Password { get => password; set => password = value; }        
        public string Level { get => level; set => level = value; }       
        
    }
}

DAL层

namespace DAL
{
    public class LoginDAL:IDAL.LoginIDAL   
    {
        public DataTable selectUser(Entity.UserInfo UserInfo)  //在内存中的一个数据表
        {           
            SqlHelper sqlHelper = new SqlHelper();
            string sql = @"SELECT * FROM [User_info] WHERE UserID= @userID and Pwd= @PassWord";
            SqlParameter[] sqlParams = { new SqlParameter("@userID", UserInfo.Userid), new SqlParameter("@PassWord", UserInfo.Password) };
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;          
        }
    }
}

sqlHelp类前面博客中已经完成

IDAL层

namespace IDAL
{
    public interface LoginIDAL
    {
        DataTable selectUser(Entity.UserInfo UserInfo);
    }
}

 

Factory层

using System.Reflection;

namespace Factory
{
    public class LoginFactory
    {


        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];   //接收来自配置文件的数据
        public IDAL.LoginIDAL CreateUser()
        {
            string ClassName = StrDB + "." + "LoginDAL";//DAL层的类名
            return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);//反射+工厂的应用
        }
    }
}

 

BLL层

namespace BLL
{
    public class LoginBLL
    {
        public bool UserBLL(Entity.UserInfo UserInfo)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂
            IDAL.LoginIDAL idal = fact.CreateUser();  //调用工厂方法创建接口
            DataTable table = idal.selectUser(UserInfo);//接收D层的返回值
            bool flag;
            if(table.Rows.Count==0)
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            return flag;
        }
    }
}

 

Facade层

namespace Facade
{
    public class LoginFacade
    {
        public Boolean SelectUser(Entity.UserInfo user)
        {
            
            bool flag;
            BLL.LoginBLL userBLL = new BLL.LoginBLL();
            flag = userBLL.UserBLL(user);
            return flag;
        }
    }
}

 

UI层

namespace UI
{
    public partial class UILogin : Form
    {
        public UILogin()
        {
            InitializeComponent();
        }

        private void butOk_Click(object sender, EventArgs e)
        {
            if (txtUserID.Text.Trim() == "")
            {
                MessageBox.Show("用户名不能为空,请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (txtPassword.Text == "")
            {
                MessageBox.Show("密码不能为空,请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            //try
            //{
                
                Entity.UserInfo user = new Entity.UserInfo();
                user.Userid = Convert.ToInt32(txtUserID.Text.Trim());
                user.Password = txtPassword.Text;


                Boolean flag = false;
                Facade.LoginFacade FLogin = new Facade.LoginFacade(); //实例化外观

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

                if (flag!=false)
                {
                    MessageBox.Show("登录成功");
                    this.Hide();//隐藏当前窗体
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    frmMain frmMain = new frmMain();//实例化一个窗体
                    frmMain.Show();
                }
                else
                {
                    MessageBox.Show("密码或用户名错误");
                }

            //}
            //catch (Exception)
            //{

            //    throw;
            //}
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);//这是最彻底的退出方式,不管什么线程都能被强制退出

            //this.Close();//只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外,若有托管线程(非主线程),也无法干净退出
            //Application.Exit();//强制所有消息终止,退出所有的窗体,但是若有托管线程(非主线程),也是无法干净退出的;
            //Application.ExitThread();//强制终止调用线程上的所有消息,同样面临其他线程无法正确退出的难题
        }

        
    }
}

猜你喜欢

转载自blog.csdn.net/zhaofen_7/article/details/82142546
今日推荐