A declarative way of handling property changed notifications

A calculated property is something that is calculated based on the results of some other properties. For example:

  • Subtotal = Unit Cost * Quantity
  • The finalize button should be enabled if the order status is Open
  • The unit cost is editable if the order isn’t finalized and the user has the appropriate access rights
  • Password change is valid if NewPassword.Length >= 6 && NewPassword == NewPasswordConfirmation

You’d go ahead and define something like:

        public decimal Subtotal
        {
            get { return UnitCost * Quantity; }
        }

This is all fine and dandy so far. But if you’re doing this in a Windows Forms application and you want to use DataBinding or an ErrorProvider, you need to implement INotifyPropertyChanged, i.e. raise an event called PropertyChanged whenever your calculated properties change.

The most straightforward way of doing this is whenever the UnitCost or Quantity changes, you raise PropertyChanged on Subtotal as well, so something like:

        private decimal _unitCost;
        public decimal UnitCost
        {
            get
            {
                return _unitCost;
            }
            set
            {
                _unitCost = value;
                PropertyChanged("UnitCost");
                PropertyChanged("Subtotal");
            }
        }

Or in other cases, you might update it right in the UI code, like this:

        public void OnOrderStatusChanged(Status newStatus)
        {
            buttonFinalize.Enabled = (newStatus == Status.Open);
        }

This is fine in a small application, but as things grow and evolve, it quickly becomes an unmaintainable mess. You might tweak the conditions for your calculated properties and forget to update the PropertyChanged notifications, introducing subtle bugs. You might have to dig through multiple places in your code just to figure out when a button should be enabled. This was one of the most painful parts of our codebase, and I was looking for a way to make this simpler. If Excel can figure out everything it needs to update whenever a value changes, why can’t Windows Forms?

Turns it, it is more or less possible, with some tricks and some boilerplate. We’ve got it working with something that looks like this:

public static PropertyInfo SubtotalProperty = For<Line>(l => l.Subtotal).Define(l => l.Qty * l.PriceEa);

We wrote some framework code to support this and ended up doing:

  • Define a View Model class for most of these calculated properties
  • Use DataBinding to link the View Model properties and the associated properties on the Form, e.g. buttonFinalize.Enabled
  • In the View Model, write the calculated property and the expression that defines it. This way, when you look at the FinalizeButtonEnabled property, you see its definition newStatus == Status.Open right above it.
  • Use System.Linq.Expressions to interpret the expression definition and track at the source properties it depends on.
  • Write some library code that automatically updates the calculated properties and raises PropertyChanged whenever the source properties change.

The end result is that we can just define these calculated properties and the framework code will automatically interpret when to update them, raising PropertyChanged notifications appropriately. It’s easy to debug when things go wrong (you just look at the declaration and see if it makes sense) and a pleasure to work with. I’d love to see this sort of feature get added to frameworks like CSLA .NET.

Update: I’ve now uploaded some sample code to demo this concept. Download sample code.

A calculated property is something that is calculated based on the results of some other properties. For example: Subtotal = Unit Cost * Quantity The finalize button should be enabled if the order status is Open The unit cost is… Continue reading ‘A declarative way of handling property changed notifications’

3 Comments

Blizzcon 2011

Our company is named Archon Systems so it should be no surprise that some of us love Starcraft. I’ll be heading down to the Anaheim for Blizzcon this year. If you see someone with the “Archon Systems” t-shirt (as shown a few posts ago) come and say hi!

you can even come and challenge me to Starcraft2 🙂

Blizzcon

Our company is named Archon Systems so it should be no surprise that some of us love Starcraft. I’ll be heading down to the Anaheim for Blizzcon this year. If you see someone with the “Archon Systems” t-shirt (as shown… Continue reading ‘Blizzcon 2011’

19 Comments

Design o’ the times*

Today the appearance of a motorcar is a most important factor in the selling end of the business—perhaps the most important factor— because everyone knows the car will run.
— Alfred P. Sloan, CEO General Motors

“In most people’s vocabularies, design means veneer. It’s interior decorating. It’s the fabric of the curtains of the sofa. But to me, nothing could be further from the meaning of design. Design is the fundamental soul of a human-made creation that ends up expressing itself in successive outer layers of the product or service.”
— Steven P. Jobs, CEO, Apple Computers

With the passing of Steve Jobs, a lot has been said about the impact that he’s had on a wide range of industries. But it’s pretty much agreed that his most significant contribution has been to show how important design, done at a really deep level, is to the success of a modern company.

I think the two quotes above illustrate perfectly the shift in the way that people view design in the post Steve world.

The way things were

The quote up top is from Alfred Sloan, CEO of a booming mid 20th century General Motors. Following that is Steve Jobs, creator of the most successful post-industrial company of the early 21st century. To Sloan, design was merely a “veneer”; a way of differentiating one car maker’s product from the next (and of making next year’s model more desirable than this one’s). In a world bounded by material constraints, a consumer was limited by cost to about one car per household; and the cost of switching was high. Once you’ve been seduced by the veneer of the product you are stuck and the conversation needn’t go further.

Buddy, can you spare a minute?

On the internet, there are dozens of web sites competing in any space. Ad-based and freemium models mean that the cost of ownership is as low as creating an account, and the cost of switching is so low that users can choose to use several competing products simultaneously. In a marketplace of infinite, low cost choices, what is scarce is not the product; what is scarce is the user’s own attention.

When attention is the barrier to user adoption, designed experiences have to go deeper. Everyone knows the car will run, but how well it runs and how it feels to run it make the difference between an active, engaged and productive user and a lost sale to a competitor. A well designed user experience can capture the attention of users and convince them to spend their precious time with your product.

Steve Jobs realized this, and he made sure that every aspect of the experience with an Apple product was thought through; that his products were appreciative of the time we spend with them.

Rock and Roll all night

Other companies are demonstrating that they have learned from Jobs:

To me, the most fascinating thing about Facebook’s f8 keynote was how focused it was on design. Despite the wealth of powerful new technology behind the new Timeline profile design the discussion was mostly about usability and user experience. They even closed the keynote with an amazing mini biography of internet-famous designer Nicholas Felton (from about 1:24 to 1:37 in the video).

Mobile forces you to focus

Mobile forces you to focus

Luke Wroblewski’s Mobile First manifesto has gained a lot of traction recently. One of the central observations of Mobile First is that mobile forces you to focus. Limited screen real-estate and distracted users mean that designers have to focus more on giving the user a productive experience. The design insights gleaned from building mobile first can be folded back into providing a lean and usable desktop experience as well. In an endless world of apps, it’s no longer enough to throw every little feature into your product; to create good design, you have to think about the user’s needs first.

I love the Little Big Details blog because it shows how much attention companies of all sizes are paying to design. It also shows that design is not just a nice colour palette or an eye-grabbing logo; it’s the little things that you might not even notice that add up to a great overall experience. From scrolling through the posts, you can also tell that despite their reputation for cold, metrics-driven product design, Google gets it too.

The river I stand in

When I say that a deeper approach to design is about capturing busy users’ attention what I am saying is this; good design can give the user back the one thing that we can never regain; time. We can optimize our designs to help the user get work done faster by optimizing workflows, providing useful short cuts, tightening feature sets and attending to many other tiny details. In cases where something can’t be made faster we can at least strive to make the experience more enjoyable by reducing visual clutter, leaving intuitive visual cues and providing helpful feedback amongst other things.

It is this magic ability of design to create more time that makes good design such a valuable commodity in today’s market where a user’s time and attention is the only scarce resource.

* The title of this post is a reference to Prince’s terrific album Sign o the times, which is a great way to spend your time.

Today the appearance of a motorcar is a most important factor in the selling end of the business—perhaps the most important factor— because everyone knows the car will run. — Alfred P. Sloan, CEO General Motors “In most people’s vocabularies,… Continue reading ‘Design o’ the times*’

Leave a comment

@ Web 2.0 Expo in NYC!

We are at the Web 2.0 Expo in New York City!

If you see a bunch of people in white T-shirt with the pattern below tomorrow, come and say hi!

T-shirt pattern

We are at the Web 2.0 Expo in New York City! If you see a bunch of people in white T-shirt with the pattern below tomorrow, come and say hi!

Leave a comment

Steve, thank you.

He taught us “design is not just what it looks like and feels like. Design is how it works.”

He taught us the mantra of “focus and simplicity”, that you have to “work hard to get your thinking clean to make it simple.”

He taught us innovation doesn’t come from focus group, because “people don’t know what they want until you show it to them.”

He taught us that the “crazy ones, the misfits, the ones who think differently” can still “make a ding in the universe”.

Most importantly, he taught us “there is no reason not to follow your heart.”

Thank you, Steve. You’ll be missed.

He taught us “design is not just what it looks like and feels like. Design is how it works.” He taught us the mantra of “focus and simplicity”, that you have to “work hard to get your thinking clean to… Continue reading ‘Steve, thank you.’

3 Comments