refactor: rename context variable to dbContext for consistency and clarity
This commit is contained in:
@@ -12,19 +12,19 @@ namespace Shop.Infrastructure.Data.Repositories.Common;
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The type of the entity.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the entity's key.</typeparam>
|
||||
internal abstract class BaseWriteOnlyRepository<TEntity, TKey>(WriteDbContext context) : IWriteOnlyRepository<TEntity, TKey>
|
||||
internal abstract class BaseWriteOnlyRepository<TEntity, TKey>(WriteDbContext dbContext) : IWriteOnlyRepository<TEntity, TKey>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
private static readonly Func<WriteDbContext, TKey, Task<TEntity>> GetByIdCompiledAsync =
|
||||
EF.CompileAsyncQuery((WriteDbContext context, TKey id) =>
|
||||
context
|
||||
EF.CompileAsyncQuery((WriteDbContext dbContext, TKey id) =>
|
||||
dbContext
|
||||
.Set<TEntity>()
|
||||
.AsNoTrackingWithIdentityResolution()
|
||||
.FirstOrDefault(entity => entity.Id.Equals(id)));
|
||||
|
||||
private readonly DbSet<TEntity> _dbSet = context.Set<TEntity>();
|
||||
protected readonly WriteDbContext Context = context;
|
||||
private readonly DbSet<TEntity> _dbSet = dbContext.Set<TEntity>();
|
||||
protected readonly WriteDbContext DbContext = dbContext;
|
||||
|
||||
public void Add(TEntity entity) =>
|
||||
_dbSet.Add(entity);
|
||||
@@ -36,7 +36,7 @@ internal abstract class BaseWriteOnlyRepository<TEntity, TKey>(WriteDbContext co
|
||||
_dbSet.Remove(entity);
|
||||
|
||||
public async Task<TEntity> GetByIdAsync(TKey id) =>
|
||||
await GetByIdCompiledAsync(Context, id);
|
||||
await GetByIdCompiledAsync(DbContext, id);
|
||||
|
||||
#region IDisposable
|
||||
|
||||
@@ -60,7 +60,7 @@ internal abstract class BaseWriteOnlyRepository<TEntity, TKey>(WriteDbContext co
|
||||
|
||||
// Dispose managed state (managed objects).
|
||||
if (disposing)
|
||||
Context.Dispose();
|
||||
DbContext.Dispose();
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
@@ -9,26 +9,26 @@ using Shop.Infrastructure.Data.Repositories.Common;
|
||||
|
||||
namespace Shop.Infrastructure.Data.Repositories;
|
||||
|
||||
internal class CustomerWriteOnlyRepository(WriteDbContext context)
|
||||
: BaseWriteOnlyRepository<Customer, Guid>(context), ICustomerWriteOnlyRepository
|
||||
internal class CustomerWriteOnlyRepository(WriteDbContext dbContext)
|
||||
: BaseWriteOnlyRepository<Customer, Guid>(dbContext), ICustomerWriteOnlyRepository
|
||||
{
|
||||
private static readonly Func<WriteDbContext, string, Task<bool>> ExistsByEmailCompiledAsync =
|
||||
EF.CompileAsyncQuery((WriteDbContext context, string email) =>
|
||||
context
|
||||
EF.CompileAsyncQuery((WriteDbContext dbContext, string email) =>
|
||||
dbContext
|
||||
.Customers
|
||||
.AsNoTracking()
|
||||
.Any(customer => customer.Email.Address == email));
|
||||
|
||||
private static readonly Func<WriteDbContext, string, Guid, Task<bool>> ExistsByEmailAndIdCompiledAsync =
|
||||
EF.CompileAsyncQuery((WriteDbContext context, string email, Guid currentId) =>
|
||||
context
|
||||
EF.CompileAsyncQuery((WriteDbContext dbContext, string email, Guid currentId) =>
|
||||
dbContext
|
||||
.Customers
|
||||
.AsNoTracking()
|
||||
.Any(customer => customer.Email.Address == email && customer.Id != currentId));
|
||||
|
||||
public Task<bool> ExistsByEmailAsync(Email email) =>
|
||||
ExistsByEmailCompiledAsync(Context, email.Address);
|
||||
ExistsByEmailCompiledAsync(DbContext, email.Address);
|
||||
|
||||
public Task<bool> ExistsByEmailAsync(Email email, Guid currentId) =>
|
||||
ExistsByEmailAndIdCompiledAsync(Context, email.Address, currentId);
|
||||
ExistsByEmailAndIdCompiledAsync(DbContext, email.Address, currentId);
|
||||
}
|
||||
@@ -6,12 +6,12 @@ using Shop.Infrastructure.Data.Context;
|
||||
|
||||
namespace Shop.Infrastructure.Data.Repositories;
|
||||
|
||||
internal sealed class EventStoreRepository(EventStoreDbContext context) : IEventStoreRepository
|
||||
internal sealed class EventStoreRepository(EventStoreDbContext dbContext) : IEventStoreRepository
|
||||
{
|
||||
public async Task StoreAsync(IEnumerable<EventStore> eventStores)
|
||||
{
|
||||
await context.EventStores.AddRangeAsync(eventStores);
|
||||
await context.SaveChangesAsync();
|
||||
await dbContext.EventStores.AddRangeAsync(eventStores);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
@@ -37,7 +37,7 @@ internal sealed class EventStoreRepository(EventStoreDbContext context) : IEvent
|
||||
|
||||
// Dispose managed state (managed objects).
|
||||
if (disposing)
|
||||
context.Dispose();
|
||||
dbContext.Dispose();
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user