Sunday, April 05, 2009

I’ve been doing some work with the WCF REST Starter Kit for our website http://rocksolidknowledge.com. Preview 2 of the start kit has a bunch of client side plumbing (the original release concentrated on the service side)

The client side code looks something like this:

HttpClient client = new HttpClient("http://twitter.com");

HttpResponseMessage response = client.Get("statuses/user_timeline/richardblewett.xml");

Console.WriteLine(response.Content.ReadAsString());

As compact as this is I was a bit disappointed to see that I only had a few options for processing the content: ReadAsString, ReadAsStream and ReadAsByteArray. Now seeing as they had a free hand to give you all sorts of processing options I was surprised there weren’t more. However, one of the assemblies with the start kit is called Microsoft.Http.Extensions. So I opened it up in Reflector and lo and behold there are a whole bunch of extension methods in there – so why wasn’t I seeing them?

Extension methods become available to your code when their namespace is in scope (e.g. when you have a using statement for the namespace in your code). It turns out that the team put the extension methods in the namespaces appropriate to the technology they were exposing. So for example the ReadAsXElement extension method is in the System.Xml.Linq namespace and the ReadAsXmlSerializable<T> method is in the System.Xml.Serialization namespace.

Although I really like the functionality of the WCF Starter Kit, this particular practice, to me, seems bizarre. Firstly, it makes the API counter intuitive – you use the HttpClient class and then there is no hint in the library that there are a bunch of hidden extensions. Secondly, injecting your extensions into someone else’s namespace increases the likelihood of extension method collision (where two libraries define the same extension method in the same namespace). The same named extension method in difference namespaces can be disambiguated, the same named extension in the same namespace gives you no chance.

I think if you want to define extension methods then you should keep them in your own namespace – it makes everyone’s life simpler in the long run.

 |  | 
Sunday, April 05, 2009 2:58:48 PM (GMT Daylight Time, UTC+01:00)  #    Comments [4]Trackback
Sunday, April 19, 2009 6:12:38 PM (GMT Daylight Time, UTC+01:00)
Extension methods are simply the worst thing ever.

Think versioning. Think violating OO principles. LINQ needs them, ack. For everything else, they hurt more than they help.
Monday, April 20, 2009 12:30:23 PM (GMT Daylight Time, UTC+01:00)
Actually I disagree.

I think extension methods can provide much greater clarity to the code than utility methods on static classes

e.g.

string s = "Hello";
byte[] b = s.Encrypt();

I think is more readable than

string s = "Hello";
byte[] b = Utils.Encrypt(s);
Richard
Monday, April 20, 2009 12:31:42 PM (GMT Daylight Time, UTC+01:00)
Oh missed this bit - why do you think OO principles are violated? You get no more access to the object than any other static utility method - just has a neater syntax
Richard
Monday, May 24, 2010 4:34:15 PM (GMT Daylight Time, UTC+01:00)
Actually , i think extenstion methods make code more readable.
But they should be in your name space if you want to avoid the situation like one with the REST Starterkit.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):