Add bulk create and upsert methods to repository

Added `CreateAsync` for bulk entity creation and `UpsertAsync`
and `UpsertSingleAsync` for upsert operations in `IRepository`
and `Repository`. Improved error handling in `UpdateSingleAsync`
and `DeleteSingleAsync` by throwing `NotFoundException` when no
matching entity is found. Refactored `FindAsync` and `UpdateAsync`
for better readability and performance. Cleaned up formatting in
`DeleteAsync`.
This commit is contained in:
2026-08-13 09:36:48 +02:00
parent 5c37b2ef92
commit 00ae8e1ba0
2 changed files with 74 additions and 12 deletions

View File

@@ -11,6 +11,8 @@ public interface IRepository<TEntity> where TEntity : class
// CREATE
Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancellationToken = default);
Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default);
// READ
Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);
@@ -20,6 +22,11 @@ public interface IRepository<TEntity> where TEntity : class
Task<int> CountAsync(Expression<Func<TEntity, bool>>? predicate = null, CancellationToken cancellationToken = default);
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
// UPSERT
Task<(TEntity Entity, bool Created)> UpsertAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
Task<(TEntity Entity, bool Created)> UpsertSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
// UPDATE
Task UpdateSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
Task<int> UpdateAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);

View File

@@ -25,6 +25,14 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
return entity;
}
public async Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default)
{
var entities = Mapper.Map<IEnumerable<TEntity>>(dtos);
await _dbSet.AddRangeAsync(entities, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
return entities;
}
// --- READ ---
public async Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
@@ -84,6 +92,59 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
return await _dbSet.AnyAsync(predicate, cancellationToken);
}
// --- UPSERT ---
/// <summary>
/// Upsert: if no record matches the predicate, creates a new entity;
/// if one or more match, updates the FIRST match.
/// Returns the entity and a flag indicating whether it was created (true) or updated (false).
/// Auto-saves changes.
/// </summary>
public async Task<(TEntity Entity, bool Created)> UpsertAsync<TDto>(
Expression<Func<TEntity, bool>> predicate,
TDto dto,
CancellationToken cancellationToken = default)
{
var entity = await _dbSet.FirstOrDefaultAsync(predicate, cancellationToken);
if (entity is null)
{
entity = Mapper.Map<TEntity>(dto);
await _dbSet.AddAsync(entity, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
return (entity, true);
}
Mapper.Map(dto, entity);
await Context.SaveChangesAsync(cancellationToken);
return (entity, false);
}
/// <summary>
/// Upsert (single-safe): if no record matches the predicate, creates a new entity;
/// if exactly one matches, updates it. Throws InvalidOperationException if 2+ match.
/// Auto-saves changes.
/// </summary>
public async Task<(TEntity Entity, bool Created)> UpsertSingleAsync<TDto>(
Expression<Func<TEntity, bool>> predicate,
TDto dto,
CancellationToken cancellationToken = default)
{
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken);
if (entity is null)
{
entity = Mapper.Map<TEntity>(dto);
await _dbSet.AddAsync(entity, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
return (entity, true);
}
Mapper.Map(dto, entity);
await Context.SaveChangesAsync(cancellationToken);
return (entity, false);
}
// --- UPDATE ---
/// <summary>
@@ -113,12 +174,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
CancellationToken cancellationToken = default)
{
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
foreach (var entity in entities)
{
Mapper.Map(dto, entity);
}
entities.ForEach(entity => Mapper.Map(dto, entity));
await Context.SaveChangesAsync(cancellationToken);
return entities.Count;
}
@@ -150,7 +206,6 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
CancellationToken cancellationToken = default)
{
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
_dbSet.RemoveRange(entities);
await Context.SaveChangesAsync(cancellationToken);
return entities.Count;