一, 分层架构 - 3 层架构 - 多层架构
架构:
架构一般是针对整个系统的, 并非对某个单独的问题(单独的问题可以用模式等来解决)
针对整个系统的 "一张蓝图", 对系统的抽象. 架构与具体的语言平台无关.
架构设计, 模式应用的经验积累的具体代码实现, 方便以后的复用. ASP.NET mvc,NHibemate,NSpring,...
模式:
软件开发中遇到的一些特定问题, 前人总结出来特定的经验, 解决方法.(复制某某企业的成功模式)
23 种设计模式
MVC,MVP 等模式
理解分层:
• 逻辑分层 N-Layer
逻辑上将系统中的不同功能模块, 不同子系统等进行分层.
好的逻辑分层可以让后续选择物理架构更灵活, 选择性更大
• 物理分层 N-Tier
物理部署时将系统的不同模式部署在不同的服务器上
一句话总结架构: 项目的组成, 分布, 什么问题该怎么处理(对于一些关键性问题的预见性与解决方法). 对整个项目的规划, 设计, 以及在一个系统中各个组件间的组合, 交互, 集成. 架构保证了系统的可用性, 稳定性, 灵活性, 可伸缩性, 安全性等等.
界面层 UI:
数据访问层 DAL(Data Access Layer)
业务逻辑层 BLL(business logic layer). 实体类就是 Model; 对数据进行操作的代码写在 DAL 中, 一般就是 SQL 语句, DAL 只是对数据的操作. BLL 调用 DAL 中的代码进行逻辑操作. SQL 语句, ADO.NET 的类一般只应该出现在 DAL 中.
表现层 UI:
1, 采集数据
2, 展示数据
业务逻辑层 BLL:
1, 业务相关的代码如: 删除前判断权限是否足够, 删除时是否需要备份
数据访问层 DAL:
1, 只做与数据库相关的操作, 不涉及任何其他业务逻辑
二, 实现
功能: 点击按钮, 用户年龄自增 1
界面:
非三层实现
- private void button1_Click(object sender, EventArgs e)
- {
- string strSql = "UPDATE student SET age=age+1 WHERE id='2'";
- SqlHelp.ExecuteNonQuery(strSql,CommandType.Text);
- MessageBox.Show("ok");
- }
三层实现
写三层的步骤:
1, 分析功能.
2, 根据功能确认 SQL 语句.
3, 在数据访问层 (DAL) 编写执行该 SQL 语句的函数.
该函数应该在哪个类中, 一般情况下, 当前的 SQL 语句要操作那张表, 那么就在数据访问层创建一个类, 这个类的名字与该表名相同, 所有操作这个表的函数都写在该类中.
4, 编写业务逻辑层的方法.
编写该类中的方法
4.1, 先确定方法的参数与返回值.
4.1.1, 业务逻辑层方法的返回值, 一般取决于表现层调用该方法的时候需要什么样的返回值.
5, 编写表现层代码
5.1, 采集数据
5.2, 展示数据
5.3, 调用对应的业务逻辑层来实现具体功能
注意: 在表现层中, 只调用业务逻辑层方法, 绝对不能在表现层直接调用数据访问层的代码.
文件目录解析:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
using 三层实现. DAL;
namespace 三层实现. BLL
- {
- // 业务逻辑层
- /// <summary>
- /// 操作 student 表的业务逻辑层类
- /// </summary>
- public class studentBll
- {
- /// <summary>
- /// 将指定 id 的人的年龄自增 + 1 岁
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public bool IncAge(int id)
- {
- // 实例化数据访问层
- studentDal dal = new studentDal();
- return dal.IncAge(id)>0;
- }
- }
- }
业务层: studentBll.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- namespace sln2019_05_07
- {
- public static class SqlHelp
- {
- //private static readonly string conStr = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
- private static readonly string conStr = "server=.;uid=sa;pwd=0;database=DEMO";
- /// <summary>
- /// 执行 SQL 语句
- /// </summary>
- /// <param name="strSql">sql 语句</param>
- /// <param name="cmdType">CommandType.Text 代表执行的 SQL 语句, CommandType.StoreProcedure 代表执行的是存储过程</param>
- /// <param name="pms">可变参数数组</param>
- /// <returns></returns>
- public static int ExecuteNonQuery(string strSql, CommandType cmdType, params SqlParameter[] pms)
- {
- SqlConnection conn = new SqlConnection(conStr);
- SqlCommand cmd = new SqlCommand(strSql, conn);
- cmd.CommandType = cmdType;
- if (pms != null)
- {
- cmd.Parameters.AddRange(pms);
- }
- conn.Open();
- SqlTransaction trans = conn.BeginTransaction();
- try
- {
- cmd.Transaction = trans;
- int count = cmd.ExecuteNonQuery();
- if (count> 0)
- {
- trans.Commit(); // 提交事务
- return 1;
- }
- else
- {
- trans.Rollback(); // 回滚事务
- return -1;
- }
- }
- catch (Exception EX)
- {
- trans.Rollback(); // 回滚事务
- MessageBox.Show(EX.Message.ToString());
- return -1;
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- cmd.Dispose();
- }
- }
- /// <summary>
- /// 返回一第一条记录
- /// </summary>
- /// <param name="strSql">sql 语句</param>
- /// <param name="cmdType">CommandType.Text 代表执行的 SQL 语句, CommandType.StoreProcedure 代表执行的是存储过程</param>
- /// <param name="pms">可变参数数组</param>
- /// <returns></returns>
- public static object ExecuteScalar(string strSql,CommandType cmdType,params SqlParameter[] pms)
- {
- SqlConnection conn = new SqlConnection(conStr);
- SqlCommand cmd = new SqlCommand(strSql, conn);
- cmd.CommandType = cmdType;
- if (pms != null)
- {
- cmd.Parameters.AddRange(pms);
- }
- conn.Open();
- SqlTransaction trans = conn.BeginTransaction();
- try
- {
- cmd.Transaction = trans;
- object obj = cmd.ExecuteScalar();
- if ((int)obj> 0)
- {
- trans.Commit(); // 提交事务
- return obj;
- }
- else
- {
- trans.Rollback(); // 回滚事务
- return -1;
- }
- }
- catch (Exception EX)
- {
- trans.Rollback(); // 回滚事务
- MessageBox.Show(EX.Message.ToString());
- return -1;
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- cmd.Dispose();
- }
- }
- public static SqlDataReader ExecuteReader(string strSql,CommandType cmdType,params SqlParameter[] pms)
- {
- SqlDataReader sr = null;
- SqlConnection conn = new SqlConnection(conStr);
- SqlCommand cmd = new SqlCommand(strSql,conn);
- cmd.CommandType = cmdType;
- if (pms!=null)
- {
- cmd.Parameters.AddRange(pms);
- }
- try
- {
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- sr = cmd.ExecuteReader();
- return sr;
- }
- catch (Exception EX)
- {
- MessageBox.Show(EX.Message.ToString());
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- cmd.Dispose();
- }
- return sr;
- }
- public static DataTable ExecuteDataTable(string strSql,CommandType cmdType,params SqlParameter[] pms)
- {
- DataTable dt = new DataTable();
- SqlConnection conn = new SqlConnection(conStr);
- SqlDataAdapter adap = new SqlDataAdapter(strSql, conn);
- try
- {
- adap.SelectCommand.CommandType = cmdType;
- if (pms != null)
- {
- adap.SelectCommand.Parameters.AddRange(pms);
- }
- adap.Fill(dt);
- return dt;
- }
- catch (Exception EX)
- {
- MessageBox.Show(EX.Message.ToString());
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- adap.Dispose();
- }
- return dt;
- }
- }
- }
数据访问层: SqlHelp.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Data.SqlClient;
- using sln2019_05_07;
namespace 三层实现. DAL
- {
- /// <summary>
- /// 操作 student 表的数据访问层的类
- /// </summary>
- public class studentDal
- {
- public int IncAge(int id)
- {
- string strSql = "UPDATE student SET age=age+1 WHERE [email protected]";
- return SqlHelp.ExecuteNonQuery(strSql, CommandType.Text,new SqlParameter("@id", SqlDbType.Int) { Value=id});
- }
- }
- }
数据访问层: studentDal.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
using 三层实现. BLL;
namespace 三层实现. UI
- {
- public partial class frmIncreasAge : Form
- {
- public frmIncreasAge()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //1, 实例化业务逻辑层类
- //2, 调用对应的方法.
- studentBll bll = new studentBll();
- bool bl = bll.IncAge(2);
- if (bl)
- {
- MessageBox.Show("成功!");
- }
- else
- {
- MessageBox.Show("失败!");
- }
- }
- }
- }
界面层
项目链接: https://pan.baidu.com/s/1iqORivgj1DfujFzwRKvaqg
提取码: twyz
来源: http://www.bubuko.com/infodetail-3059521.html