DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs

120 lines
3.8 KiB
C#
Raw Normal View History

2021-12-20 15:15:20 +05:00
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class CrudCacheServiceBase<TDto, TModel> : ICrudService<TDto>
where TDto : AsbCloudApp.Data.IId
where TModel : class, AsbCloudDb.Model.IId
{
private CacheTable<TModel> cache = null;
private readonly IAsbCloudDbContext db;
private readonly CacheDb cacheDb;
2021-12-22 11:41:18 +05:00
public ISet<string> Includes { get; } = new SortedSet<string>();
2021-12-20 15:15:20 +05:00
2022-04-11 18:00:34 +05:00
protected CacheTable<TModel> Cache
{
get
{
if (cache is null)
2021-12-20 15:15:20 +05:00
cache = cacheDb.GetCachedTable<TModel>((AsbCloudDbContext)db, Includes);
return cache;
}
}
public CrudCacheServiceBase(IAsbCloudDbContext db, CacheDb cacheDb)
2022-04-11 18:00:34 +05:00
{
2021-12-20 15:15:20 +05:00
this.db = db;
this.cacheDb = cacheDb;
}
public virtual async Task<int> InsertAsync(TDto newItem, CancellationToken token = default)
{
var entity = Convert(newItem);
var insertedEntity = await Cache.InsertAsync(entity, token)
.ConfigureAwait(false);
2022-04-11 18:00:34 +05:00
return insertedEntity?.Id ?? -1;
2021-12-20 15:15:20 +05:00
}
public virtual async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
{
2022-04-11 18:00:34 +05:00
var entities = dtos.Select(Convert);
2021-12-20 15:15:20 +05:00
var insertedEntities = await Cache.InsertAsync(entities, token)
.ConfigureAwait(false);
2022-04-11 18:00:34 +05:00
return insertedEntities?.Count() ?? 0;
2021-12-20 15:15:20 +05:00
}
public virtual async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
{
var entities = await Cache.WhereAsync(token)
.ConfigureAwait(false);
var dtos = entities?.Select(Convert);
return dtos;
}
public virtual async Task<TDto> GetAsync(int id, CancellationToken token)
{
var entity = await Cache
.FirstOrDefaultAsync(p => p.Id == id, token)
.ConfigureAwait(false);
if (entity is null)
return default;
var dto = Convert(entity);
return dto;
}
public virtual async Task<int> UpdateAsync(int id, TDto dto, CancellationToken token)
{
if (dto.Id != id)
{
2022-04-11 18:00:34 +05:00
var exist = await Cache.ContainsAsync(i => i.Id == dto.Id, token)
2021-12-20 15:15:20 +05:00
.ConfigureAwait(false);
2022-04-11 18:00:34 +05:00
2021-12-20 15:15:20 +05:00
if (exist)
return -1;
2022-04-11 18:00:34 +05:00
2021-12-20 15:15:20 +05:00
await Cache.RemoveAsync(i => i.Id == id, token)
.ConfigureAwait(false);
}
var entity = Convert(dto);
await Cache.UpsertAsync(entity, token)
.ConfigureAwait(false);
return 1;
}
public virtual async Task<int> DeleteAsync(int id, CancellationToken token)
{
var affected = await Cache.RemoveAsync(p => p.Id == id, token)
.ConfigureAwait(false);
return affected;
}
public virtual async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
{
var affected = await Cache.RemoveAsync(p => ids.Contains(p.Id), token)
.ConfigureAwait(false);
return affected;
}
2021-12-21 11:52:53 +05:00
protected virtual TModel Convert(TDto src)
2021-12-20 15:15:20 +05:00
{
var entity = src.Adapt<TModel>();
return entity;
}
2021-12-21 11:52:53 +05:00
protected virtual TDto Convert(TModel src)
2021-12-20 15:15:20 +05:00
{
var dto = src.Adapt<TDto>();
return dto;
}
}
}