A UI Design Pattern Love Song

Much has been written about the MVC but never, I believe, a love song … so here you go: a love song to the Model-View-Controller. 

This small experiment in erotic didacticism is sung to Some Enchanted Evening from Rogers and Hammerstein’s South Pacific.  I have embedded a video of Ezio Pinza and Mary Martin singing the original in case you need some help with the tune.

Some enchanted evening
You may see a pattern,
you may see a pattern
Across a crowded room
And somehow you know,
You know even then
That somewhere you’ll see it
Again and again.

Model-View-Controller
Someone may be coding
You may hear him coding
Across a crowded room
And night after night,
As strange as it seems
The shape of that pattern
Will drift through your dreams.

Who can explain it?
Who can tell you why?
Fools give you reasons,
Wise men never try.

Model-View-Controller
First it has a model,
Yes it has a model
To represent your store,
It’s your data store
Your persistence layer
Where all of your data
Is written and saved.

Model-View-Controller
Has a presentation,
And that presentation
Of state is called a View.
A view of your store
On your GUI layer
It’s bound to your model
And registers change.

Model-View-Controller
Third has a controller
That facilitates
Changes to model state
On your model layer
In response to acts
That come from your user
With keyboard or mouse.

Once you understand it,
Never let it go.
Once you understand it,
Never let it go!

Using statements with unknown types

I recently came across some interesting code in Juval Löwy’s Programming WCF Services and wanted to share.  It’s simply something I had never run across before:

 

            IMyContract proxy = new MyContractClient();

            using (proxy as IDisposable)

            {

                proxy.MyMethod();

            }

 

The first thing to notice is that the proxy object is instantiated outside of the using block.  I don’t think I’ve ever actually tried this, but it is perfectly permissible (if not recommended).  I used a dissembler to look at the IL this generates, and it is pretty much the same as instantiating the proxy object inside of the using brackets.  The main difference is that in this case, the scope of the proxy object extends beyond the using block.

Within the using brackets, this code casts the proxy object to the IDisposable interface so the Dispose method will be available.  Since a Using Block is basically syntactic sugar for a try-catch-finally structure that calls an object’s Dispose method in the finally block, the equivalent try-catch-finally block would look like this:

 

            IMyContract proxy = new MyContractClient();

            try

            {

                proxy.MyMethod();

            }

            finally

            {

                ((IDisposable)proxy).Dispose();

            }

 

However, Juval’s using statement does one additional thing.  It also checks to see if the proxy object even implements the IDisposable interface.  If it does, then the Dispose method is called on it.  If it does not, then nothing happens in the finally block.  The equivalent full blown code, then, would actually look something like this:

 

            IMyContract proxy = new MyContractClient();

            try

            {

                proxy.MyMethod();

            }

            finally

            {

                IDisposable disposable = proxy as IDisposable;

                if (disposable != null)

                {

                    disposable.Dispose();

                }

 

            }

 

… and we’ve condensed it to this …

 

            IMyContract proxy = new MyContractClient();

            using (proxy as IDisposable)

            {

                proxy.MyMethod();

            }

 

It’s probably not something that will come up too often, but if you have a situation in which you do not know whether an object implements IDisposable or not, but still want to implement a using block for readability and good coding practice, this is how you would go about doing it.  

Besides Juval’s proxy example, I can imagine it coming in handy when dealing with collections in which you don’t necessarily know whether all of the members of the collection implement IDisposable, for instance:

 

            foreach(IDog dog in myDogsCollection)

            {

                using (dog as IDisposable)

                {

                    dog.Bark();

                }

            }

 

It also just looks really cool.  h/t to Bill Ryan for pointing this out to me.