Files
ECMJobRunner/ECMJobRunner.Infrastructure/Repositories/Repository.cs
TekH 45a7086c9a Refactor repository pattern and modernize codebase
Removed `SaveChangesAsync` from `IRepository` and `Repository` to centralize transaction management in `DbContext`. Updated `CfgProfileRepository` to use `Context` property instead of `_context`. Refactored `Repository` class to use C# 9.0 record-like constructor syntax and replaced private fields with properties (`Context`, `DbSet`, `Mapper`).

Replaced `Any()` with `Count == 0` for null checks and updated `FindAsync` to use C# 11 object array syntax. Added conditional compilation for framework-specific differences. These changes improve readability, consistency, and leverage modern C# features.
2026-08-03 12:07:09 +02:00

178 lines
5.7 KiB
C#

#if NET48
using System.Data.Entity;
#else
using Microsoft.EntityFrameworkCore;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using ECMJobRunner.Domain.Interfaces;
using ECMJobRunner.Infrastructure.Data;
namespace ECMJobRunner.Infrastructure.Repositories
{
/// <summary>
/// Generic repository implementation for Entity Framework
/// Uses AutoMapper for DTO mapping
/// </summary>
/// <typeparam name="TEntity">Entity type</typeparam>
/// <remarks>
/// Constructor
/// </remarks>
public class Repository<TEntity>(JobRunnerDbContext context, IMapper mapper) : IRepository<TEntity> where TEntity : class
{
/// <summary>
/// Database context
/// </summary>
protected readonly JobRunnerDbContext Context = context ?? throw new ArgumentNullException(nameof(context));
/// <summary>
/// DbSet for the entity
/// </summary>
protected readonly DbSet<TEntity> DbSet = context.Set<TEntity>();
/// <summary>
/// AutoMapper instance for DTO mapping
/// </summary>
protected readonly IMapper Mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
/// <inheritdoc/>
public virtual async Task<TEntity?> GetByIdAsync(long id, CancellationToken cancellationToken = default)
{
#if NET48
return await DbSet.FindAsync(cancellationToken, id);
#else
return await DbSet.FindAsync([id], cancellationToken);
#endif
}
/// <inheritdoc/>
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await DbSet.ToListAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
return await DbSet.Where(predicate).ToListAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
return await DbSet.SingleOrDefaultAsync(predicate, cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<TEntity> AddAsync<TDto>(TDto dto, CancellationToken cancellationToken = default) where TDto : class
{
#if NETFRAMEWORK
if (dto == null) throw new ArgumentNullException(nameof(dto));
#endif
var entity = Mapper.Map<TEntity>(dto);
#if NET48
DbSet.Add(entity);
#else
await DbSet.AddAsync(entity, cancellationToken);
#endif
await Context.SaveChangesAsync(cancellationToken);
return entity;
}
/// <inheritdoc/>
public virtual async Task<int> AddRangeAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default) where TDto : class
{
#if NETFRAMEWORK
if (dtos == null) throw new ArgumentNullException(nameof(dtos));
#endif
var dtoList = dtos.ToList();
if (dtoList.Count == 0)
return 0;
var entities = Mapper.Map<List<TEntity>>(dtoList);
#if NET48
DbSet.AddRange(entities);
#else
await DbSet.AddRangeAsync(entities, cancellationToken);
#endif
return await Context.SaveChangesAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<int> UpdateAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default) where TDto : class
{
#if NETFRAMEWORK
if (dto == null) throw new ArgumentNullException(nameof(dto));
#endif
var entities = await DbSet.Where(predicate).ToListAsync(cancellationToken);
if (entities.Count == 0)
return 0;
foreach (var entity in entities)
{
Mapper.Map(dto, entity);
}
return await Context.SaveChangesAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<bool> UpdateSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default) where TDto : class
{
#if NETFRAMEWORK
if (dto == null) throw new ArgumentNullException(nameof(dto));
#endif
var entity = await SingleOrDefaultAsync(predicate, cancellationToken);
if (entity == null)
return false;
Mapper.Map(dto, entity);
await Context.SaveChangesAsync(cancellationToken);
return true;
}
/// <inheritdoc/>
public virtual async Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
var entities = await DbSet.Where(predicate).ToListAsync(cancellationToken);
if (entities.Count == 0)
return 0;
DbSet.RemoveRange(entities);
return await Context.SaveChangesAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<bool> DeleteSingleAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
var entity = await SingleOrDefaultAsync(predicate, cancellationToken);
if (entity == null)
return false;
DbSet.Remove(entity);
await Context.SaveChangesAsync(cancellationToken);
return true;
}
}
}