仓储模式
原理:
仓储(Repository)模式自2004年首次作为领域驱动模式DDD设计的一部分引入,仓储本质上是提供提供数据的抽象,以便应用程序可以使用具有接口相似的简单抽象集合。从此集合中CURD是通过一些列直接的方法完成,无需处理连接、命令等问题,使用此模式可以帮组实现松耦合,并保持领域对象的持久性无知。
1.仓促模式是为了在程序的数据访问层和业务逻辑层之间建立一个抽象。
2.仓促模式是一种数据访问模式,提供一种更松散耦合的数据访问方法。
3.将创建数据访问的逻辑写在单独的类中即仓储。
4.仓储负责的业务层进行持久化通信。
仓储(Repository)是存在于工作单元和数据库之间单独分离出来的一层,是数据的封装。
优点:
1.业务层无需知道具体实现到分离关注点。
2.提高对数据库访问的维护,对于仓储的改变并不改变业务的逻辑。
3.万一哪天需要更换底层如果你使用了仓储模式的话,这样更换底层就十分的方便,只需要将仓储中的实现挨个重写一遍,这就是好处。
创建IRepository类库添加IBaseRepository类:
using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; namespace MiMall.IRepository { public interface IBaseRepository<T> where T : class, new() { /// <summary> /// 添加单个实体 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public Task<int> Add(T entity); /// <summary> /// 添加多个实体 /// </summary> /// <param name="entitys">实体集合</param> /// <returns></returns> public Task<int> Add(List<T> entitys); /// <summary> /// 根据id删除实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<int> Delete(int id); /// <summary> /// 根据id删除多个实体 /// </summary> /// <param name="ids">id集合</param> /// <returns></returns> public Task<int> Delete(params int[] ids); /// <summary> /// 如果不传入prototypes,则默认修改整个实体,反之,只修改某个属性值 /// </summary> /// <param name="entity">实体</param> /// <param name="prototypes">属性</param> /// <returns></returns> public Task<int> Update(T entity, params string[] prototypes); /// <summary> /// 根据条件查询符合条件的第一个元素 /// </summary> /// <param name="where">条件</param> /// <returns></returns> public Task<T> FirstOrDefault(Expression<Func<T, bool>> where); /// <summary> /// 可根据条件查询数据,并可指定排序规则 /// </summary> /// <typeparam name="S">排序字段的类型</typeparam> /// <param name="where">查询条件</param> /// <param name="orderBy">排序字段</param> /// <param name="isAsc">是否为顺序</param> /// <returns></returns> public Task<List<T>> GetList<S>(Expression<Func<T, bool>> where = null , Expression<Func<T, S>> orderBy = null, bool isAsc = true); /// <summary> /// 查询分页数据, /// </summary> /// <typeparam name="S">排序字段的类型</typeparam> /// <param name="where">查询条件</param> /// <param name="orderBy">排序字段</param> /// <param name="isAsc">是否为顺序</param> /// <param name="skip">要跳过几条</param> /// <param name="take">要查询几条</param> /// <returns></returns> public Task<List<T>> GetPage<S>(Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy , bool isAsc, int skip, int take); /// <summary> /// 根据id查询实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<T> Find(int id); /// <summary> /// 提交 /// </summary> /// <returns></returns> public Task<int> Commit(); } }
创建Repository类库添加BaseRepository类:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using MiMall.IRepository; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace MiMall.Repository { public class BaseRepository<T> : IBaseRepository<T> where T : class, new() { public DbContext context; public BaseRepository(DbContext context) { this.context = context; this.context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; } public async Task<int> Add(T entity) { context.Set<T>().Add(entity); return await Commit(); } public async Task<int> Add(List<T> entitys) { context.Set<T>().AddRange(entitys); return await Commit(); } public async Task<int> Delete(int id) { T model = await Find(id); context.Set<T>().Remove(model); return await Commit(); } public async Task<int> Delete(params int[] ids) { for (int i = 0; i < ids.Length; i++) { T model = await Find(ids[i]); context.Set<T>().Remove(model); } return await Commit(); } public Task<int> Update(T entity, params string[] prototypes) { context.Attach<T>(entity); if (prototypes.Length > 0) { for (int i = 0; i < prototypes.Length; i++) { context.Entry<T>(entity).Property(prototypes[i]).IsModified = true; } } else { context.Entry<T>(entity).State = EntityState.Modified; } return Commit(); } public async Task<T> FirstOrDefault(Expression<Func<T, bool>> where) { return await context.Set<T>().FirstOrDefaultAsync(where); } public async Task<List<T>> GetList<S>(Expression<Func<T, bool>> where = null , Expression<Func<T, S>> orderBy = null, bool isAsc = true) { bool b = true; List<T> list = new List<T>(); if (where != null) { list = await context.Set<T>().Where(where).AsNoTracking().ToListAsync(); b = false; } if (orderBy != null) { if (isAsc) { list = await context.Set<T>().Where(where).OrderBy(orderBy).AsNoTracking().ToListAsync(); } else { list = await context.Set<T>().Where(where).OrderByDescending(orderBy) .AsNoTracking().ToListAsync(); } b = false; } if (b) { list = await context.Set<T>().ToListAsync(); } return list; } public async Task<List<T>> GetPage<S>(Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy , bool isAsc, int skip, int take) { List<T> list = new List<T>(); if (isAsc) { list = await context.Set<T>().Where(where).OrderBy(orderBy).Skip(skip).Take(take) .AsNoTracking().ToListAsync(); } else { list = await context.Set<T>().Where(where).OrderByDescending(orderBy).Skip(skip).Take(take) .AsNoTracking().ToListAsync(); } return list; } public async Task<T> Find(int id) { return await context.Set<T>().FindAsync(id); } public async Task<int> Commit() { return await context.SaveChangesAsync(); } } }
创建IService类库添加IBaseService类:
using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; namespace MiMall.IService { public interface IBaseService<T> where T : class, new() { /// <summary> /// 添加单个实体 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public Task<int> Add(T entity); /// <summary> /// 添加多个实体 /// </summary> /// <param name="entitys">实体集合</param> /// <returns></returns> public Task<int> Add(List<T> entitys); /// <summary> /// 根据id删除实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<int> Delete(int id); /// <summary> /// 根据id删除多个实体 /// </summary> /// <param name="ids">id集合</param> /// <returns></returns> public Task<int> Delete(params int[] ids); /// <summary> /// 如果不传入prototypes,则默认修改整个实体,反之,只修改某个属性值 /// </summary> /// <param name="entity">实体</param> /// <param name="prototypes">属性</param> /// <returns></returns> public Task<int> Update(T entity, params string[] prototypes); /// <summary> /// 根据条件查询符合条件的第一个元素 /// </summary> /// <param name="where">条件</param> /// <returns></returns> public Task<T> FirstOrDefault(Expression<Func<T, bool>> where); /// <summary> /// 可根据条件查询数据,并可指定排序规则 /// </summary> /// <typeparam name="S">排序字段的类型</typeparam> /// <param name="where">查询条件</param> /// <param name="orderBy">排序字段</param> /// <param name="isAsc">是否为顺序</param> /// <returns></returns> public Task<List<T>> GetList<S>(Expression<Func<T, bool>> where = null , Expression<Func<T, S>> orderBy = null, bool isAsc = true); /// <summary> /// 查询分页数据, /// </summary> /// <typeparam name="S">排序字段的类型</typeparam> /// <param name="where">查询条件</param> /// <param name="orderBy">排序字段</param> /// <param name="isAsc">是否为顺序</param> /// <param name="skip">要跳过几条</param> /// <param name="take">要查询几条</param> /// <returns></returns> public Task<List<T>> GetPage<S>(Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy , bool isAsc, int skip, int take); /// <summary> /// 根据id查询实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<T> Find(int id); /// <summary> /// 提交 /// </summary> /// <returns></returns> public Task<int> Commit(); } }
创建Service类库添加BaseService类:
using MiMall.IRepository; using MiMall.IService; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; namespace MiMall.Service { public class BaseService<T> : IBaseService<T> where T : class, new() { public readonly IBaseRepository<T> _baseRepository; public BaseService(IBaseRepository<T> baseRepository) { _baseRepository = baseRepository; } public Task<int> Add(T entity) { return _baseRepository.Add(entity); } public Task<int> Add(List<T> entitys) { return _baseRepository.Add(entitys); } public Task<int> Commit() { return _baseRepository.Commit(); } public Task<int> Delete(int id) { return _baseRepository.Delete(id); } public Task<int> Delete(params int[] ids) { return _baseRepository.Delete(ids); } public Task<T> Find(int id) { return _baseRepository.Find(id); } public Task<T> FirstOrDefault(Expression<Func<T, bool>> where) { return _baseRepository.FirstOrDefault(where); } public Task<List<T>> GetList<S>(Expression<Func<T, bool>> where = null, Expression<Func<T, S>> orderBy = null, bool isAsc = true) { return _baseRepository.GetList(where, orderBy, isAsc); } public Task<List<T>> GetPage<S>(Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy, bool isAsc, int skip, int take) { return _baseRepository.GetPage<S>(where, orderBy, isAsc, skip, take); } public Task<int> Update(T entity, params string[] prototypes) { return _baseRepository.Update(entity, prototypes); } } }