DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/CrudCacheRepositoryBase.cs

112 lines
3.4 KiB
C#
Raw Permalink Normal View History

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;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Repository;
2024-08-19 10:01:07 +05:00
/// <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;
2024-08-19 10:01:07 +05:00
public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
: base(dbContext, memoryCache)
{
crudServiceBase = new CrudRepositoryBase<TDto, TEntity>(dbContext);
}
2024-08-19 10:01:07 +05:00
public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(dbContext, memoryCache, makeQuery)
{
crudServiceBase = new CrudRepositoryBase<TDto, TEntity>(dbContext, makeQuery);
}
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public virtual async Task<int> InsertAsync(TDto newItem, CancellationToken token)
{
var result = await crudServiceBase.InsertAsync(newItem, token);
if (result > 0)
DropCache();
return result;
}
2024-08-19 10:01:07 +05:00
/// <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;
}
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public virtual async Task<int> UpdateAsync(TDto dto, CancellationToken token)
{
var result = await crudServiceBase.UpdateAsync(dto, token);
if (result > 0)
DropCache();
return result;
}
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
{
var cache = await GetCacheAsync(token);
var dtos = cache.Select(Convert);
return dtos;
}
2024-08-19 10:01:07 +05:00
/// <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;
}
2024-08-19 10:01:07 +05:00
/// <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;
}
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public virtual async Task<int> DeleteAsync(int id, CancellationToken token)
{
var result = await crudServiceBase.DeleteAsync(id, token);
if (result > 0)
DropCache();
return result;
}
2024-08-19 10:01:07 +05:00
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
}