Typically I don’t use third-party libraries for simple tasks because, often, the game is not worth the candle.
In fact, integrate a not perfectly realized tool is more expensive than building one yourself with a minimal set of functionality.
Two exceptions about .NET MVC applications are undoubtedly AutoMapper and PagedList, which are tools simple, effective and easy to use.
The problem, in particular, is to make them coexist.
Let me explain: Suppose we have a need to map a PagedList of objects of type Class1 on a PagedList of objects of type Class2, trivially the code would be as follows:
Mapper.CreateMap <Class1, Class2> (); IPagedList <Class1> class1 = new List <Class1> (). ToPagedList (1, 1); IPagedList <Class2> class2 = Mapper.Map <PagedList <Class2>> (class1);
Indeed, this code does not work and we receive the following error message: “Type ‘PagedList.PagedList` 1 [Class2]‘ does not have a default constructor. ”
The class PagedList, in fact, does not expose a default constructor and to get what you want you must use a workaround.
In the first instance, you must map the generic lists and then, thanks to the class StaticPagedList we can build a PagedList with the data of Class2 and the metadata of Class1, as follows:
IPagedList <Class1> class1 = new List <Class1> (). ToPagedList (1, 1); <Class2> Class2 = Mapper.Map IEnumerable <List <Class2>> (class1); IPagedList <Class2> pagedClass2 = new StaticPagedList <Class2> (class2, ((IPagedList <Class1>) class1). GetMetaData ());