namespace Webzine.Repository;
///
/// Fournit des méthodes génériques pour paginer des collections dans la couche Repository.
///
public static class RepositoryPaginationExtensions
{
///
/// Retourne une portion paginée d'une requête.
///
/// Type des éléments paginés.
/// Source à paginer.
/// Nombre d'éléments à ignorer.
/// Nombre maximal d'éléments à retourner.
/// La requête paginée.
public static IQueryable Paginate(this IQueryable source, int offset, int limit)
{
ArgumentNullException.ThrowIfNull(source);
ValidatePaginationArguments(offset, limit);
return source
.Skip(offset)
.Take(limit);
}
///
/// Retourne une portion paginée d'une collection en mémoire.
///
/// Type des éléments paginés.
/// Source à paginer.
/// Nombre d'éléments à ignorer.
/// Nombre maximal d'éléments à retourner.
/// La collection paginée.
public static IEnumerable Paginate(this IEnumerable source, int offset, int limit)
{
ArgumentNullException.ThrowIfNull(source);
ValidatePaginationArguments(offset, limit);
return source
.Skip(offset)
.Take(limit);
}
private static void ValidatePaginationArguments(int offset, int limit)
{
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset), "L'offset doit être supérieur ou égal à 0.");
}
if (limit <= 0)
{
throw new ArgumentOutOfRangeException(nameof(limit), "La limite doit être strictement supérieure à 0.");
}
}
}