Usability tip: Implement web page titles as inverted breadcrumbs

In the past, I implemented web page titles (i.e. the code in the <title> tag) the same way that one would implement a breadcrumb – that is, from the least-specific to most specific. For example:

Web Site Name: Category: Page Name

That is similar to how a breadcrumb would look:

Home > Category > Page Name

Note that I said this is how I did it “in the past.” Today, I read 9 Common Usability Mistakes In Web Design over at Smashing Magazine, which states in mistake #3:

Putting the name of the website last puts more emphasis on what the page itself is about, rather than on website branding, which is still there.

This makes perfect sense, and the article illustrates the idea well with screenshots.

Needless to say, I’ll be refactoring web projects to invert titles at my earliest convenience.

Book Review: The Zombie Survival Guide by Max Brooks

For a guy who spends most of his days commuting reading things like The Economist and Scientific American, it felt somewhat strange pulling out a book whose subtitle is Complete protection from the living dead. Yet here I was, reading just that book, on a recommendation by an old friend, Mr. Vikar (who knows a good book when he reads one)… and enjoying every minute of it.

The Zombie Survival Guide is the work of Max Brooks, son of comedian/writer/director Mel Brooks (the brains behind of one of my all-time favorite movies, Blazing Saddles). In it, the author takes us on a journey that describes, as only a self-help survival guide can, how to survive a zombie invasion.

Everything zombie-related is covered, including their physiology (they’re dead) and psychology (none); their source (a virus, Solanum); and, of course, an in-depth education on how to identify them, kill them, and survive in a world where there presence is rare to common.

What’s most interesting about the book is that you’ll not just be entertained by the amount of thoughtfulness that went into it, you’ll actually learn something by reading it. There are a few underlying themes of zombie survival:

  • know your enemy’s — and your own — strengths and weaknesses;
  • keep your cool; and
  • be prepared.

No doubt, such a book would have pleased Sun Tzu.

This book certainly isn’t for everyone. If you can’t suspend disbelief and accept the fact that you’re reading a book that is clearly made up but written as if it’s absolutely real, and if you’ve got a problem reading books about the living dead, move on. But if you enjoy novels of the horror genre, and get a kick out of guys like Bear Grills and shows like Survivorman, you’ll no doubt enjoy this quick read.

Besides, you never know… there just might be some truth behind the fiction.

Executing native SQL using NHibernate named queries

I’ve been doing a lot of work with NHibernate lately, particularly with named queries. It took a while to get it just right, so I figured it would be helpful to others (and to myself in the future) to note some of the gotchas and how-to steps to get it just right.

What is a named query?

An NHibernate named query is essentially a native SQL statement that can be invoked by NHibernate and can return strongly-typed object data. It allows you to leverage native SQL code — parametrized statements or stored procedures — to perform complex data manipulation and retrieval.

In other words, let’s say you’re writing a baseball game, and you have three objects in your domain model: a Player, a Team, and a DrugTestResult model. You write a stored procedure, spSelectPlayersByLastDrugTestDate, which returns all players who haven’t had a drug test since a given date.

Now that you have a domain model and a SQL statement, how do you execute it through NHibernate?

Mapping your named queries

NHibernate needs to know about your named queries in order to execute them. Much like you tell NHibernate about your domain model using XML mapping files (*.hbm.xml), you tell NHibernate about your named queries using those same files.

Since the named query mappings do not relate directly to your domain model mappings, you can create a separate mappings file just for your named queries. A sample mappings file for our spSelectPlayersByLastDrugTestDate could look like the following.

< ?xml version="1.0" encoding="utf-8"?>
<hibernate -mapping xmlns="urn:nhibernate-mapping-2.2">
<sql -query name="FindPlayerByLastDrugTest">
<query -param name="LastDrugTestDate" />
<return class="Player">
<return -property column="PlayerID" name="PlayerID" />
<return -property column="PlayerName" name="Name" />
<return -property column="TeamID" name="Team" />
<return -property column="LastDrugTestID" name="LastDrugTestResult" />
</return>
exec spSelectPlayersByLastDrugTestDate @LastDrugTest=:LastDrugTestDate
</sql>
</hibernate>

The named query part of the mapping is in the <sql-query> section. I’ll describe its most important sections below.

  • The sql-query tag defines the named query. The name attribute is the name that NHibernate (and, in turn, your code) will use to reference the query. In our example, we are stating that this named query is called FindPlayerByLastDrugTest.
  • The query-param tag defines a parameter in your query. The name attribute reflects the name by which you are referring to that parameter, not the parameter name in the actual SQL statement. The type is the data type of that parameter. In our example, we have one parameter, LastDrugTestDate, which is a DateTime.
  • The return tag defines the type which is returned by the named query. In our example, the return type is a class, Player. Our SQL statement must therefore return fields which map to the Player object. Note that named queries do not have to return domain model objects; they can return ordinals, other types, or nothing at all. (As a result, the return tag is optional.)
  • The many return-property tags tells NHibernate how to map a column in the query results (the column attribute) to a property name in the return type. NHibernate gets rather crafty here. For normal properties (PlayerID and Name in our example) the mappings are simple column-name-to-property-name. Note, however, the TeamID mapping, where we map a TeamID in our result set (which could be an integer, the primary key for a Team domain object) to a property named Team. In our domain model, the Player.Team property is not an integer, it is a Team. NHibernate, because it knows how a Player relates to a Team, will auto-map the player to the team. This nuance is important in getting your named queries to work with your domain model: you map foreign key values in your result set to objects in your data model. (A similar thing happens for the LastDrugTestResult property.)
  • Finally, after we close our return tag, we provide our actual SQL statement. This is a standard parametrized SQL statement with a twist. To inject your query-param values into the SQL statement, you specify :Name — that is, a colon followed by the name specified for the query-param.

Got all that? Good. We’re almost done!

Running your named queries

Finally, the easy part — running the named query! Named queries are run against the NHibernate ISession. Various methods are available to set parameters using a fluent interface.

Running our FindPlayerByLastDrugTest query could look like this:

// get your NHibernate session however you normally would
//ISession session = ...

// create an IQuery based on your named query, specifying parameters
IQuery namedQuery = session.GetNamedQuery("FindPlayerByLastDrugTest")
.SetDateTime(new DateTime(2003, 1, 1));

// execute the query
IList list = query.List();

// if you were running a query that returned a single, scalar value, you could say...
var result = query.UniqueResult();

Note that there are generic versions of List() and UniqueResult(), but I had problems getting them to work. Not sure why (I didn’t dig that far), but the above queries will do the same (though they require some type-casting).

Named queries do a lot more. The following links may prove useful in your learning.

If you find other great resources, be sure to share!

Looking for advice on open source licenses

I’ve been developing (and operating) CSFBL, my multiplayer, web-based baseball game, for over eight years. After mulling for quite some time as to the future of the game, I’m seriously considering the transition of the game to an open source project. That being said, finding the right open source license is important.

After doing my reading, I’m thinking of going the route taken by MySQL – i.e., open-source under the GPL, but the potential for closed-source and commercial options available with a separate license.

Realistically, I highly doubt people would come to license the software for commercial use, but I do want to protect the product, intellectual property, and my sweat equity (I’ve spent an inordinate amount of time, money, and energy on it over 8+ years). My interest is in sharing it (and getting help from others), not letting others profit off it. (Hence I am avoiding licenses such as BSD).

Does anyone have suggestions, thoughts, or words of advice on this matter?

$646,214 Per Government Job, or $2,000 per citizen?

From The Wall Street Journal, via Cato.org:

The December unemployment rate was only 2.3% for government workers and 3.8% in education and health. Unemployment rates in manufacturing and construction, by contrast, were 8.3% and 15.2% respectively. Yet 39% of the $550 billion in the bill would go to state and local governments. Another 17.3% would go to health and education — sectors where relatively secure government jobs are also prevalent.

If the intent of the plan is to alleviate unemployment, why spend over half of the money on sectors where unemployment is lowest?

… [O]nly a fifth of the original $550 billion is left for notoriously slow infrastructure projects, such as rebuilding highways and the electricity grid.

What a disaster. Government picking winners and losers with our money (after all, it’s taxpayer money they spend reallocate) is a recipe for disaster. This time, they want the recipe in order of some $800 billion, with no real guarantee that it’ll help anyone aside from those who were picked to receive the stimulus handouts.

Or, they can give every man, woman, and child in this country $2,000 to spend. Even if only 1% of that money was spent each week, that would increase commerce by $8 billion each week, without creating additional bureaucracy. Even further, if 10% of that commerce generated tax revenue (a fair estimate considering sales and income tax rates), it would increase tax revenues by $800 million each week.

Or, government can do nothing, and wait for the markets to sort things out.

So, choose your poison.

  1. Government doles out $800 billion to various programs, agencies, and projects that it deems are economically politically worthwhile, thereby increasing the numbers of folks who become dependent on government intervention.
  2. Government gives each citizen a $2,000 one-time tax rebate, which in turn has the conservative potential to increase commerce by $8 billion a week and generate $41.6 billion in new tax revenue. Oh, by the way, $41.6 billion is about 5.2% of $800 billion – which means the tax revenue gained could offset the interest payments on the initial outlay, making it revenue-neutral. Unfortunately, that doesn’t do much to pay off the principal, but that’s OK, we’ve been doing it for years with Social Security, why stop now?
  3. Government can do nothing, creating no long-term government debt and allowing individual effort, success, and failure dictate the winners and losers.

I vote for #3. What say you?

Support Eric Sundwall for Congress

The appointment of Hillary Clinton for Secretary of State, and the subsequent appointment of (former Representative) Kirsten Gillibrand to replace her, has left a void in Congress. A special election is pending – and, if you’re among those eligible to vote in it, you should vote for Eric Sundwall to fill that void.

In the interest of full disclosure, I’ve never met Mr. Sundwall, nor do I pretend to know him personally. I do know him as the Chairman for the Libertarian Party of New York, and I do support him for the following reasons.

  • He’s an IT guy. Many readers of my blog are IT people, and as we all know, IT people are typically smart, pragmatic, and results-driven. No doubt Sundwall shares those characteristics.
  • He’s a small-government guy. Sundwall recognizes the danger in deficit spending (which simply puts the burden of payment to our children), and no doubt would be strongly against the $800-billion (so-called) bailout.
  • He recognizes the limitations of government. Sundwall believes in protecting personal liberties. Though supportive of the rule of law, he also sees the extension of law beyond its Constitutionally-defined limits as dangerous, and sees an over-reaching government as a great danger to a free, prosperous society.
  • He’s a free-trade, free-market guy. It has been widely studied, and shown, that free trade and free  markets make societies more open, prosperous, and safe. (Strong trade partners don’t war with each other.) Sundwall is a believer in free trade and free international markets, and believes that labor should be as free to interact with international markets just as products are.

I don’t agree with everything Sundwall says – I’m a bit more conservative on some foreign policy initiatives, perhaps not as liberal in some other concerns – but taken as a whole, Sundwall is one of the best candidates we have for Congress that will support free minds and free markets, and recognize government’s limited role.

Unfortunately, I am not a resident of New York’s 20th Congressional District, so I can’t vote for him, but I can help raise awareness of him to those who can.

To find out more about Eric Sundwall and to support his candidacy, go to sundwall4congress.org.

Good luck, Eric!

Using MonoRail ViewComponents with the Spark view engine

Although I was able to find some documentation and samples (mostly through the unit tests) of how to use a ViewComponent with the Spark view engine, details were sketchy, so I’ll share some quick tips to those who are scratching their heads as I was.

Using a block ViewComponent

Support for block view components is pretty evident in Spark, as shown in the following example, which illustrates the use of the AuthenticatedContent view component.

<authenticatedcontent>
	<logged>
		This content is shown when the user is logged in.
	</logged>
	<notlogged>
		This content is shown when the user is NOT logged in.
	</notlogged>
</authenticatedcontent>

Using the CaptureFor ViewComponent

The CaptureFor component is one useful way to allow a sub-view to “inject” data into a layout. I use this all the time to allow a rescue page to change the page title when an error occurs.

On your layout page, you inject the page title as you normally would (so a controller can inject it via the property bag).

<title>ComputerSims Baseball: $!{ViewData["PageTitle"]}</title>

In the view, the CaptureFor component is called as an HTML tag.

<capturefor id="pageTitle">An unexpected error has occurred</capturefor>

Calling a ViewComponent with view and static data

This is one that perplexed me. I have a custom menu component (called MenuComponent) which accepts a name parameter. I tried calling it like this:

<menu name="mainmenu" />

Unfortunately, that threw an error: “The name ‘mainmenu’ does not exist in the current context“. Fortunately, Spark exceptions are easy to read (you can see the C# code it generates and thus find the error). Apparently, in the above example, “mainmenu” was expected to be a variable in the view page and was not being treated as static text. To use static text, you put single quotes inside the double quotes.

<menu name="'mainmenu" />

Problem solved. I can’t say it’s the preferred way based on my experience thus far (I’d prefer requiring ${mainmenu} for variable injection), but since I’m new to this, I won’t criticize much.