dbSet initialization in CacheTable is moved to constructor

This commit is contained in:
KharchenkoVV 2021-08-10 16:49:14 +05:00
parent 50fc46e007
commit 814f809c3e

View File

@ -12,12 +12,14 @@ namespace AsbCloudInfrastructure.Services.Cache
private readonly DbContext context;
private (DateTime refreshDate, IEnumerable<object> entities) data;
private readonly List<TEntity> cached;
private readonly DbSet<TEntity> dbSet;
internal CacheTable(DbContext context, (DateTime refreshDate, IEnumerable<object> entities) data)
{
this.context = context;
this.data = data;
this.cached = (List<TEntity>)data.entities;
dbSet = context.Set<TEntity>();
}
public TEntity this[int index] { get => cached.ElementAt(index); }
@ -168,7 +170,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public IEnumerable<TEntity> Mutate(Func<TEntity, bool> predicate, Action<TEntity> mutation)
{
var dbSet = context.Set<TEntity>();
var dbEntities = dbSet.Where(predicate);
if (dbEntities.Any())
{
@ -183,7 +184,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public async Task<IEnumerable<TEntity>> MutateAsync(Func<TEntity, bool> predicate, Action<TEntity> mutation, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
var dbEntities = dbSet.Where(predicate);
if (dbEntities.Any())
{
@ -198,7 +198,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public TEntity Upsert(TEntity entity)
{
var dbSet = context.Set<TEntity>();
var updated = dbSet.Update(entity);
context.SaveChanges();
Refresh();
@ -207,7 +206,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public async Task<TEntity> UpsertAsync(TEntity entity, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> updated;
if (dbSet.Contains(entity))
updated = dbSet.Update(entity);
@ -220,7 +218,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public IEnumerable<TEntity> Upsert(IEnumerable<TEntity> entities)
{
var dbSet = context.Set<TEntity>();
var upsertedEntries = new List<TEntity>(entities.Count());
foreach (var entity in entities)
{
@ -238,7 +235,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public async Task<IEnumerable<TEntity>> UpsertAsync(IEnumerable<TEntity> entities, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
var upsertedEntries = new List<TEntity>(entities.Count());
foreach (var entity in entities)
{
@ -256,7 +252,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public void Remove(Func<TEntity, bool> predicate)
{
var dbSet = context.Set<TEntity>();
cached.RemoveAll(e => predicate(e));
dbSet.RemoveRange(dbSet.Where(predicate));
context.SaveChanges();
@ -265,7 +260,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public async Task RemoveAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
{
var dbSet = context.Set<TEntity>();
cached.RemoveAll(e => predicate(e));
dbSet.RemoveRange(dbSet.Where(predicate));
await context.SaveChangesAsync(token).ConfigureAwait(false);
@ -274,7 +268,6 @@ namespace AsbCloudInfrastructure.Services.Cache
public TEntity Insert(TEntity entity)
{
var dbSet = context.Set<TEntity>();
var dbEntity = dbSet.Add(entity).Entity;
context.SaveChanges();
cached.Add(dbEntity);
@ -283,7 +276,6 @@ namespace AsbCloudInfrastructure.Services.Cache
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);
cached.Add(dbEntity);
@ -292,7 +284,6 @@ namespace AsbCloudInfrastructure.Services.Cache
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);
@ -303,7 +294,6 @@ namespace AsbCloudInfrastructure.Services.Cache
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);