martijnboland / MvcPaging

ASP.NET MVC Paging HTML helper
https://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/
MIT License
290 stars 144 forks source link

Create an empty paged list #44

Open wis3guy opened 9 years ago

wis3guy commented 9 years ago

Sometimes it is useful to be able to create an empty instance of PagedList. As i did not want to change the library, i worked around this by doing this:

public static class PagedList
{
    /// <summary>
    /// Factory method for an empty <see cref="PagedList{T}"/>
    /// </summary>
    /// <typeparam name="T">Type of data contained in the list</typeparam>
    /// <returns>An empty paged list</returns>
    public static IPagedList<T> Empty<T>()
    {
        return new List<T>().ToPagedList(0, 1);
    }
}

and then using

var empty = PagedList.Empty<SomeType>();

it would be cleaner if i could simply do:

var empty = new PagedList<SomeType>();
martijnboland commented 9 years ago

Sure, it's possible. Just curious: in what situations do you need an empty list?

wis3guy commented 9 years ago

F.ex when i want to return an empty search result. Sometimes i can already determine that a search is not going to yield any results without firing a query.

Or, f.ex. as a default values on my models. I always want my objects to be in a valid state, which means that in the default constructor i want to set my collection properties to an empty collection.

martijnboland commented 9 years ago

Alright, clear

martijnboland commented 9 years ago

Just looking at it, but I think it doesn't make any sense to create an empty constructor, because we'll have to assume some pagesize. Probably better to just use one of the existing constructors:

var emptyList = new PagedList<SomeType>(null, 0, 1);
wis3guy commented 9 years ago

The idea is not to create a mutable pagedlist, rather an immutable one which indicates no results.

On Sunday, October 5, 2014, Martijn Boland notifications@github.com wrote:

Just looking at it, but I think it doesn't make any sense to create an empty constructor, because we'll have to assume some pagesize. Probably better to just use one of the existing constructors:

var emptyList = new PagedList(null, 0, 1);

— Reply to this email directly or view it on GitHub https://github.com/martijnboland/MvcPaging/issues/44#issuecomment-57951905 .

Ciao, Geoffrey

Geoffrey Braaf | +31655793290 Freelance .NET Software Architect & Passionate Developer | http://wis3guy.net Findsi: find-as-i | http://www.findsi.com

meatgithub commented 9 years ago

Thanks for this tip wis3guy. It was indeed helpful.