机房重构--C#版三层圆满
程序员文章站
2022-06-13 12:30:52
...
前言:
第一天将视频上面的代码和框架给建立好,下载数据库以及绑定ODBC(后面才发现不需要),第二天和第三天开始调试代码以及数据库,在调试过程中,逐渐领悟三层的意思以及三层的的大致思路。现在就全面的总结一下三层架构之系统登录。
框架结构:
UI(User Interface):显示层BLL(Business Logic Layer):业务逻辑层DAL(Data Access Layer):数据访问层UI层面是跟客户直接接触的,客户的需求以及对客户的反馈直接体现在这一层面上,BLL层是跟UI层联系上的,UI层的需求能通过BLL层来解决,DAL层只跟BLL层接触,所以,UI层与DAL层是没有任何关联的。这种设计方式能够让码农们更快速的了解到代码出错的位置以及修改的方法,每个层面与每个层面之间有联系,但是也是有区别的,所以非常方便代码的优化及修改。将这些层面比喻成顾客、服务员(UI)、厨师(BLL)、采购(DAL)是非常形象生动的。比喻的详情请见:三层架构(我的理解及详细分析)韩雪敏版本
C#代码
D层UserDAO
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace Login.DAL { public class UserDAO { public Login.Model.UserInfo SelectUser(string UserName,string Password) { //using (SqlConnection conn = new SqlConnection(DbUtil.ConnString)) SqlConnection conn = new SqlConnection(DbUtil.ConnString); { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @"SELECT ID,UserName,Password,Email FROM USERS WHERE aaa@qq.com AND aaa@qq.com"; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@UserName", UserName)); cmd.Parameters.Add(new SqlParameter("@Password", Password)); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); Login.Model.UserInfo user = null; while (reader.Read()) { //reader.GetInt32(0); if (user == null) { user = new Login.Model.UserInfo(); //实例化实体层 } user.ID = reader.GetInt32(0); user.UserName = reader.GetString(1); //读取数据库中第1列中内容(获取用户名) user.Password = reader.GetString(2); //获取密码 if (!reader.IsDBNull(3)) { user.Email = reader.GetString(3); } } return user; } } } }
D层ScoreDAO
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace Login.DAL { public class ScoreDAO { public void UpdateScore(string userName, int value) { //throw new NotFiniteNumberException(); 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(); } } } }
D层UbUtil
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Login.DAL { public class DbUtil { public static string ConnString = @"Server=.;Database=Login;User ID = sa;Password =123"; } }
Model块UserInfo
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Login.Model { public class UserInfo //声明一个公共访问类实体 { /// <summary> /// 获取封装信息 /// </summary> public int ID { get; set; } /* 类似与: private string ID; //声明私有字段值ID * public string ID; //声明公有属性ID(在外界可以访问到) * { * get{ return ID ; } * set{ ID = value ; } * } */ public string UserName { get; set; } public string Password { get; set; } public string Email { get; set; } } }
B层LoginManager
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Login.BLL { public class LoginManager { public Login.Model.UserInfo Login(string UserName,string password) { Login.DAL.UserDAO uDao = new Login.DAL.UserDAO(); Login.Model.UserInfo user = uDao.SelectUser(UserName, password); if (user != null) { Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO(); sDao.UpdateScore(UserName,10); return user; } else { throw new Exception("登录失败!"); } } } }
U层Form
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LoginUI { public partial class lblUserName : Form { public lblUserName() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { string UserName = txtUserName.Text; string password = txtPassword.Text; Login.BLL.LoginManager mgr = new Login.BLL.LoginManager(); Login.Model.UserInfo user = new Login.Model.UserInfo(); user = mgr.Login(UserName, password); //实例化逻辑业务 //Login.Model.UserInfo user = mgr.Login(userName,password); MessageBox.Show("登录用户:" + user.UserName); } } }
U层Program
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace LoginUI { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new lblUserName()); } } }
上一篇: 学成在线--day10 课程发布 ElasticSearch
下一篇: 【数据库】基础知识总结