文章目录
- 项目地址
- 一、创建Repository文件夹结构
- 1.1 定义通用的IRepository接口
- 1.2 实现通用仓储类
- 1.3 创建一个具体的实体类的接口ICategoryRepository
- 1.4 实现ICategoryRepository接口
- 1.5 修改之前的方法,使用Repository
- 二、使用Unit of Work(不知道)
项目地址
C#
一、创建Repository文件夹结构
- 项目文件夹下创建
Repository
文件夹 - 在
Repository
文件夹下,创建IRepository
接口文件夹
1.1 定义通用的IRepository接口
- 在
IRepository
接口文件夹下创建IRepository.cs
文件
IEnumerable<T> GetAll()
:IEnumerable<T>
,表示一个可以遍历的集合,包含所有实体,获取数据库中所有 T 类型的实体对象;Expression<Func<T, bool>> filter
:表达式树,允许将查询条件作为函数传递,例如 c => c.Id == 1,可以在运行时解析为 SQL 查询。
using System.Linq.Expressions;namespace MyMvcDemo.DataAccess.Repository.IRepository
{public interface IRepository<T> where T : class{IEnumerable<T> GetAll(); T Get(Expression< Func<T, bool> > filter); void Add(T entity); void Remove(T entity); void RemoveRange(IEnumerable<T> entities