In this post, which is part of an ongoing series relating relating the .NET Framework (C#) and the provided Facebook API I will address some of the seriously lacking elements; poor returned data types and the Data Store.
The first of these is easily demonstrable. Consider the method Facebook.API.users.getInfo(userIds) which has a return type of IList<facebook.Schema.user>. This is useful for iterating, but makes it tough work to actually meaningfully look up a list of users and then instantly get a schema.user object from a Facebook user ID (long). To solve this problem I wrapped the call inside a a Generic dictionary with the key as a nullable long: Dictionary<long?, facebook.Schema.user>
public static Dictionary<long?, facebook.Schema.user> getUsers(facebook.API api, List<long> userIds)
{
IList<facebook.Schema.user> friendsInfo = api.users.getInfo(userIds);
Dictionary<long?, facebook.Schema.user> users = new Dictionary<long?, facebook.Schema.user>();
foreach (facebook.Schema.user user in friendsInfo)
{
users.Add(user.uid, user);
}
return users;
}
The other thing you mught notice is that Facebook.API.users.getInfo(userIds) expects a Generic List<long:gt; which is not returned by any of the Facebook functions. Furthermore, an attempt to cast straight to the Generic Long List results in a type exception. I came up with the following, hideous, wrapper function to get round this:
public static List<long> XLINQtoList(IList<long> input)
{
List<long> ret = new List<long>();
foreach (long l in input)
{
ret.Add(l);
}
return ret;
}
You could then call, from within a sub page:
getUsers(Master.fbService.API, SocialLib.XLINQtoList(friends));
Where friends could be, for instance, the return value from facebook.API.friends.getAppUsers()
The next issue I want to address is the Facebook Data Store.
Firstly, I couldn't actually get this to work in my brief tests. Secondly, I fail to see how it can be meaningful. My rationale behind this is that to put an object in the Data Store and retrieve it you need to know its unique ID. This means that you have to store the Unique ID in your own system somewhere. If you use the Facebook Data Store then you are effectively doubling the number of lookups that you will be performing because you will have to do a first lookup in your own database and then a second lookup in their (offsite) database to get the object. Scalable, maybe. Practical, perhaps not.
The next post will be a small piece on styling Facebook applications, with a little renovation work to the existing stylesheets provided.