Unrewarded Persistence

Boy, do I really want to love this technique.
I mean, who wouldn’t prefer this:


[PersistField(Location = PersistLocation.Session)]
public bool IsLoggedIn;

Over this:

public bool IsLoggedIn
{
get
{
return Convert.ToBoolean(Session["IsLoggedIn"]);
}
set
{
Session["IsLoggedIn"] = value;
}
}

It’s so elegant and so concise. But it operates through reflection and
reflection has a bad rap for being hoggy. Can you imagine checking every
property for the attribute on load and then checking to see if they’ve
changed at on unload? Talk about a tax on resources.

This beautiful snippet trades readability for performance and I think the
bargain isn’t worth it. The time it takes to write out the getters and
setters of a property is a one-time deal. The decoration method outlined
above makes the code easier to read and easier to describe, but at the
expense of perennial runtime delays.

It’s just not worth it.

Advertisements

%d bloggers like this: