03 Dec 2009

Sometimes you just need to CodingHorror it!

4 Comments Uncategorized

The title of this post is a tongue-in-cheek reference to SubSonic’s inline query by the same name, which in turn is a reference to the blogger Jeff Atwood’s blog who you should all know.  Rob Conery, the SubSonic project leader, named the inline query class CodingHorror after he allegedly read Jeff Atwood’s post titled Embracing Languages Inside Languages, in which he bestowed the virtues of inline SQL inside your code, instead of the standard bequeathed statement that I bet you all have heard “We do all database work in stored procedures.”.  Jeff outlined his though process on ad-hoc vs stored procs as follows:

The Subsonic project attempts to do something similar for SQL. Consider this SQL query:

SELECT * from Customers WHERE Country = "USA"
ORDER BY CompanyName

Here’s how we would express that same SQL query in SubSonic’s fluent interface:

CustomerCollection c = new CustomerCollection();
c.Where(Customer.Columns.Country, "USA");
c.OrderByAsc(Customer.Columns.CompanyName);
c.Load();

I’ve mentioned before that I’m no fan of object-oriented rendering when a simple string will suffice. That’s exactly the reaction I had here; why in the world would I want to use four lines of code instead of one? This seems like a particularly egregious example. The SQL is harder to write and more difficult to understand when it’s wrapped in all that proprietary SubSonic object noise. Furthermore, if you don’t learn the underlying SQL– and how databases work– you’re in serious trouble as a software developer.

Enough of the History lesson, why are you writing this post

Well the reason for this post is two fold, I haven’t written a post in a couple of weeks, and I wanted to write about my own CodingHorror moment, while working with the Entity Framework, that solved a big problem I was having with getting the LIKE statement to work in LINQ.

I am working with a client of mine trying to expose their data to the web via a simple REST service.  Their whole database model has been constructed in the Entity Framework in a simple active record format.  One of the requirements, I was given, was to support wildcards on a couple of the input fields.  I thought this was no big deal, because I knew that the StartWith, EndsWith, and Contains methods of the System.String object, when used in LINQ, translated down to the SQL LIKE operator. 

However at the time I didn’t anticipate how much of a pain in the butt it was to actually figure out which of these three methods I should use depending on where the wildcard was placed in the string.  And how I would support a wildcard that was placed in the middle of a string.

Enter Entity Frameworks’s parameterized Where method

The parameterized Where method, of the ObjectQuery<T> object, really saved my butt.  It allowed me to produce the exact effect of the query I was looking for, and with out a lot of hacking.  Here is what I did:

if (!String.IsNullOrEmpty(toAddress))
	table = table.Where(
		"it.ToAddress LIKE @toAddress", 
		new ObjectParameter("toAddress", toAddress)
	);

var results = 
	from result in table
	where result.Account.AccountId == accountId
		&& result.DateTime >= startDate

return View(results.ToList());

The best part of the above code is that I can still use LINQ, and jump in to standard SQL syntax when the LINQ syntax fell short. 

This is my opinion gives the Entity Framework a step above the rest, because it means that I don’t have to complicate my life and my program by moving to a stored proc or making the whole select statement inline-SQL (which I wouldn’t ever do). 

Features like this that make developing software easier, and obviously have the programmer in mind, are what we should all strive for.  I like this for the very fact that the Entity Framework team has acknowledged that LINQ, while a wonderful addition to .NET, doesn’t meet all the needs for pulling data from the database and they sought to minimize the short comings by allowing developers to jump back in to the natural SQL language.  This is a wonderful addition in my book.

14 Aug 2008

Is Stackoverflow.com really a Web 2.0 site?

1 Comment Uncategorized

I have been lucky enough to be one of the few and many people that have had the chance to preview the beta of stackoverflow.com. It has a very nice look and feel in my opinion and seems to work very well for an early beta. Jeff Atwood deserves major kudos. However I have had one plaguing question?

Is stackoverflow.com really a Web 2.0 site?

I started thinking about this question a couple days ago, because as many of you know I have my own project, that isn’t much different functionality wise than stack overflow. As I started cataloging everything that a Web 2.0 site is suppose to consist of, the more I asked the question what is a Web 2.0 site, and is stackoverflow.com really one?

Tim O’Reilly defines Web 2.0 as the following:

Web 2.0 is the business revolution in the computer industry caused by the move to the Internet as platform, and an attempt to understand the rules for success on that new platform.

In my opinion a platform has the following characteristics and so does a Web 2.0.  There are probably many more, but these are the top 4.

  1. It must have a fluent interface.  (this is usually implemented through AJAX)
  2. It must have an externally available API.  (because a closed platform is what Web 1.0 was all about)
  3. Users can own data and have control over who sees it.
  4. It is an obvious advancement from the previous Web 1.0 version of the software if one exisited.

http://stackoverflow.com

Just as a precursor to the following discussion, I have never heard Jeff proclaim that stack overflow is a Web 2.0 site, so this is just my ramblings.  Jeff has also done an awesome job with the site in a short period of time so everything I am saying now will probably change in the future.

Stackoverflow.com has only really done #1 of the first 3.  However what I really want to have a discussion on is if it really has advanced it self enough beyond the old forum model to really be considered 2.0 worthy or is it just a display layer on the 1.0.  For all intents and purposes we are going to use the forums on ASP.NET for comparison.

  • Allows users to create posts? (both yes)
  • Allows users to create reply to the posts? (both yes)
  • Allows users to talk to each other? (asp.net only)
  • Allows users to rank posts? (both yes, but different mechanisms)
  • Allows users to rank replies to posts? (stackoverflow.com only)
  • Allows users to get a system ranking against other users? (both yes)
  • Allows users to tag posts? (both yes)
  • Allows users to tag replies? (asp.net only)
  • Allows users to mark a reply as an answer? (both yes)
  • Allows categorization of posts? (asp.net only)
  • Users aquire badges of honor in the system? (both yes)
  • Users can have a profile of themself and their activity? (both yes)
  • Can easily follow a posting? (asp.net only)
  • Can easily follow a grouping of posts? (asp.net only)
  • Allow users to delete posts? (stackoverflow.com only)
  • Allow users to delete replies? (stackoverflow.com only)

Using the above questions it makes stackoverflow.com look like it is playing catch up to the asp.net forums, which has had a 6 year head start.  But it still begs to ask the question is the technology and application of it worth of the title 2.0 or just 1.1?  I think Jeff needs to impliment the following beyond the typical forum to really claim that 2.0 title.

  • An external API (REST seems popular)
  • Become less of a destination and more of a service:
    • Render in other platforms. (Facebook and/or Open Social)
    • Allow posting and following via SMS and IM.
  • Allow users to follow certain tags, categorizations, users, etc. through RSS, JSON, XML, etc.

I do beleive that Jeff has a long way to go before stack overflow is considered an advancement beyond the standard forum, but if anybody can make that leap it is Jeff.