18.C# —— 三层结构 + 接口架构实战(UI+Model+DAL+IDAL)
目录前言一、分层架构整体介绍各层级职责分层架构优点项目解决方案结构编辑编辑编辑二、分层代码实现1.Model 层实体映射类User.cs 用户实体Product.cs 商品实体2.IDAL 接口层抽象规范IBase 泛型基接口公共 CRUD 规范IUser.cs 用户业务接口继承基接口无独有方法IProduct.cs 商品业务接口继承基接口 分页独有方法3.DAL 数据访问层接口具体实现UseDAL.cs 用户数据实现List 模拟数据库ProductDAL.cs 商品数据实现4.UI 层控制台菜单交互三、总结前言分层架构是后端项目最基础的设计方案把项目拆分成UI 表现层、Model 实体层、IDAL 抽象接口层、DAL 数据访问层实现高内聚、松耦合便于团队协作、后期迭代维护整套方案用控制台项目演示配套实体、接口、数据存取、菜单交互完整代码。一、分层架构整体介绍各层级职责UI 层表现层控制台 / Winform/WPF负责页面交互、接收用户输入、展示数据不直接操作数据与 SQL只调用 DAL 提供的方法。Model 实体层类库数据库表映射实体类封装属性用来承载数据在各层之间传递数据。IDAL 接口层抽象层类库抽取数据层通用 CRUD 为泛型基接口IBaseT各业务接口继承基接口定义业务独有方法用于解耦规范 DAL 方法签名。DAL 数据访问层类库实现 IDAL 对应的接口完成数据增删改查本案例用内存List模拟数据库存储。分层架构优点项目拆分为多个独立类库项目方便源码管理高内聚、松耦合修改某一层不影响其他层便于团队分工开发前端写 UI、后端写 DAL、建模写 Model后期更换数据源内存→MySQL→SQLServer只改动 DALUI 代码无需修改。项目解决方案结构解决方案 ├─ UI控制台应用启动项目 ├─ Model类库User、Product实体 ├─ IDAL类库IBaseT、IUser、IProduct └─ DAL类库UseDAL、ProductDAL实现IDAL接口类库不能单独运行被其他项目引用调用。右键解决方法添加库文件在库文件中可以添加接口二、分层代码实现1.Model 层实体映射类User.cs 用户实体namespace Model { public class User { public string Id { get; set; } public string Account { get; set; } public string Password { get; set; } //重写ToString控制台直接打印详情 public override string ToString() { return $编号:{Id},账号:{Account},密码:{Password}; } } }Product.cs 商品实体namespace Model { public class Product { public string Id { get; set; } public string ProductName { get; set; } public float Price { get; set; } public override string ToString() { return $商品编号:{Id},商品名称:{ProductName},价格:{Price}; } } }2.IDAL 接口层抽象规范IBaseT 泛型基接口公共 CRUD 规范using Model; namespace IDAL { /// summary所有数据操作通用接口增删改查/summary public interface IBaseT { bool Add(T model); bool Update(T model); bool Delete(T model); T GetModel(T model); } }IUser.cs 用户业务接口继承基接口无独有方法using Model; namespace IDAL { public interface IUser : IBaseUser { //用户独有扩展方法写在此处 } }IProduct.cs 商品业务接口继承基接口 分页独有方法using Model; namespace IDAL { public interface IProduct : IBaseProduct { int GetPage();//商品独有分页方法 } }3.DAL 数据访问层接口具体实现UseDAL.cs 用户数据实现List 模拟数据库using IDAL; using Model; using System.Collections.Generic; namespace DAL { public class UseDAL : IUser { //静态集合充当内存数据库 public static ListUser Users new ListUser(); public bool Add(User model) { Users.Add(model); return true; } public bool Delete(User model) { int idx Users.FindIndex(v v.Id model.Id); if (idx -1) return false; Users.RemoveAt(idx); return true; } public User GetModel(User model) { return Users.Find(v v.Id model.Id); } public bool Update(User model) { int idx Users.FindIndex(v v.Id model.Id); Users[idx] model; return true; } } }ProductDAL.cs 商品数据实现using IDAL; using Model; namespace DAL { public class ProductDAL : IProduct { public bool Add(Product model) { throw new NotImplementedException(); } public bool Delete(Product model) { throw new NotImplementedException(); } public Product GetModel(Product model) { throw new NotImplementedException(); } public int GetPage() { throw new NotImplementedException(); } public bool Update(Product model) { throw new NotImplementedException(); } } }4.UI 层控制台菜单交互using DAL; using Model; using System; namespace UI { internal class Program { static UseDAL useDal new UseDAL(); static ProductDAL productDal new ProductDAL(); static void Main(string[] args) { Top: Console.ForegroundColor ConsoleColor.Red; Console.WriteLine(请选择功能\n 1 添加用户 2 修改用户 3 删除用户 4 查询用户 5 添加产品 6 修改产品 7 删除产品 8 查找产品); Console.ForegroundColor ConsoleColor.White; string num Console.ReadLine(); if (num 1) { Add: Console.WriteLine(请输入要添加的用户信息格式ID,Account,Password输入exit返回主菜单); string info Console.ReadLine(); if (info exit) goto Top; string[] arr info.Split(,); if (arr.Length ! 3) { Console.WriteLine(格式错误重新输入); goto Add; } User user new User { Id arr[0], Account arr[1], Password arr[2] }; //校验ID、账号重复 if (UseDAL.Users.Find(v v.Id user.Id) ! null) { Console.WriteLine(ID重复); goto Add; } if (UseDAL.Users.Find(v v.Account user.Account) ! null) { Console.WriteLine(账号重复); goto Add; } useDal.Add(user); Console.WriteLine(全部用户); UseDAL.Users.ForEach(u Console.WriteLine(u)); goto Add; } //2/3/4/5...功能可自行扩展完善 } } }三、总结IDAL 做抽象、DAL 做实现依托泛型基接口精简重复 CRUD 代码Model 充当数据载体在 UI 与 DAL 之间传递参数UI 只依赖 DAL 实例业务与数据存取隔离后续更换数据库只改动 DAL 层分层核心面向接口编程、解耦、易扩展、易维护。