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.
95 lines
4.6 KiB
C#
95 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq.Expressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ECMJobRunner.Domain.Interfaces
|
|
{
|
|
/// <summary>
|
|
/// Generic repository interface for data access operations
|
|
/// </summary>
|
|
/// <typeparam name="TEntity">Entity type</typeparam>
|
|
public interface IRepository<TEntity> where TEntity : class
|
|
{
|
|
/// <summary>
|
|
/// Get entity by ID asynchronously
|
|
/// </summary>
|
|
Task<TEntity?> GetByIdAsync(long id, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Get all entities asynchronously
|
|
/// </summary>
|
|
Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Find entities by predicate asynchronously
|
|
/// </summary>
|
|
Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Get single entity by predicate asynchronously
|
|
/// </summary>
|
|
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Add new entity from DTO asynchronously
|
|
/// Maps DTO to entity and adds it
|
|
/// </summary>
|
|
/// <typeparam name="TDto">DTO type</typeparam>
|
|
/// <param name="dto">DTO containing values for new entity</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Created entity</returns>
|
|
Task<TEntity> AddAsync<TDto>(TDto dto, CancellationToken cancellationToken = default) where TDto : class;
|
|
|
|
/// <summary>
|
|
/// Add multiple entities from DTOs asynchronously
|
|
/// Maps DTOs to entities and adds them
|
|
/// </summary>
|
|
/// <typeparam name="TDto">DTO type</typeparam>
|
|
/// <param name="dtos">DTOs containing values for new entities</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Number of entities added</returns>
|
|
Task<int> AddRangeAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default) where TDto : class;
|
|
|
|
/// <summary>
|
|
/// Update entities matching predicate with DTO values asynchronously
|
|
/// Maps DTO properties onto matching entities
|
|
/// </summary>
|
|
/// <typeparam name="TDto">DTO type</typeparam>
|
|
/// <param name="predicate">Predicate to find entities</param>
|
|
/// <param name="dto">DTO containing values to update</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Number of entities updated</returns>
|
|
Task<int> UpdateAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default) where TDto : class;
|
|
|
|
/// <summary>
|
|
/// Update single entity matching predicate with DTO values asynchronously
|
|
/// Throws exception if multiple entities match
|
|
/// </summary>
|
|
/// <typeparam name="TDto">DTO type</typeparam>
|
|
/// <param name="predicate">Predicate to find entity</param>
|
|
/// <param name="dto">DTO containing values to update</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>True if entity was found and updated, false otherwise</returns>
|
|
Task<bool> UpdateSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default) where TDto : class;
|
|
|
|
/// <summary>
|
|
/// Delete entities matching predicate asynchronously (hard delete)
|
|
/// </summary>
|
|
/// <param name="predicate">Predicate to find entities to delete</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Number of entities deleted</returns>
|
|
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Delete single entity matching predicate asynchronously (hard delete)
|
|
/// Throws exception if multiple entities match
|
|
/// </summary>
|
|
/// <param name="predicate">Predicate to find entity to delete</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>True if entity was found and deleted, false otherwise</returns>
|
|
Task<bool> DeleteSingleAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
|
|
}
|
|
}
|