DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Cache/CacheTable.cs

273 lines
9.5 KiB
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.Cache
{
class CacheTable<TEntity> : ICacheTable<TEntity> where TEntity : class
{
private readonly DbContext context;
private List<TEntity> entities;
internal CacheTable(DbContext context, List<TEntity> entities)
{
this.context = context;
this.entities = entities;
}
public TEntity this[int index] { get => entities.ElementAt(index); }
public int Refresh()
{
entities.Clear();
var dbEntities = context.Set<TEntity>().ToList();
entities.AddRange(dbEntities);
return entities.Count();
}
public async Task<int> RefreshAsync(CancellationToken token = default)
{
entities.Clear();
var dbEntities = await context.Set<TEntity>().ToListAsync(token).ConfigureAwait(false);
entities.AddRange(dbEntities);
return entities.Count();
}
private bool CheckRefresh(RefreshMode refreshMode)
{
if (refreshMode == RefreshMode.Force)
{
Refresh();
return true;
}
if ((!entities.Any()) && (refreshMode == RefreshMode.IfResultEmpty))
{
Refresh();
return true;
}
return false;
}
private async Task<bool> CheckRefreshAsync(RefreshMode refreshMode, CancellationToken token = default)
{
if (refreshMode == RefreshMode.Force)
{
await RefreshAsync(token);
return true;
}
if ((!entities.Any()) && (refreshMode == RefreshMode.IfResultEmpty))
{
await RefreshAsync(token);
return true;
}
return false;
}
public bool Contains(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty)
=> FirstOrDefault(predicate, refreshMode) != default;
public Task<bool> ContainsAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
=> ContainsAsync(predicate, RefreshMode.IfResultEmpty, token);
public async Task<bool> ContainsAsync(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty, CancellationToken token = default)
=> await FirstOrDefaultAsync(predicate, refreshMode, token) != default;
public Task<TEntity> FirstOrDefaultAsync(CancellationToken token = default)
=> FirstOrDefaultAsync(RefreshMode.IfResultEmpty, token);
public TEntity FirstOrDefault(RefreshMode refreshMode = RefreshMode.IfResultEmpty)
{
bool isUpdated = CheckRefresh(refreshMode);
var result = entities.FirstOrDefault();
if (result == default && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
Refresh();
return entities.FirstOrDefault();
}
return result;
}
public async Task<TEntity> FirstOrDefaultAsync(RefreshMode refreshMode = RefreshMode.IfResultEmpty, CancellationToken token = default)
{
bool isUpdated = await CheckRefreshAsync(refreshMode, token);
var result = entities.FirstOrDefault();
if (result == default && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
await RefreshAsync(token);
return entities.FirstOrDefault();
}
return result;
}
public Task<TEntity> FirstOrDefaultAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
=> FirstOrDefaultAsync(predicate, RefreshMode.IfResultEmpty, token);
public TEntity FirstOrDefault(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty)
{
bool isUpdated = CheckRefresh(refreshMode);
var result = entities.FirstOrDefault(predicate);
if (result == default && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
Refresh();
return entities.FirstOrDefault(predicate);
}
return result;
}
public async Task<TEntity> FirstOrDefaultAsync(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty, CancellationToken token = default)
{
bool isUpdated = await CheckRefreshAsync(refreshMode, token);
var result = entities.FirstOrDefault(predicate);
if (result == default && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
await RefreshAsync(token);
return entities.FirstOrDefault(predicate);
}
return result;
}
public Task<IEnumerable<TEntity>> SelectAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
=> SelectAsync(predicate, RefreshMode.IfResultEmpty, token);
public IEnumerable<TEntity> Select(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty)
{
bool isUpdated = CheckRefresh(refreshMode);
var result = entities.Where(predicate);
if (!result.Any() && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
Refresh();
return entities.Where(predicate);
}
return result;
}
public async Task<IEnumerable<TEntity>> SelectAsync(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty, CancellationToken token = default)
{
bool isUpdated = await CheckRefreshAsync(refreshMode, token);
var result = entities.Where(predicate);
if (!result.Any() && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
await RefreshAsync(token);
return entities.Where(predicate);
}
return result;
}
public IEnumerable<TEntity> Update(Func<TEntity, bool> predicate, Action<TEntity> mutation)
{
var dbSet = context.Set<TEntity>();
var dbEntities = dbSet.Where(predicate);
if (dbEntities.Any())
{
foreach (var dbEntity in dbEntities)
mutation(dbEntity);
context.SaveChanges();
}
entities.RemoveAll(e => predicate(e));
entities.AddRange(dbEntities);
return dbEntities;
}
public async Task<IEnumerable<TEntity>> UpdateAsync(Func<TEntity, bool> predicate, Action<TEntity> mutation, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
var dbEntities = dbSet.Where(predicate);
if (dbEntities.Any())
{
foreach (var dbEntity in dbEntities)
mutation(dbEntity);
await context.SaveChangesAsync(token).ConfigureAwait(false);
}
entities.RemoveAll(e => predicate(e));
entities.AddRange(dbEntities);
return dbEntities;
}
public void Remove(Func<TEntity, bool> predicate)
{
var dbSet = context.Set<TEntity>();
entities.RemoveAll(e => predicate(e));
dbSet.RemoveRange(dbSet.Where(predicate));
context.SaveChanges();
return;
}
public async Task RemoveAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
entities.RemoveAll(e => predicate(e));
dbSet.RemoveRange(dbSet.Where(predicate));
await context.SaveChangesAsync(token).ConfigureAwait(false);
return;
}
public TEntity Insert(TEntity entity)
{
var dbSet = context.Set<TEntity>();
var dbEntity = dbSet.Add(entity).Entity;
context.SaveChanges();
entities.Add(dbEntity);
return dbEntity;
}
public async Task<TEntity> InsertAsync(TEntity entity, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
var dbEntity = dbSet.Add(entity).Entity;
await context.SaveChangesAsync(token).ConfigureAwait(false);
entities.Add(dbEntity);
return dbEntity;
}
public IEnumerable<TEntity> Insert(IEnumerable<TEntity> newEntities)
{
var dbSet = context.Set<TEntity>();
var dbEntities = new List<TEntity>(newEntities.Count());
foreach (var item in newEntities)
dbEntities.Add(dbSet.Add(item).Entity);
context.SaveChanges();
entities.AddRange(dbEntities);
return dbEntities;
}
public async Task<IEnumerable<TEntity>> InsertAsync(IEnumerable<TEntity> newEntities, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
var dbEntities = new List<TEntity>(newEntities.Count());
foreach (var item in newEntities)
dbEntities.Add(dbSet.Add(item).Entity);
await context.SaveChangesAsync(token).ConfigureAwait(false);
entities.AddRange(dbEntities);
return dbEntities;
}
}
}