【.Net Core一个MVC简单项目开发历程】(2)-----实现基础的仓储+服务
实现基本通用的类
1.EntityFramework:
NuGet中添加Microsoft.EntityFrameworkCore的引用, 添加一个类继承 DbContext
2.IRepositories:
添加一个泛型类,并添加基础的CRUD接口,给Repository去实现:
点击查看代码
///
/// 数据访问仓储基类接口
///
///
public interface IBaseRepository where TEntity : class
{
///
/// 增加一条数据
///
///
///
///
///
Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 增加多条数据
///
///
///
///
///
Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 修改一条数据
///
///
///
///
///
Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 修改多条数据
///
///
///
///
///
Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 删除一条数据
///
///
///
///
///
Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 删除多条数据
///
///
///
///
///
Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 根据条件删除数据
///
///
///
///
///
Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 根据条件查找一条数据
///
///
///
///
Task FindAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 根据条件获取一条数据
///
///
///
///
Task GetAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 获取多条数据
///
///
///
Task> GetListAsync(CancellationToken cancellationToken = default);
///
/// 根据条件获取多条数据
///
///
///
///
Task> GetListAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 获取分页的数据
///
///
///
///
///
///
Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, CancellationToken cancellationToken = default);
///
/// 根据条件获取数据的数量
///
///
///
///
Task GetCountAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 获取数据的数量
///
///
///
Task GetCountAsync(CancellationToken cancellationToken = default);
}
添加IRepositories项目的引用,然后新建个仓储基类去实现接口
点击查看代码
public class BaseRepository : IBaseRepository where TEntity : class,new()
{
private readonly DemkinBlogContext context;
public BaseRepository(DemkinBlogContext context)
{
this.context = context;
}
protected DemkinBlogContext DbContext()
{
return context;
}
public async Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
var saveEntity = (await context.Set().AddAsync(entity, cancellationToken)).Entity;
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
return saveEntity;
}
public async Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
var entityArray = entities.ToArray();
await context.Set().AddRangeAsync(entityArray, cancellationToken);
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
}
public async Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
context.Attach(entity);
var updateEntity = context.Update(entity).Entity;
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
return updateEntity;
}
public async Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
context.Set().UpdateRange(entities);
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
}
public async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
context.Set().Remove(entity);
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
}
public async Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
context.RemoveRange(entities);
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
}
public async Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
{
var dbSet = context.Set();
var entities = await dbSet.Where(predicate).ToListAsync(cancellationToken);
await DeleteManyAsync(entities, autoSave, cancellationToken);
if (autoSave)
{
await context.SaveChangesAsync(cancellationToken);
}
}
public Task FindAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return context.Set().Where(predicate).SingleOrDefaultAsync(cancellationToken);
}
public async Task GetAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
var entity = await FindAsync(predicate, cancellationToken);
if (entity==null)
{
throw new Exception(nameof(TEntity) + ":数据不存在");
}
return entity;
}
public Task> GetListAsync(CancellationToken cancellationToken = default)
{
return context.Set().ToListAsync(cancellationToken);
}
public Task> GetListAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return context.Set().Where(predicate).ToListAsync(cancellationToken);
}
public Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, CancellationToken cancellationToken = default)
{
return context.Set().OrderBy(sorting).Skip(skipCount).Take(maxResultCount).ToListAsync(cancellationToken);
}
public Task GetCountAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return context.Set().Where(predicate).LongCountAsync(cancellationToken);
}
public Task GetCountAsync(CancellationToken cancellationToken = default)
{
return context.Set().LongCountAsync(cancellationToken);
}
}
添加一个业务逻辑基类的泛型接口
点击查看代码
///
/// 业务逻辑基类接口
///
///
public interface IBaseServices where TEntity:class
{
///
/// 增加一条数据
///
///
///
///
///
Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 增加多条数据
///
///
///
///
///
Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 修改一条数据
///
///
///
///
///
Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 修改多条数据
///
///
///
///
///
Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 删除一条数据
///
///
///
///
///
Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 删除多条数据
///
///
///
///
///
Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 根据条件删除数据
///
///
///
///
///
Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default);
///
/// 根据条件查找一条数据
///
///
///
///
Task FindAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 根据条件获取一条数据
///
///
///
///
Task GetAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 获取多条数据
///
///
///
Task> GetListAsync(CancellationToken cancellationToken = default);
///
/// 根据条件获取多条数据
///
///
///
///
Task> GetListAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 获取分页的数据
///
///
///
///
///
///
Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, CancellationToken cancellationToken = default);
///
/// 根据条件获取数据的数量
///
///
///
///
Task GetCountAsync(Expression> predicate, CancellationToken cancellationToken = default);
///
/// 获取数据的数量
///
///
///
Task GetCountAsync(CancellationToken cancellationToken = default);
}
添加IServices和IRepositories项目的引用,然后创建基类实现接口
点击查看代码
public class BaseServices : IBaseServices where TEntity : class, new()
{
private readonly IBaseRepository baseRepository;
public BaseServices(IBaseRepository baseRepository)
{
this.baseRepository = baseRepository;
}
public async Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
return await baseRepository.InsertAsync(entity, autoSave, cancellationToken);
}
public async Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
await baseRepository.InsertManyAsync(entities, autoSave, cancellationToken);
}
public async Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
return await baseRepository.UpdateAsync(entity, autoSave, cancellationToken);
}
public async Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
await baseRepository.UpdateManyAsync(entities, autoSave, cancellationToken);
}
public async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
await baseRepository.DeleteAsync(entity, autoSave, cancellationToken);
}
public async Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
{
await baseRepository.DeleteAsync(predicate, autoSave, cancellationToken);
}
public async Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
await baseRepository.DeleteManyAsync(entities, autoSave, cancellationToken);
}
public async Task FindAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return await baseRepository.FindAsync(predicate, cancellationToken);
}
public async Task GetAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return await baseRepository.GetAsync(predicate, cancellationToken);
}
public async Task> GetListAsync(CancellationToken cancellationToken = default)
{
return await baseRepository.GetListAsync(cancellationToken);
}
public async Task> GetListAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return await baseRepository.GetListAsync(predicate, cancellationToken);
}
public async Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, CancellationToken cancellationToken = default)
{
return await baseRepository.GetPagedListAsync(skipCount, maxResultCount, sorting, cancellationToken);
}
public async Task GetCountAsync(Expression> predicate, CancellationToken cancellationToken = default)
{
return await baseRepository.GetCountAsync(predicate, cancellationToken);
}
public async Task GetCountAsync(CancellationToken cancellationToken = default)
{
return await baseRepository.GetCountAsync(cancellationToken);
}
}