Saturday 31 January 2015

A Mix of Thoughts and News

Time for a mix of thoughts and news.

  • I have to/need to learn French. There are many reasons for it, but no doubt that this paragraph in this Wikitravel article joins that list of reasons in one of the first positions :-D

    It has long been the language of international diplomacy and communication, and although largely supplanted by English since World War II, it remains de rigueur (of obligatory requirement) for educated people in many societies around the world to have some level of basic French ability. It is also an official language of the UN and the EU.

  • This article about the Lava Layer Anti-Pattern is one of the best things published in InfoQ in a long while. It immediately came to my mind a different scenario for this Anti-Pattern. A Web Application started around 15 years ago with classic ASP. Around 2003/2004 it was extended with some ASP.NET WebForms. In 2007 some Ajax panels where added to these WebForms. In 2009 some clever guy tried to do some real Ajax development by hacking these WebForms to return clean html pages and pieces of html and JSON requested via jQuery Ajax. Then in 2011 some ASP.NET MVC came to the rescue, and then in 2013 some Web API magic was added. All of it living together in a huge IIS Site. Seems unrealistic? Well, think that still 3 or 4 years ago the EasyJet site contained some classic ASP pages...
  • Talking about Asp.Net, much has been said in the last months about .NET going partially OpenSouce, about .NET Core and so on... the most interesting of all this (bearing in mind that the multiplatform thing is nothing new, as Mono has been around since the early beginnings) is ASP.NET 5 (aka ASP vnext). First and foremost we get a unified programming model for MVC and Web API (for example no more similar but different classes in charge of routing for each subsystem), as a consequence, you'll be able to self-host MVC! As you'll be able to run it the Core CLR, you can deploy your complete self-hosted application in just a few MBs, without depending on any installed component, and you'll be able to do it on Windows or Linux (and yes, Mac, but I don't give a shit about rotten apples). On the other side, as OWIN continues to gain momentum, hosting on a separate web Server will be much easier.
  • Well, this is quite old news, but I hadn't found it until recently. Gimp has since v2.8 a Single-Window Mode! This is for me the more needed feature in this nice software, those fucking multiple windows floating around have been a pain for many users (me included of course) and kept many of them away, so this is definitely a great step forward.

Monday 26 January 2015

Place de la Concorde

Probably the title of this post will make you think of Paris, but not, I'm talking about Toulouse. After more than 1 year living in this gorgeous city I have a series of places that I really love but that probably are not in most tourist guides. La Place de la Concorde is one of them.

I didn't come across it until a few months ago, and when I did I was delighted by the amazing piece of Art Nouveau Sculpture located there. It's quite unique in Toulouse and I'd never heard about it, but it's a rather complete example of Art Nouveau sensitivity: a delicate representation of femininity combined with flowers and animals.

I can not add anything to what you can already read in that French Wikipedia article, so I'll just post some pictures of my own. By the way, the quartier were it's located, "Les Chalets" has some very beautiful houses (for example the one hosting the Instituto Cervantes), the kind that make you wish you lived there

Sunday 25 January 2015

A War to be Won

Well, I want to write a long post about the tragedy of January 7, but I've not managed to take the time for it, so for the moment I'll just mention here some interesting material concerning the war against madness that our society is waging on, this war between civilization and Wahhabism/Islamofascism or whatever you want to call that scum.

Continuing with my obsession with the fight of the Kurdish Socialists against ISIS, this short Documentary about Syria's Stalingrad depressed me, enraged me and inspired me in equal parts.

This documentary about the threat of ISIS to Lebanon is scary and depressive. That fucking scum seems willing to destroy any place in the Middle East where different religions have managed to coexist for centuries. Why don't they invade Quatar, Saudi Arabia or any other of those Medieval Kingdoms? Ah, yes, because that scum has been funding them with their petrodollars for years... The position of Hezbollah is quite interesting, they don't want to act against ISIS in areas populated mainly by Sunnis in order to prevent those actions from igniting internal tensions. Such a demonized "terrorist group" is now the main defender of Christians and Coexistence in the Region...

While in Montpellier this weekend I came across this nice poster:

Finally this ducumentary about al-Qaeda in the Islamic Maghreb is a real must (unfortunately it's in Spanish) for anyone interested in the history of madness. It tells the story of Islamofascism in Algeria, that I think is a topic that is quite unknown. In my case it was not so unknown, I already knew about the atrocities conducted by the Islamic Salvation Front and related groups, and along with Jomeini's dictatorship they helped to make me develop a deep fear and hate for fundamentalist Islam many years ago. Well before September 11 these beasts were planning to crash a plane against the Eiffel Tower and were committing attacks in French soil, and well before ISIS they were massacring villages, slaughtering pregnant women and kidnapping young ones.

Sunday 11 January 2015

AsQueryable

The discussions as to whether Repositories should return IEnumerable or IQueryable has been hot and present for a long while. There are pros and cons, and I don't think I can add anything particularly useful on what path to follow. However, if you decide to go the IQueryable path, I've found something of some interest related to it.

One problem with returning IQueryable could seem to be, "and what do I do with my Mocks?" The typical case is having some mock repositories returning List<Entity>, so what can you do to comply with that contract that expects IQueryable<Entity>? Well, the BCL itself provides all you need, so no effort required, all you have to do is invoke AsQueryable in your collections and your normal IEnumerable will be turned into an IQueryable!

Seems interesting to take a fast look at how this thing works, but first let's do quick recap of how the different Linq flavours work.

  • Linq to Objects is implemented as just a set of extension methods in the System.Linq.Enumerable class.
  • As discussed in this previous post, the Extension Methods for Parallel Linq are located in the System.Linq.ParallelEnumerable class.
  • The Extension Methods for IQueryable are located in the System.Linq.Queryable class (that along with the other classes is located in the System.Core assembly)
  • As explained in this old post (time flies...) an IQueryable Linq Provider is based on implementing Sytem.Linq.IQueryProviderT> and having in your Sytem.Linq.IQueryable classes a Provider property pointing to it and an Expression property referencing to an Expression Tree that represents the Query (this means that when you chain Linq calls on an IQueryable, this Expression property will be the composition of those queries).
  • The IQueryable<T> interface implements IEnumerable<T>. If you have a class implementing IQueryable but want to use the Enumerable methods on it you can just call the Enumerable.AsEnumerable extension method (or just do a casting, that is just what that method does, as we know Extension Methods resolution is purely done at compile time). When you do this with your DataBase related IQueryable collection you'll be losing all the Sql construction magic that you LinqProvider probably implements under the covers, and you'll end up with a "select all" query and the filtering will be done in that huge in memory collection. Sometimes you need this cause you have a complex query and can't write a Expression Tree for it or your Provider can't convert it into SQL statements.

So, what happens when you invoke AsQueryable (that is an Extension Method in the System.Linq.Queryable class expecting an IEnumerable parameter)?

Well, it can not be too complex. The IQueryProvider provided by the new IQueryable object will receive an expression Tree representing the query, so it occurs to me that if it keeps a reference to the IEnumerable object, it can compile the Expression Tree into a normal delegate and use it with any of the System.Linq.Enumerable extension methods.

Let's go a bit through the Framework code (out of habit I still feel more comfortable going through the decompiled classes shown by ILSpy that using the Reference Source code...) to see how it is implemented. AsQueryable returns a System.Linq.EnumerableQuery object. Same as some objects implement both IEnumerable and IEnumerator (they know how to iterate themselves), this class implements both IQueryable and IQueryProvider (it knows how to query itself). As MSDN says:

Represents an IEnumerable collection as an IQueryable data source.

This EnumerableQuery object contains a reference (the Enumerable property) to the original IEnumerable object. When we invoke "Linq queries" on it (the System.Linq.Queryable extension methods), they'll retrieve the provider (that in this case we've seen that is the EnumerableQuery object itself) and will invoke the CreateQuery method on it (source.Provider.CreateQuery), that in turn returns another EnumerableQuery object. As we do so, the Expression tree for the current Query is composed to the Expression Tree representing previous queries, this way (extract from System.Linq.Enumerable.Where):
return source.Provider.CreateQuery(Expression.Call(null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[]
	{
		typeof(TSource)
	}), new Expression[]
	{
		source.Expression,
		Expression.Quote(predicate)
	}));

Finally, when the data represented by this query is really needed, the GetEnumerator method in EnumerableQuery will get invoked:

private IEnumerator GetEnumerator()
{
	if (this.enumerable == null)
	{
		EnumerableRewriter enumerableRewriter = new EnumerableRewriter();
		Expression body = enumerableRewriter.Visit(this.expression);
		Expression>> expression = Expression.Lambda>>(body, null);
		this.enumerable = expression.Compile()();
	}
	return this.enumerable.GetEnumerator();
}

which will finally compile and execute that Expression that has been composed all over the chain of queries.