Files
Core.BluePrint.Packages/Core.Blueprint.SQLServer/Contracts/IEntityRepository.cs
Sergio Matias Urquin 83fc1878c4 Add project files.
2025-04-29 18:42:29 -06:00

109 lines
5.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
namespace Core.Blueprint.DAL.SQLServer
{
/// <summary>
/// Defines the contract for a generic repository to manage entities in a SQL Server database.
/// </summary>
/// <typeparam name="TEntity">The type of the entity managed by the repository. Must be a class.</typeparam>
/// <typeparam name="TContext">The type of the database context used by the repository. Must inherit from <see cref="DbContext"/>.</typeparam>
public interface IEntityRepository<TEntity, TContext>
where TEntity : class
where TContext : DbContext
{
/// <summary>
/// Retrieves all entities of type <typeparamref name="T"/> from the database.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a collection of entities as the result.</returns>
Task<IEnumerable<TEntity>> GetAllAsync();
/// <summary>
/// Retrieves all entities of type <typeparamref name="T"/> from the database that match a specified condition.
/// </summary>
/// <param name="predicate">An expression to filter the entities.</param>
/// <returns>A task representing the asynchronous operation, with a collection of matching entities as the result.</returns>
Task<IEnumerable<TEntity>> GetByConditionAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Retrieves a single entity of type <typeparamref name="T"/> by its identifier.
/// </summary>
/// <param name="id">The identifier of the entity to retrieve.</param>
/// <returns>A task representing the asynchronous operation, with the entity as the result, or null if not found.</returns>
Task<TEntity?> GetByIdAsync(int id);
/// <summary>
/// Retrieves the first entity of type <typeparamref name="T"/> that matches a specified condition, or null if no match is found.
/// </summary>
/// <param name="predicate">An expression to filter the entities.</param>
/// <returns>A task representing the asynchronous operation, with the matching entity as the result, or null if none match.</returns>
Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Adds a new entity of type <typeparamref name="T"/> to the database.
/// </summary>
/// <param name="entity">The entity to add.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task AddAsync(TEntity entity);
/// <summary>
/// Adds multiple entities of type <typeparamref name="T"/> to the database.
/// </summary>
/// <param name="entities">The collection of entities to add.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task AddRangeAsync(IEnumerable<TEntity> entities);
/// <summary>
/// Updates an existing entity of type <typeparamref name="T"/> in the database.
/// </summary>
/// <param name="entity">The entity to update.</param>
/// <returns>The updated entity.</returns>
TEntity Update(TEntity entity);
/// <summary>
/// Updates multiple entities of type <typeparamref name="T"/> in the database.
/// </summary>
/// <param name="entities">The collection of entities to update.</param>
void UpdateRange(IEnumerable<TEntity> entities);
/// <summary>
/// Deletes an entity of type <typeparamref name="T"/> from the database.
/// </summary>
/// <param name="entity">The entity to delete.</param>
void Delete(TEntity entity);
/// <summary>
/// Deletes multiple entities of type <typeparamref name="T"/> from the database.
/// </summary>
/// <param name="entities">The collection of entities to delete.</param>
void DeleteRange(IEnumerable<TEntity> entities);
/// <summary>
/// Determines whether any entities of type <typeparamref name="T"/> exist in the database that match a specified condition.
/// </summary>
/// <param name="predicate">An expression to filter the entities.</param>
/// <returns>A task representing the asynchronous operation, with a boolean result indicating whether any match exists.</returns>
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Executes a raw SQL query and maps the result to entities of type <typeparamref name="T"/>.
/// </summary>
/// <param name="sql">The raw SQL query to execute.</param>
/// <param name="parameters">Optional parameters for the SQL query.</param>
/// <returns>A task representing the asynchronous operation, with a collection of entities as the result.</returns>
Task<IEnumerable<TEntity>> ExecuteRawSqlAsync(string sql, params object[] parameters);
/// <summary>
/// Counts the total number of entities of type <typeparamref name="T"/> in the database.
/// </summary>
/// <returns>A task representing the asynchronous operation, with the count as the result.</returns>
Task<int> CountAsync();
/// <summary>
/// Saves all pending changes to the database.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
Task SaveAsync();
}
}