locked
Entity Framework: How to enable paging support for repository pattern RRS feed

  • Question

  • i found a code which seems work. just tell me how use the below code to return paged customer...may be 20 customer. also tell me how to enable sorting.

    give me a small sample code which fetch customer with paging and sorting. thanks

    public interface IPaged<T> : IEnumerable<T>
        {
            ///<summary>
            /// Get the total entity count.
            ///</summary>
            int Count { get; }
    
            ///<summary>
            /// Get a range of persited entities.
            ///</summary>
            IEnumerable<T> GetRange(int index, int count);
        }
    
    
    public class Paged<T> : IPaged<T>
        {
            private readonly IQueryable<T> source;
    
            public Paged(IQueryable<T> source)
            {
                this.source = source;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return source.GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
    
            public int Count
            {
                get { return source.Count(); }
            }
    
            public IEnumerable<T> GetRange(int index, int count)
            {
                return source.Skip(index).Take(count);
            }
        }
    
    
    public IPaged<Customer> GetCustomers();
    Wednesday, December 13, 2017 1:15 PM

Answers

  • Hi Sudip_inn,

    >>give me a small sample code which fetch customer with paging and sorting. thanks

    If you use Repository pattern, you could add a method in Interface and implement it in Repository class, like this:

    #Interface.

    public interface ICustomerRepository : IDisposable
        {
            IEnumerable<Customer> ListPageCustomers(int page, int pageSize);
           //Other method
        }

    #Repository class

    public class CustomerRepository : ICustomerRepository
        {
            private CustomerContext _context;
    
            public BookRepository(CustomerContext customerContext)
            {
                this._context = customerContext;
            }
    
            
            public IEnumerable<Customer> ListPageCustoms(int page, int pageSize)
            {
                IQueryable<Customer> results = from o in _context.Customers
                orderby o.Id
                select o;
                Paged<Customer> paged = new Paged<Customer>(results);
                return paged.GetRange(page, pageSize);
            }
          
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        _context.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
        }

    #Usage in MVC control

    private CustomerContext _context = new CustomerContext();
            public ActionResult Index()
            {
                CustomerRepository bRes = new CustomerRepository(_context);
                var customers = bRes.ListPageCustoms(1, 20);
                return View();
            }

    #Or you could use it directly, like this:

     using (var db = new CustomerContext())
                {
                    IQueryable<Customer> results = from o in _context.Customers
                                               orderby o.Id  //sorting here
                                               select o;
                    Paged<Customer> paged = new Paged<Customer>(results);
                    var query = paged.GetRange(1, 20);
     }

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, December 21, 2017 11:44 AM
    Thursday, December 14, 2017 3:14 AM
  • Hi Sudip_inn

    >>what is Paged<Customer> ,is there ant built-in class called paged?

    Paged is a class that you provided in your post. Customer is a custom model like this:

    #Customer

    public class Customer
        {
            [Key]
            public int Id { get; set; }
            [Required]
            [MaxLength(30)]
            public string Name { get; set; }
          
            //other property
        }

    >>does the above query fetch all customer data from db and store in memory and then again fetch 20 data from memory? or does the above query fetch 20 data from db?

    Above query fetch 20 data from db, because the method named GetRange in Paged class has the input arguments is an IQueryable type.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, December 15, 2017 1:31 PM
    Friday, December 15, 2017 5:02 AM

All replies

  • Hi Sudip_inn,

    >>give me a small sample code which fetch customer with paging and sorting. thanks

    If you use Repository pattern, you could add a method in Interface and implement it in Repository class, like this:

    #Interface.

    public interface ICustomerRepository : IDisposable
        {
            IEnumerable<Customer> ListPageCustomers(int page, int pageSize);
           //Other method
        }

    #Repository class

    public class CustomerRepository : ICustomerRepository
        {
            private CustomerContext _context;
    
            public BookRepository(CustomerContext customerContext)
            {
                this._context = customerContext;
            }
    
            
            public IEnumerable<Customer> ListPageCustoms(int page, int pageSize)
            {
                IQueryable<Customer> results = from o in _context.Customers
                orderby o.Id
                select o;
                Paged<Customer> paged = new Paged<Customer>(results);
                return paged.GetRange(page, pageSize);
            }
          
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        _context.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
        }

    #Usage in MVC control

    private CustomerContext _context = new CustomerContext();
            public ActionResult Index()
            {
                CustomerRepository bRes = new CustomerRepository(_context);
                var customers = bRes.ListPageCustoms(1, 20);
                return View();
            }

    #Or you could use it directly, like this:

     using (var db = new CustomerContext())
                {
                    IQueryable<Customer> results = from o in _context.Customers
                                               orderby o.Id  //sorting here
                                               select o;
                    Paged<Customer> paged = new Paged<Customer>(results);
                    var query = paged.GetRange(1, 20);
     }

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, December 21, 2017 11:44 AM
    Thursday, December 14, 2017 3:14 AM
  • what is 

    Paged<Customer>

    is there ant built-in class called paged?

    IQueryable<Customer> results = from o in _context.Customers
                                               orderby o.Id  //sorting here
                                               select o;
                    Paged<Customer> paged = new Paged<Customer>(results);
                    var query = paged.GetRange(1, 20);

    does the above query fetch all customer data from db and store in memory and then again fetch 20 data from memory?

    or does the above query fetch 20 data from db?

    Thursday, December 14, 2017 6:12 PM
  • Hi Sudip_inn

    >>what is Paged<Customer> ,is there ant built-in class called paged?

    Paged is a class that you provided in your post. Customer is a custom model like this:

    #Customer

    public class Customer
        {
            [Key]
            public int Id { get; set; }
            [Required]
            [MaxLength(30)]
            public string Name { get; set; }
          
            //other property
        }

    >>does the above query fetch all customer data from db and store in memory and then again fetch 20 data from memory? or does the above query fetch 20 data from db?

    Above query fetch 20 data from db, because the method named GetRange in Paged class has the input arguments is an IQueryable type.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, December 15, 2017 1:31 PM
    Friday, December 15, 2017 5:02 AM