How to do paging using this repository pattern implementation

Jun 22, 2012 at 10:12 PM

how can i implement paging using these patterns.

Coordinator
Jun 25, 2012 at 11:12 PM

Hi guaravkhanna,


you might want to create a new repository that add this functionality.

Maybe something like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using EntityFramework.Patterns;

namespace efPatternsWorkbench
{
    public class PagingRepository<T> : Repository<T> where T : class
    {
        public PagingRepository(IObjectSetFactory objectSetFactory) : base(objectSetFactory){}

        public IEnumerable<T> GetPage<TKey>(int pageNumber, int pageSize, Expression<Func<T, TKey>> orderBy)
        {
            return AsQueryable().OrderBy(orderBy).Skip(pageSize * (pageNumber - 1)).Take(pageSize);
        }
    }
}

and then you can use it like this :

using (EfContext ctx = new EfContext())
            {
                DbContextAdapter adpt = new DbContextAdapter(ctx);
                PagingRepository<User> repo = new PagingRepository<User>(adpt);
                IEnumerable<User> usr = repo.GetPage(2, 1, user => user.Id);
            }

I hope this helps.