Refactor Repository to use protected DbSet field
Renamed `_dbSet` to `DbSet` and changed its accessibility from `private` to `protected` to allow access in derived classes. Updated all methods in the `Repository` class to use the new `DbSet` field for querying, adding, updating, and removing entities. This includes methods like `CreateAsync`, `GetByIdAsync`, `FindAsync`, `UpsertAsync`, `UpdateAsync`, and `DeleteAsync`. Improved code consistency and readability by removing redundant `_dbSet` references and standardizing on the `DbSet` field.
This commit is contained in:
@@ -13,14 +13,14 @@ namespace DigitalData.MessagingService.Infrastructure.Repositories;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapper) : IRepository<TEntity> where TEntity : class
|
public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapper) : IRepository<TEntity> where TEntity : class
|
||||||
{
|
{
|
||||||
private readonly DbSet<TEntity> _dbSet = Context.Set<TEntity>();
|
protected readonly DbSet<TEntity> DbSet = Context.Set<TEntity>();
|
||||||
|
|
||||||
// --- CREATE ---
|
// --- CREATE ---
|
||||||
|
|
||||||
public async Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancellationToken = default)
|
public async Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = Mapper.Map<TEntity>(dto);
|
var entity = Mapper.Map<TEntity>(dto);
|
||||||
await _dbSet.AddAsync(entity, cancellationToken);
|
await DbSet.AddAsync(entity, cancellationToken);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
public async Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default)
|
public async Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entities = Mapper.Map<IEnumerable<TEntity>>(dtos);
|
var entities = Mapper.Map<IEnumerable<TEntity>>(dtos);
|
||||||
await _dbSet.AddRangeAsync(entities, cancellationToken);
|
await DbSet.AddRangeAsync(entities, cancellationToken);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
@@ -37,12 +37,12 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
|
|
||||||
public async Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
public async Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return await _dbSet.FindAsync([id], cancellationToken);
|
return await DbSet.FindAsync([id], cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default)
|
public async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return await _dbSet.ToListAsync(cancellationToken);
|
return await DbSet.ToListAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<TEntity>> FindAsync(
|
public async Task<IEnumerable<TEntity>> FindAsync(
|
||||||
@@ -51,7 +51,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
int? take = null,
|
int? take = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var query = _dbSet.Where(predicate);
|
var query = DbSet.Where(predicate);
|
||||||
|
|
||||||
if (skip.HasValue)
|
if (skip.HasValue)
|
||||||
query = query.Skip(skip.Value);
|
query = query.Skip(skip.Value);
|
||||||
@@ -66,14 +66,14 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
Expression<Func<TEntity, bool>> predicate,
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return await _dbSet.FirstOrDefaultAsync(predicate, cancellationToken);
|
return await DbSet.FirstOrDefaultAsync(predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TEntity?> FindSingleAsync(
|
public async Task<TEntity?> FindSingleAsync(
|
||||||
Expression<Func<TEntity, bool>> predicate,
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken);
|
return await DbSet.SingleOrDefaultAsync(predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> CountAsync(
|
public async Task<int> CountAsync(
|
||||||
@@ -81,15 +81,15 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return predicate == null
|
return predicate == null
|
||||||
? await _dbSet.CountAsync(cancellationToken)
|
? await DbSet.CountAsync(cancellationToken)
|
||||||
: await _dbSet.CountAsync(predicate, cancellationToken);
|
: await DbSet.CountAsync(predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> AnyAsync(
|
public async Task<bool> AnyAsync(
|
||||||
Expression<Func<TEntity, bool>> predicate,
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return await _dbSet.AnyAsync(predicate, cancellationToken);
|
return await DbSet.AnyAsync(predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- UPSERT ---
|
// --- UPSERT ---
|
||||||
@@ -105,12 +105,12 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
TDto dto,
|
TDto dto,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = await _dbSet.FirstOrDefaultAsync(predicate, cancellationToken);
|
var entity = await DbSet.FirstOrDefaultAsync(predicate, cancellationToken);
|
||||||
|
|
||||||
if (entity is null)
|
if (entity is null)
|
||||||
{
|
{
|
||||||
entity = Mapper.Map<TEntity>(dto);
|
entity = Mapper.Map<TEntity>(dto);
|
||||||
await _dbSet.AddAsync(entity, cancellationToken);
|
await DbSet.AddAsync(entity, cancellationToken);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return (entity, true);
|
return (entity, true);
|
||||||
}
|
}
|
||||||
@@ -130,12 +130,12 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
TDto dto,
|
TDto dto,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken);
|
var entity = await DbSet.SingleOrDefaultAsync(predicate, cancellationToken);
|
||||||
|
|
||||||
if (entity is null)
|
if (entity is null)
|
||||||
{
|
{
|
||||||
entity = Mapper.Map<TEntity>(dto);
|
entity = Mapper.Map<TEntity>(dto);
|
||||||
await _dbSet.AddAsync(entity, cancellationToken);
|
await DbSet.AddAsync(entity, cancellationToken);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return (entity, true);
|
return (entity, true);
|
||||||
}
|
}
|
||||||
@@ -157,7 +157,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
TDto dto,
|
TDto dto,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
var entity = await DbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
||||||
?? throw new NotFoundException($"No {typeof(TEntity).Name} found matching the predicate.");
|
?? throw new NotFoundException($"No {typeof(TEntity).Name} found matching the predicate.");
|
||||||
Mapper.Map(dto, entity);
|
Mapper.Map(dto, entity);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
@@ -173,7 +173,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
TDto dto,
|
TDto dto,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
var entities = await DbSet.Where(predicate).ToListAsync(cancellationToken);
|
||||||
entities.ForEach(entity => Mapper.Map(dto, entity));
|
entities.ForEach(entity => Mapper.Map(dto, entity));
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return entities.Count;
|
return entities.Count;
|
||||||
@@ -190,9 +190,9 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
Expression<Func<TEntity, bool>> predicate,
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
var entity = await DbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
||||||
?? throw new NotFoundException($"No {typeof(TEntity).Name} found matching the predicate.");
|
?? throw new NotFoundException($"No {typeof(TEntity).Name} found matching the predicate.");
|
||||||
_dbSet.Remove(entity);
|
DbSet.Remove(entity);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,8 +205,8 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
Expression<Func<TEntity, bool>> predicate,
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
var entities = await DbSet.Where(predicate).ToListAsync(cancellationToken);
|
||||||
_dbSet.RemoveRange(entities);
|
DbSet.RemoveRange(entities);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return entities.Count;
|
return entities.Count;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user