From 43d7c393bb847adb341ddb9c8f80c92dc79c6f29 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 13 Aug 2026 13:17:43 +0200 Subject: [PATCH] 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. --- .../Repositories/Repository.cs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Repositories/Repository.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Repositories/Repository.cs index 6ef4a35..8619c92 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Repositories/Repository.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Repositories/Repository.cs @@ -13,14 +13,14 @@ namespace DigitalData.MessagingService.Infrastructure.Repositories; /// public class Repository(MessagingServiceDbContext Context, IMapper Mapper) : IRepository where TEntity : class { - private readonly DbSet _dbSet = Context.Set(); + protected readonly DbSet DbSet = Context.Set(); // --- CREATE --- public async Task CreateAsync(TDto dto, CancellationToken cancellationToken = default) { var entity = Mapper.Map(dto); - await _dbSet.AddAsync(entity, cancellationToken); + await DbSet.AddAsync(entity, cancellationToken); await Context.SaveChangesAsync(cancellationToken); return entity; } @@ -28,7 +28,7 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp public async Task> CreateAsync(IEnumerable dtos, CancellationToken cancellationToken = default) { var entities = Mapper.Map>(dtos); - await _dbSet.AddRangeAsync(entities, cancellationToken); + await DbSet.AddRangeAsync(entities, cancellationToken); await Context.SaveChangesAsync(cancellationToken); return entities; } @@ -37,12 +37,12 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { - return await _dbSet.FindAsync([id], cancellationToken); + return await DbSet.FindAsync([id], cancellationToken); } public async Task> GetAllAsync(CancellationToken cancellationToken = default) { - return await _dbSet.ToListAsync(cancellationToken); + return await DbSet.ToListAsync(cancellationToken); } public async Task> FindAsync( @@ -51,7 +51,7 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp int? take = null, CancellationToken cancellationToken = default) { - var query = _dbSet.Where(predicate); + var query = DbSet.Where(predicate); if (skip.HasValue) query = query.Skip(skip.Value); @@ -66,14 +66,14 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp Expression> predicate, CancellationToken cancellationToken = default) { - return await _dbSet.FirstOrDefaultAsync(predicate, cancellationToken); + return await DbSet.FirstOrDefaultAsync(predicate, cancellationToken); } public async Task FindSingleAsync( Expression> predicate, CancellationToken cancellationToken = default) { - return await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken); + return await DbSet.SingleOrDefaultAsync(predicate, cancellationToken); } public async Task CountAsync( @@ -81,15 +81,15 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp CancellationToken cancellationToken = default) { return predicate == null - ? await _dbSet.CountAsync(cancellationToken) - : await _dbSet.CountAsync(predicate, cancellationToken); + ? await DbSet.CountAsync(cancellationToken) + : await DbSet.CountAsync(predicate, cancellationToken); } public async Task AnyAsync( Expression> predicate, CancellationToken cancellationToken = default) { - return await _dbSet.AnyAsync(predicate, cancellationToken); + return await DbSet.AnyAsync(predicate, cancellationToken); } // --- UPSERT --- @@ -105,12 +105,12 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp TDto dto, CancellationToken cancellationToken = default) { - var entity = await _dbSet.FirstOrDefaultAsync(predicate, cancellationToken); + var entity = await DbSet.FirstOrDefaultAsync(predicate, cancellationToken); if (entity is null) { entity = Mapper.Map(dto); - await _dbSet.AddAsync(entity, cancellationToken); + await DbSet.AddAsync(entity, cancellationToken); await Context.SaveChangesAsync(cancellationToken); return (entity, true); } @@ -130,12 +130,12 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp TDto dto, CancellationToken cancellationToken = default) { - var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken); + var entity = await DbSet.SingleOrDefaultAsync(predicate, cancellationToken); if (entity is null) { entity = Mapper.Map(dto); - await _dbSet.AddAsync(entity, cancellationToken); + await DbSet.AddAsync(entity, cancellationToken); await Context.SaveChangesAsync(cancellationToken); return (entity, true); } @@ -157,7 +157,7 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp TDto dto, 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."); Mapper.Map(dto, entity); await Context.SaveChangesAsync(cancellationToken); @@ -173,7 +173,7 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp TDto dto, 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)); await Context.SaveChangesAsync(cancellationToken); return entities.Count; @@ -190,9 +190,9 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp Expression> predicate, 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."); - _dbSet.Remove(entity); + DbSet.Remove(entity); await Context.SaveChangesAsync(cancellationToken); } @@ -205,8 +205,8 @@ public class Repository(MessagingServiceDbContext Context, IMapper Mapp Expression> predicate, CancellationToken cancellationToken = default) { - var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken); - _dbSet.RemoveRange(entities); + var entities = await DbSet.Where(predicate).ToListAsync(cancellationToken); + DbSet.RemoveRange(entities); await Context.SaveChangesAsync(cancellationToken); return entities.Count; }