【6】VS Code 新建上位机项目---项目分层
【6】VS Code 新建上位机项目---项目分层
- 1 项目分层(layer)
- 2 项目分层实现数据插入SQL
- 3 项目分层实现 (实体类封装参数)
- 4 项目分层的实现SQL查询数据
1 项目分层(layer)
- 表示层(UI):与用户交互使用。比如按钮,输入信息等;
- 业务层(BLL):传递数据,业务逻辑。根据用户需求执行动作命令;
- 数据层(DAL):数据采集,存储,发送等。与下位机的数据交流;
- 通用层:数据加密,验证等
- 通信层:与其它设备通信
- 接口层:对外数据的交互
层可以是用一个或者多个类库组成,或者在同一个项目使用多个文件夹区分,不同文件夹中有不同的类。
2 项目分层实现数据插入SQL
项目分层框架搭建:
表示层类的文件:实体类名称+UI
数据层类的文件:实体类名称+Service
业务层类的文件:实体类名称+Manager
- Step1:新建项目ProjectLayer。在ProjectLayer中添加UI层,BLL层,DAL层。
新建EmployeeUI类来给用户输入数据,用户输入的数据要传递给BLL层进行处理;
新建EmployeeManager类接受用户输入的数据,执行完之后发送个DAL层。
新建EmployeeServies类接收BLL层的信息执行对数据操作,并返回数据库的信息。SqlHelper类是用来连接数据库。
( 项目基础的3层: 数据更新:U1–>BLL–>DAL)
- Step2:EmployeeUI类
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectLayer.BLL;//要往BLL层传信息
namespace ProjectLayer.UI
{
/// <summary>
/// 表示层UI,用户用来输入
/// </summary>
public class EmployeeUI
{
public void UserAdd()
{
//封装对象
Console.Write("请输入员工姓名:");
string employeeName = Console.ReadLine();
Console.Write("请输入员工性别:");
string gender = Console.ReadLine();
Console.Write("请输入员工作址:");
string nowAddress = Console.ReadLine();
Console.Write("请输入员工身份证号码:");
string idNo = Console.ReadLine();
Console.Write("请输入员工电话:");
string phoneNumber = Console.ReadLine();
Console.Write("请输入员工其它工作:");
string otherWork = Console.ReadLine();
Console.Write("请输入员工入职日期:");
string entryDate = Console.ReadLine();
Console.Write("请输入员工职位:");
int postld = int.Parse(Console.ReadLine());
Console.Write("请输入员工部门编号:");
int departmentld = int.Parse(Console.ReadLine());
//UI层输入的信息要被业务层BLL执行,然后才能发送给DAL层,因此要调用业务层的方法(命名空间)
EmployeeManager employeeManager = new EmployeeManager();
int result= employeeManager.AddEmployee(employeeName, gender, nowAddress, idNo, phoneNumber, otherWork, Convert.ToDateTime(entryDate), postld, departmentld);
Console.WriteLine("受影响的行数:"+result);
}
}
}
- Step3:EmployeeManger类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectLayer.DAL;
namespace ProjectLayer.BLL
{
/// <summary>
/// 业务层BLL
/// </summary>
public class EmployeeManager
{
//业务层BLL要把从UI过来输入信息,经过执行,发送给数据层DAL,因此要调用数据层的方法和命名空间
public int AddEmployee(string employeeName, string gender, string nowAddress, string idNo, string phoneNumber, string otherWork, DateTime entryDate, int postld, int departmentld)
{
//调用数据层方法
EmployeeServies employeeServies = new EmployeeServies();
//给数据层传递参数
int result= employee