Dusting Off the Cobwebs

I have taken a bit of a hiatus from the usual blogging over the past few months. Between the birth of my daughter and other personal matters, I haven’t had the time or energy to really produce content at a level I’d feel is acceptable. I’ve spent the time re-evaluating my activities outside of work. My gift and my curse is that I love what I do for a profession. I have been tweeting consistently, and in my absence from the blog, I’ve presented at two code camp type events so I feel that might give me some “karma points” for the silence here.

Anyway, I’ve been waiting for something inspirational that I just HAD to blog about. And Ayende provided a perfect topic to act as kindling for my flame. It’s a simple scenario (and one we’ve probably seen a dozen times).

public class Prisoner
{
    public virtual bool CanBePutInIsolation() { ... }
    public virtual bool IsEligibleForVacations() { ... }
    public virtual bool CanSendToWork() { ... }
    //Other code here
}

What we see here is a domain object with view logic embedded. Basically the functions above provide hooks for the UI to decide whether certain elements should be enabled like so:

commandUI

In WPF, the solution is simple, embed the logic into an ICommand.CanExecute() request. But the Command Pattern extends beyond just WPF. WPF just makes the pattern a more mainstream concept. So if you find yourself putting a bunch of logic on your domain objects for the explicit purpose of enabling/disabling your UI, don’t. Wrap it in a command either freestanding, or on a ViewModel.

0 Comments

Leave a Reply

Your email address will not be published.