Decorator Pattern

I always enjoy good blog posts on patterns.  This week the Los Techies blogger Sean Chambers covers the decorator pattern as part of the Pablo’s Topic of the Month (PTOM).

As always Sean does a good job explaining how the decorator pattern works with code snippets as well.

My favorite part is the explanation of why to use the decorator pattern instead of just a traditional inheritance model:

This would work, however it’s not very desirable. Eventually as you add more and more permutations, (Coffee w/ Espresso, Coffee w/ Espress and Whip cream, etc..) it would lead to a class explosion and you would have a new class for every new combination of ingredients someone could have for a cup of coffee. A better option would be if we could add or "decorate" the original cost and name with our added options. Enter the Decorator Pattern.

Check it out!

Advertisements

System vs. Microsoft Namespace

Brad Abrams from MS is asking us how we view these namespaces

Personally, I would like to see, or envision the namespaces as being ‘core’ vs. ‘non-core’

For example, ‘core’ for me, System.* represents the namespaces in the .NET framework.  When I install the .NET 3.5 framework, I would expect all the namespaces to start with ‘System’ – CLR.

If there are external installs – ie. Silverlight, Ajax Toolkit, even the current ASP.NET MVC – I would expect all those to be ‘Microsoft’.  *If* later they are added to the framework – then it would be System.

ie. since WPF shipped in .NET 3.0 – all it’s namespaces would start with ‘System’ – but since Silverlight is not a part of a framework install – it would be Microsoft.Silverlight

I could then look at my references and clearly see what is a separate install vs. the core framework install.

My current asp.net mvc application would have a System.Web namespace and a Microsoft.Web.Mvc namespace.

VS 2008 Possible fix to an annoying bug

“visual studio: an error was encountered while opening associated documents during the last time this solution was loaded”

I get this after opening some solutions – the first thing, it just crashes to desktop when opening the solution.  Then I reopen it, and get the message from above.

After doing some googling, it was suggested in a forum to try deleting the .suo (hidden file) from the solution (goto the file system and show hidden files and delete it).

The suo holds local VS settings and can become corrupted, which can lead to problems.

So…let’s hope this resolves this annoying problem!

(it appears to happen to me when I have silverlight projects in my solution)

Interesting posts of the week

Couple of good reads I wanted to comment on this week:

1. Jeremy Miller on ‘Evolution of a Developer’:

Developers are all on a journey, some are comfortable with the ‘this is how I’ve always done it’ , others just want to ‘use a hammer’, others are progressing forward, asking questions on ‘is there a better way’, etc… I think it’s definitely an evolutionary process.  I know for myself, I can definitely relate to the progression.   But again, we are all on a journey with it – some in different spots.

I did chuckle with the line of "where should this code go?" – I know I’m always questioning it – sometimes I see it, sometimes I don’t.  The journey from ‘coder to craftsmanship’ is full of reward I think – and in the long run, it helps the industry. 

I’ve seen (and written!) code that was just thrown at a problem, and some would still justify it, but it’s costly and in the long run, whatever was gained by just slapping it together was lost later down the line.

I do caution though on the idea of ‘over engineering’  – but that is sometimes a hard line to draw and many would have differing views of what that is. 

2. Billy McCafferty on ‘A Gem of a Lesson from Rails’

I wondered what Billy was up to – as he talks about his Sharp Architecture for asp.net mvc in that post, I had assumed he was doing a asp.net mvc project  :)  So it was interesting to read his experience in working on a Ruby on Rails project.

These types of posts always inspire me to want to reach and learn other technologies and how they differ from what I’m doing.  It can be so easy to get locked into what I’m doing everyday.  ie. I have desired to learn more about WPF, yet at the same time, there is so much to learn and build in this asp.net mvc application that uses Windsor, NHibernate, jQuery, etc… that it barely allows me to keep my head above the water!

 

Thanks to Jeremy and Billy for their enlightening posts of the week – it’s the simple things that seem to inspire me the most!

ASP.NET MVC – Castle Binders from MvcContrib

Unfortunately, there is no wiki documentation, etc… on the Castle Binders in the MvcContrib.  So I thought I’d hop in and write up a sample for anyone else interested in using these binders:

First, for some background information from the Castle website : http://www.castleproject.org/monorail/documentation/trunk/usersguide/smartcontroller.html

These binders were originally created for Monorail, and outside of a few items, most of that documentation applies to using with MvcContrib.

I’m using the latest MvcContrib from the trunk (SVN) at :

http://mvccontrib.googlecode.com/svn/trunk

After building the MvcContrib, there assemblies referenced are from:

trunksrcMvcContrib.CastlebinRelease (I just added them all to a new Mvc project)

(I’ll include the source here if you just want to download and try it)

The goal of the binders is to provide the ability to include a strongly typed object as the recipient to a form post to an action.  As described in the document above, the default would be:

ie.

Person person = new Person();
person.FirstName = Request.Form[“FirstName”];

Step One: Create Controller implementing the ICastleBindingContainer interface

image

Step Two : Create Actions to handle the Form post

image

(note: the HttpVerbs allow you to specify one named Action – for different uses: the Get is for the form load, the Post is for the form post)

The post action of BindersView takes a ‘Person’ object.   We use the ‘CastleBind’ attribute to specify the binding.

There are a few optional parameters with the CastleBind: The prefix to use, and the Exclude option. 

image

This allows you to specify a prefix of any name in the form itself (I show form next).  The exclude allows for properties to not be included in the post request.

(For example, if you have a sensitive property of Person, ie. social security – you can exclude it from the binding)

Step Three : The Model

image 

Step Four : The Form

Let’s show the form now:

image

The first form fields represent the ‘Person’, and I’m also including form fields for the spouse to be added.

Note the ‘person.FirstName’ – the prefix is required.  Also, the Spouse object is referenced as ‘person.Spouse.FirstName’.  This will help instruct how to bind to the object.

(You can read more about this in the Castle documention  link above)

Let’s run it:

image

On save:

image

As you can see – it has bound to the person object as expected.

I hope this helps show, in a simple example, how to use the Castle Binders in MvcContrib.

(source here)

ASP.NET MVC – Castle Binders from MvcContrib

Unfortunately, there is no wiki documentation, etc… on the Castle Binders in the MvcContrib.  So I thought I’d hop in and write up a sample for anyone else interested in using these binders:

First, for some background information from the Castle website : http://www.castleproject.org/monorail/documentation/trunk/usersguide/smartcontroller.html

These binders were originally created for Monorail, and outside of a few items, most of that documentation applies to using with MvcContrib.

I’m using the latest MvcContrib from the trunk (SVN) at :

http://mvccontrib.googlecode.com/svn/trunk

After building the MvcContrib, there assemblies referenced are from:

trunk\src\MvcContrib.Castle\bin\Release (I just added them all to a new Mvc project)

(I’ll include the source here if you just want to download and try it)

The goal of the binders is to provide the ability to include a strongly typed object as the recipient to a form post to an action.  As described in the document above, the default would be:

ie.

Person person = new Person();
person.FirstName = Request.Form[“FirstName”];

Step One: Create Controller implementing the ICastleBindingContainer interface

image

Step Two : Create Actions to handle the Form post

image

(note: the HttpVerbs allow you to specify one named Action – for different uses: the Get is for the form load, the Post is for the form post)

The post action of BindersView takes a ‘Person’ object.   We use the ‘CastleBind’ attribute to specify the binding.

There are a few optional parameters with the CastleBind: The prefix to use, and the Exclude option. 

image

This allows you to specify a prefix of any name in the form itself (I show form next).  The exclude allows for properties to not be included in the post request.

(For example, if you have a sensitive property of Person, ie. social security – you can exclude it from the binding)

Step Three : The Model

image 

Step Four : The Form

Let’s show the form now:

image

The first form fields represent the ‘Person’, and I’m also including form fields for the spouse to be added.

Note the ‘person.FirstName’ – the prefix is required.  Also, the Spouse object is referenced as ‘person.Spouse.FirstName’.  This will help instruct how to bind to the object.

(You can read more about this in the Castle documention  link above)

Let’s run it:

image

On save:

image

As you can see – it has bound to the person object as expected.

I hope this helps show, in a simple example, how to use the Castle Binders in MvcContrib.

(source here)

Ayende’s NHibernate Profiler – early looks

Ayende has blogged about a commercial product he is creating : the NHibernate profiler.

I’m really looking forward to seeing this tool in action – one of the difficult parts of using an ORM  is tracking ‘how’ it is hitting the database, how sessions are created, for how long, etc…

Obviously, you can use SQL Profiler, but many times, your weeding through the calls, and getting all the visibility into the lifecycle’s of the sessions, etc…

But, go read  – he has good pictures up that show how it works.  🙂

I feel this tool will be a ‘must have’ for those using NHibernate on their projects.