Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET

Architecture Guide: ASP.NET MVC Framework + N-tier + Entity Framework and Many More

Rate me:
Please Sign up or sign in to vote.
4.88/5 (217 votes)
21 Nov 2013CPOL40 min read 1.3M   38.5K   710  
If you want to use ASP.NET MVC framework but is strugling to get things arrange to confidently use for your next business project. This Article is just for you. The article guide you to use ASP.NET MVC framework to architect a small Document Management System.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcDemo.Dal.Interfaces;
using System.Collections;

namespace MvcDemo.Dal.Infrastructure
{
    public class LazyPagination<T> : IPagination<T>
    {
        /// <summary>
        /// Default page size.
        /// </summary>
        public const int DefaultPageSize = 20;
        private IList<T> results;
        private int totalItems;
        public int PageSize { get; private set; }
        /// <summary>
        /// The query to execute.
        /// </summary>
        public IQueryable<T> Query { get; private set; }
        public int PageNumber { get; private set; }


        /// <summary>
        /// Creates a new instance of the <see cref="LazyPagination{T}"/> class.
        /// </summary>
        /// <param name="query">The query to page.</param>
        /// <param name="pageNumber">The current page number.</param>
        /// <param name="pageSize">Number of items per page.</param>
        public LazyPagination(IQueryable<T> query, int pageNumber, int pageSize)
        {
            PageNumber = pageNumber;
            PageSize = pageSize;
            Query = query;
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            TryExecuteQuery();

            foreach (var item in results)
            {
                yield return item;
            }
        }

        /// <summary>
        /// Executes the query if it has not already been executed.
        /// </summary>
        protected void TryExecuteQuery()
        {
            //Results is not null, means query has already been executed.
            if (results != null)
                return;

            totalItems = Query.Count();
            results = ExecuteQuery();
        }

        /// <summary>
        /// Calls Queryable.Skip/Take to perform the pagination.
        /// </summary>
        /// <returns>The paged set of results.</returns>
        protected virtual IList<T> ExecuteQuery()
        {
            int numberToSkip = (PageNumber - 1) * PageSize;
            return Query.Skip(numberToSkip).Take(PageSize).ToList();
        }

        public IEnumerator GetEnumerator()
        {
            return ((IEnumerable<T>)this).GetEnumerator();
        }

        public int TotalItems
        {
            get
            {
                TryExecuteQuery();
                return totalItems;
            }
        }

        public int TotalPages
        {
            get { return (int)Math.Ceiling(((double)TotalItems) / PageSize); }
        }

        public int FirstItem
        {
            get
            {
                TryExecuteQuery();
                return ((PageNumber - 1) * PageSize) + 1;
            }
        }

        public int LastItem
        {
            get
            {
                return FirstItem + results.Count - 1;
            }
        }

        public bool HasPreviousPage
        {
            get { return PageNumber > 1; }
        }

        public bool HasNextPage
        {
            get { return PageNumber < TotalPages; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Virtusa Pvt. Ltd.
Sri Lanka Sri Lanka
In-depth coverage of Microsoft .Net, Cloud and many other cutting-edge Technologies.

- The Mandelbrot set – someone has called it the thumb-print of God – is one of the most beautiful and remarkable discoveries in the entire history of mathematics. My profile picture is generated with that equation.

You may contact Nirosh for Consultations, Code Reviews and Architecture Guide Workshops via c_nir*o*sh@hotmail.com (Remove * to use)



View Nirosh L.W.C.'s profile on LinkedIn


Other Links

Comments and Discussions