#190 : Application d'une méthode générique pour la pagination.
This commit is contained in:
@@ -217,8 +217,7 @@ namespace Webzine.Repository
|
||||
.AsNoTracking()
|
||||
.OrderBy(a => a.Nom)
|
||||
.Include(t => t.Titres)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
.Paginate(offset, limit);
|
||||
this.logger.LogDebug("Page {PageNumber} d'artistes récupérée avec {PageSize} artistes par page.", offset, limit);
|
||||
return artistes;
|
||||
}
|
||||
|
||||
@@ -103,8 +103,7 @@ public class DbCommentaireRepository : ICommentaireRepository
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Titre)
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
.Paginate(offset, limit);
|
||||
|
||||
return commentaires;
|
||||
}
|
||||
|
||||
@@ -191,8 +191,7 @@ public class DbStyleRepository : IStyleRepository
|
||||
var styles = this.context.Styles
|
||||
.AsNoTracking()
|
||||
.OrderBy(s => s.Libelle)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
.Paginate(offset, limit);
|
||||
|
||||
this.logger.LogDebug("La liste paginée de styles a été récupérée.");
|
||||
return styles;
|
||||
|
||||
@@ -107,8 +107,7 @@ public class DbTitreRepository : ITitreRepository
|
||||
.ThenBy(t => t.Libelle)
|
||||
.Include(t => t.Artiste)
|
||||
.Include(t => t.Styles)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.Paginate(offset, limit)
|
||||
.AsNoTracking();
|
||||
|
||||
return titres;
|
||||
|
||||
@@ -112,8 +112,7 @@ namespace Webzine.Repository
|
||||
{
|
||||
return this.dataStore.Artistes
|
||||
.OrderBy(a => a.Nom)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.Paginate(offset, limit)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,7 @@ namespace Webzine.Repository
|
||||
{
|
||||
return this.dataStore.Commentaires
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
.Paginate(offset, limit);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -77,8 +77,7 @@ public class LocalStyleRepository : IStyleRepository
|
||||
{
|
||||
return this.dataStore.Styles
|
||||
.OrderBy(s => s.Libelle)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.Paginate(offset, limit)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -50,8 +50,7 @@ public class LocalTitreRepository : ITitreRepository
|
||||
return this.dataStore.Titres
|
||||
.OrderByDescending(t => t.DateCreation)
|
||||
.ThenBy(t => t.Libelle)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
.Paginate(offset, limit);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
56
Webzine.Repository/RepositoryPaginationExtensions.cs
Normal file
56
Webzine.Repository/RepositoryPaginationExtensions.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Fournit des méthodes génériques pour paginer des collections dans la couche Repository.
|
||||
/// </summary>
|
||||
public static class RepositoryPaginationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Retourne une portion paginée d'une requête.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type des éléments paginés.</typeparam>
|
||||
/// <param name="source">Source à paginer.</param>
|
||||
/// <param name="offset">Nombre d'éléments à ignorer.</param>
|
||||
/// <param name="limit">Nombre maximal d'éléments à retourner.</param>
|
||||
/// <returns>La requête paginée.</returns>
|
||||
public static IQueryable<T> Paginate<T>(this IQueryable<T> source, int offset, int limit)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(source);
|
||||
ValidatePaginationArguments(offset, limit);
|
||||
|
||||
return source
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retourne une portion paginée d'une collection en mémoire.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type des éléments paginés.</typeparam>
|
||||
/// <param name="source">Source à paginer.</param>
|
||||
/// <param name="offset">Nombre d'éléments à ignorer.</param>
|
||||
/// <param name="limit">Nombre maximal d'éléments à retourner.</param>
|
||||
/// <returns>La collection paginée.</returns>
|
||||
public static IEnumerable<T> Paginate<T>(this IEnumerable<T> 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user