diff --git a/AsbCloudInfrastructure/Repository/CacheBase.cs b/AsbCloudInfrastructure/Repository/CacheBase.cs index 88761ddb..042f2517 100644 --- a/AsbCloudInfrastructure/Repository/CacheBase.cs +++ b/AsbCloudInfrastructure/Repository/CacheBase.cs @@ -1,7 +1,4 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using Mapster; +using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using System; @@ -12,12 +9,12 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Repository { - public class CacheBase : QueryContainer - where TDto : AsbCloudApp.Data.IId +#nullable enable + public class CacheBase : QueryContainer where TEntity : class, AsbCloudDb.Model.IId { protected readonly IMemoryCache memoryCache; - protected string CacheTag = typeof(TDto).Name; + protected string CacheTag = typeof(TEntity).Name; protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5); public CacheBase(IAsbCloudDbContext context, IMemoryCache memoryCache) @@ -35,7 +32,7 @@ namespace AsbCloudInfrastructure.Repository protected virtual void DropCache() => memoryCache.Remove(CacheTag); - protected virtual IEnumerable GetCache() + protected virtual IEnumerable GetCache() { var cache = memoryCache.GetOrCreate(CacheTag, cacheEntry => { @@ -43,13 +40,12 @@ namespace AsbCloudInfrastructure.Repository cacheEntry.SlidingExpiration = CacheOlescence; var entities = this.GetQuery().ToArray(); - var dtos = entities.Select(Convert); - return dtos.ToArray(); + return entities; }); return cache; } - protected virtual Task> GetCacheAsync(CancellationToken token) + protected virtual Task> GetCacheAsync(CancellationToken token) { var cache = memoryCache.GetOrCreateAsync(CacheTag, async (cacheEntry) => { @@ -57,14 +53,10 @@ namespace AsbCloudInfrastructure.Repository cacheEntry.SlidingExpiration = CacheOlescence; var entities = await this.GetQuery().ToArrayAsync(token); - var dtos = entities.Select(Convert); - return dtos.ToArray().AsEnumerable(); + return entities.AsEnumerable(); }); return cache; } - - protected virtual TDto Convert(TEntity src) => src.Adapt(); - - protected virtual TEntity Convert(TDto src) => src.Adapt(); } +#nullable disable } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/CrudCacheRepositoryBase.cs b/AsbCloudInfrastructure/Repository/CrudCacheRepositoryBase.cs index 30aaf673..ebea5d96 100644 --- a/AsbCloudInfrastructure/Repository/CrudCacheRepositoryBase.cs +++ b/AsbCloudInfrastructure/Repository/CrudCacheRepositoryBase.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Services; using AsbCloudDb.Model; +using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using System; @@ -16,7 +17,7 @@ namespace AsbCloudInfrastructure.Repository /// /// /// - public class CrudCacheRepositoryBase : CacheBase, ICrudRepository + public class CrudCacheRepositoryBase : CacheBase, ICrudRepository where TDto : AsbCloudApp.Data.IId where TEntity : class, IId { @@ -36,7 +37,7 @@ namespace AsbCloudInfrastructure.Repository } /// - public async Task InsertAsync(TDto newItem, CancellationToken token) + public virtual async Task InsertAsync(TDto newItem, CancellationToken token) { var result = await crudServiceBase.InsertAsync(newItem, token); if (result > 0) @@ -45,7 +46,7 @@ namespace AsbCloudInfrastructure.Repository } /// - public async Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) + public virtual async Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) { var result = await crudServiceBase.InsertRangeAsync(dtos, token); if (result > 0) @@ -53,11 +54,21 @@ namespace AsbCloudInfrastructure.Repository return result; } + /// + public virtual async Task UpdateAsync(TDto dto, CancellationToken token) + { + var result = await crudServiceBase.UpdateAsync(dto, token); + if (result > 0) + DropCache(); + return result; + } + /// public async Task> GetAllAsync(CancellationToken token) { var cache = await GetCacheAsync(token); - return cache; + var dtos = cache.Select(Convert); + return dtos; } /// @@ -68,33 +79,36 @@ namespace AsbCloudInfrastructure.Repository public TDto? GetOrDefault(int id) { var cache = GetCache(); - return cache.FirstOrDefault(d => d.Id == id); + var cacheItem = cache.FirstOrDefault(d => d.Id == id); + if (cacheItem is null) + return default; + var dto = Convert(cacheItem); + return dto; } /// public async Task GetOrDefaultAsync(int id, CancellationToken token) { var cache = await GetCacheAsync(token); - return cache.FirstOrDefault(d => d.Id == id); + var cacheItem = cache.FirstOrDefault(d => d.Id == id); + if (cacheItem is null) + return default; + var dto = Convert(cacheItem); + return dto; } /// - public async Task UpdateAsync(TDto dto, CancellationToken token) - { - var result = await crudServiceBase.UpdateAsync(dto, token); - if (result > 0) - DropCache(); - return result; - } - - /// - public async Task DeleteAsync(int id, CancellationToken token) + public virtual async Task 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(); + + protected virtual TEntity Convert(TDto src) => src.Adapt(); } #nullable disable } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs b/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs index 29362c89..9ac2c8b7 100644 --- a/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs +++ b/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs @@ -27,7 +27,7 @@ namespace AsbCloudInfrastructure.Repository var dtos = cache .Where(e => e.IdWell == idWell) - .ToList(); + .Select(Convert); return dtos; } @@ -41,7 +41,8 @@ namespace AsbCloudInfrastructure.Repository var dtos = cache .Where(e => idsWells.Contains(e.IdWell)) - .ToList(); + .Select(Convert); + return dtos; } } diff --git a/AsbCloudInfrastructure/Services/FileCategoryService.cs b/AsbCloudInfrastructure/Services/FileCategoryService.cs index e7370ee8..b50c9418 100644 --- a/AsbCloudInfrastructure/Services/FileCategoryService.cs +++ b/AsbCloudInfrastructure/Services/FileCategoryService.cs @@ -22,7 +22,8 @@ namespace AsbCloudInfrastructure.Services .ConfigureAwait(false); var dtos = cache .Where(f => f.Id >= 10000) - .Where(f => f.Id <= 20000); + .Where(f => f.Id <= 20000) + .Select(Convert); return dtos; } diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 8967ad78..3d73dbe4 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -81,12 +81,13 @@ namespace AsbCloudInfrastructure.Services .Select(r => r.IdWell); var wellsDtos = (await GetCacheAsync(token)) - .Where(w => wellsIds.Contains(w.Id)); + .Where(w => wellsIds.Contains(w.Id)) + .Select(Convert); - return wellsDtos.ToList(); + return wellsDtos; } - public async Task InsertAsync(WellDto dto, CancellationToken token = default) + public override async Task InsertAsync(WellDto dto, CancellationToken token = default) { if (dto.IdWellType is < 1 or > 2) throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto)); @@ -112,12 +113,12 @@ namespace AsbCloudInfrastructure.Services return result; } - public Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) + public override Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) { throw new NotImplementedException(); } - public async Task UpdateAsync(WellDto dto, + public override async Task UpdateAsync(WellDto dto, CancellationToken token = default) { if (dto.IdWellType is < 1 or > 2) @@ -154,9 +155,8 @@ namespace AsbCloudInfrastructure.Services public async Task GetWellCaptionByIdAsync(int idWell, CancellationToken token) { - var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false); - var dto = Convert(entity); - return dto.Caption; + var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false); + return entity!.Caption; } public async Task> GetCompaniesAsync(int idWell, CancellationToken token)