Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Build your own CAB Part #1 - The Preamble

Yesterday I made a somewhat unsubstantiated claim that you simply don't need the Composite Application Block (CAB) to build nontrivial WinForms user interfaces in a maintainable fashion.  I claimed that mere mortal developers can master the underlying design patterns in the CAB and pick up a better Inversion of Control/Dependency Injection tool to organically grow an application structure that meets the specific needs of that application.  I even think that mere mortals can do this as or more efficiently as they can by utilizing the very generalized CAB.  To try to substantiate that claim, and because there seemed to be enough interest to warrant this series, I'm going to convert my WinForms patterns talk from DevTeach into a series of blog posts to demonstrate some design strategies for creating maintainable WinForms code.  Even if you're going to stick to the CAB (and that is a valid choice), I can hopefully add to your ability to use the CAB by exploring the very same patterns you'll find lurking underneath the covers in the CAB.  If you like the way the CAB does something better than the ways I present, please write in and say so.

One of my favorite authors is Alexandre Dumas, author of the Three Musketeers books and the Count of Monte Cristo.  The Three Musketeers was the soap opera of its day, written in installments as a serial in whatever passed for magazines in those days.  Likewise, I'm going to try to dribble these posts out in little installments.  Basically the size of post I can write in a single sitting on the train ride from and to work.

Note on terminology -- I'm a card carrying Fowlbot, so I'm using the terms set forth in Martin's forthcoming book on enterprise design patterns.

It's just the User Interface right?

"Just" the user interface?  I haven't seen this so much lately, but earlier in my career there was a definite attitude that User Interface (UI) programming was simple, grunt work -- unworthy of the attention of the serious developer.  In my mind I always think of the line "Just pick it up" from Caddyshack, or Kramer extolling Jerry to just "write it off."  That's a dangerous attitude because user interface code can easily be much more complex than the server side code in many systems.  It's worth spending some mental cycles on the design and structure of the UI code.  Here's why:

  • Creating user interface code is time intensive.  Not just in terms of the initial coding, but in my experience UI code generates the most defects and hence takes the most time to fix bugs.  We can cut down the defects by extending unit testing as far into the user interface code as possible.  TDD opponents often use the UI as an example of code that just can't be tested.  They're wrong, but UI testability doesn't come for free.  It takes some care to craft a structure that makes UI code easier to test.
  • User interface code interfaces with an extremely unpredictable external dependency -- those wacky users!  It takes a great deal of care to protect the system from garbage input and user error.
  • The User interface changes frequently.  Again, my experience is that it's much easier for a user and analysts to get the backend logic requirements upfront than it is to specify the user interface.  Making the UI code easier to change just makes sense, but that again takes some design cycles.
  • UI code is largely event driven, and event driven code can be nasty to debug.  Again, testability and a cleaner separation of concerns makes that debugging either go away or at least become easier.

A Note about Design Patterns

I know many people blow off design patterns as ivory tower twaddle and silly jargon, but I think they're very important in regards to designing user interface code.  Design patterns give us a common vocabulary that we can use in design discussions.  A study of patterns opens up the accumulated wisdom of developers who have come before us.  Finally, the design patterns we're going to examine in this series give us a framework of ideas on the best way to divide responsibilities in our user interface code.  That's a good thing because...

User Interface code contains a lot of different responsibilities

It was almost comical, but sitting in the Agile track at DevTeach almost every speaker, including me, had at least one slide dedicated to the Single Responsibility Principle (SRP).  Just to recap, the SRP states that any one class "should have only one reason to change."  Basically, you should strive for each class to contain a single coherent responsibility or concern.  Like I said before, UI code can easily become complex.  Just to prove that point, let's list some of the responsibilities that could easily be contained in a single screen:

  • The actual presentation to the user
  • Capturing to user input events
  • Input validation and business rules
  • Authorization rules
  • Customization
  • Screen flow and synchronization
  • Interaction with the rest of the application

Lots of important things.  Lots of things that would really be easier to code if I could just work on one thing at a time.  Things I'd really like to be able to test in isolation from one another.  Back to the Single Responsibility Principle, we can better enable change and improve maintainability in our UI code by assigning each of these responsibilities to distinct, loosely areas of the code.  What we need is some sort of divide and conquer strategy for our WinForms code.

Of course, a large problem in WinForms development today is that the most common pattern of assigning responsibilities is...

The Autonomous View

You might not be running around saying "I'm building an application on the Autonomous View pattern," but you'll recognize it in a heartbeat.  The autonomous view is a single piece of code that encapsulates both presentation and screen behavior in one single lump of code.  In WinForms the autonomous view is characterized by completely self-contained Form and UserControl's.  While it's perfectly suitable for simpler screens, it can present some maintainability issues for screens of even moderate complexity because of the mixture of concerns.  The biggest issue for me is that the code in an autonomous view is significantly harder to unit test than code in POCO classes.  It's also potentially harder to understand because so many concerns are intermingled with the WinForms machinery.  To some degree you can instantiate WinForms classes in memory and run unit tests against this code with or even without NUnitForms, but it's often much more trouble than it's worth. 

To explain what I mean by the difficulty in testing, let's look at this example..,

To be continued in Part 2:  Humble Views


Posted Mon, May 21 2007 9:25 PM by Jeremy D. Miller
Filed under:

[Advertisement]

Comments

Chris Holmes wrote re: Build your own CAB Part #1 - The Preamble
on Mon, May 21 2007 10:43 PM

Can't wait to read this series Jeremy. Looking forward to learning something new :-)

Jim Bonnie wrote re: Build your own CAB Part #1 - The Preamble
on Tue, May 22 2007 10:48 AM

Great stuff for someone like me struggling to move beyond Autonomous view!. FYI - not sure if your train ride takes you into CT, but we have a new user group in Stamford with some good sessions coming up.

www.fairfieldwestchester.net

Hope you can make it or give a talk!

Jeremy D. Miller wrote re: Build your own CAB Part #1 - The Preamble
on Tue, May 22 2007 11:01 AM

Is this the one Chris Shain was trying to set up?  I've been wanting something like this up here.  Going into events in Manhattan just gets you home way too late.  

I'll be there.  Just coincidentally, I live in Stamford too.

Shane Courtrille wrote re: Build your own CAB Part #1 - The Preamble
on Tue, May 22 2007 11:41 AM

Really looking forward to the rest of this series.  I've recently joined the.. for lack of a better term ALT or ALT.Net movement and am always looking for new things to move my knowledge forward.  Thanks for taking the time and effort to do this!

Eric wrote re: Build your own CAB Part #1 - The Preamble
on Tue, May 22 2007 12:08 PM

Also really looking forward to the rest of this series.Cant wait to get rid of Autonomous View Winforms UIs

Jeremy D. Miller -- The Shade Tree Developer wrote Build your own CAB Part #10 - Unit Testing the UI with NUnitForms
on Tue, Jun 26 2007 9:57 PM

I've long, long since left the rails of "Build your own CAB" topics and wandered off into

Jeremy D. Miller -- The Shade Tree Developer wrote Build your own CAB #11 - Event Aggregator
on Fri, Jun 29 2007 11:12 AM

I will finish "Build your own CAB" at least before Acropolis hits and makes it all obsolete

Jeremy D. Miller -- The Shade Tree Developer wrote Build your own CAB #13 - Embedded Controllers with a Dash of DSL
on Fri, Jul 6 2007 12:40 PM

Just to continue the world's longest run on sentence. Before I start, here's the table of contents

Insane World wrote The
on Wed, Jul 11 2007 10:16 AM

The

Jeremy D. Miller -- The Shade Tree Developer wrote Build your own CAB #14: Managing Menu State with MicroController's, Command's, a Layer SuperType, some StructureMap Pixie Dust, and a Dollop of Fluent Interface
on Tue, Jul 24 2007 5:18 AM

The title is a mouthful and accurately implies an alarmingly high jargon to code ration, but I just didn't

Jeremy D. Miller -- The Shade Tree Developer wrote The Build Your Own CAB Series Table of Contents
on Wed, Jul 25 2007 9:21 PM

Yes, this is overdue. Here is an introduction and table of contents to my "Build Your Own CAB"

Jeremy D. Miller -- The Shade Tree Developer wrote Build your own CAB #14: Managing Menu State with MicroController's, Command's, a Layer SuperType, some StructureMap Pixie Dust, and a Dollop of Fluent Interface
on Thu, Jul 26 2007 10:57 AM

The title is a mouthful and accurately implies an alarmingly high jargon to code ration, but I just didn't

Ellipse wrote Build your own CAB Part #2 - The Humble Dialog Box -- Jeremy D. Miller
on Tue, Sep 11 2007 6:20 AM
Trevor westerdahl wrote re: Build your own CAB Part #1 - The Preamble
on Mon, Oct 29 2007 8:23 PM

I hope you actually read this;

I was reading your post: "Build your own CAB #1 - The Preamble" and you did not provide any links to the next post in the sequence. Further, you don;t use categories, so I can;t find the other osts easily.

I just recommend that whenever you create a series that you also create appropriate link for Previous/ Next so that the readers can traverse through the series.

Right now I have to go through Google hell to find your other related articles.

Jeremy D. Miller wrote re: Build your own CAB Part #1 - The Preamble
on Mon, Oct 29 2007 8:39 PM

Trevor,

The table of contents is at codebetter.com/.../the-build-your-own-cab-series-table-of-contents.aspx

The tag is "Build your own CAB"

codebetter.com/.../default.aspx

Jeremy D. Miller -- The Shade Tree Developer wrote Resources from my DevTeach talks
on Thu, Nov 29 2007 12:03 PM

To everybody that attended one of my talks at DevTeach this week. All of the materials are now online

Bob on Medical Device Software » Blog Archive » Selecting a MVC/MVP Implementation for a Winforms Project wrote Bob on Medical Device Software » Blog Archive » Selecting a MVC/MVP Implementation for a Winforms Project
on Fri, Feb 1 2008 11:39 AM

Pingback from  Bob on Medical Device Software  » Blog Archive   » Selecting a MVC/MVP Implementation for a Winforms Project

Geek Daily » Blog Archive » Build your own CAB series wrote Geek Daily » Blog Archive » Build your own CAB series
on Mon, Feb 18 2008 11:13 AM

Pingback from  Geek Daily  » Blog Archive   » Build your own CAB series

江南白衣 wrote View to Presenter Communication(转)
on Sat, Aug 2 2008 7:40 AM

Alright, I've purposely hid the View to Presenter communication in my previous posts on Supervising Controller and Passive View because I thought that subject was worthy of its own post. As I see it, there are 2 1/2 basic ways to communicate screen events

Glenn Block wrote “MEF in 2 hours our less”, I am all for it.
on Wed, Dec 30 2009 7:37 PM

Some posts titles just really grab you. I couldn’t help but think about one of those annoying ads

Glenn Block wrote “MEF in 2 hours or less”, I am all for it.
on Wed, Dec 30 2009 7:39 PM

Some posts titles just really grab you. I couldn’t help but think about one of those annoying ads

Add a Comment

(required)  
(optional)
(required)  
Remember Me?