Tuesday, July 13, 2010

Tweeting taxis

I was talking with some folks from Canada at an event I recently attended during the Desire2Learn Fusion 2010 conference in Chicago. One of the things they pointed out was that, in Chicago, you hear constant taxi horns. Now, sitting in my hotel room, I can't seem to block them out.

I think taxi cab drivers have developed a code with which they communicate via their horns. It has nothing to do with the warning "beep" that you and I use, or even the "angry beeeeppp" that you occasionally hear (and fear). No, I think they've taken "tweet"ing literally and let each other know what they think of their passengers, their current destination, what they're doing after work...all manner of things through the subtle shifts in tone and length of the beep. How else do you explain that I hear constant taxi horns outside my hotel room, but have not yet heard or seen a single crash to go along with any of them. Moreover, you hear them even when there are only two w-i-d-e-l-y separated taxis on the same street.

I suppose it could be extremely defensive and safe driving habits, but when was the last time you thought to apply that description to a taxi driver? I think it's safer to assume that it's not as primitive a signaling system as you and I are used to. There's much more than simple emotional content blasting across the streets of Chicago; who needs 140 characters to get your message across when you have a few seconds of car horn.

Tuesday, February 23, 2010

Expressions, functions, and case sensitivity

I've been developing a LINQ library with some useful classes and extensions to support, primarily, testability with my LINQ-based data layers. To this end, I have an IDataContextWrapper interface and a DataContextWrapper<T> generic implementation (where T derives from DataContext). I notice in my use of this code I was making calls looking like:

var user = this.ContextWrapper
.Table<User>()
.SingleOrDefault( u => u.Username == username );

Here, Table<T>() is the method on the wrapper that returns the strongly typed Table corresponding to the type T from the wrapped DataContext.

I thought to myself, this would be a lot shorter if I simply added a method to my wrapper class to get the single (or first) matching element from the table. So I proceeded, in my naive way, to add such a method.

public T SingleOrDefault<T>( Func<T,bool> selector )
{
return this.db.GetTable<T>().SingleOrDefault( selector );
}

Then simplify my code from above, slightly, to:

var user = this.ContextWrapper
.SingleOrDefault<User>( u => u.Username == username );

I did the same for Single(), FirstOrDefault(), and First().

After awhile I started getting reports of spurious errors in some code that is dependent on this library. Perhaps, you can see the subtle error lurking in the above. Here'a hint: the failures that were being reported all had to do with usernames that had uppercase characters in the database, but were typed in as lowercase in the login box.


It turns out that the extension method SingleOrDefault() that takes a Func<T,bool> performs the function, not by translation to SQL (duh), but by loading up the table and applying the function to each result in turn to filter the matches. What I really needed to use was the signature that takes an Expression<Func<T,bool>>. This allows the expression to be translated to SQL and performs the query in the same a case insensitive way as the original query.


public T SingleOrDefault<T>( Expression<Func<T,bool>> selector )
{
return this.db.GetTable<T>().SingleOrDefault( selector );
}

The most interesting bit is that I didn't need to change my calling code at all, as the lambda expression maps equally well onto the Func<T,bool> or the Expression<Func<T,bool>>.

Just another subtle thing to remember when using or extending the LINQ classes.

Wednesday, January 27, 2010

ASP.NET MVC links for jQuery tabs

To work fully, including the loading indicator when using AJAX tab loading, jQuery expects your tab set up to be something like:

<div id="tabs">
<ul>
<li><a href="/controller/action1"><span>Tab 1</span></a></li>
<li><a href="/controller/action2"><span>Tab 2</span></a></li>
<li><a href="/controller/action3"><span>Tab 3</span></a></li>
</ul>
</div>

Note that the standard HtmlHelper extension for a link has trouble producing the correct anchor tag. If you specify HTML in the link text, it will be encoded on output. This is decidedly not what you want.


After several project where I used jQuery to wrap the anchor text in a span on page load, I decided that it would be just as easy (perhaps easier) to simply add a new extension named TabLink to my existing collection of extensions and simply generate the anchors with the embedded span for tab links.



public static string TabLink( this HtmlHelper helper, string text, string action )
{
return TabLink( helper, text, action, null, null, null );
}

public static string TabLink( this HtmlHelper helper, string text, string action, string controller )
{
return TabLink( helper, text, action, controller, null, null );
}

public static string TabLink( this HtmlHelper helper, string text, string action, string controller, object htmlAttributes )
{
return TabLink( helper, text, action, controller, null, htmlAttributes );
}

public static string TabLink( this HtmlHelper helper, string text, string action, string controller, object routeValues, object htmlAttributes )
{
var urlHelper = new UrlHelper( helper.ViewContext.RequestContext );

var anchorBuilder = new TagBuilder( "a" );
anchorBuilder.Attributes.Add( "href", urlHelper.Action( action, controller, routeValues ) );
anchorBuilder.MergeAttributes( new ParameterDictionary( htmlAttributes ), true );

var spanBuilder = new TagBuilder( "span" );
spanBuilder.SetInnerText( text );

anchorBuilder.InnerHtml = spanBuilder.ToString( TagRenderMode.Normal );

return anchorBuilder.ToString( TagRenderMode.Normal );
}


Used as:


<div id="tabs">
<ul>
<li><%= Html.TabLink( "action1" ) %></li>
<li><%= Html.TabLink( "action2", "controller" ) %></li>
<li><%= Html.TabLink( "action3", "controller", new { id = "tab3" } %></li>
<li><%= Html.TabLink( "action4", "controller", new { page = 1 }, new { id = "tab4" } %></li>
</ul>
</div>


<script type="text/javascript">
$(function() {
$('#tabs').tabs();
});
</script>