Skip navigation

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: