Using AutoMapper with PagedList

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 ());

Web.Net Conference 2012, Milan

The Web.Net Conf is ended and I must say it was an excellent conference, both in terms of content and organization.

The location was great (though slightly undersized) few steps far from the central station and very comfortable chairs.

Cool gadgets as usual: the conference’s t-shirt and GitHub stickers.

webnet12-1

webnet12-2

The speaker of the first talk to which I wanted to attend (“We thought we were doing the right thing, but we lost 2 years” by Niels Hartvig) has given forfait, so I attended “REST with ASP. NET Web API” by the young Jef Claes, who offered great ideas on how to use this quite new technology.

Then I saw the more interesting talk of the day.
The speaker was an Italian boy who spoke English really fluently.
Alessandro Giorgietti has explained the (transparent) use of Web Sockets in .NET and in particular the development of real-time applications with SignalR.
A very interesting library that solves problems in a very ingenious way:  in fact, it is able to fall back  to simulate real time communications (polling, long polling and forever frame) when the browser does not support web sockets.
I’ll try it after the post.

After the second talk we moved to the lunch room. 10 euro menu consisted of pizza, sushi and a few cans. Personally I do not like “self service” and the drinks were very few. I hope they will improve it next time.
Anyway I had the opportunity to chat with Sandro, Davide and Michele (good luck for your startup!).

In the afternoon I attended a talk on a very “hot” topic as far as I’m concerned. In the last few days I had the opportunity to talk about it with Luciano, since I documented and I used various methods but I have never been satisfied with the actual auth systems for my APIs.
Maarten Balliaw instead showed us how to offer a service based on OAuth2 with Windows Azure.

Then I moved to follow the talk of Ayende on scalability, another topic that touches me very closely given the size of some of our projects.
Super cheered by the audience I have to say that his speech gave me very little in terms of practical informations.

Then I came back to the lake because I also need to rest in the week-end :-)

So many compliments to the staff, great ideas and a lot of energy as every time I come back from a conference.

Infinite scroll on iOS with UITableView

We made the artedelcorpo.com iPhone App following the “fail fast, fail cheap” philosophy.

The project is, in fact, a simple (but effective) application with a limited set of functionality.

One of the features that we considered fundamental for the first release is the infinite scroll.

So, after a thorough study on the class UITableView and its logical I designed and implemented the following:

- (void)scrollViewDidEndDecelerating:(UITableView *)tableView {
    if (tableView.contentOffset.y > 0) {
        // Fetch data
    }
}

The scrollViewDidEndDecelerating method, offered by the UIScrollViewDelegate protocol, indicates to our ViewController that a view, in the specific case a UITableView, has finished the scroll phase.

In particular, I had the need to identify the direction of the scroll and its member contentOffset indicates the difference of position between the content and the scroll itself.

If this difference is greater than zero, it means that the user has a scroll down, so you must run the fetch of the new data.

Select N to M records with Linq To Entities and SelectMany

Suppose you have a relationship articulated as follows:
a user may be present in n groups, each of which are associated n documents.
All this, of course, with a n to m table.
Suppose also that you have the need, given a user, to list all the documents associated with it (according to the groups).
With LINQ to Entities and SelectMany you can accomplish this without making costly iterations.

    public partial class User
    {
        public IEnumerable<Document> GetDocuments()
        {
            return UsersGroups.Select(u => u.Group).SelectMany(g => g.GroupsDocuments).Select(g => g.Document);
        }
    }

Select geolocalized records by radius

With geolocation, it’s possible to offer space-related informations to users.

For example, we can show job offers ordered by the distance from a user.

To obtain the desired result is sufficient to run the following query on the table containing the geolocated records.

SELECT
    *,
    ACOS(SIN($latitudeInRadians) * SIN(RADIANS([Latitude])) + COS($latitudeInRadians) * COS(RADIANS([Latitude])) * COS(RADIANS([Longitude]) - $longitudeInRadians)) * 6371 AS [Distance]
FROM
    [Table]
WHERE
    ACOS(SIN($latitudeInRadians) * SIN(RADIANS([Latitude])) + COS($latitudeInRadians) * COS(RADIANS([Latitude])) * COS(RADIANS([Longitude]) - $longitudeInRadians)) * 6371 < $radius
ORDER BY
    [Distance]

Where $latitudeInRadians is the latitude of the user, converted to radians, $longitudeInRadians the same calculation for longitude and $radius is the radius within which it makes sense to search.