2023年6月21日发(作者:)

C#WinForm项⽬三层架构简述基于C#.NET的WinForm项⽬,我们经常使⽤基于三层架构,来构建项⽬框架,这⾥简单的梳理⼀下三层架构的相关知识哪三层?我们通常所说的三层框架指的是DAL、BIL和UIL三层,分别是数据层、业务逻辑层和界⾯层,以及与之搭配的实体类和通⽤类库,下⾯分别概述实体类- Model我们将数据存放在数据库中,数据表的结构,我们通常会⽤⼀个类来抽象,表的属性就是类的属性,我们通常将表的⼀⾏存储在⼀个类中。我们在Java中,通常将其称为实体类Entity,在C#中,我们通常将其称为Model。如图,我们定义了⼀个数据表,表结构如下表结构我们根据表的信息,创建了如下的实体类namespace mtWeightModel{ [Serializable] //表⽰可以序列化 public class User { public int Id { get; set; } public String username { get; set; } public String password { get; set; } public int status { get; set; } }}我们可以看到,在SQLServer数据库中,char varchar nchar nvarchar都⽤String类型,int就对应Int。Model类库⼀般来说需要被DAL、BIL和UI引⽤。DAL-数据访问层 - DataAccessLayer数据访问层,就是调⽤我们数据库访问⽅法,专注于数据的增删改查操作,构建SQL语句,构建参数等,以下是⼀个典型DAL⽅法namespace mtWeightDAL{ ///

/// ⽤户访问数据类 /// public class UserService { /// /// 根据账号和密码⽐对⽤户信息 /// /// 包含⽤户名和密码的⽤户对象 /// 返回⽤户对象信息(若⽆⽤户信息则对象为null) public User UserLogin(User objUser) { String sql = "SELECT Id,username,password,status FROM Users where username=@username and password=@password"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@username",me), new SqlParameter("@password", rd) }; SqlDataReader objReader = der(sql, param); if (()) { = 32(objReader["Id"]); = 32(objReader["status"]); } else { objUser = null; } (); return objUser; } }}这⾥⽤到了⼀个SqlHelper类,使我们后⾯要单独说的数据库帮助类DAL就是根据业务需求,构建SQL语句,构建参数,调⽤帮助类,获取结果,DAL层被BIL层调⽤BLL-业务逻辑层 - Business Logic LayerBLL层索要负责的,就是处理业务逻辑上的问题,⽐如在调⽤数据库访问之前,对数据的处理、判断等。下⾯是⼀个最简单的业务逻辑⽅法,不处理任何信息,只做参数传递。namespace mtWeightBLL{ /// /// ⽤户业务逻辑类 /// public class UserManager { //创建数据访问对象 private UserService objUserService = new UserService(); public User UserLogin(User objUser) { return gin(objUser); } }}那你可能就会有疑问,为什么不把业务逻辑和数据访问合在⼀起呢,偏要搞出两个层,不是多此⼀举么。那其实呢,我们分层解决问题的意义就是,功能专⼀,并且解耦我们的程序,我们在DAL层只关⼼我的数据库访问操作,我默认你给我的数据是合法的、正确的,那⾄于你如何保证数据的合法性和正确性就是你需要在BLL层⾥去做的了。BLL层只被UIL层引⽤UIL-⽤户表现层就是窗体Form这⾥有⼀个click事件private void btnLogin_Click(object sender, EventArgs e) { //数据验证 if (().Length == 0) { ("请输⼊⽤户名", "登录提⽰"); (); } if (().Length == 0) { ("请输⼊密码", "登录提⽰"); (); } // 封装对象 User objUser = new User { username = (), password = () }; try { objUser = gin(objUser); if (objUser != null) { if ( == 1) { rentUser = objUser; Result = ; (); } else { ("⽤户被禁⽤,请联系管理员", "登录提⽰"); } } else { ("⽤户名或密码错误!", "登录提⽰"); } } catch (Exception ex) { ("登录异常:"+e,"登录提⽰"); } }通⽤类库对于⼀些程序中⽤到的其他封装的类库,可以统⼀放在这⾥,⽐如⼀些第三⽅类库等引⽤关系引⽤关系图数据库帮助类我们可以⽤过封装形成⾃⼰的⼀套⽅法,但是我们知道在中,SQLServer、Access、Mysql和Oracle使⽤的是不同的类,那么我们可能需要对每⼀个数据库封装⼀套帮助类。其中常⽤的⽅法包括⾮查询类⽅法(两个重载[sql],[sql,参数])和查询类⽅法(两个重载[sql],[sql,参数]),事务类⽅法、存储过程类⽅法等。我们通常为会先规定⼀个接⼝,然后在帮助类中实现结构。这样后期可以通过反射或⼯⼚的⽅式来实现不同数据库的切换(后⾯另说)。下⾯是我定义的⼀个简单的数据库帮助类using System;using c;using ;using ;using ;using uration;using ;using ent;namespace DbUtil{ public class SqlHelper { private static String ConnString = tionStrings["ConnString"].ToString(); #region 格式化SQL语句 /// /// 增删改⾮查询类⽅法 /// /// SQL语句 /// public static int UPDATE(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); return eNonQuery(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 返回单条结果查询类⽅法 /// /// /// SQL语句 /// public static object getSingleResult(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); return eScalar(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 多条结果查询类⽅法 /// /// SQL语句 /// public static SqlDataReader getReader(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); return eReader(onnection); } catch (Exception ex) { (); throw new Exception(e); }

} ///

/// 返回DataSet数据集⽅法 /// /// SQL语句 /// 结果集DataSet public static DataSet getDataSet(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); try { (); (ds); return ds; } catch (Exception ex) { throw new Exception(e); } finally { (); } } #endregion #region 带参数SQL语句 /// /// /// 增删改⾮查询类⽅法 /// /// SQL语句 /// SQl参数 /// public static int UPDATE(string sql,SqlParameter[] param) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(param); return eNonQuery(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 返回单条结果查询类⽅法 /// /// SQL语句 /// SQL参数 /// public static object getSingleResult(string sql, SqlParameter[] param) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(param); return eScalar(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 多条结果查询类⽅法 /// /// SQL语句 /// SQL参数 /// public static SqlDataReader getReader(string sql,SqlParameter[] param) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(param); return eReader(onnection); } catch (Exception ex) { (); throw new Exception(e); } } } #endregion }}项⽬创建⾸先创建解决⽅案的过程中,创建默认项⽬Windows窗体应⽤程序UIL然后分别在解决⽅案中添加类库项⽬DAL、BLL和CL(通⽤类库)和Model然后新建⼀个DBUtil类库,⾥⾯放SQLHeader类下⾯添加引⽤,从下⽹往上添加,先给DAL添加Model和DBHelper的引⽤,再为BLL添加DAL和Model的引⽤,再为UIL添加BLL和Model引⽤,然后在为需要使⽤通⽤类库的项⽬添加CL的引⽤。引⽤完成后:

2023年6月21日发(作者:)

C#WinForm项⽬三层架构简述基于C#.NET的WinForm项⽬,我们经常使⽤基于三层架构,来构建项⽬框架,这⾥简单的梳理⼀下三层架构的相关知识哪三层?我们通常所说的三层框架指的是DAL、BIL和UIL三层,分别是数据层、业务逻辑层和界⾯层,以及与之搭配的实体类和通⽤类库,下⾯分别概述实体类- Model我们将数据存放在数据库中,数据表的结构,我们通常会⽤⼀个类来抽象,表的属性就是类的属性,我们通常将表的⼀⾏存储在⼀个类中。我们在Java中,通常将其称为实体类Entity,在C#中,我们通常将其称为Model。如图,我们定义了⼀个数据表,表结构如下表结构我们根据表的信息,创建了如下的实体类namespace mtWeightModel{ [Serializable] //表⽰可以序列化 public class User { public int Id { get; set; } public String username { get; set; } public String password { get; set; } public int status { get; set; } }}我们可以看到,在SQLServer数据库中,char varchar nchar nvarchar都⽤String类型,int就对应Int。Model类库⼀般来说需要被DAL、BIL和UI引⽤。DAL-数据访问层 - DataAccessLayer数据访问层,就是调⽤我们数据库访问⽅法,专注于数据的增删改查操作,构建SQL语句,构建参数等,以下是⼀个典型DAL⽅法namespace mtWeightDAL{ ///

/// ⽤户访问数据类 /// public class UserService { /// /// 根据账号和密码⽐对⽤户信息 /// /// 包含⽤户名和密码的⽤户对象 /// 返回⽤户对象信息(若⽆⽤户信息则对象为null) public User UserLogin(User objUser) { String sql = "SELECT Id,username,password,status FROM Users where username=@username and password=@password"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@username",me), new SqlParameter("@password", rd) }; SqlDataReader objReader = der(sql, param); if (()) { = 32(objReader["Id"]); = 32(objReader["status"]); } else { objUser = null; } (); return objUser; } }}这⾥⽤到了⼀个SqlHelper类,使我们后⾯要单独说的数据库帮助类DAL就是根据业务需求,构建SQL语句,构建参数,调⽤帮助类,获取结果,DAL层被BIL层调⽤BLL-业务逻辑层 - Business Logic LayerBLL层索要负责的,就是处理业务逻辑上的问题,⽐如在调⽤数据库访问之前,对数据的处理、判断等。下⾯是⼀个最简单的业务逻辑⽅法,不处理任何信息,只做参数传递。namespace mtWeightBLL{ /// /// ⽤户业务逻辑类 /// public class UserManager { //创建数据访问对象 private UserService objUserService = new UserService(); public User UserLogin(User objUser) { return gin(objUser); } }}那你可能就会有疑问,为什么不把业务逻辑和数据访问合在⼀起呢,偏要搞出两个层,不是多此⼀举么。那其实呢,我们分层解决问题的意义就是,功能专⼀,并且解耦我们的程序,我们在DAL层只关⼼我的数据库访问操作,我默认你给我的数据是合法的、正确的,那⾄于你如何保证数据的合法性和正确性就是你需要在BLL层⾥去做的了。BLL层只被UIL层引⽤UIL-⽤户表现层就是窗体Form这⾥有⼀个click事件private void btnLogin_Click(object sender, EventArgs e) { //数据验证 if (().Length == 0) { ("请输⼊⽤户名", "登录提⽰"); (); } if (().Length == 0) { ("请输⼊密码", "登录提⽰"); (); } // 封装对象 User objUser = new User { username = (), password = () }; try { objUser = gin(objUser); if (objUser != null) { if ( == 1) { rentUser = objUser; Result = ; (); } else { ("⽤户被禁⽤,请联系管理员", "登录提⽰"); } } else { ("⽤户名或密码错误!", "登录提⽰"); } } catch (Exception ex) { ("登录异常:"+e,"登录提⽰"); } }通⽤类库对于⼀些程序中⽤到的其他封装的类库,可以统⼀放在这⾥,⽐如⼀些第三⽅类库等引⽤关系引⽤关系图数据库帮助类我们可以⽤过封装形成⾃⼰的⼀套⽅法,但是我们知道在中,SQLServer、Access、Mysql和Oracle使⽤的是不同的类,那么我们可能需要对每⼀个数据库封装⼀套帮助类。其中常⽤的⽅法包括⾮查询类⽅法(两个重载[sql],[sql,参数])和查询类⽅法(两个重载[sql],[sql,参数]),事务类⽅法、存储过程类⽅法等。我们通常为会先规定⼀个接⼝,然后在帮助类中实现结构。这样后期可以通过反射或⼯⼚的⽅式来实现不同数据库的切换(后⾯另说)。下⾯是我定义的⼀个简单的数据库帮助类using System;using c;using ;using ;using ;using uration;using ;using ent;namespace DbUtil{ public class SqlHelper { private static String ConnString = tionStrings["ConnString"].ToString(); #region 格式化SQL语句 /// /// 增删改⾮查询类⽅法 /// /// SQL语句 /// public static int UPDATE(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); return eNonQuery(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 返回单条结果查询类⽅法 /// ///
/// SQL语句 /// public static object getSingleResult(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); return eScalar(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 多条结果查询类⽅法 /// /// SQL语句 /// public static SqlDataReader getReader(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); return eReader(onnection); } catch (Exception ex) { (); throw new Exception(e); }

} ///

/// 返回DataSet数据集⽅法 /// /// SQL语句 /// 结果集DataSet public static DataSet getDataSet(string sql) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); try { (); (ds); return ds; } catch (Exception ex) { throw new Exception(e); } finally { (); } } #endregion #region 带参数SQL语句 /// /// /// 增删改⾮查询类⽅法 /// /// SQL语句 /// SQl参数 /// public static int UPDATE(string sql,SqlParameter[] param) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(param); return eNonQuery(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 返回单条结果查询类⽅法 /// /// SQL语句 /// SQL参数 /// public static object getSingleResult(string sql, SqlParameter[] param) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(param); return eScalar(); } catch (Exception ex) { throw new Exception(e); } finally { (); } } /// /// 多条结果查询类⽅法 /// /// SQL语句 /// SQL参数 /// public static SqlDataReader getReader(string sql,SqlParameter[] param) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(param); return eReader(onnection); } catch (Exception ex) { (); throw new Exception(e); } } } #endregion }}项⽬创建⾸先创建解决⽅案的过程中,创建默认项⽬Windows窗体应⽤程序UIL然后分别在解决⽅案中添加类库项⽬DAL、BLL和CL(通⽤类库)和Model然后新建⼀个DBUtil类库,⾥⾯放SQLHeader类下⾯添加引⽤,从下⽹往上添加,先给DAL添加Model和DBHelper的引⽤,再为BLL添加DAL和Model的引⽤,再为UIL添加BLL和Model引⽤,然后在为需要使⽤通⽤类库的项⽬添加CL的引⽤。引⽤完成后: