From 814f809c3e0d4ea08822e0b5992b4100442213fd Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Tue, 10 Aug 2021 16:49:14 +0500 Subject: [PATCH] dbSet initialization in CacheTable is moved to constructor --- .../Services/Cache/CacheTable.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs index f317753b..f93d89d5 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs @@ -12,12 +12,14 @@ namespace AsbCloudInfrastructure.Services.Cache private readonly DbContext context; private (DateTime refreshDate, IEnumerable entities) data; private readonly List cached; + private readonly DbSet dbSet; internal CacheTable(DbContext context, (DateTime refreshDate, IEnumerable entities) data) { this.context = context; this.data = data; this.cached = (List)data.entities; + dbSet = context.Set(); } public TEntity this[int index] { get => cached.ElementAt(index); } @@ -168,7 +170,6 @@ namespace AsbCloudInfrastructure.Services.Cache public IEnumerable Mutate(Func predicate, Action mutation) { - var dbSet = context.Set(); var dbEntities = dbSet.Where(predicate); if (dbEntities.Any()) { @@ -183,7 +184,6 @@ namespace AsbCloudInfrastructure.Services.Cache public async Task> MutateAsync(Func predicate, Action mutation, CancellationToken token = default) { - var dbSet = context.Set(); 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(); var updated = dbSet.Update(entity); context.SaveChanges(); Refresh(); @@ -207,7 +206,6 @@ namespace AsbCloudInfrastructure.Services.Cache public async Task UpsertAsync(TEntity entity, CancellationToken token = default) { - var dbSet = context.Set(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry updated; if (dbSet.Contains(entity)) updated = dbSet.Update(entity); @@ -220,7 +218,6 @@ namespace AsbCloudInfrastructure.Services.Cache public IEnumerable Upsert(IEnumerable entities) { - var dbSet = context.Set(); var upsertedEntries = new List(entities.Count()); foreach (var entity in entities) { @@ -238,7 +235,6 @@ namespace AsbCloudInfrastructure.Services.Cache public async Task> UpsertAsync(IEnumerable entities, CancellationToken token = default) { - var dbSet = context.Set(); var upsertedEntries = new List(entities.Count()); foreach (var entity in entities) { @@ -256,7 +252,6 @@ namespace AsbCloudInfrastructure.Services.Cache public void Remove(Func predicate) { - var dbSet = context.Set(); 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 predicate, CancellationToken token = default) { - var dbSet = context.Set(); 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(); var dbEntity = dbSet.Add(entity).Entity; context.SaveChanges(); cached.Add(dbEntity); @@ -283,7 +276,6 @@ namespace AsbCloudInfrastructure.Services.Cache public async Task InsertAsync(TEntity entity, CancellationToken token = default) { - var dbSet = context.Set(); 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 Insert(IEnumerable newEntities) { - var dbSet = context.Set(); var dbEntities = new List(newEntities.Count()); foreach (var item in newEntities) dbEntities.Add(dbSet.Add(item).Entity); @@ -303,7 +294,6 @@ namespace AsbCloudInfrastructure.Services.Cache public async Task> InsertAsync(IEnumerable newEntities, CancellationToken token = default) { - var dbSet = context.Set(); var dbEntities = new List(newEntities.Count()); foreach (var item in newEntities) dbEntities.Add(dbSet.Add(item).Entity);