一、使用仓储的好处
- 1.提高CRUD接口复用性
- 2.解耦业务逻辑(BLL)和增删改查(CRUD),换ORM特别方便,不需要改应用层,直接改仓储层
- 3.做复杂查询
- 4.事务支持
二、Demo
public class BookRepository : IBookRepository, ITransientDependency //接口依赖注入
{
private readonly IFreeSql _fsql;
public BookRepository(IFreeSql fsql)
{
_fsql = fsql;
}
/// <summary>
/// 获取FreeSql
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public IFreeSql GetFreeSql()
{
return _fsql;
}
/// <summary>
/// 分页查询:Book
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route(nameof(GetBookPageListAsync))]
public async Task<IPageList<BookDto>> GetBookPageListAsync(BookSerachInput input)
{
var output = new PageList<BookDto>(new List<BookDto>(), input.PageIndex, input.maxResultCount);
if (false)
{
return output;//不符合要求,返回空数据
}
var fsql = GetFreeSql();
//查表
var queryList = await fsql.Select<Book>()
.Where(x => x.TimePoint.Between(input.StartTime, input.EndTime))
.WhereIf(input.StationCodes.Any(), x => input.StationCodes.Contains(x.CityCode))
.OrderBy(!string.IsNullOrEmpty(input.orderby), input.orderby)
.Page(input.PageIndex, input.maxResultCount)
.Count(out var totalCount)
.ToListAsync();
//映射结果
var dtoList = _objectMapper.Map<List<Book>, List<BookDto>>(queryList);
return new PageList<BookDto>(dtoList, input.PageIndex, input.maxResultCount, totalCount);
}
/// <summary>
/// Book 单一实体 修改
/// </summary>
public async Task<bool> UpdateBookAsync(BookDto input)
{
var fsql = GetFreeSql();
return fsql.Update<BookDto>(input).ExecuteAffrows() != 0;
}
/// <summary>
/// Book 单一实体 保存
/// </summary>
public async Task<bool> SaveBookAsync(BookDto input)
{
var fsql = GetFreeSql();
bool isExist = await fsql.Select<Book>().AnyAsync(x => x.Id == input.Id);//【根据实际修改】根据(联合)主键确定唯一性
if (isExist)
{
return await UpdateBookAsync(input);
}
return await InsertBookAsync(input);
}
/// <summary>
/// Book 单一实体 插入
/// </summary>
public async Task<bool> InsertBookAsync(BookDto input)
{
var fsql = GetFreeSql();
return fsql.Insert(input).ExecuteAffrows() != 0;
}
/// <summary>
/// Book 单一实体 删除
/// </summary>
public async Task<bool> DeleteBookAsync(BookDto input)
{
var fsql = GetFreeSql();
return fsql.Delete<BookDto>(input).ExecuteAffrows() != 0;
}
/// <summary>
/// Book 列表 保存
/// </summary>
public async Task<bool> SaveBookListAsync(List<BookDto> inputList)
{
bool result = true;
var fsql = GetFreeSql();
//获取输入主键
var inputKeyList = inputList.Select(x => x.Id ).Distinct().ToList();
//根据输入主键获取已存在实体
var existList = await fsql.Select<Book>().Where(x => inputKeyList.Contains(x.Id)).ToListAsync();
//获取已存在实体的主键列表
var existKeyList = existList.Select(x => x.Id).Distinct().ToList();
// 获取更新列表和插入列表
var updateList = inputList.Where(x => existKeyList.Contains(x.Id)).ToList();
var insertList = inputList.Where(x => !existKeyList.Contains(x.Id)).ToList();
/*联合主键:
//获取输入主键
var inputKeyList = inputList.Select(x => new { x.Id , x.BookName }).Distinct().ToList();
//根据输入主键获取已存在实体
var existList = await fsql.Select<Book>().Where(x=> inputKeyList.Any(key=>key.Id == x.Id && key.BookName == x.BookName)).ToListAsync();
//获取已存在实体的主键列表
var existKeyList = existList.Select(x => new { x.Id, x.BookName }).Distinct().ToList();
//获取更新列表和插入列表
var updateList = inputList.Where(x => existKeyList.Contains(new { x.Id, x.BookName })).ToList();
var insertList = inputList.Where(x => !existKeyList.Contains(new { x.Id, x.BookName })).ToList();
*/
// 使用批量更新和批量插入方法
bool updateSuccess = await UpdateBookListAsync(updateList); // 批量更新
bool insertSuccess = await InsertBookListAsync(insertList); // 批量插入
return updateSuccess && insertSuccess; // 返回是否全部成功
}
/// <summary>
/// Book 列表 修改
/// </summary>
public async Task<bool> UpdateBookListAsync(List<BookDto> input)
{
var fsql = GetFreeSql();
return fsql.Update<BookDto>(input).ExecuteAffrows() != 0;
}
/// <summary>
/// Book 列表 插入
/// </summary>
public async Task<bool> InsertBookListAsync(List<BookDto> input)
{
var fsql = GetFreeSql();
return fsql.Insert(input).ExecuteAffrows() != 0;
}
/// <summary>
/// Book 列表 删除
/// </summary>
public async Task<bool> DeleteBookListAsync(List<BookDto> input)
{
var fsql = GetFreeSql();
return fsql.Delete<BookDto>(input).ExecuteAffrows() != 0;
}
}