Guest User

Fazer ordenação por Reflection com GetProperty

a guest
Jun 24th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. // ExtensionsQueries.cs
  2. namespace HICE.Domain.Core.ExtensionMethods
  3. {
  4.     public static class ExtensionsQueries
  5.     {
  6.         /// <summary>
  7.         /// Generate Paginated List
  8.         /// </summary>
  9.         /// <typeparam name="T">Entidade</typeparam>
  10.         /// <param name="query">Query</param>
  11.         /// <param name="initialPage">Página Inicial (Valor Padrão => [ 0 ])</param>
  12.         /// <param name="recordsPerPage">Quantidade de Registros por Página (Valor Padrão => [ 50 ])</param>
  13.         /// <returns>Retorna uma lista paginada</returns>
  14.         public static IQueryable<T> Pagination<T>(this IQueryable<T> query, int initialPage = 0, int recordsPerPage = 50)
  15.         {
  16.             return query.Skip(initialPage * recordsPerPage).Take(recordsPerPage);
  17.         }
  18. }
  19.  
  20. // Service.cs
  21. namespace HICE.Domain.Services
  22. {
  23.     public abstract class Service<TEntity> : AbstractValidator<TEntity>, IService<TEntity> where TEntity : Entity<TEntity>
  24.     {
  25.         private readonly IDomainNotificationHandler _dnh;
  26.         private readonly IRepository<TEntity> _repository;
  27.  
  28.         public Service(IRepository<TEntity> repository, IDomainNotificationHandler dnh)
  29.         {
  30.             _repository = repository;
  31.             _dnh = dnh;
  32.         }
  33.  
  34.         public IEnumerable<TEntity> GetAll()
  35.         {
  36.             var obj = _repository.GetAll()
  37.                 .OrderByDescending(x => x.GetType().GetProperty(typeof(TEntity).Name + "Id"));
  38.  
  39.             return obj.Pagination();
  40.         }
  41. }
  42.  
  43. // ClienteService.cs
  44. namespace HICE.Domain.Services
  45. {
  46.     public class ClienteService : Service<Cliente>, IClienteService
  47.     {
  48.         private readonly IClienteRepository _clienteRepository;
  49.  
  50.         public ClienteService(IClienteRepository clienteRepository, IDomainNotificationHandler dnh)
  51.             : base(clienteRepository, dnh)
  52.         {
  53.             _clienteRepository = clienteRepository;
  54.         }
  55. }
Add Comment
Please, Sign In to add comment