forked from ddrilling/AsbCloudServer
114 lines
3.7 KiB
C#
114 lines
3.7 KiB
C#
using AsbCloudApp.Services;
|
||
using AsbCloudDb.Model;
|
||
using Mapster;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Caching.Memory;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AsbCloudInfrastructure.Repository
|
||
{
|
||
|
||
/// <summary>
|
||
/// CRUD сервис с кешем в оперативке
|
||
/// </summary>
|
||
/// <typeparam name="TDto"></typeparam>
|
||
/// <typeparam name="TEntity"></typeparam>
|
||
public class CrudCacheRepositoryBase<TDto, TEntity> : CacheBase<TEntity>, ICrudRepository<TDto>
|
||
where TDto : AsbCloudApp.Data.IId
|
||
where TEntity : class, IId
|
||
{
|
||
protected int KeySelector(TEntity entity) => entity.Id;
|
||
protected readonly ICrudRepository<TDto> crudServiceBase;
|
||
|
||
public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
|
||
: base(dbContext, memoryCache)
|
||
{
|
||
crudServiceBase = new CrudRepositoryBase<TDto, TEntity>(dbContext);
|
||
}
|
||
|
||
public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||
: base(dbContext, memoryCache, makeQuery)
|
||
{
|
||
crudServiceBase = new CrudRepositoryBase<TDto, TEntity>(dbContext, makeQuery);
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<int> InsertAsync(TDto newItem, CancellationToken token)
|
||
{
|
||
var result = await crudServiceBase.InsertAsync(newItem, token);
|
||
if (result > 0)
|
||
DropCache();
|
||
return result;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
||
{
|
||
var result = await crudServiceBase.InsertRangeAsync(dtos, token);
|
||
if (result > 0)
|
||
DropCache();
|
||
return result;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<int> UpdateAsync(TDto dto, CancellationToken token)
|
||
{
|
||
var result = await crudServiceBase.UpdateAsync(dto, token);
|
||
if (result > 0)
|
||
DropCache();
|
||
return result;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
|
||
{
|
||
var cache = await GetCacheAsync(token);
|
||
var dtos = cache.Select(Convert);
|
||
return dtos;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Синхронно получить запись по ИД
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public TDto? GetOrDefault(int id)
|
||
{
|
||
var cache = GetCache();
|
||
var cacheItem = cache.FirstOrDefault(d => d.Id == id);
|
||
if (cacheItem is null)
|
||
return default;
|
||
var dto = Convert(cacheItem);
|
||
return dto;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
||
{
|
||
var cache = await GetCacheAsync(token);
|
||
var cacheItem = cache.FirstOrDefault(d => d.Id == id);
|
||
if (cacheItem is null)
|
||
return default;
|
||
var dto = Convert(cacheItem);
|
||
return dto;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<int> DeleteAsync(int id, CancellationToken token)
|
||
{
|
||
var result = await crudServiceBase.DeleteAsync(id, token);
|
||
if (result > 0)
|
||
DropCache();
|
||
return result;
|
||
}
|
||
|
||
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
||
|
||
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
||
}
|
||
|
||
} |