<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-26108077</atom:id><lastBuildDate>Fri, 05 Mar 2010 20:26:03 +0000</lastBuildDate><title>Becoming Agile</title><description>A journal of lean thinking, Agile principles and some fancy code work</description><link>http://blog.gravityfree.ca/</link><managingEditor>noreply@blogger.com (Neil Bourgeois)</managingEditor><generator>Blogger</generator><openSearch:totalResults>30</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/blogspot/QHdC" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="blogspot/qhdc" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-1072570993680301353</guid><pubDate>Sun, 15 Mar 2009 20:54:00 +0000</pubDate><atom:updated>2009-03-15T15:09:30.693-06:00</atom:updated><title>Message Passing</title><description>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mitpress.mit.edu/sicp/full-text/book/cover.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 200px; height: 290px;" src="http://mitpress.mit.edu/sicp/full-text/book/cover.jpg" alt="" border="0" /&gt;&lt;/a&gt;After a week of hiatus from SICP I opened up the book to another blatantly simple yet profoundly important concept: Message Passing.&lt;br /&gt;&lt;br /&gt;Essentially the authors talk about passing around "intelligent data objects" that can dispatch other procedures based on the operation name.  The implementation in LISP is simply passing a procedure around that accepts the name of the required operation and through a switch calls the appropriate behavior implementation.&lt;br /&gt;&lt;br /&gt;This technique is called "Message Passing" and seems eerily familiar to the way methods are dispatched in OO.&lt;br /&gt;&lt;br /&gt;Read for yourself by following the link and scrolling to the bottom:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-17.html#%_sec_2.4.3"&gt;http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-17.html#%_sec_2.4.3&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-1072570993680301353?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2009/03/message-passing.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-8279532253285202485</guid><pubDate>Fri, 20 Feb 2009 20:44:00 +0000</pubDate><atom:updated>2009-02-20T15:57:48.139-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">books software programming abstraction ddd agile</category><title>Abstraction: The Sunshine of your Code</title><description>&lt;a href="http://mitpress.mit.edu/sicp/full-text/book/cover.jpg"&gt;&lt;img style="MARGIN: 0px 0px 10px 10px; WIDTH: 200px; FLOAT: right; HEIGHT: 290px; CURSOR: hand" border="0" alt="" src="http://mitpress.mit.edu/sicp/full-text/book/cover.jpg" /&gt;&lt;/a&gt;As part of a "back to basics" kick I'm currently reading &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book.html"&gt;Structure and Interpretation of Computer Programs&lt;/a&gt;. This book is a gem and although I'm only through the first two chapters I've been fascinated several times by fundamental and obvious programming concepts that I seem to have overlooked as my career progressed beyond those carefree University days. I'm particularly absorbed in the author's consideration of abstraction.&lt;br /&gt;&lt;br /&gt;Abstraction is something I have personally taken for granted even in University. Recalling lectures where profs used analogies of the abstraction of cars or TVs I approached the topic with the blasé "Ya, ya, ya we get it... lets get to the code!" attitude. Now seven years later this book has caused me to reflect upon abstraction and realize that it is the fundamental ingredient of nearly every advancement in the construction of software.&lt;br /&gt;&lt;br /&gt;The core idea is in creating Abstraction Barriers to speak a different language in each layer. In doing so, we can work most efficiently with the software itself and more importantly with other humans (of which most are computer language illiterate). To steal the authors' &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_sec_2.1.2"&gt;example &lt;/a&gt;at the highest level we can all speak about rational numbers, create them, add, subtract and multiply them. Anyone can "program" a calculator to make rational numbers dance. But there's a special class of people we try to avoid at parties who know that a rational number is comprised of a numerator and a denominator. Further there are the computer scientists who would look at a rational number as some kind of pairing of two integers. Now consider that each computer scientist may choose to implement a rational number a different way. Does this change the way the mathemetician's work with numerators and denominators? Does it change the way the average person does math? Absolutely not. But it may be the reason us computer science nerds don't even get invited to those parties.&lt;br /&gt;&lt;br /&gt;In essence Abstraction Barriers are the driving force beneath Design by Contract and Interface Driven Design. Object Oriented languages are based on abstraction as there is no inheritance or polymorphism with out it. Even the often abused principle of encapsulation is about protecting the abstraction barrier. Abstraction is the foundation of every OO design pattern ever invented as a design pattern is just summarizing algorithms into a vocabulary. Even the cornucopia of patterns that emerged from Fowler's PoEAA and Evan's Domain Driven Design are again just abstraction barriers applied.&lt;br /&gt;&lt;br /&gt;Consider, for example, software engineering tenants such those found in the Domain Driven Design camp which are some of the most powerful tools introduced to software engineering. Layered architectures and Bounded Contexts compartmentalize subsets of Ubiquitous Language into abstraction layers. Each layer or bounded context has a particular language used throughout. For example, Persistence Ignorance is keeping the implementation of physically storing data out of the realm of those who speak about domain and into the realm of those who speak about databases. Knowing the power of abstraction its possible to take the fundamental concept and apply it to your own design techniques.&lt;br /&gt;&lt;br /&gt;Quite a while ago I learned the exercise of anthropomorphising software components to help break down software design. I've just recently started to consider abstraction barriers as part of the excercise with great success. For instance, I'm always running into the nagging question of "Where does this particular piece of code belong?". Now, while playing the part of each object in the orchestration I pay attention to the language being used (that is the verbs and nouns of each component's vocabulary) along with the data in play and try to find the lines between abstraction layers. It becomes quite obvious when you've crossed a barrier as the language will change and finding where a piece of code belongs is a simple as identifying components that have the same vocabulary.&lt;br /&gt;&lt;br /&gt;For example, on my current project we have a 3D control that works in terms of Geometries. We also have components that can be drawn in this control such as Pipes, Elbows and Tees. The 3D control works entirely with Geometries knowing nothing about Pipes, Elbows or Tees. Conversely Pipes, Elbows and Tees know nothing about Geometries. Here we have two abstraction layers, lets call them the 3D layer and the Domain layer. If I find myself with a piece of code that does something with Geometries its fairly obvious that it should be grouped with the 3D layer. Or more likely I'm tempted to throw some code that works specifically with Pipes on the 3D control. During an intense anthropomorphise excersise this would raise a red flag as the 3D layer doesn't include Pipes in it's vocabulary. I should relocate that code to a service in the Domain layer so the language is consistent. I could then inject the service into the 3D control so it can call on the service to achieve its goal.&lt;br /&gt;&lt;br /&gt;As you can see abstraction is a powerful tool for software engineers, and definitely not a concept to be overlooked. So for you kids out there, don't be like me. Treat abstraction with the respect it deserves. For those out in the industry who have forgotten about it whether you read SCIP or not give abstraction a second thought.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;If you're interested you can also watch the videos at &lt;/em&gt;&lt;a href="http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/"&gt;&lt;em&gt;http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. They're worth a viewing if at least for a chuckle at the stylish '80s stashes.&lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-8279532253285202485?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2009/02/abstraction-sunshine-of-your-code.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-8512516393629471827</guid><pubDate>Sat, 14 Feb 2009 00:37:00 +0000</pubDate><atom:updated>2009-02-15T15:01:01.401-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">subversion feature branch software development</category><title>Using Feature Branches</title><description>One of the biggest changes we've made to our development process in the past month was to introduce feature branches.  Historically our team ran with two branches: the trunk and a production maintenance branch.  All new development occurred on the trunk while bug fixes were committed to the maintenance branch and merged down to the trunk.  This was a somewhat successful approach in that it's simplicity was perfect for a team that had little experience with version control but it suffered one annoying problem: the trunk was largely unstable.&lt;br /&gt;&lt;br /&gt;The first indication that we had a problem was that deployment became a timely event.   We would wait until there was low risk that the trunk had bugs in it, close our eyes, pray to our respective gods and slam our fists on the big red deploy button.  Sure enough there'd be a partially completed or untested feature in the package that would cause problems down the line.&lt;br /&gt;&lt;br /&gt;This led to Code Freezes and the Scheduled Commits anti-patterns.   Where around deploy time we'd have to postpone the risky commits in favor of the solid bug fixes.  From a lean perspective this is a huge waste as there was code just waiting to be committed to the repository and a developer surfing Facebook waiting for Cruise Control to scream out "Woo hoo!".&lt;br /&gt;&lt;br /&gt;So, we decided to try using feature branches for new development.  A &lt;a href="http://svnbook.red-bean.com/en/1.5/svn-book.html#svn.branchmerge.commonpatterns.feature"&gt;feature branch&lt;/a&gt; is simply a branch that has all of the changes from the trunk plus all of the changes required to implement a particular feature.  When the feature has been tested and is ready to be released we merge the branch back to the trunk and delete it.  The end result is that the trunk stays stable even while new development is occurring because the feature branches are the ones assuming the instability.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Remember:&lt;/span&gt; &lt;/span&gt;&lt;a style="font-style: italic;" href="http://www.ericsink.com/scm/scm_branches.html"&gt;Branches are like puppies&lt;/a&gt;&lt;span style="font-style: italic;"&gt; so don't create a branch unless you're prepared to take care of it.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The maintenance costs of  a branch are that every day or two a developer has to merge all of the changes from the trunk up to the branch.  With Subversion 1.5 this is simple task but you should be aware that it could result in a couple of problems.  First, you will likely run into situations where a single file changes on both the trunk and the branch resulting in a conflict.  These conflicts are no different than the conflicts you receive while doing an svn update and can be resolved as such.  Another more severe problem is that changes can be lost if a file is removed on the trunk but changed on the branch.  Unfortunately there is no easy way to detect this other than to scan your merge log for deleted files.  I have not yet personally experienced this problem but I am hoping that if it does occur my test fixture will catch it.  Finally, performing a large merge is much harder than small incremental merges, so merge often!  Depending on your team size and the amount of change on the trunk you'll want to tweak your merge frequency.  My team is currently merging every day or every other day depending on the state of the trunk.  Plus we always make sure to merge the trunk to each feature branch after another feature branch has been merged down to the trunk.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Remember:  &lt;/span&gt;&lt;span style="font-style: italic;"&gt;Merging works with a range of revisions so when merging from the trunk&lt;/span&gt; to the branch you need to include &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;ALL&lt;/span&gt;&lt;span style="font-style: italic;"&gt; revisions since the last merge not just the HEAD revision.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Subversion mergeinfo tracking in SVN version 1.5 makes feature branches a breeze.  I remember using feature branches under early SVN builds (and even CVS &lt;gag&gt;) where we would have to track which versions were brought up to the trunk on an index card so we wouldn't duplicate efforts.  Now the mergeinfo is tracked inside of SVN properties and SVN is smart enough to handle these tedious tasks for us.  So just make sure your working directory is clean and from it run:&lt;br /&gt;&lt;pre class="screen"&gt;&lt;span style="font-size:130%;"&gt;svn merge svn://svnserver/myrepository/myproject/trunk&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;Sometimes, however, you will want to know which revisions have been applied.  In this case you can run the following command from your branch working directory to get a list of revisions merged up to the branch:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;svn mergeinfo svn://svnserver/myrepository/myproject/trunk&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Similarly you may want to know which trunk revisions are eligible to be merged onto the branch.  In this case you can run:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;svn mergeinfo svn://svnserver/myrepository/myproject/trunk --show-revs eligible&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Remember: &lt;/span&gt;&lt;span style="font-style: italic;"&gt;A small incremental merge is far easier than a massive merge, so merge often!  Don't wait until the end of the feature to sync up your branch&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;  &lt;span style="font-style: italic;"&gt;You &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;WILL&lt;/span&gt;&lt;span style="font-style: italic;"&gt; feel pain.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The final task in feature branch maintenance is after the feature has been developed you will need to merge it down to the trunk.  This is a step in our Kanban that occurs after Peer Testing but before UAT.  In other words we are committing zero defects (as far as we know) to the trunk.  Since our branch already contains all the changes from the trunk that occurred during the development of the feature the only changes that need to be merged down are the ones that belong to new feature.  The beauty of keeping your branch in sync is that this happens natually in SVN 1.5 by running the merge command with the reintegrate flag from a clean trunk working directory:&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="screen"&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:courier new;"&gt;svn merge --reintegrate svn://svnserver/myrepository/myproject/branch&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;Once the branch has been merged down to the trunk you will want to make sure that no more change occurs on it.  This is accomplished by simply deleting the branch.  It will remain in your repository so you can always browse back to find those changes but developers will not be able to check it out and commit against it.&lt;br /&gt;&lt;br /&gt;Thats about all the mechanics there is to feature branches.  But the question remains how do we know when we need to create a new feature branch?  This is more of a fuzzy area.  Some teams branch for every Story but the granularity of these branches is really dependant on what and how often you will be releasing.  In general my team will create a feature branch based off a portion of a Stories in a Theme or Epic that should be released together.  We call these chunks Minimally Markettable Features and we choose each one so it takes about an iteration to complete.  So our feature branches deliver a useful feature to the business every 2 weeks.&lt;br /&gt;&lt;br /&gt;The early results show that feature branches have greatly improved our ability to deliver stable releases and in turn make our client much happier.  Our testing efforts are far more efficient since UAT now contains stable changes and there is no waste in developer cycles due to Code Freezes or Scheduled Commits.  The downside is that feature branches require much more SVN savvy.  To successfully implement feature branches your development team should be fairly experienced with version control systems.  If your team is struggling to manage a trunk and a single maintenance branch then I would recommend mastering that before using feature branches.&lt;br /&gt;&lt;br /&gt;If, however, you feel your team is ready for the next step and would like to implement feature branches I highly recommend reading the branching topics in the &lt;a href="http://svnbook.red-bean.com/"&gt;Subversion Book&lt;/a&gt;.  Also get your hands dirty and play around with branches in a sandbox svn repository.  You'll quickly get a feel for how they work and what they're all about.  Lastly, drop me a line if you're have any questions.&lt;br /&gt;&lt;br /&gt;&lt;/gag&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-8512516393629471827?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2009/02/using-feature-branches.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-7674078037007938626</guid><pubDate>Sun, 18 Jan 2009 15:57:00 +0000</pubDate><atom:updated>2009-01-18T10:15:23.718-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">agile</category><category domain="http://www.blogger.com/atom/ns#">kanban</category><category domain="http://www.blogger.com/atom/ns#">lean</category><title>Becoming Lean</title><description>I've spent the past three years building up an internal software development team.  This team was built with a foundation of principles instilled by XP-styled Agile (or as they were ingrained in me by ThoughtWorks).  The team has changed dramatically over the three years, ever increasing in size, skill and agility.  We are currently running with what I would consider to be a dream team.  Each team member is a skilled and experienced generalist and everyone on the team either has 2+ years of agile experience or is an apt and enthusiastic participant in their own learning process.  I couldn't be more excited about our future prospects and what we will deliver to the business in the coming months.&lt;br /&gt;&lt;br /&gt;But even fielding this roster we experience some Agile anti-patterns that turned into pains for the team.  An analysis of these anti-patterns is leading us towards a Lean Kanban style and away from the traditional Agile.  I know there is not much out there about making this transition so I will try to document as much as I can to help others who are attempting the same thing.&lt;br /&gt;&lt;br /&gt;The first of our pains was that we could not get a stable velocity.  It was not uncommon for us to have a huge variance between iterations... embarrasingly so.&lt;br /&gt;&lt;br /&gt;Iterations always seemed to result in hangover and the perpetual work in progress made testing very difficult.  Not to mention cutting releases was difficult when they contained half completed work.&lt;br /&gt;&lt;br /&gt;When we switching into maintenenance mode between phases everything went out the window.  We moved into chaos mode and during this time there was some team churn (for the better) that changed the entire dynamics of the team so any historical metrics were rendered useless.  Not to mention our rhythm, and I believe the inertia behind a strong rhythm is important to a dev team.&lt;br /&gt;&lt;br /&gt;Finally, while our development team is Agile the business is hunkered down in a Waterfall mindset.  They fear fast and cyclical releases opting for several big-bang releases per year.  Admittedly we haven't done much to quell those fears since the work in progress problem plagues our own testing process.  But even if those fears were gone the business has a gated approval process better suited for the space shuttle than it is for software.&lt;br /&gt;&lt;br /&gt;We tried to band-aide these pains.  For example we tried to fix the varying velocity by accounting for half-completed stories (Story A is %65 complete) to give a more realistic velocity.  We injected empty buffer iterations to allow for work in progress completion and stabilization.  We tried to estimate bugs and maintenenance work but unfortunately metrics taken during this span were used to estimate the schedule dates for future development work.  As for the waterfall business problem there was unfortunately nothing we could do to reduce the release cycles.  Perhaps the real problem is that changing the business is beyond the team's sphere of influence at this point and we have to earn the business' trust before that can be fixed.&lt;br /&gt;&lt;br /&gt;After looking at these problems from a root-cause perspective we have decided to focus on the following principles:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Reduce work in progress&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Zero defect mentality&lt;/li&gt;&lt;li&gt;Kaizen&lt;/li&gt;&lt;/ol&gt;We've also decided to switch to an iterationless Kanban system because it more closely represents the way we are working.   In a subsequent post I will go through these principles and describe how we expect them to work in concert with Kanban to improve our process.&lt;br /&gt;&lt;br /&gt;To be continued...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-7674078037007938626?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2009/01/becoming-lean.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-1127666146031608247</guid><pubDate>Sat, 01 Nov 2008 16:46:00 +0000</pubDate><atom:updated>2008-11-01T10:50:30.217-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ClickOnce</category><title>ClickOnce in General</title><description>I've had many emails lately about my &lt;a href="http://blog.gravityfree.ca/2006/07/click-once-deployment-using-nant.html"&gt;ClickOnce deployment using NAnt&lt;/a&gt; post so I thought I'd post a little bit about how ClickOnce works in general.&lt;br /&gt;&lt;br /&gt;First, there are two files necessary to deploy a ClickOnce app:  the deployment manifest and the application manifest.  The deployment manifest contains version specific information about your app.  This is the file that makes version 1.2 different from 1.3.  You should have one deployment manifest for every version you deploy.&lt;br /&gt;&lt;br /&gt;The application manifest, however, is really the main entry point into ClickOnce.  It contains information about which version is the most recent (eg.  1.3) and redirects the client to the appropriate deployment manifest.  You will have just a single application manifest for your project.  It is the URL of the application manifest you should give to your users so they can open the application.&lt;br /&gt;&lt;br /&gt;The tricky thing about ClickOnce manifests is that in addition to containing a list of files necessary to run an application they contain a hash of each file.  This hash is like a digital signature and used to prevent ClickOnce from delivering malicious files.  So, any time you release a new version (or change your ClickOnce payload) you must do the following (using Mage):&lt;br /&gt;&lt;br /&gt;1.  Create a new deployment manifest (for the new version)&lt;br /&gt;2.  Sign the deployment manifest (to regenerate the hash of each file)&lt;br /&gt;3.  Update your application mainfest (so it points to the new deployment manifest from 1)&lt;br /&gt;4.  Sign your application manifest&lt;br /&gt;&lt;br /&gt;Once that is done you're pretty much good to go :-)&lt;br /&gt;&lt;br /&gt;Also, for further reading my colleague Jonas Avellana has a great post on setting up &lt;a href="http://blog.ezed.ca/2008/06/clickonce-and-multiple-environments.html"&gt;ClickOnce to deploy to multiple environments&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-1127666146031608247?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/11/clickonce-in-general.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-3287521006774587738</guid><pubDate>Fri, 06 Jun 2008 02:36:00 +0000</pubDate><atom:updated>2008-06-05T21:32:27.444-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">binsor boo</category><title>Binsor Method Interceptor Macro</title><description>So, I just picked up a story that is one of those really annoying feature requests-- you know the kind: adding log statements to analyze performance of about 40 key use cases scattered all over the app.  Its the kind of story every developer dreads and the worst part is that it is throw away code.  In a week when data has been collected there will be another story coming down the pipe to revert those changes.  While I could've spent the day hunting down the 40 culprits and performing mind numbing cut and paste code injection I decided instead to attempt an implementation that would stick around and have potential uses in other scenarios.&lt;br /&gt;&lt;br /&gt;Since our application uses &lt;a href="http://castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt; entirely configured through Ayende's &lt;a href="http://www.ayende.com/Blog/category/451.aspx"&gt;Binsor&lt;/a&gt; scripts I figured I'd take a stab at writing my own binsor macro for interceptors.  The functionality I required was that only specific methods of a service need to be logged for time and occasionally other contextual information dumped.&lt;br /&gt;&lt;br /&gt;I know I could perform the same action by creating a class that implements IInterceptor and registering it in Binsor like:&lt;br /&gt;IoC.Container.Kernel.GetHandler('textProvider').ComponentModel.Interceptors.Add(InterceptorReference(MyInterceptor))&lt;br /&gt;&lt;br /&gt;But I wanted something more flexible.  Ultimately I want to be able to craft a custom binsor config file and not have to perform any code changes.  This way when the story comes down the pipe to revert changes it is as simple as replacing a config file.   Additionally having the ability to throw interceptors on specific methods is an enticing feature should we ever need to peer down the business end of the app running in production.&lt;br /&gt;&lt;br /&gt;The syntax I was after looked something like this:&lt;br /&gt;&lt;br /&gt;component 'textProvider', ITextProvider, HelloWorldProvider:&lt;br /&gt;intercept 'GetText':&lt;br /&gt;  Log.Info('Invoking: ' + invocation.Method.Name + ' at ' + DateTime.Now())&lt;br /&gt;  stopwatch.Start()&lt;br /&gt;  invocation.Proceed()&lt;br /&gt;  Log.Info('Duration: ' + stopwatch.Stop())&lt;br /&gt;&lt;br /&gt;Make sense?  If not, I'll paraphrase in English:  I want to intercept the GetText method on my ITextProvider service and perform the block of code that logs the invocation, lets the invocation proceed and then logs the duration of the invocation.  Essentially my goal is to inject that block of code as a delegate into a class implementing IInterceptor.   Its actually easier than it sounds!&lt;br /&gt;&lt;br /&gt;The first step was to create the MethodInterceptor.  This is the class that implements IInterceptor:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20    \cf3 public\cf0  \cf3 class\cf0  \cf4 MethodInterceptor\cf0  : \cf4 IInterceptor\par ??\cf0     \{\par ??        \cf3 public\cf0  \cf3 delegate\cf0  \cf3 void\cf0  \cf4 InterceptHandler\cf0 (\cf4 IInvocation\cf0  invocation);\par ??\par ??        \cf3 private\cf0  \cf3 readonly\cf0  \cf3 string\cf0  methodName;\par ??        \cf3 private\cf0  \cf3 readonly\cf0  \cf4 InterceptHandler\cf0  interceptHandler;\par ??\par ??        \cf3 public\cf0  MethodInterceptor(\cf3 string\cf0  methodName, \cf4 InterceptHandler\cf0  interceptHandler)\par ??        \{\par ??            \cf3 this\cf0 .methodName = methodName;\par ??            \cf3 this\cf0 .interceptHandler = interceptHandler;\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf3 void\cf0  Intercept(\cf4 IInvocation\cf0  invocation)\par ??        \{\par ??            \cf3 if\cf0 (methodName.Equals(invocation.Method.Name))\par ??                interceptHandler(invocation);\par ??            \cf3 else\par ??\cf0                 invocation.Proceed();\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;   &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MethodInterceptor&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptor&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;delegate&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;InterceptHandler&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IInvocation&lt;/span&gt; invocation);&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; methodName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;InterceptHandler&lt;/span&gt; interceptHandler;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; MethodInterceptor(&lt;span style="color:blue;"&gt;string&lt;/span&gt; methodName, &lt;span style="color: rgb(43, 145, 175);"&gt;InterceptHandler&lt;/span&gt; interceptHandler)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;this&lt;/span&gt;.methodName = methodName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;this&lt;/span&gt;.interceptHandler = interceptHandler;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Intercept(&lt;span style="color: rgb(43, 145, 175);"&gt;IInvocation&lt;/span&gt; invocation)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(methodName.Equals(invocation.Method.Name))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                interceptHandler(invocation);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                invocation.Proceed();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Notice the delegate?  Thats the magic... otherwise, its pretty straight forward.&lt;br /&gt;&lt;br /&gt;Step 2 is to create something that will do the Windsor configuration for us.  This class needs to:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Add a new component for the interceptor&lt;/li&gt;&lt;li&gt;Add the dependency values to the interceptor component's configuration&lt;/li&gt;&lt;li&gt;Register the interceptor component as an interceptor on the parent component&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;If you are unclear about interceptors in Windsor I suggest reading up on them in the Windsor documentation.&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20    \cf3 public\cf0  \cf3 class\cf0  \cf4 MethodInterceptExtension\cf0  : \cf4 IComponentExtension\par ??\cf0     \{\par ??        \cf3 private\cf0  \cf3 const\cf0  \cf3 string\cf0  INTERCEPTOR = \cf5 "interceptor"\cf0 ;\par ??        \cf3 private\cf0  \cf3 const\cf0  \cf3 string\cf0  INTERCEPTORS = \cf5 "interceptors"\cf0 ;\par ??        \cf3 private\cf0  \cf3 readonly\cf0  \cf4 MethodInterceptor\cf0 .\cf4 InterceptHandler\cf0  interceptHandler;\par ??        \cf3 private\cf0  \cf3 readonly\cf0  \cf3 string\cf0  methodName;\par ??\par ??        \cf3 public\cf0  MethodInterceptExtension(\cf3 string\cf0  methodName, \cf4 MethodInterceptor\cf0 .\cf4 InterceptHandler\cf0  interceptHandler)\par ??        \{\par ??            \cf3 this\cf0 .methodName = methodName;\par ??            \cf3 this\cf0 .interceptHandler = interceptHandler;\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf3 void\cf0  Apply(\cf4 Component\cf0  component)\par ??        \{\par ??            \cf3 var\cf0  interceptorKey = component.Name + methodName + \cf5 "Interceptor"\cf0 ;\par ??            \cf4 AbstractConfigurationRunner\cf0 .IoC.Container.Kernel.AddComponent(interceptorKey, \cf3 typeof\cf0  (\cf4 MethodInterceptor\cf0 ));\par ??            \cf4 AbstractConfigurationRunner\cf0 .IoC.Container.Kernel.GetHandler(interceptorKey).AddCustomDependencyValue(\cf5 "methodName"\cf0 , methodName);\par ??            \cf4 AbstractConfigurationRunner\cf0 .IoC.Container.Kernel.GetHandler(interceptorKey).AddCustomDependencyValue(\cf5 "interceptHandler"\cf0 , interceptHandler);\par ??            AddInterceptorConfigItem(FindOrCreateInterceptorsConfigItem(component), interceptorKey);\par ??        \}\par ??\par ??        \cf3 private\cf0  \cf3 static\cf0  \cf4 IConfiguration\cf0  FindOrCreateInterceptorsConfigItem(\cf4 Component\cf0  component)\par ??        \{\par ??            \cf3 foreach\cf0  (\cf4 IConfiguration\cf0  configChild \cf3 in\cf0  component.Configuration.Children)\par ??            \{\par ??                \cf3 if\cf0  (INTERCEPTORS.Equals(configChild.Name))\par ??                    \cf3 return\cf0  configChild;\par ??            \}\par ??            \cf3 var\cf0  interceptorsConfig = \cf3 new\cf0  \cf4 MutableConfiguration\cf0 (INTERCEPTORS);\par ??            component.Configuration.Children.Add(interceptorsConfig);\par ??            \cf3 return\cf0  interceptorsConfig;\par ??        \}\par ??\par ??        \cf3 private\cf0  \cf3 static\cf0  \cf3 void\cf0  AddInterceptorConfigItem(\cf4 IConfiguration\cf0  parent, \cf3 string\cf0  interceptorKey)\par ??        \{\par ??            parent.Children.Add(\cf3 new\cf0  \cf4 MutableConfiguration\cf0 (INTERCEPTOR, \cf5 "$\{"\cf0  + interceptorKey + \cf5 "\}"\cf0 ));\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;   &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MethodInterceptExtension&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;IComponentExtension&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; INTERCEPTOR = &lt;span style="color: rgb(163, 21, 21);"&gt;"interceptor"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; INTERCEPTORS = &lt;span style="color: rgb(163, 21, 21);"&gt;"interceptors"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MethodInterceptor&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;InterceptHandler&lt;/span&gt; interceptHandler;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; methodName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; MethodInterceptExtension(&lt;span style="color:blue;"&gt;string&lt;/span&gt; methodName, &lt;span style="color: rgb(43, 145, 175);"&gt;MethodInterceptor&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;InterceptHandler&lt;/span&gt; interceptHandler)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;this&lt;/span&gt;.methodName = methodName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;this&lt;/span&gt;.interceptHandler = interceptHandler;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Apply(&lt;span style="color: rgb(43, 145, 175);"&gt;Component&lt;/span&gt; component)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;var&lt;/span&gt; interceptorKey = component.Name + methodName + &lt;span style="color: rgb(163, 21, 21);"&gt;"Interceptor"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;AbstractConfigurationRunner&lt;/span&gt;.IoC.Container.Kernel.AddComponent(interceptorKey, &lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;MethodInterceptor&lt;/span&gt;));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;AbstractConfigurationRunner&lt;/span&gt;.IoC.Container.Kernel.GetHandler(interceptorKey).AddCustomDependencyValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"methodName"&lt;/span&gt;, methodName);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;AbstractConfigurationRunner&lt;/span&gt;.IoC.Container.Kernel.GetHandler(interceptorKey).AddCustomDependencyValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"interceptHandler"&lt;/span&gt;, interceptHandler);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            AddInterceptorConfigItem(FindOrCreateInterceptorsConfigItem(component), interceptorKey);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IConfiguration&lt;/span&gt; FindOrCreateInterceptorsConfigItem(&lt;span style="color: rgb(43, 145, 175);"&gt;Component&lt;/span&gt; component)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;IConfiguration&lt;/span&gt; configChild &lt;span style="color:blue;"&gt;in&lt;/span&gt; component.Configuration.Children)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt; (INTERCEPTORS.Equals(configChild.Name))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;return&lt;/span&gt; configChild;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;var&lt;/span&gt; interceptorsConfig = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MutableConfiguration&lt;/span&gt;(INTERCEPTORS);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            component.Configuration.Children.Add(interceptorsConfig);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; interceptorsConfig;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; AddInterceptorConfigItem(&lt;span style="color: rgb(43, 145, 175);"&gt;IConfiguration&lt;/span&gt; parent, &lt;span style="color:blue;"&gt;string&lt;/span&gt; interceptorKey)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            parent.Children.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MutableConfiguration&lt;/span&gt;(INTERCEPTOR, &lt;span style="color: rgb(163, 21, 21);"&gt;"${"&lt;/span&gt; + interceptorKey + &lt;span style="color: rgb(163, 21, 21);"&gt;"}"&lt;/span&gt;));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Finally we need to create the actual Macro.  Binsor has a very handy base class called BaseBinsorExtensionMacro that I will inherit from so most of the heavy lifting is done for me&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20    \cf3 public\cf0  \cf3 class\cf0  \cf4 InterceptMacro\cf0  : \cf4 BaseBinsorExtensionMacro\cf0 &lt;\cf4 MethodInterceptExtension\cf0&gt;\par ??    \{\par ??        \cf3 public\cf0  InterceptMacro() : \cf3 base\cf0 (\cf5 "intercept"\cf0 , \cf3 false\cf0 , \cf3 new\cf0 [] \{\cf5 "component"\cf0 \})\par ??        \{\par ??        \}\par ??\par ??        \cf3 protected\cf0  \cf3 override\cf0  \cf3 bool\cf0  ExpandExtension(\cf3 ref\cf0  \cf4 MethodInvocationExpression\cf0  extension, \cf4 MacroStatement\cf0  macro, \cf4 MacroStatement\cf0  parent, \cf3 ref\cf0  \cf4 Statement\cf0  expansion)\par ??        \{\par ??            extension.Arguments.Add(macro.Arguments[0]);\par ??            extension.Arguments.Add(BuildInterceptHandler(macro));\par ??            \cf3 return\cf0  \cf3 true\cf0 ;\par ??        \}\par ??\par ??        \cf3 private\cf0  \cf3 static\cf0  \cf4 Expression\cf0  BuildInterceptHandler(\cf4 MacroStatement\cf0  macro)\par ??        \{\par ??            \cf3 var\cf0  e = \cf3 new\cf0  \cf4 BlockExpression\cf0 ();\par ??            e.Parameters.Add(\cf3 new\cf0  \cf4 ParameterDeclaration\cf0 (\cf5 "invocation"\cf0 , \cf3 new\cf0  \cf4 SimpleTypeReference\cf0 (\cf5 "Castle.Core.Interceptor.IInvocation"\cf0 )));\par ??            e.Body = macro.Block;\par ??            \cf3 return\cf0  e;\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;   &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;InterceptMacro&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;BaseBinsorExtensionMacro&lt;/span&gt;&lt;&lt;span style="color: rgb(43, 145, 175);"&gt;MethodInterceptExtension&lt;/span&gt;&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; InterceptMacro() : &lt;span style="color:blue;"&gt;base&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"intercept"&lt;/span&gt;, &lt;span style="color:blue;"&gt;false&lt;/span&gt;, &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {&lt;span style="color: rgb(163, 21, 21);"&gt;"component"&lt;/span&gt;})&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; ExpandExtension(&lt;span style="color:blue;"&gt;ref&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MethodInvocationExpression&lt;/span&gt; extension, &lt;span style="color: rgb(43, 145, 175);"&gt;MacroStatement&lt;/span&gt; macro, &lt;span style="color: rgb(43, 145, 175);"&gt;MacroStatement&lt;/span&gt; parent, &lt;span style="color:blue;"&gt;ref&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Statement&lt;/span&gt; expansion)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            extension.Arguments.Add(macro.Arguments[0]);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            extension.Arguments.Add(BuildInterceptHandler(macro));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; BuildInterceptHandler(&lt;span style="color: rgb(43, 145, 175);"&gt;MacroStatement&lt;/span&gt; macro)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;var&lt;/span&gt; e = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;BlockExpression&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            e.Parameters.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ParameterDeclaration&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"invocation"&lt;/span&gt;, &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SimpleTypeReference&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"Castle.Core.Interceptor.IInvocation"&lt;/span&gt;)));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            e.Body = macro.Block;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; e;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;In order to understand this class you have to know that behind the scenes what happens is that by default a new instance of a MethodInterceptExtension is created and injected into the Component being registered (ITextProvider in my example).  Unfortunately by default Binsor does not know how many or what parameters to pass into the constructor.  So we want to modify the call to construct the MethodInterceptExtension so that it takes in the method name that we want to intercept along with a delegate that actually does the intercepting.&lt;br /&gt;&lt;br /&gt;The InterceptorMacro just takes the first argument passed to it as the method name we want to intercept.  &lt;span style="font-weight: bold;"&gt;Now for some magic:&lt;/span&gt;  the macro wraps it's body block in a delegate.  Not to be outdone it then modifies the call to construct a new MethodInterceptExtension to accept two new parameters (method name and delegate).  Now the MethodInterceptExtension will be constructed with the method name and delegate injected into it.  Cool huh??&lt;br /&gt;&lt;br /&gt;These three classes are pretty straight forward and the additional functionality in Binsor is incredibly useful.  Plus rather than spending a day writing boring log statements I learned heaps about Binsor and Windsor.  Tomorrow I will actually use this and craft up a binsor container configuration file in record time...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-3287521006774587738?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/06/binsor-method-interceptor-macro.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-2287329200837137954</guid><pubDate>Fri, 30 May 2008 04:38:00 +0000</pubDate><atom:updated>2008-05-29T23:20:16.103-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">edmug</category><title>Top 10 Jonasims From the LinqToYourBrain Talk</title><description>Awwya fool... Jonas Avellana (aka &lt;a href="http://youtube.com/user/DjLedude"&gt;DJ LeDude&lt;/a&gt;) brought both his coding speedos and .Net skills to the &lt;a href="http://www.edmug.net/"&gt;edmug&lt;/a&gt; meeting this evening. Jonas delivered a couple of hours of Linq, SQL and .Net 3.5 nyahhhhh with a flair that's all his own. For those who missed it, here are the top 10 Jonasisms of the evening:&lt;br /&gt;  &lt;br /&gt;10. Awwww ya!!!&lt;br /&gt;9.  Thats Frickin' Cool&lt;br /&gt;8.  Thats Frickin' Awesome&lt;br /&gt;7.  You can do it like that if you're deepcore&lt;br /&gt;6.  var crapDataContext = new DragAndDropCrapDataContext(); [typed not vocalized]&lt;br /&gt;5.  First, I'm gonna show you the ghetto way...&lt;br /&gt;4.  ... now we'll do some Linq Kung Foo&lt;br /&gt;3.  Well, you know we don't have many chicks in the west end so the woman's prison is pretty good&lt;br /&gt;2.  Aghhhh my eyes!!!&lt;br /&gt;1.  I don't know how it works but its pretty awesome&lt;br /&gt;&lt;br /&gt;A little bit of a let down was that he didn't find a way to work &lt;a href="http://www.neatorama.com/2008/05/03/10-insulting-words-you-should-know/"&gt;bescumber&lt;/a&gt; into the presentation but he made up for it by delivering an excellent Linq packed presentation.  Thanks Jonas!  It takes a lot of time and effort to do a presentation like that and myself and the rest of the .Net community really appreciate it.  Now you can get back to &lt;a href="http://youtube.com/watch?v=8cGyeYtvb4M"&gt;ghost riding the whip&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;You can read more from Jonas on his blog: &lt;a href="http://blog.ezed.ca/"&gt;http://blog.ezed.ca&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-2287329200837137954?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/05/top-10-jonasims-from-linqtoyourbrain.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-2670854907470176451</guid><pubDate>Sat, 24 May 2008 15:43:00 +0000</pubDate><atom:updated>2008-05-24T11:07:02.243-06:00</atom:updated><title>How to read a programming book</title><description>Earlier this week Seth Godin had an inspirational post titled &lt;a href="http://sethgodin.typepad.com/seths_blog/2008/05/how-to-read-a-b.html"&gt;How to read a business book&lt;/a&gt; in which he postulates that many people read non-fiction for the enjoyment of a factual adventure.  Magic books rarely produce the next Houdini but the masses still enjoy reading about the mechanics behind the tricks.  Similarly not everyone who reads a business book becomes a millionaire, rather we enjoy hearing about the daring capitalist who went from rags to riches.&lt;br /&gt;&lt;br /&gt;Seth supports this claim by stating the &lt;span style="font-style:italic;"&gt;recipe&lt;/span&gt; of a business book can be distilled down to a couple of pages.  If one just wanted to read a business recipe they could do so in just a few minutes.  But the truth is, as Seth writes:&lt;br /&gt;&lt;blockquote&gt;-  Bullet points are not the point.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;The value in a business book is the fluffy stuff around the recipe.  If read in the right state of mind it will motivate you to reflect on your own perspective and behavior.  If read with the intent to change these books can, in fact, help you to change your life.&lt;br /&gt;&lt;br /&gt;In our own industry the electronic aisles of Amazon are littered with How To and recipe style books that miss the essence of persuasion.  These books tend to have a shelf life rarely exceeding that of a top 10 pop hit.  These books will not change your life even if you wanted them too.&lt;br /&gt;&lt;br /&gt;There are other books, however, that can change your life if you read them with the intent to change your life.  As Seth says, &lt;blockquote&gt;the goal of reading, then, isn't to persuade you to change rather to help choose what to change.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;While I'm not an expert on the matter, my advice to reading a programming book requires a few tools.  First go out and buy a stack of index cards, or post-it notes or even a pad of paper.  Second, find a pencil, pen or feather and ink... check between the cushions of your car seat you'll find something to write with.  Once you've got those things try these 6 steps:&lt;br /&gt;&lt;br /&gt;1.  &lt;span style="font-weight:bold;"&gt;Start with an intent to change.&lt;/span&gt;  Make the decision before ever opening the book.  Pick a handful of pain points in your daily grind.  It may be technical pain that can be solved with design patterns. Perhaps its pain with requirement gathering that may be solved through understanding a different software process.  In any event, write each pain down on its own index card.  At the end of this process each card will form a recipe for your success.  &lt;br /&gt;&lt;br /&gt;2.  &lt;span style="font-weight:bold;"&gt;Read and write.&lt;/span&gt; Now read the book.  But don't just read it, write about it as well.  As you're reading keep your pain points in mind and when you encounter something that may help solve your problem jot it down  on the card.  These are your solutions.&lt;br /&gt;&lt;br /&gt;3.  &lt;span style="font-weight:bold;"&gt;Set a goal.&lt;/span&gt;  Now that you've identified problems and solutions you just need a goal.  Missing the important step of setting goals is where most people fall short, &lt;span style="font-weight:bold;"&gt;but not you!&lt;/span&gt;.  Rise above the rest and write down what you want to achieve and a reasonable time frame in which you will achieve it.  Remember there are no unrealistic goals only unrealistic deadlines.&lt;br /&gt;&lt;br /&gt;4.  &lt;span style="font-weight:bold;"&gt;Just do it.&lt;/span&gt;  Remember that you've invested your valuable time in reading the book, you've invested the effort of reflection and you now have a card that tells you exactly how to change.  Bring those cards to work and read them a few times during the day.  Keep your goals at the forefront of your concentration and the rest will follow suit.  You will achieve your goals if you concentrate on them.  Whenever you achieve one give yourself a gold star.  You've earned it!&lt;br /&gt;&lt;br /&gt;5.  &lt;span style="font-weight:bold;"&gt;Don't stop there.&lt;/span&gt;  Repeat this cycle in small increments over and over again. The process is called Kaizen by the Japanese and it means making continuous changes over time through tight increments of small change.  The software industry is more susceptible to change than any other.  We are at the mercy of change in technology, paradigms, business, globalization and an unnamed butterfly in the amazon jungle.  The only way to avoid extinction in this field is to change continuously.&lt;br /&gt;&lt;br /&gt;6.  &lt;span style="font-weight:bold;"&gt;Tell everyone.&lt;/span&gt;  Tell your team, your manager, your friends and even your grandmother.  Pass the book around and tell them what you've achieved.  Blog about it and encourage others to change as well.  If you're in a position of leadership consider this quote from Seth:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Effective managers hand books to their team.  Not so they can be reminded of high school, but so that next week she can say to them, "are we there yet?".&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Remember that as exciting as the plot of a programming book may be, reading it is an investment of your time.  Cash in on your investment by changing something in your life.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-2670854907470176451?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/05/how-to-read-programming-book.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-4144974342295229147</guid><pubDate>Wed, 21 May 2008 01:54:00 +0000</pubDate><atom:updated>2008-05-20T20:41:54.429-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">boo</category><title>Fun With Boo's List Generators</title><description>I know I'm a bit &lt;a href="http://langexplr.blogspot.com/2007/02/list-comprehensions-across-languages_18.html"&gt;late to the party&lt;/a&gt; but I've just recently become infatuated with Boo's List Generators.  Being a bit of a nerd I thought I'd take a shot at implementing a &lt;a href="http://www.jimloy.com/number/sieve.htm"&gt;Eratosthenes Prime Number Sieve&lt;/a&gt; using one of these generators.  I know this may not be an optimal solution from an algorithmic perspective but the syntax is really cool.  Here's what I came up with:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;last = int.Parse(argv[0])&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;filters = []&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;def IsPrime(number as int):&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for filter as ICallable in filters:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if filter(number): return false&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;def HandlePrime(i as int):&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;unless i &gt; System.Math.Sqrt(last): filters.Add({ number as int | return number%i == 0})&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return i&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;print List(HandlePrime(i) for i in range(2,last) if IsPrime(i))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:trebuchet ms;" &gt;NYAHHHH!!!&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-4144974342295229147?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/05/fun-with-boos-list-generators.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-2754047356020277759</guid><pubDate>Sat, 17 May 2008 16:33:00 +0000</pubDate><atom:updated>2008-05-19T08:28:48.778-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">boo ast</category><title>Kicking Ast with Boo</title><description>I'm currently working on a project that consumes a third party web service.  This web service uses xml serializable DTOs to pass a fairly complex object model across the wire.  The downside of this interface is that our code base (especially the tests) is littered with blocky new statements and setters like this (we're still in 2.0 land so we can't use object initializers):&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20         \cf3 public\cf0  \cf3 void\cf0  Before()\par ??        \{\par ??            \cf3 var\cf0  person = \cf3 new\cf0  \cf4 PersonDTO\cf0 ();\par ??        \}} --&gt;    &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;}??\fs20             \cf3 PersonDTO\cf0  person = \cf4 new\cf0  \cf3 PersonDTO\cf0 ();\par ??            person.FirstName = \cf5 "Neil"\cf0 ;\par ??            person.LastName = \cf5 "Bourgeois"\cf0 ;\par ??            person.Address = \cf4 new\cf0  \cf3 AddressDTO\cf0 ();\par ??            person.Address.Number = 10160;\par ??            person.Address.Street = \cf5 "115 St."\cf0 ;\par ??            person.Address.City = \cf5 "Edmonton"\cf0 ;\par ??            person.Address.PostalCode = \cf5 "T5K 1T6"\cf0 ;\par ??            person.Address.Country = \cf5 "Canada"\cf0 ;\par ??            person.Email = \cf5 "neil.bourgeois.junk@gmail.com"\cf0 ;\par ??            person.Age = 30;} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;    PersonDTO&lt;/span&gt; person = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonDTO&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.FirstName = &lt;span style="color: rgb(163, 21, 21);"&gt;"Neil"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.LastName = &lt;span style="color: rgb(163, 21, 21);"&gt;"Bourgeois"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Address = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;AddressDTO&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Address.Number = 10160;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Address.Street = &lt;span style="color: rgb(163, 21, 21);"&gt;"115 St."&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Address.City = &lt;span style="color: rgb(163, 21, 21);"&gt;"Edmonton"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Address.PostalCode = &lt;span style="color: rgb(163, 21, 21);"&gt;"T5K 1T6"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Address.Country = &lt;span style="color: rgb(163, 21, 21);"&gt;"Canada"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Email = &lt;span style="color: rgb(163, 21, 21);"&gt;"neil.bourgeois.junk@gmail.com"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    person.Age = 30;&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;I started writing builders with a fluent interface to help alleviate some of the typing and improve the readability so a person can be constructed like this:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20             \cf3 PersonDTO\cf0  person = \cf3 BuildPerson\cf0 .With\par ??                .FirstName(\cf4 "Neil"\cf0 )\par ??                .LastName(\cf4 "Bourgeois"\cf0 )\par ??                .Address(\cf3 BuildAddress\cf0 .With\par ??                             .Number(10160)\par ??                             .Street(\cf4 "115 St."\cf0 )\par ??                             .City(\cf4 "Edmonton"\cf0 )\par ??                             .PostalCode(\cf4 "T5K 1T6"\cf0 )\par ??                             .Country(\cf4 "Canada"\cf0 )\par ??                             .Build())\par ??                .Email(\cf4 "neil.bourgeois.junk@gmail.com"\cf0 )\par ??                .Age(30)\par ??                .Build();} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;PersonDTO&lt;/span&gt; person = &lt;span style="color: rgb(43, 145, 175);"&gt;BuildPerson&lt;/span&gt;.With&lt;/p&gt; &lt;p style="margin: 0px;"&gt;   .FirstName(&lt;span style="color: rgb(163, 21, 21);"&gt;"Neil"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;   .LastName(&lt;span style="color: rgb(163, 21, 21);"&gt;"Bourgeois"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;   .Address(&lt;span style="color: rgb(43, 145, 175);"&gt;BuildAddress&lt;/span&gt;.With&lt;/p&gt; &lt;p style="margin: 0px;"&gt;      .Number(10160)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;      .Street(&lt;span style="color: rgb(163, 21, 21);"&gt;"115 St."&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;      .City(&lt;span style="color: rgb(163, 21, 21);"&gt;"Edmonton"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;      .PostalCode(&lt;span style="color: rgb(163, 21, 21);"&gt;"T5K 1T6"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;      .Country(&lt;span style="color: rgb(163, 21, 21);"&gt;"Canada"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;      .Build())&lt;/p&gt; &lt;p style="margin: 0px;"&gt;   .Email(&lt;span style="color: rgb(163, 21, 21);"&gt;"neil.bourgeois.junk@gmail.com"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;   .Age(30)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;   .Build();&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;The builder looks something like this:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20     \cf3 public\cf0  \cf3 class\cf0  \cf4 PersonBuilder\par ??\cf0     \{\par ??        \cf3 private\cf0  \cf3 readonly\cf0  \cf4 PersonDTO\cf0  person = \cf3 new\cf0  \cf4 PersonDTO\cf0 ();\par ??\par ??        \cf3 public\cf0  \cf3 static\cf0  \cf4 PersonBuilder\cf0  With\par ??        \{\par ??            \cf3 get\par ??\cf0             \{\par ??                \cf3 return\cf0  \cf3 new\cf0  \cf4 PersonBuilder\cf0 ();\par ??            \}\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf4 PersonBuilder\cf0  FirstName(\cf3 string\cf0  firstName)\par ??        \{\par ??            person.FirstName = firstName;\par ??            \cf3 return\cf0  \cf3 this\cf0 ;\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf4 PersonBuilder\cf0  LastName(\cf3 string\cf0  lastName)\par ??        \{\par ??            person.LastName = lastName;\par ??            \cf3 return\cf0  \cf3 this\cf0 ;\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf4 PersonDTO\cf0  Build()\par ??        \{\par ??            \cf3 return\cf0  person;\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20     \cf3 public\cf0  \cf3 class\cf0  \cf4 PersonBuilder\par ??\cf0     \{\par ??        \cf3 private\cf0  \cf3 readonly\cf0  \cf4 PersonDTO\cf0  person = \cf3 new\cf0  \cf4 PersonDTO\cf0 ();\par ??\par ??        \cf3 public\cf0  \cf3 static\cf0  \cf4 PersonBuilder\cf0  With\par ??        \{\par ??            \cf3 get\cf0  \{ \cf3 return\cf0  \cf3 new\cf0  \cf4 PersonBuilder\cf0 (); \}\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf4 PersonBuilder\cf0  FirstName(\cf3 string\cf0  firstName)\par ??        \{\par ??            person.FirstName = firstName;                        \par ??            \cf3 return\cf0  \cf3 this\cf0 ;\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf4 PersonBuilder\cf0  LastName(\cf3 string\cf0  lastName)\par ??        \{\par ??            person.LastName = lastName;\par ??            \cf3 return\cf0  \cf3 this\cf0 ;\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf4 PersonDTO\cf0  Build()\par ??        \{\par ??            \cf3 return\cf0  person;\par ??        \}\par ??    \}} --&gt; &lt;div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonBuilder&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonDTO&lt;/span&gt; person = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonDTO&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonBuilder&lt;/span&gt; With&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonBuilder&lt;/span&gt;(); }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonBuilder&lt;/span&gt; FirstName(&lt;span style="color: blue;"&gt;string&lt;/span&gt; firstName)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            person.FirstName = firstName;                        &lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonBuilder&lt;/span&gt; LastName(&lt;span style="color: blue;"&gt;string&lt;/span&gt; lastName)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            person.LastName = lastName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonDTO&lt;/span&gt; Build()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; person;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;  &lt;/div&gt;&lt;br /&gt;Its a much nicer way to define our DTOs, however, building the builder class proved a taxing experience.  Even with a dab of generics and a splash of inheritance the monotony of defining the builders made me almost throw this pattern out the window.  But then I remembered a bit of Boo AST magic described by Ayende and thought, why can't I get a compiler to do this work for me?  I did some googling to find &lt;a href="http://langexplr.blogspot.com/2007/06/calling-boo-compiler-from-boo-program.html"&gt;this helpful post by Luis Diego Fallas&lt;/a&gt; and started to cut some code.&lt;br /&gt;&lt;br /&gt;Essentially this code creates the a builder class for each DTO in our source assembly (hardcoded as SampleDTO).  The result of running it is a new assembly called builder.dll that contains all the fluent builders.  Builder classes are defined by creating expressions in an Abstract Syntax Tree (AST) and running that tree through the boo compiler.&lt;br /&gt;&lt;br /&gt;To start with we need to define a new class.  I like fluent interfaces to read like an English sentence so I chose to give the builder classes names like: BuildSomething.  I also strip out the DTO portion of the name as just adds clutter.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;    baseName = type.Name.Replace("DTO", string.Empty)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    newClass = ClassDefinition(Name:"Build" + baseName)    &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Once the class is defined we need to add a member variable for the type we are going to create:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;   newClass.Members.Add(Field(Name:"item",&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                          Type:SimpleTypeReference(type.FullName),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                          Modifiers:TypeMemberModifiers.Private))&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Next I want the constructor to initialize the member variable (eg. &lt;span style="font-family:courier new;"&gt;item = new PersonDTO()&lt;/span&gt;):&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;ctr = Constructor()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;     ctr.Body.Add(ExpressionStatement(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    Expression:BinaryExpression(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        BinaryOperatorType.Assign,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        MemberReferenceExpression(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                                Target:SelfLiteralExpression(),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                                Name:"item"),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        MethodInvocationExpression(ReferenceExpression(type.Name)))))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;      newClass.Members.Add(ctr) &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now we can add the Build() method that simply returns our member variable..&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;    buildMethod = Method(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                Name:"Build",&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                Modifiers:TypeMemberModifiers.Public,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                Body:Block(),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                ReturnType:SimpleTypeReference(type.FullName))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    buildMethod.Body.Add(ReturnStatement(Expression:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        MemberReferenceExpression(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                            Target:SelfLiteralExpression(),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                            Name:"item")))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    newClass.Members.Add(buildMethod)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;... and the static With property to give us a natural way to construct the builder.  Remember that properties actually require a method behind the scenes to do the real work.  So we will start with a getter that constructs a new instance of our builder and immediately returns it:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;    withGetMethod = Method(                                    &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    Name:"internalWithGetter",&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    Modifiers:TypeMemberModifiers.Public | TypeMemberModifiers.Static,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    Body:Block(),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    ReturnType:SimpleTypeReference(newClass.Name))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;      withGetMethod.Body.Add(ReturnStatement(Expression:MethodInvocationExpression(ReferenceExpression(newClass.Name))))                                            &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Now we can add the With property:&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;    withProperty = Property(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                Name:"With", &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                Getter:withGetMethod, &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                Modifiers:TypeMemberModifiers.Public | TypeMemberModifiers.Static)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    newClass.Members.Add(withProperty)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Finally we want to loop through all properties in our DTO and create fluent setter methods.   We want this method to look like this:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20         \cf3 public\cf0  \cf4 PersonBuilder\cf0  FirstName(\cf3 string\cf0  firstName)\par ??        \{\par ??            person.FirstName = firstName;\par ??            \cf3 return\cf0  \cf3 this\cf0 ;\par ??        \}} --&gt; &lt;div face="Courier New" size="10pt" color="black" style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PersonBuilder&lt;/span&gt; FirstName(&lt;span style="color:blue;"&gt;string&lt;/span&gt; firstName)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            person.FirstName = firstName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;this&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;We'll start by creating a new method that has the same name as the property we want to set.  To facilitate the fluent interface the return type of this method is the type of our builder class:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;   m = Method(Name: property.Name,&lt;br /&gt;         Modifiers:TypeMemberModifiers.Public,&lt;br /&gt;         Body:Block(),&lt;br /&gt;         ReturnType:SimpleTypeReference(newClass.Name))&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Next we want to add a parameter argument to this method.  This is the value we want to set the DTO property to:&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;     parameterName = property.Name&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;     m.Parameters.Add(ParameterDeclaration(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        Name: parameterName,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        Type:SimpleTypeReference(&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;                            Name:property.PropertyType.FullName)))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Now lets take that parameter and assign it to the appropriate property in our DTO.  This step was a bit tricky to get right the first time but it makes sense when you think about assignment (a = b) as a binary operation (like a + b or a &gt; b):&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;        m.Body.Add(ExpressionStatement(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    Expression:BinaryExpression(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        BinaryOperatorType.Assign,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        MemberReferenceExpression(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                                Target:ReferenceExpression("item"),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                                Name:property.Name),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        ReferenceExpression(parameterName))))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Last but not least we need to return the instance of the builder class (this):&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;        m.Body.Add(ReturnStatement(Expression:SelfLiteralExpression()))&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Once the AST for our new builder types is complete we can take it and compile it into an assembly.  Here is the code that brings it all together (Note the call to BuildClass, this is where the AST for our builder is created):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;a = Assembly.Load("SampleDTO")&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;cu = CompileUnit()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;mod = Boo.Lang.Compiler.Ast.Module(&lt;br /&gt;                     Name:"Builder",&lt;br /&gt;                     Namespace:NamespaceDeclaration(Name:"Builder"))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;cu.Modules.Add(mod)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;mod.Imports.Add(Import(Namespace:"System"))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;mod.Imports.Add(Import(Namespace:"SampleDTO"))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;for type in a.GetTypes():&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    mod.Members.Add(BuildClass(type))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;pipeline = Pipelines.CompileToFile()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ctxt = CompilerContext(cu)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ctxt.Parameters.OutputType = CompilerOutputType.Library&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;pipeline.Run(ctxt)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Defining types in AST is a bit like walking in mud: slow and painful.  What can be accomplished with 3 lines of code using a higher level language can take many many more.  Hang in there, the frustration wears off after a few eureka moments and the benefit of learning this stuff can be huge.  In my case a few hours writing AST (which was really interesting work) saved many more dull and tedious hours of writing boring property setters.&lt;br /&gt;&lt;br /&gt;Remember that Reflector is your best friend for this type of work!!&lt;br /&gt;&lt;br /&gt;As a side note, the overall framework for this is pretty cool.  Whenever there is a new release of a web service we can run a NAnt build script that does the following:&lt;br /&gt;1.  Use wsdl to generate a proxy class (and all DTOs)&lt;br /&gt;2.  Compile the proxy and DTOs into an assembly&lt;br /&gt;3.  Run the fluent builder generator on the new assembly&lt;br /&gt;4.  Run ILMerge to create a new assembly with the outputs from 2 and 3&lt;br /&gt;&lt;br /&gt;The result is a single assembly containing the Proxy, DTOs and Builders... its pretty slick!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-2754047356020277759?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/05/kicking-ast-with-boo.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-1824890970759894920</guid><pubDate>Sat, 08 Mar 2008 05:58:00 +0000</pubDate><atom:updated>2008-03-07T23:22:11.095-07:00</atom:updated><title>Deploy ASP MVC Preview 2 your Shared Host Environment</title><description>The latest drop of ASP.NET MVC (Preview 2) enables you to deploy your app by simply uploading your assemblies and MVC dependencies to your bin directory and run in a partial trust.  There's still one catch, however, if your host is running IIS 6 and you have no access to manage your web site IIS properties.&lt;br /&gt;&lt;br /&gt;Since MVC routing employs a .mvc extension to identify incoming requests IIS needs to have a asp.net mapping to that extension.  If you have no access to the IIS management tools you'll end up with a bunch of 404 errors.  The real answer is to request a mapping for the .mvc extension from your host.&lt;br /&gt;&lt;br /&gt;The hack, if you're impatient (like me) or if your host is unwilling to map that extension is to reuse the .aspx extension.  In other words set up your routes in GlobalApplication.RegisterRoutes to use the .aspx extension rather than .mvc.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;public static void &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;RegisterRoutes(&lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);font-family:courier new;" &gt;RouteCollection &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;routes)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    routes.Add(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; Route&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;"Default.aspx"&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;, &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);font-family:courier new;" &gt;MvcRouteHandler&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;())&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;          {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Defaults = &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);font-family:courier new;" &gt;RouteValueDictionary&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;{controller = &lt;span style="color: rgb(204, 0, 0);"&gt;"Home"&lt;/span&gt;, action = &lt;span style="color: rgb(204, 0, 0);"&gt;"Index"&lt;/span&gt;, id = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt;}),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;           });&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    routes.Add(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Route(&lt;span style="color: rgb(204, 0, 0);"&gt;"{controller}&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);font-family:courier new;" &gt;.aspx&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;/{action}/{id}"&lt;/span&gt;, &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);font-family:courier new;" &gt;MvcRouteHandler&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;())&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;           {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;               Defaults = &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);font-family:courier new;" &gt;RouteValueDictionary&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;new &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;{action = &lt;span style="color: rgb(204, 0, 0);"&gt;"Index"&lt;/span&gt;, id = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt;}),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            });&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that you have to bump the default route up as the first route.  This is because routing rules are evaluated in the order in which they are declared.   The limitation of this method is that you can never have a controller called Default :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-1824890970759894920?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2008/03/deploy-asp-mvc-preview-2-your-shared.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-6351991078152458941</guid><pubDate>Wed, 14 Nov 2007 00:58:00 +0000</pubDate><atom:updated>2007-11-13T18:30:51.949-07:00</atom:updated><title>Creating a Custom Binsor Lifestyle</title><description>I'm currently evaluating Windsor (well, &lt;a href="http://ayende.com/"&gt;Ayende's&lt;/a&gt; Binsor interpretation of it to be more precise) to help alleviate some UI stresses in a WinForms application.  Essentially I am looking for something that allows me to track a certain UI context.  For example, I have broken down the application flow into a number of key contexts: Application (user login), Project, Widget.  I would like each context to build upon the previously loaded contexts and when a context is ended I want to clear data associated with it.  For example repositories, caches, unsaved changes, etc.&lt;br /&gt;&lt;br /&gt;Since our project is using the Castle Project's &lt;a href="http://castleproject.org/container/index.html"&gt;Windsor Container&lt;/a&gt; it seemed like a natural place to start implementing this functionality.  My first kick at this cat involved a facility that managed contexts and the data associated with each one, however, I quickly realized that my facility was mimicking the behaviour of a Lifestyle.   Basically what I wanted was the ability to resolve components as per-context singletons so I decided to try and create a Custom Lifestyle to achieve it.&lt;br /&gt;&lt;br /&gt;The first step is to create a Lifestyle Manager.  I just derived a class from the MicroKernel AbstractLifestyleManager and implemented the Resolve Method:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20     \cf2 public\cf0  \cf2 class\cf0  \cf10 ContextualLifestyleManager\cf0  : \cf10 AbstractLifestyleManager\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf2 const\cf0  \cf2 string\cf0  CONTEXT_ATTRIBUTE = \cf13 "context"\cf0 ;\par ??        \cf2 private\cf0  \cf2 readonly\cf0  \cf10 IDictionary\cf0 &lt;\cf10 Type\cf0 , \cf10 IContext\cf0&gt; contextMap = \cf2 new\cf0  \cf10 Dictionary\cf0 &lt;\cf10 Type\cf0 , \cf10 IContext\cf0&gt;();\par ??        \cf2 private\cf0  \cf2 readonly\cf0  \cf10 IDictionary\cf0 &lt;\cf10 IContext\cf0 , \cf10 IDictionary\cf0 &lt;\cf10 Type\cf0 , \cf2 object\cf0&gt;&gt; contextualComponentMap = \cf2 new\cf0  \cf10 Dictionary\cf0 &lt;\cf10 IContext\cf0 , \cf10 IDictionary\cf0 &lt;\cf10 Type\cf0 , \cf2 object\cf0&gt;&gt;();} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;ContextualLifestyleManager&lt;/span&gt; : &lt;span style="color:teal;"&gt;AbstractLifestyleManager&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;{&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; CONTEXT_ATTRIBUTE = &lt;span style="color:maroon;"&gt;"context"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;IDictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;Type&lt;/span&gt;, &lt;span style="color:teal;"&gt;IContext&lt;/span&gt;&gt; contextMap = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;Dictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;Type&lt;/span&gt;, &lt;span style="color:teal;"&gt;IContext&lt;/span&gt;&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;IDictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;IContext&lt;/span&gt;, &lt;span style="color:teal;"&gt;IDictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;Type&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&gt;&gt; contextualComponentMap = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;Dictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;IContext&lt;/span&gt;, &lt;span style="color:teal;"&gt;IDictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;Type&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&gt;&gt;();&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20         \cf2 public\cf0  \cf2 override\cf0  \cf2 object\cf0  Resolve(\cf10 CreationContext\cf0  context)\par ??        \{\par ??            \cf10 Type\cf0  contextType = RegisterApplicationContext(context);\par ??            \cf10 IContext\cf0  appContext = contextMap[contextType];\par ??            \cf2 string\cf0  serviceTypeName = context.Handler.ComponentModel.Service.FullName;\par ??            \cf10 Type\cf0  serviceType = \cf10 Type\cf0 .GetType(serviceTypeName);\par ??            \cf2 if\cf0  (!contextualComponentMap[appContext].ContainsKey(serviceType))\par ??            \{\par ??                contextualComponentMap[appContext].Add(serviceType, \cf2 base\cf0 .Resolve(context));\par ??            \}\par ??            \cf2 return\cf0  contextualComponentMap[appContext][serviceType];\par ??        \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Resolve(&lt;span style="color:teal;"&gt;CreationContext&lt;/span&gt; context)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;{&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Type&lt;/span&gt; contextType = RegisterApplicationContext(context);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;IContext&lt;/span&gt; appContext = contextMap[contextType];&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; serviceTypeName = context.Handler.ComponentModel.Service.FullName;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Type&lt;/span&gt; serviceType = &lt;span style="color:teal;"&gt;Type&lt;/span&gt;.GetType(serviceTypeName);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!contextualComponentMap[appContext].ContainsKey(serviceType))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        contextualComponentMap[appContext].Add(serviceType, &lt;span style="color:blue;"&gt;base&lt;/span&gt;.Resolve(context));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; contextualComponentMap[appContext][serviceType];&lt;/p&gt; &lt;p style="margin: 0px;"&gt;}&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20         \cf2 private\cf0  \cf10 Type\cf0  RegisterApplicationContext(\cf10 CreationContext\cf0  context)\par ??        \{\par ??            \cf2 string\cf0  contextTypeName = context.Handler.ComponentModel.Configuration.Attributes[CONTEXT_ATTRIBUTE];\par ??            \cf10 Type\cf0  contextType = \cf10 Type\cf0 .GetType(contextTypeName);\par ??            \cf2 if\cf0  (!contextMap.ContainsKey(contextType))\par ??            \{\par ??                \cf10 IContext\cf0  appContext = (\cf10 IContext\cf0 ) Kernel.Resolve(contextType);\par ??                appContext.Ended += HandleContextEnded;\par ??                contextMap.Add(contextType, appContext);\par ??                contextualComponentMap.Add(appContext, \cf2 new\cf0  \cf10 Dictionary\cf0 &lt;\cf10 Type\cf0 , \cf2 object\cf0&gt;());\par ??            \}\par ??            \cf2 return\cf0  contextType;\par ??        \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:teal;"&gt;Type&lt;/span&gt; RegisterApplicationContext(&lt;span style="color:teal;"&gt;CreationContext&lt;/span&gt; context)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;{&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; contextTypeName = context.Handler&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                                    .ComponentModel&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                                    .Configuration&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                                    .Attributes[CONTEXT_ATTRIBUTE];&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Type&lt;/span&gt; contextType = &lt;span style="color:teal;"&gt;Type&lt;/span&gt;.GetType(contextTypeName);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!contextMap.ContainsKey(contextType))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:teal;"&gt;IContext&lt;/span&gt; appContext = (&lt;span style="color:teal;"&gt;IContext&lt;/span&gt;) Kernel.Resolve(contextType);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        appContext.Ended += HandleContextEnded;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        contextMap.Add(contextType, appContext);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        contextualComponentMap.Add(appContext, &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;Dictionary&lt;/span&gt;&lt;&lt;span style="color:teal;"&gt;Type&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&gt;());&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; contextType;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;br /&gt;&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Keep in mind that you can override resolve to do whatever your heart desires... in this snip you can see I'm grabbing the name of the Service I'm requesting and also an Attribute from the ComponentModel configuration.  Basically anything in the kernel is at your mercy!&lt;br /&gt;&lt;br /&gt;Now, I also wanted to clean up after the context ends so added an event handler to respond to  the IContext.Ended event:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20         \cf2 private\cf0  \cf2 void\cf0  HandleContextEnded(\cf2 object\cf0  sender, \cf10 EventArgs\cf0  e)\par ??        \{\par ??            \cf10 IContext\cf0  context = (\cf10 IContext\cf0 ) sender;\par ??            \cf10 Console\cf0 .WriteLine(\cf2 string\cf0 .Format(\cf13 "Removing context: \{0\}"\cf0 , context.Name));\par ??            context.Ended -= HandleContextEnded;\par ??            contextMap.Remove(context.GetType());\par ??            contextualComponentMap.Remove(context);\par ??        \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; HandleContextEnded(&lt;span style="color:blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color:teal;"&gt;EventArgs&lt;/span&gt; e)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;{&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;IContext&lt;/span&gt; context = (&lt;span style="color:teal;"&gt;IContext&lt;/span&gt;) sender;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color:maroon;"&gt;"Removing context: {0}"&lt;/span&gt;, context.Name));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    context.Ended -= HandleContextEnded;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    contextMap.Remove(context.GetType());&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    contextualComponentMap.Remove(context);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;  Now,  I know its not pretty, but it does the trick for a proof of concept.  :-)&lt;br /&gt;&lt;br /&gt;The next step was to configure this custom lifestyle in the boo container configuration.  I got stuck on this for a while but eventually created a LifestyleExtension to help me out:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20     \cf2 public\cf0  \cf2 class\cf0  \cf10 Contextual\cf0  : \cf10 LifestyleExtension\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf2 readonly\cf0  \cf10 Type\cf0  customLifestyleType = \cf2 typeof\cf0 (\cf10 ContextualLifestyleManager\cf0 );\par ??\par ??        \cf2 public\cf0  Contextual() : \cf2 base\cf0 (\cf10 LifestyleType\cf0 .Custom)\par ??        \{\par ??        \}\par ??\par ??        \cf2 protected\cf0  \cf2 override\cf0  \cf2 void\cf0  AddLifestyleDetails(\cf10 IConfiguration\cf0  configuration)\par ??        \{\par ??            configuration.Attributes[\cf13 "customLifestyleType"\cf0 ] = \cf2 string\cf0 .Format(\cf13 "\{0\}, SpikeWinForms"\cf0 , customLifestyleType.FullName);\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;Contextual&lt;/span&gt; : &lt;span style="color:teal;"&gt;LifestyleExtension&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;{&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;Type&lt;/span&gt; customLifestyleType = &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:teal;"&gt;ContextualLifestyleManager&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; Contextual() : &lt;span style="color:blue;"&gt;base&lt;/span&gt;(&lt;span style="color:teal;"&gt;LifestyleType&lt;/span&gt;.Custom)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; AddLifestyleDetails(&lt;span style="color:teal;"&gt;IConfiguration&lt;/span&gt; configuration)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        configuration.Attributes[&lt;span style="color:maroon;"&gt;"customLifestyleType"&lt;/span&gt;] = &lt;span style="color:blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color:maroon;"&gt;"{0}, SpikeWinForms"&lt;/span&gt;, customLifestyleType.FullName);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;}&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Here is what my Binsor config file ended up looking like (hint: @context is an attribute!):&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 component repository, IRepository, Repository:\par ??\tab lifestyle Contextual\par ??\tab @context = MasterContext\par ??\par ??component applicationContext, ApplicationContext, ApplicationContext\par ??component masterContext, MasterContext, MasterContext} --&gt; &lt;div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;component repository, IRepository, Repository:&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    lifestyle Contextual&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    @context = MasterContext&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;component applicationContext, ApplicationContext, ApplicationContext&lt;/p&gt; &lt;p style="margin: 0px;"&gt;component masterContext, MasterContext, MasterContext&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Anyway, hope this post helps you out if you're trying to create custom Lifestyles using Binsor.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-6351991078152458941?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2007/11/creating-custom-binsor-lifestyle.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-1996134644278672445</guid><pubDate>Fri, 02 Nov 2007 23:02:00 +0000</pubDate><atom:updated>2007-11-02T18:29:45.421-06:00</atom:updated><title>Get Your Strings To Perform</title><description>Alright, we are probably all aware that strings are well employed in applications. We use them to display information to the user, we abuse them in dynamic sql statements and we confuse them with type definitions through their use in reflection. I'm willing to bet that if we inspected application's memory, strings would represent the majority of bytes. Not that there's anything wrong with that. After all the CLR has some pretty cool tweaks to make strings as primitive and performant as possible. Unfortunately for us... the CLR's string nyahh is sometimes not enough.&lt;br /&gt;&lt;br /&gt;There are a few important things to remember about strings in C#.  First, they are immediately derived from Object making them a reference type and living on the heap. The second important fact about strings is that they are an immutable array of characters. That means, they can not be altered in any way after they are constructed. The immutability of strings allows us to do some funky things without worrying about altering the original string.&lt;br /&gt;&lt;br /&gt;Take the following code snip for example:&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16             \cf2 string\cf0  message = \cf13 "Hello World"\cf0 ;\par ??            \cf10 Console\cf0 .WriteLine(message);\par ??            \cf10 Console\cf0 .WriteLine(message.Replace(\cf13 "Hello"\cf0 , \cf13 "Adios"\cf0 )\par ??                                  .Replace(\cf13 "World"\cf0 , \cf13 "Suckah"\cf0 )\par ??                                  .ToUpper());\par ??            \cf10 Console\cf0 .WriteLine(message);} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;    string&lt;/span&gt; message = &lt;span style="color:maroon;"&gt;"Hello World"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:teal;"&gt;    Console&lt;/span&gt;.WriteLine(message);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:teal;"&gt;    Console&lt;/span&gt;.WriteLine(message.Replace(&lt;span style="color:maroon;"&gt;"Hello"&lt;/span&gt;, &lt;span style="color:maroon;"&gt;"Adios"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                             .Replace(&lt;span style="color:maroon;"&gt;"World"&lt;/span&gt;, &lt;span style="color:maroon;"&gt;"Suckah"&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                             .ToUpper());&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Console&lt;/span&gt;.WriteLine(message);&lt;/p&gt; &lt;/div&gt;  &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The result of running this is:&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Hello World&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ADIOS SUCKAH&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Hello World&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The string contained in the message variable is unchanged while each method returns a newly created string.  It is important to understand that line 3 of this snip actually constructs three different string objects on the heap and then lets leaves them for the garbage collector to clean up.  Its easy to see then that if many operations are performed on a string we could quickly run into a large amount of memory being managed.  It is preferred, for that reason, to use the StringBuilder class if you are doing many string manipulation operations.&lt;br /&gt;&lt;br /&gt;Another bonus about strings being immutable is that they are inherently thread safe.   Since there are no thread synchronization issues the the CLR can help you reduce the number of actual strings allocated in memory by sharing them as a single string object.  This is the concept of string interning.  You should consider using string interning when your code has many instances of the same string value (wasted memory) or if you are doing many checks for equality (expensive operation).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Note: String equality can get expensive because if the strings are the same length, they are checked character by character.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;The CLR contains an internal hash table of strings mapped to actual string objects on the heap.  You can access this hash table through two static methods on the String object itself:&lt;br /&gt;&lt;span style="color: rgb(16, 0, 160);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(16, 0, 160);"&gt;static&lt;/span&gt; &lt;a title="System.String" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:1.0.5000.0:b77a5c561934e089/System.String"&gt;string&lt;/a&gt; &lt;b&gt;&lt;a class="bold" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:1.0.5000.0:b77a5c561934e089/System.String/Intern%28String%29:String"&gt;Intern&lt;/a&gt;&lt;/b&gt;(&lt;a title="System.String" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:1.0.5000.0:b77a5c561934e089/System.String"&gt;string&lt;/a&gt; str);&lt;br /&gt;&lt;span style="color: rgb(16, 0, 160);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(16, 0, 160);"&gt;static&lt;/span&gt; &lt;a title="System.String" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:1.0.5000.0:b77a5c561934e089/System.String"&gt;string&lt;/a&gt; &lt;b&gt;&lt;a class="bold" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:1.0.5000.0:b77a5c561934e089/System.String/IsInterned%28String%29:String"&gt;IsInterned&lt;/a&gt;&lt;/b&gt;(&lt;a title="System.String" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:1.0.5000.0:b77a5c561934e089/System.String"&gt;string&lt;/a&gt; str);&lt;br /&gt;&lt;br /&gt;The Intern method takes a string as a parameter and checks in the internal hash table for a reference to a matching string object that has already been allocated in memory.  If one exists that reference is returned.  Otherwise it makes a copy of your string, adds it to the hash table and returns a reference to the copy.&lt;br /&gt;&lt;br /&gt;The IsInterned method behaves very similar except if the string does not exist in the hash table null is returned.&lt;br /&gt;&lt;br /&gt;The end result is a single string being allocated in memory.  These methods assist in both reducing memory consumption by reducing the total number of strings allocated on the heap as well as increased performance by replacing ordinal string comparisons with a quick pointer comparison.&lt;br /&gt;&lt;br /&gt;So go ahead and run the following code snip:&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16             \cf2 string\cf0  message1 = \cf13 "Hello World"\cf0 ;\par ??            \cf2 string\cf0  message2 = \cf13 "Hello World"\cf0 ;\par ??            \cf10 Assert\cf0 .IsFalse(ReferenceEquals(message1, message2));} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;    string&lt;/span&gt; message1 = &lt;span style="color:maroon;"&gt;"Hello World"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;    string&lt;/span&gt; message2 = &lt;span style="color:maroon;"&gt;"Hello World"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:teal;"&gt;    Assert&lt;/span&gt;.IsFalse(ReferenceEquals(message1, message2));&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Um, they were the same... heh.    Turns out the CLR interns all literal strings described in an assembly's metadata.  This is where the ECMA specifications for string interning gets confusing.  It seems that by default the CLR will try and intern literal strings for us, but, this can be disabled by adding the  CompilationRelaxations.NoStringInterning  flag to the assembly:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16 [assembly : \cf10 CompilationRelaxations\cf0 (\cf10 CompilationRelaxations\cf0 .NoStringInterning)]} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;[assembly : &lt;span style="color:teal;"&gt;CompilationRelaxations&lt;/span&gt;(&lt;span style="color:teal;"&gt;CompilationRelaxations&lt;/span&gt;.NoStringInterning)]&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;But, the ECMA also states that the CLR &lt;span style="font-style: italic;"&gt;may&lt;/span&gt; choose not to intern all of the strings in an assembly's metadata.  So in reality, you can still end up with string interning by default.  Additionally, there are no guarantees that future versions of CLR will honor this flag.  So, the long and short of this is that you should only count on interning if you explicitly write code that uses the Intern or IsInterned methods.  The following code snip demonstrates how to do this:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16             \cf2 string\cf0  intern1 = \cf10 String\cf0 .Intern(message1);\par ??            \cf2 string\cf0  intern2 = \cf10 String\cf0 .Intern(message2);\par ??            \cf10 Assert\cf0 .IsTrue(ReferenceEquals(intern1, intern2));} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; intern1 = &lt;span style="color:teal;"&gt;String&lt;/span&gt;.Intern(message1);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; intern2 = &lt;span style="color:teal;"&gt;String&lt;/span&gt;.Intern(message2);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Assert&lt;/span&gt;.IsTrue(ReferenceEquals(intern1, intern2));&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/span&gt;By the way, to prove that interning doesn't work on non literal strings you can concatenating strings:&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16             \cf2 string\cf0  message1 = \cf13 "Hello"\cf0 ;\par ??            message1 += \cf13 " World"\cf0 ;\par ??            \cf2 string\cf0  message2 = \cf13 "Hello World"\cf0 ;\par ??            \cf10 Assert\cf0 .IsFalse(ReferenceEquals(message1, message2));} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; message1 = &lt;span style="color:maroon;"&gt;"Hello"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    message1 += &lt;span style="color:maroon;"&gt;" World"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; message2 = &lt;span style="color:maroon;"&gt;"Hello World"&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Assert&lt;/span&gt;.IsFalse(ReferenceEquals(message1, message2));&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;So, there you go.  Some tips and pitfalls of string performance.&lt;br /&gt;&lt;hints id="hah_hints"&gt;&lt;/hints&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-1996134644278672445?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2007/11/get-your-strings-to-perform.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-7565105339704476273</guid><pubDate>Fri, 26 Oct 2007 05:33:00 +0000</pubDate><atom:updated>2007-10-26T00:19:52.434-06:00</atom:updated><title>Edmonton Code Camp '07</title><description>Wow, thanks to the &lt;a href="http://www.edmug.net/"&gt;EDMUG&lt;/a&gt; guys... you did a fantastic job of organizing this event and what a great turn out.  I think this year had a very cohesive series of sessions and really show that the&lt;br /&gt;Edmonton development community is engaging more and more in ALT.NET and moving away from the MS endorsed drag 'n drop &lt;a href="http://www.ayende.com/Blog/archive/2007/10/15/D.O.A-Software.aspx"&gt;Designer Oriented Architecture&lt;/a&gt; (DOA for short... no really).  I met a lot of great people (even some nice &lt;span style="text-decoration: underline;"&gt;&lt;/span&gt;&lt;a href="http://mokhan.ca/"&gt;Calgarians&lt;/a&gt; ... heh heh) and had some great conversations.   Hope to see you all around!&lt;br /&gt;&lt;br /&gt;In case you missed it I did a session on Unit of Work where we implemented a hand rolled Unit of Work to demonstrate all the moving pieces: Object State, Identity Map, Repositories and Workspace.  We also talked a bit about some problems inherent in the Unit of Work including how to deal with object state during and after persistence as well as what to do when objects are getting persisted in the wrong order.&lt;br /&gt;&lt;br /&gt;You'll be able to download the code using the following two tags representing the start and completion of the session:&lt;br /&gt;http://bourgeois-codecamp.googlecode.com/svn/T4.EdCodeCamp.UnitOfWorkStart&lt;br /&gt;http://bourgeois-codecamp.googlecode.com/svn/T4.EdCodeCamp.UnitOfWorkComplete&lt;br /&gt;&lt;br /&gt;My slide deck is posted here (please forgive the tacky background):&lt;br /&gt;http://bourgeois-codecamp.googlecode.com/files/UnitofWork.ppt&lt;br /&gt;&lt;br /&gt;The Unit of Work session was actually part of a bigger course.  In this SVN repository you will find the entire contents of this day long course which covers the following topics:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Refactoring out the Code Smells&lt;/li&gt;&lt;li&gt;Introducing the Model View Presenter&lt;/li&gt;&lt;li&gt;Inversion of Control using Windsor&lt;/li&gt;&lt;li&gt;Unit of Work&lt;/li&gt;&lt;li&gt;Building with NAnt&lt;/li&gt;&lt;/ul&gt;In conversations with others I realized a lot of interest in these topics and would like to share these practices with your team.  So if you think your dev team would benefit from a day of hands-on immersion in these topics please feel free to contact me.&lt;br /&gt;&lt;hints id="hah_hints"&gt;&lt;/hints&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-7565105339704476273?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2007/10/edmonton-code-camp-07.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-2774655125990893070</guid><pubDate>Fri, 26 Oct 2007 04:56:00 +0000</pubDate><atom:updated>2007-10-25T23:09:38.383-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">boo</category><title>Booya! on a Mini-Friday</title><description>&lt;a href="http://http//blog.wekeroad.com/2007/10/25/thoughts-for-a-thursday/"&gt;Rob Conery posted a nice binary poem&lt;/a&gt; for a mini-Friday and unable to resist my curiosity I resigned myself to solving it in boo with minimal LOC:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);font-family:courier new;" &gt;for word in @/ /.Split(poem):&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);font-family:courier new;" &gt;    Console.Write(Convert.ToChar(Convert.ToByte(word.Trim(), 2)))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Once you solve that see if you can figure out &lt;a href="http://ayende.com/Blog/archive/2007/10/26/Must-resist.-decoding.aspx"&gt;Ayende's question d'jour&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;As a side note it was disheartening to realize all that I've forgotten about bits and bytes since my days in CS class.&lt;hints id="hah_hints"&gt;&lt;/hints&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-2774655125990893070?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2007/10/booya-on-mini-friday.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-772334320111382134</guid><pubDate>Sat, 13 Oct 2007 18:04:00 +0000</pubDate><atom:updated>2007-10-13T12:12:49.903-06:00</atom:updated><title>Now Serving Ice Cold Code</title><description>I've had many requests for the code I talk about on my blog and thought it would be a time saver for both myself and those requesting the code if it was available for download.  So I started a project on code.google.com and licensed it under an MIT License.  I've just checked in code for  the Raising Events in NMock and Lazy Loading .NET  Objects but I'll be committing any new stuff to this project as well.&lt;br /&gt;&lt;br /&gt;You can have access whenever the Internet is open by doing an SVN Checkout:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;svn checkout &lt;b&gt;&lt;i&gt;http&lt;/i&gt;&lt;/b&gt;://peodeas.googlecode.com/svn/trunk/ peodeas&lt;/tt&gt;&lt;hints id="hah_hints"&gt;&lt;/hints&gt;&lt;br /&gt;&lt;br /&gt;Enjoy!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-772334320111382134?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2007/10/now-serving-ice-cold-code.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-12965145644255941</guid><pubDate>Sat, 03 Mar 2007 17:18:00 +0000</pubDate><atom:updated>2007-03-03T10:55:16.366-07:00</atom:updated><title>Raising Events in NMock 2.0</title><description>One of the problems with NMock 2.0 is  the inability to raise events on a mock object.  I've run across this problem in many scenarios,  but most frequently in testing &lt;a href="http://www.martinfowler.com/eaaDev/PassiveScreen.html"&gt;passive views&lt;/a&gt;.  For example in a passive view asp.net application your interface will probably look something like:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 interface\cf0  \cf10 IAccountSummaryView\par ??\cf0     \{\par ??        \cf2 event\cf0  \cf10 EventHandler\cf0  Load;\par ??        \cf2 void\cf0  Display(\cf10 AccountSummaryDTO\cf0 [] accounts);\par ??    \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;event&lt;/span&gt; &lt;span style="color: teal;"&gt;EventHandler&lt;/span&gt; Load;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;void&lt;/span&gt; Display(&lt;span style="color: teal;"&gt;AccountSummaryDTO&lt;/span&gt;[] accounts);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt; &lt;br /&gt;The presenter then registers an EventHandler with the Load event and performs the display as follows:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 AccountSummaryPresenter\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf2 readonly\cf0  \cf10 IAccountSummaryView\cf0  view;\par ??\par ??        \cf2 public\cf0  AccountSummaryPresenter(\cf10 IAccountSummaryView\cf0  view)\par ??        \{\par ??            \cf2 this\cf0 .view = view;\par ??            HookupEventHandlers(view);\par ??        \}\par ??\par ??        \cf2 private\cf0  \cf2 static\cf0  \cf2 void\cf0  HookupEventHandlers(\cf10 IAccountSummaryView\cf0  view)\par ??        \{\par ??            view.Load += \cf2 delegate\cf0  \{ LoadAccountsInto(view); \};\par ??        \}\par ??\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 void\cf0  LoadAccountsInto(\cf10 IAccountSummaryView\cf0  view)\par ??        \{\par ??            view.Display(\cf2 new\cf0  \cf10 AccountSummaryDTO\cf0 [0]);\par ??        \}\par ??    \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: teal;"&gt;AccountSummaryPresenter&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt; view;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; AccountSummaryPresenter(&lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt; view)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.view = view;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            HookupEventHandlers(view);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; HookupEventHandlers(&lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt; view)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            view.Load += &lt;span style="color: blue;"&gt;delegate&lt;/span&gt; { LoadAccountsInto(view); };&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         \cf2 private\cf0  \cf2 static\cf0  \cf2 void\cf0  LoadAccountsInto(\cf10 IAccountSummaryView\cf0  view)\par ??        \{\par ??            view.Display(\cf2 new\cf0  \cf10 AccountSummaryDTO\cf0 [0]);\par ??        \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; LoadAccountsInto(&lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt; view)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            view.Display(GetAccountSummaries());&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;/div&gt;  &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;/p&gt;&lt;/div&gt; &lt;br /&gt;You'll notice that the &lt;span style="font-style: italic;"&gt;LoadAccountsInto &lt;/span&gt;method is not public because the call is handled internally within the presenter class.  This makes testing a little tricky since there is no facility in NMock 2.0 to raise this event and we have no way to call the &lt;span style="font-style: italic;"&gt;LoadAccountsInto &lt;/span&gt;method from our test class.&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         [Test]\par ??        \cf2 public\cf0  \cf2 void\cf0  ShouldLoadAccountsIntoViewOnPageLoad()\par ??        \{\par ??            \cf10 Expect\cf0 .Once.On(view).Method(\cf13 "Display"\cf0 ).With(\cf10 Is\cf0 .Anything);\par ??            presenter.??????\par ??            mockery.VerifyAllExpectationsHaveBeenMet();\par ??        \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;        [Test]&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ShouldLoadAccountsIntoViewOnPageLoad()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: teal;"&gt;Expect&lt;/span&gt;.Once.On(view).Method(&lt;span style="color: maroon;"&gt;"Display"&lt;/span&gt;).With(&lt;span style="color: teal;"&gt;Is&lt;/span&gt;.Anything);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            presenter.??????&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            mockery.VerifyAllExpectationsHaveBeenMet();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;/div&gt; &lt;br /&gt;&lt;br /&gt;Which more often than not results in granting the &lt;span style="font-style: italic;"&gt;LoadAccountsInto &lt;/span&gt;method public accessibility for the purpose of testing:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         [Test]\par ??        \cf2 public\cf0  \cf2 void\cf0  ShouldLoadAccountsIntoViewOnPageLoad()\par ??        \{\par ??            \cf10 Expect\cf0 .Once.On(view).Method(\cf13 "Display"\cf0 ).With(\cf10 Is\cf0 .Anything);\par ??            presenter.LoadAccountsInto(view);\par ??            mockery.VerifyAllExpectationsHaveBeenMet();\par ??        \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;        [Test]&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ShouldLoadAccountsIntoViewOnPageLoad()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: teal;"&gt;Expect&lt;/span&gt;.Once.On(view).Method(&lt;span style="color: maroon;"&gt;"Display"&lt;/span&gt;).With(&lt;span style="color: teal;"&gt;Is&lt;/span&gt;.Anything);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            presenter.LoadAccountsInto(view);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            mockery.VerifyAllExpectationsHaveBeenMet();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;/div&gt; &lt;br /&gt;Not a big deal, right?  Well, I'll admit this gets the job done.   However, whenever I find myself exposing more than is absolutely needed for a somewhat orthogonal task (such as testing, or data access) I get a sinking feeling in my gut that something just isn't right.  Perhaps its because allowing this method to run around the application with public accessability gives other developers the wrong impression about it's intended use.   And we all know code that doesn't communicate is a Bad Thing.&lt;br /&gt;&lt;br /&gt;So, I spent a few minutes today to piece together the following NMock helper classes that will allow me to simulate raising an event:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 MockEvent\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf10 EventHandler\cf0  handler;\par ??\par ??        \cf2 public\cf0  \cf2 void\cf0  Initialize(\cf10 EventHandler\cf0  handler)\par ??        \{\par ??            \cf2 this\cf0 .handler = handler;\par ??        \}\par ??\par ??        \cf2 public\cf0  \cf2 void\cf0  Raise()\par ??        \{\par ??            handler.Invoke(\cf2 null\cf0 , \cf2 null\cf0 );\par ??        \}\par ??\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf10 IAction\cf0  Hookup(\cf10 MockEvent\cf0  mockEvent)\par ??        \{\par ??            \cf2 return\cf0  \cf2 new\cf0  \cf10 MockEventHookup\cf0 (mockEvent);\par ??        \}\par ??    \}\par ??\par ??    \cf2 public\cf0  \cf2 class\cf0  \cf10 MockEventHookup\cf0  : \cf10 IAction\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf2 readonly\cf0  \cf10 MockEvent\cf0  mockEvent;\par ??\par ??        \cf2 public\cf0  MockEventHookup(\cf10 MockEvent\cf0  mockEvent)\par ??        \{\par ??            \cf2 this\cf0 .mockEvent = mockEvent;\par ??        \}\par ??\par ??        \cf2 public\cf0  \cf2 void\cf0  Invoke(\cf10 Invocation\cf0  invocation)\par ??        \{\par ??            \cf10 EventHandler\cf0  handler = invocation.Parameters[0] \cf2 as\cf0  \cf10 EventHandler\cf0 ;\par ??            \cf2 if\cf0  (handler == \cf2 null\cf0 )\par ??                \cf2 throw\cf0  \cf2 new\cf0  \cf10 Exception\cf0 (\cf13 "Unknown event handler type."\cf0 );\par ??            mockEvent.Initialize(handler);\par ??        \}\par ??\par ??        \cf2 public\cf0  \cf2 void\cf0  DescribeTo(\cf10 TextWriter\cf0  writer)\par ??        \{\par ??            \cf11 // do nothing\par ??\cf0         \}\par ??    \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: teal;"&gt;EventHandler&lt;/span&gt; handler;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize(&lt;span style="color: teal;"&gt;EventHandler&lt;/span&gt; handler)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.handler = handler;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Raise()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            handler.Invoke(&lt;span style="color: blue;"&gt;null&lt;/span&gt;, &lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: teal;"&gt;IAction&lt;/span&gt; Hookup(&lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt; mockEvent)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;MockEventHookup&lt;/span&gt;(mockEvent);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: teal;"&gt;MockEventHookup&lt;/span&gt; : &lt;span style="color: teal;"&gt;IAction&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt; mockEvent;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; MockEventHookup(&lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt; mockEvent)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.mockEvent = mockEvent;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Invoke(&lt;span style="color: teal;"&gt;Invocation&lt;/span&gt; invocation)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: teal;"&gt;EventHandler&lt;/span&gt; handler = invocation.Parameters[0] &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: teal;"&gt;EventHandler&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (handler == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;throw&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;Exception&lt;/span&gt;(&lt;span style="color: maroon;"&gt;"Unknown event handler type."&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            mockEvent.Initialize(handler);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DescribeTo(&lt;span style="color: teal;"&gt;TextWriter&lt;/span&gt; writer)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: green;"&gt;// do nothing&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt; &lt;br /&gt;The &lt;span style="font-style: italic;"&gt;MockEvent&lt;/span&gt; class is simply a wrapper around the handler that gets registered with the exposed event on the mock object.  Right now the MockEvent only handles the handlers of type &lt;span style="font-style: italic;"&gt;EventHandler&lt;/span&gt;.  Its pretty simplistic but I'm sure you can figure out how to extend it for your own purposes.&lt;br /&gt;&lt;br /&gt;Here's what my test looks like now:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     [\cf10 TestFixture\cf0 ]\par ??    \cf2 public\cf0  \cf2 class\cf0  \cf10 AccountSummaryPresenterTest\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf10 Mockery\cf0  mockery;\par ??        \cf2 private\cf0  \cf10 IAccountSummaryView\cf0  view;\par ??        \cf2 private\cf0  \cf10 MockEvent\cf0  loadEvent;\par ??        \cf2 private\cf0  \cf10 AccountSummaryPresenter\cf0  presenter;\par ??\par ??        [\cf10 SetUp\cf0 ]\par ??        \cf2 public\cf0  \cf2 void\cf0  SetUp()\par ??        \{\par ??            mockery = \cf2 new\cf0  \cf10 Mockery\cf0 ();\par ??            view = mockery.NewMock&lt;\cf10 IAccountSummaryView\cf0 &gt;();\par ??            loadEvent = \cf2 new\cf0  \cf10 MockEvent\cf0 ();\par ??            \cf10 Expect\cf0 .Once.On(view).EventAdd(\cf13 "Load"\cf0 , \cf10 Is\cf0 .Anything).Will(\cf10 MockEvent\cf0 .Hookup(loadEvent));\par ??            presenter = \cf2 new\cf0  \cf10 AccountSummaryPresenter\cf0 (view);\par ??        \}\par ??\par ??        [Test]\par ??        \cf2 public\cf0  \cf2 void\cf0  ShouldLoadAccountsIntoViewOnPageLoad()\par ??        \{\par ??            \cf10 Expect\cf0 .Once.On(view).Method(\cf13 "Display"\cf0 ).With(\cf10 Is\cf0 .Anything);\par ??            loadEvent.Raise();\par ??            mockery.VerifyAllExpectationsHaveBeenMet();\par ??        \}\par ??    \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;    [&lt;span style="color: teal;"&gt;TestFixture&lt;/span&gt;]&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: teal;"&gt;AccountSummaryPresenterTest&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: teal;"&gt;Mockery&lt;/span&gt; mockery;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt; view;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt; loadEvent;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: teal;"&gt;AccountSummaryPresenter&lt;/span&gt; presenter;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        [&lt;span style="color: teal;"&gt;SetUp&lt;/span&gt;]&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SetUp()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            mockery = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;Mockery&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            view = mockery.NewMock&lt;&lt;span style="color: teal;"&gt;IAccountSummaryView&lt;/span&gt;&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            loadEvent = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: teal;"&gt;Expect&lt;/span&gt;.Once.On(view).EventAdd(&lt;span style="color: maroon;"&gt;"Load"&lt;/span&gt;, &lt;span style="color: teal;"&gt;Is&lt;/span&gt;.Anything).Will(&lt;span style="color: teal;"&gt;MockEvent&lt;/span&gt;.Hookup(loadEvent));&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            presenter = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;AccountSummaryPresenter&lt;/span&gt;(view);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        [Test]&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ShouldLoadAccountsIntoViewOnPageLoad()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: teal;"&gt;Expect&lt;/span&gt;.Once.On(view).Method(&lt;span style="color: maroon;"&gt;"Display"&lt;/span&gt;).With(&lt;span style="color: teal;"&gt;Is&lt;/span&gt;.Anything);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            loadEvent.Raise();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            mockery.VerifyAllExpectationsHaveBeenMet();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt; &lt;br /&gt;Hope this helps somebody else out there sleep better at night ;-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-12965145644255941?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2007/03/raising-events-in-nmock-20.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">13</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-116346966301099692</guid><pubDate>Tue, 14 Nov 2006 00:52:00 +0000</pubDate><atom:updated>2006-11-13T19:13:06.600-07:00</atom:updated><title>Cool Things From Bootcampers</title><description>Last weekend graduates of the first &lt;a href="http://www.thoughtworks.com/blog-21-2-05.html"&gt;ThoughtWorks Bootcamp&lt;/a&gt; conglomerated in Chicago for a weekend of American beer; sailing lake Michigan in weather even us Canadians complained about and a large mushroom, bacon, mashed potato and clam pizza.  The accomplishments of this group over the past couple of years impressed me so much that I wanted to take some time to plug some of their efforts.&lt;br /&gt;&lt;br /&gt;Vipul Kasera built a website to help reduce the rampant traffic congestion of Bangalore's streets.  The population explosion in Bangalore has left the city short on infrastructure and facing massive pollution.  Vipul's idea will not only save people in commuting time but will help to reduce the amount of greenhouse gas dumped into the atmosphere.  This site is the first of its kind in India and is a huge success boasting over 2200 carpoolers!   &lt;a href="http://www.commuteeasy.com"&gt;http://www.commuteeasy.com&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;Jacob Northey, one of the most proficient coders I've ever met, recently left TW to start up his own business.  I have to mention Jake is so fast that if you turn away to take a sip of sludgy Indian coffee while pairing with him he's already replaced your ugly nested conditional into a slick and concise strategy pattern using delegates.  Jake's new project sounds like it will be popular with all those finance guys I know who define their life through spreadsheets.  The software will import formulas in an Excel spreadsheet to drive a structured trading application so any self respecting finance guy with Excel experience can define their own trading software.  I hope to hear more about this exciting project as it reaches the market:   &lt;a href="http://lasalletech.com"&gt;&lt;span style="text-decoration: underline;"&gt;http://lasalletech.com&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Desi McAdam has brought together female developers from around the world with her girls only developer community.  No boys allowed in this one, but that's OK by us guys.  The software profession is weak in female presence and would improve in credibility and quality with a better balance of genders.  A community such as this could influence more women to become software professionals.  Quickly growing in popularity, Desi is hoping to have a devchix conference in the near future.   &lt;a href="http://devchix.com/"&gt;http://devchix.com&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Ian Carvell, meanwhile, is perfecting the art of &lt;a href="http://www.mcphee.com/items/11305.html"&gt;chicken chucking&lt;/a&gt;.  When he determines the most efficient trajectory we will rendevous in a shady lounge where almost everyone's stuff will get stolen; Suzie will experience her &lt;span style="font-weight: bold;"&gt;second &lt;/span&gt;ever fight; Chris will disappear across five of the six points in search of an after hours club for all and as a happy group of bootcampers who have become all to comfortable with mayhem and chaos will watch a plastic chicken sail from the twelfth floor across Michigan avenue.&lt;br /&gt;&lt;br /&gt;So long (for now) and thanks for all the clams, Poonfish.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-116346966301099692?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/11/cool-things-from-bootcampers.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-116313639316374270</guid><pubDate>Fri, 10 Nov 2006 05:22:00 +0000</pubDate><atom:updated>2006-11-09T22:26:33.763-07:00</atom:updated><title>The Myth of Comments</title><description>Its been hammered into us since we took our first seat in the programming 101 lecture hall.  Its been dogmatically programmed into each of us as if a religious script.  I have seen developers scoff at code that lacks them and mistrust the behavior of said software.  Because the myth crosses the boundary between technical and non-technical I've seen project managers require them and salesmen tout them.  Of course, I'm talking about comments and the myth that they are an undisputed Good Thing.  If the ultimate goal for a software project is to deliver business value the penultimate goal should be to communicate the what and how of that value.  I simply do not believe that comments are the best way to communicate.&lt;br /&gt;&lt;br /&gt;Developers have become such slaves to the myth that they start writing comments solely for the sake of writing a comment.   This usually results in wasted lines on the screen telling me something that I as a developer should be able to infer from the code itself.  I feel ill when I read code that looks like this:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16             \cf11 // initialize an integer and set its default value to zero\par ??\cf0             \cf2 int\cf0  i = 0;\par ??            \cf11 // increment i\par ??\cf0             i++;} --&gt; &lt;div face="Courier New" size="8pt" color="black" style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         \cf2 public\cf0  \cf2 void\cf0  DoSomething(\cf2 double\cf0  r)\par ??        \{\par ??            \cf11 // Calculate the area of a circle using Area=Pi*radius^2\par ??\cf0             \cf11 // where PI = 3.14\par ??\cf0             \cf2 double\cf0  a = 3.14 * r * r;\par ??            foo(a);\par ??            bar(a);\par ??        \}} --&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16             \cf11 // initialize an integer and set its value to zero\par ??\cf0             \cf2 int\cf0  i = 0;\par ??            \cf11 // increment i\par ??\cf0             i++;} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: green;"&gt;// initialize an integer and set its value to zero&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 0;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: green;"&gt;// increment i&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            i++;&lt;/p&gt; &lt;/div&gt;  &lt;/div&gt;&lt;br /&gt;Yet this style of comments run rampant in posted code (yes even code posted on "reputable" sites such as CodeProject and MSDN), open source and commercial code!  Commenting for the sake of commenting is an insult to the software profession.  Please don't waste our time with these comments.  Strip them out completely.&lt;br /&gt;&lt;br /&gt;My foremost complaint is that comments (like other forms of documentation) can go out of sync with the code.  Anecdotally, I've seen plenty of Perl scripts with concise algorithm explanations  embedded in the code that differ completely from the actual behavior of that script.  In this case the comments become not only an annoyance but a real danger as developers trusted the comments as if it were code.  But comments are not code!  Comments are not business value!   Why should we trust the communicated message from anything but the code itself?&lt;br /&gt;&lt;br /&gt;While I appreciate the spirit of communication that motivates comments, I think as software professionals we can do far better to fully communicate.  For starters, we can strongly bind the message we are communicating to the business value itself by introducing a method through refactoring that is named by the message itself.    For example, consider changing the following code from:&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         \cf2 public\cf0  \cf2 void\cf0  DoSomething(\cf2 double\cf0  r)\par ??        \{\par ??            \cf11 // Calculate the area of a circle using Area=Pi*radius^2\par ??\cf0             \cf11 // where PI = 3.14\par ??\cf0             \cf2 double\cf0  a = 3.14 * r * r;\par ??            foo(a);\par ??            bar(a);\par ??        \}} --&gt; &lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DoSomething(&lt;span style="color: blue;"&gt;double&lt;/span&gt; r)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: green;"&gt;// Calculate the area of a circle using Area=Pi*radius^2&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: green;"&gt;// where PI = 3.14&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;double&lt;/span&gt; a = 3.14 * r * r;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            foo(a);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            bar(a);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;/div&gt; &lt;br /&gt;&lt;br /&gt;To:&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         \cf2 public\cf0  \cf2 void\cf0  DoSomething(\cf2 double\cf0  r)\par ??        \{\par ??            \cf11 // The area of a circle is Area=Pi*radius^2\par ??\cf0             \cf2 double\cf0  a = PI*r*r;\par ??            foo(a);\par ??            bar(a);\par ??        \}} --&gt; &lt;div face="Courier New" size="8pt" color="black" style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         \cf2 double\cf0  PI = 3.14;\par ??        \par ??        \cf2 public\cf0  \cf2 void\cf0  DoSomethingElse(\cf2 double\cf0  radius)\par ??        \{\par ??            \cf2 double\cf0  area = CalculateAreaOfCircle(radius);\par ??            foo(area);\par ??            bar(area);\par ??        \}\par ??\par ??        \cf2 private\cf0  \cf2 double\cf0  CalculateAreaOfCircle(\cf2 double\cf0  radius)\par ??        \{\par ??            \cf2 return\cf0  PI*SquareOf(radius);\par ??        \}\par ??\par ??        \cf2 private\cf0  \cf2 static\cf0  \cf2 double\cf0  SquareOf(\cf2 double\cf0  radius)\par ??        \{\par ??            \cf2 return\cf0  \cf10 Math\cf0 .Pow(radius, 2);\par ??        \}} --&gt; &lt;/p&gt;&lt;div style="background: white none repeat scroll 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;double&lt;/span&gt; PI = 3.14;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DoSomethingElse(&lt;span style="color: blue;"&gt;double&lt;/span&gt; radius)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;double&lt;/span&gt; area = CalculateAreaOfCircle(radius);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            foo(area);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            bar(area);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;double&lt;/span&gt; CalculateAreaOfCircle(&lt;span style="color: blue;"&gt;double&lt;/span&gt; radius)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; PI*SquareOf(radius);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;double&lt;/span&gt; SquareOf(&lt;span style="color: blue;"&gt;double&lt;/span&gt; radius)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: teal;"&gt;Math&lt;/span&gt;.Pow(radius, 2);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;/div&gt;  &lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16         \cf2 public\cf0  \cf2 void\cf0  DoSomething(\cf2 double\cf0  r)\par ??        \{\par ??            \cf11 // Calculate the area of a circle using Area=Pi*radius^2\par ??\cf0             \cf11 // where PI = 3.14\par ??\cf0             \cf2 double\cf0  a = 3.14 * r * r;\par ??            foo(a);\par ??            bar(a);\par ??        \}} --&gt;&lt;/div&gt;The result is an explanation as concise as the original comment.  The difference is that when the code changes, so MUST the message.  As a believer in and practitioner of software professionalism I am more comfortable knowing that the behavior and message will not go out of sync.&lt;br /&gt;&lt;br /&gt;In fact, a goal of software professionals in modern languages should be to bind message to behavior by making the code itself read like a book.  Lets lose the grip of the myth and shift from being coding automatons to thoughtful individuals passionate in our pursuit of improving the state and image of our profession by improving the quality of our primary deliverables:  business value and the communication of that business value.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-116313639316374270?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/11/myth-of-comments.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-116145134728779948</guid><pubDate>Sat, 21 Oct 2006 14:58:00 +0000</pubDate><atom:updated>2006-10-21T11:30:40.320-06:00</atom:updated><title>Objects: Not Just For Breakfast Anymore</title><description>Objects in OO languages are a powerful concept.  They encourage collecting data and behaviour in highly cohesive bundles that effectively communicate a concept to a human.  Effectively a block of memory, objects are technically very simple.  In the managed world, we don't even need to worry about the memory because a garbage collector will clean up after us.  Yet despite their power and simplicity objects are rarely employed to their full potential.  Most applications contain a few coarse grained objects and a bedlam of procedural artifacts such as constant strings, integers and enumerations floating out in the global ether.  So why are we as developers so hesitant to create new objects?&lt;br /&gt;&lt;br /&gt;While there is nothing wrong with using enumerations in applications, there is really nothing gained from using them aside from the syntactic pattern.  An enumeration is simply a wrapper around an integer (or any other primitive) to help communicate a concept to a human reading the code.  The fact that it is an integer is irrelevant (at least it should be, but I'll  get to that later).   Rather than masquerading an integer as a type or concept, why not pass around an object? Try replacing your enumerations with something like this:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 UnitOfMeasure\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf2 object\cf0  Meters = \cf2 new\cf0  \cf2 object\cf0 ();\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf2 object\cf0  Feet = \cf2 new\cf0  \cf2 object\cf0 ();\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 abstract\cf0  \cf2 class\cf0  \cf10 UnitOfMeasure\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf2 object\cf0  Meters = \cf2 new\cf0  \cf2 object\cf0 ();\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf2 object\cf0  Feet = \cf2 new\cf0  \cf2 object\cf0 ();\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;abstract&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Meters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Feet = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;  &lt;/div&gt;&lt;br /&gt;This class is abstract so it can never be instantiated and the public fields inside of it are immutable.  Now it is an immutable memory address that we are passing around rather than an integer value.  We can do things like:&lt;br /&gt;  &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16 units.Equals(\cf10 UnitOfMeasure\cf0 .Meters)} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt; &lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 Quantity\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf2 decimal\cf0  value;\par ??        \cf2 private\cf0  \cf2 object\cf0  units;\par ??        \par ??        \cf2 public\cf0  \cf2 void\cf0  DoSomething()\par ??        \{\par ??            \cf2 if\cf0 (units.Equals(\cf10 UnitOfMeasure\cf0 .Meters))\par ??            \{\par ??                \cf11 // do something\par ??\cf0             \}\par ??        \}\par ??    \}} --&gt; &lt;/p&gt;&lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;Quantity&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; value;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; units;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; DoSomething()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(units.Equals(&lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt;.Meters))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                &lt;span style="color:green;"&gt;// do something&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;  &lt;p style="margin: 0px;"&gt;&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;But you'll notice because we are passing around a naked object we lose some type safety.  By giving a constructor to the class we gain a powerful type safe constant:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 UnitOfMeasure\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf10 UnitOfMeasure\cf0  Meters = \cf2 new\cf0  \cf10 UnitOfMeasure\cf0 ();\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf10 UnitOfMeasure\cf0  Feet = \cf2 new\cf0  \cf10 UnitOfMeasure\cf0 ();\par ??\par ??        \cf2 private\cf0  UnitOfMeasure()\par ??        \{\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt; Meters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt; Feet = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; UnitOfMeasure()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;  Now our Quantity class communicates that units are a UnitOfMeasure:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 Quantity\par ??\cf0     \{\par ??        \cf2 private\cf0  \cf2 decimal\cf0  value;\par ??        \cf2 private\cf0  \cf10 UnitOfMeasure\cf0  units;\par ??        \par ??        \cf2 public\cf0  \cf2 void\cf0  DoSomething()\par ??        \{\par ??            \cf2 if\cf0 (units.Equals(\cf10 UnitOfMeasure\cf0 .Meters))\par ??            \{\par ??                \cf11 // do something\par ??\cf0             \}\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;Quantity&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; value;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt; units;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; DoSomething()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(units.Equals(&lt;span style="color:teal;"&gt;UnitOfMeasure&lt;/span&gt;.Meters))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                &lt;span style="color:green;"&gt;// do something&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Applications are also littered with constant strings.  Often the strings are abused and conditional logic is based on the value of those constant strings.  In a recent application I caught myself in a stringy trap by passing around units as naked strings.  While this worked when we were only dealing with length units as soon as temperature units were added to the codebase the application got messed up.  Temperature strings were being passed in where length strings were expected and caused some confusing and difficult to debug problems.  So I replaced my string constants with objects:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 LengthUnits\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 readonly\cf0  \cf2 static\cf0  \cf10 LengthUnits\cf0  Metric = \cf2 new\cf0  \cf10 LengthUnits\cf0 ();\par ??        \cf2 public\cf0  \cf2 readonly\cf0  \cf2 static\cf0  \cf10 LengthUnits\cf0  Imperial = \cf2 new\cf0  \cf10 LengthUnits\cf0 ();\par ??\par ??        \cf2 private\cf0  LengthUnits()\par ??        \{\par ??        \}\par ??    \}} --&gt; &lt;div    style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;LengthUnit&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:teal;"&gt;LengthUnit&lt;/span&gt; Metric = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;LengthUnit&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:teal;"&gt;LengthUnit&lt;/span&gt; Imperial = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;LengthUnit&lt;/span&gt;();&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; LengthUnit()&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;And a similar class for temperature units.  This introduced some type safety and helps to communicate what exactly is expected.  After this refactoring I couldn't even compile if I was passing in the wrong unit of measure type.&lt;br /&gt;&lt;br /&gt;This is all fine and good in the simple case where constants are passed around to identify types.  However, we can also get into situations where we've abused enumerations and strings to the point where a simple object replacement won't suffice.  For example, in an enumeration the integer value means more than just a type, its also used as a value in &lt;gasp&gt; a calculation.  Or perhaps the string constant is used to display something to the user.&lt;br /&gt;&lt;br /&gt;Well, fortunately it is this precise situation where objects really shine.  Since we can associate  multiple pieces of data and behaviour with an object there is no need to abuse strings or enumerations anymore.  Lets look at some code:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 enum\cf0  \cf10 UnitsOfMeasure\par ??\cf0     \{\par ??        Meters = 3.28m,\par ??        Feet = 0.3048m       \par ??    \}\par ??    \par ??    \cf2 public\cf0  \cf2 class\cf0  \cf10 SomeClass\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 decimal\cf0  AddOneMeterTo(\cf2 decimal\cf0  value, \cf10 UnitsOfMeasure\cf0  units)\par ??        \{\par ??            \cf2 decimal\cf0  oneMeter = 1m;\par ??            \cf2 if\cf0 (units.Equals(\cf10 UnitsOfMeasure\cf0 .Feet))\par ??            \{\par ??                oneMeter += oneMeter*\cf10 Convert\cf0 .ToDecimal(\cf10 UnitsOfMeasure\cf0 .Feet);\par ??            \}\par ??            \cf2 return\cf0  value + oneMeter;\par ??        \}\par ??    \}} --&gt; &lt;/gasp&gt;&lt;div style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: Courier New; font-size: 8pt; color: black;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;enum&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        Meters = 3.28m,&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        Feet = 0.3048m       &lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;SomeClass&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; AddOneMeterTo(&lt;span style="color:blue;"&gt;decimal&lt;/span&gt; value, &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt; units)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; oneMeter = 1m;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(units.Equals(&lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;.Feet))&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;                oneMeter += oneMeter*&lt;span style="color:teal;"&gt;Convert&lt;/span&gt;.ToDecimal(&lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;.Feet);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; value + oneMeter;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;In this scenario I want to add 1 meter to some value.  If the original value is in feet, however, I want to convert 1 meter to the imperial equavalent.  &lt;sarcasm&gt;Fortunately, I've created an enumeration that contains the conversion from meters to feet.  Right?&lt;/sarcasm&gt;&lt;br /&gt;&lt;br /&gt;Well, after re-reading this code I felt shame.  My fellow developers won't know what that mysterious 3.28 and 0.3048 mean.  Plus conversion is a common method that will likely be applied in other places in our codebase so there's potential for code duplication.  I also don't like the conditional around feet and only applying the conversion if it is feet.  So, I opted to refactor to a more OO solution as follows:&lt;br /&gt;&lt;br /&gt;&lt;!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs16     \cf2 public\cf0  \cf2 class\cf0  \cf10 UnitsOfMeasure\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf10 UnitsOfMeasure\cf0  Meters = \cf2 new\cf0  \cf10 UnitsOfMeasure\cf0 (1m);\par ??        \cf2 public\cf0  \cf2 static\cf0  \cf2 readonly\cf0  \cf10 UnitsOfMeasure\cf0  Feet = \cf2 new\cf0  \cf10 UnitsOfMeasure\cf0 (0.3048m);\par ??        \par ??        \cf2 private\cf0  \cf2 readonly\cf0  \cf2 decimal\cf0  conversionFactor;\par ??\par ??        \cf2 private\cf0  UnitsOfMeasure(\cf2 decimal\cf0  conversionFactor)\par ??        \{\par ??            \cf2 this\cf0 .conversionFactor = conversionFactor;\par ??        \}      \par ??        \par ??        \cf2 public\cf0  \cf2 decimal\cf0  ConvertTo(\cf2 decimal\cf0  value, \cf10 UnitsOfMeasure\cf0  convertToUnits)\par ??        \{\par ??            \cf2 return\cf0  value*conversionFactor/convertToUnits.conversionFactor;\par ??        \}\par ??    \}\par ??    \par ??    \cf2 public\cf0  \cf2 class\cf0  \cf10 SomeClass\par ??\cf0     \{\par ??        \cf2 public\cf0  \cf2 decimal\cf0  AddOneMeterTo(\cf2 decimal\cf0  value, \cf10 UnitsOfMeasure\cf0  units)\par ??        \{\par ??            \cf2 decimal\cf0  oneMeter = 1m;            \par ??            \cf2 return\cf0  value + \cf10 UnitsOfMeasure\cf0 .Meters.ConvertTo(oneMeter, units);\par ??        \}\par ??    \}} --&gt; &lt;div face="Courier New" size="8pt" color="black" style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt; Meters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;(1m);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt; Feet = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;(0.3048m);&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; conversionFactor;&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; UnitsOfMeasure(&lt;span style="color:blue;"&gt;decimal&lt;/span&gt; conversionFactor)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;this&lt;/span&gt;.conversionFactor = conversionFactor;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }      &lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; ConvertTo(&lt;span style="color:blue;"&gt;decimal&lt;/span&gt; value, &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt; convertToUnits)&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; value*conversionFactor/convertToUnits.conversionFactor;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;p style="margin: 0px;"&gt; &lt;/p&gt; &lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;SomeClass&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    {&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;decimal&lt;/span&gt; AddOneMeterTo(&lt;span style="color:blue;"&gt;decimal&lt;/span&gt; value, &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt; units)&lt;/p&gt;  &lt;p style="margin: 0px;"&gt;        {&lt;br /&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; value + &lt;span style="color:teal;"&gt;UnitsOfMeasure&lt;/span&gt;.Meters.ConvertTo(1m, units);&lt;/p&gt; &lt;p style="margin: 0px;"&gt;        }&lt;/p&gt; &lt;p style="margin: 0px;"&gt;    }&lt;/p&gt; &lt;/div&gt;&lt;br /&gt;Notice how the conversion behaviour was pushed back into the UnitsOfMeasure class, where it rightfully belongs.  Because UnitsOfMeasure now know how to convert to each other I removed the need for the conditional and the potential for code duplication.  Finally, UnitsOfMeasure is now a highly cohesive class that communicates to humans that those magic numbers are actually conversion factors.&lt;br /&gt;&lt;br /&gt;In summary, finely grained objects can be used as a replacement to string constants and enumerations.  Similar data and behaviour can be collected in objects to create highly cohesive classes which helps reduce code duplication and is an effective way to communicate concepts to other developers.  Objects are also an effective use of type safety to ensure that the correct constants are being passed around.  Starting out with constant objects rather than strings or enumerations also sets you up for simpler refactorings.  For example, moving conversion behaviour to the UnitsOfMeasure class would've been a trivial refactoring if we had started with constant objects rather than enumerations.  So think twice before introducing another string constant or enumeration into your application and consider the option of employing objects to their full potential.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-116145134728779948?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/10/objects-not-just-for-breakfast-anymore.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-115657780366435910</guid><pubDate>Sat, 26 Aug 2006 07:36:00 +0000</pubDate><atom:updated>2006-08-26T01:42:47.963-06:00</atom:updated><title>Lazy Loading Using .NET Remoting Proxy</title><description>&lt;p&gt;One problem with a rich domain layer is that the reconsitution of&amp;nbsp;aggregate objects&amp;nbsp;from a persistence layer can take a long time.&amp;nbsp; Furthermore, quite frequently not all objects within the aggregate are even called upon during it's lifespan.&amp;nbsp; This means the time and elbow&amp;nbsp;grease&amp;nbsp;your application&amp;nbsp;spent pulling data from the database and building up the domain object was pretty much in vain.&lt;/p&gt; &lt;p&gt;Performance efficiencies can be realized by deferring the reconstitution of the aggregate's internal objects until they are actually needed.&amp;nbsp; This is a Fowler EAA pattern called &lt;a href="http://www.martinfowler.com/eaaCatalog/lazyLoad.html"&gt;Lazy Loading&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&amp;nbsp;In .NET Lazy Loading is&amp;nbsp;often implemented as an ugly mess of Properties such as:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public class Customer&lt;br&gt;{&lt;br&gt;private int id;&lt;br&gt;private string name;&lt;br&gt;private string description; &lt;br&gt;private bool isLoaded = false; &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public int Id&lt;br&gt;{&lt;br&gt;get { return id; }&lt;br&gt;set { id = value; }&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public string Name&lt;br&gt;{&lt;br&gt;get { &lt;br&gt;if(!isLoaded)&lt;br&gt;Load();&lt;br&gt;return name; &lt;br&gt;}&lt;br&gt;set { &lt;br&gt;if(!isLoaded)&lt;br&gt;Load();&lt;br&gt;name = value; &lt;br&gt;}&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public string Description&lt;br&gt;{&lt;br&gt;get { &lt;br&gt;if(!isLoaded)&lt;br&gt;Load();&lt;br&gt;return description; &lt;br&gt;}&lt;br&gt;set { &lt;br&gt;if(!isLoaded)&lt;br&gt;Load();&lt;br&gt;description = value; &lt;br&gt;}&lt;br&gt;}&lt;br&gt;}&lt;/font&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This flavour of Lazy Loading is called Lazy Initialization and&amp;nbsp;has&amp;nbsp;two main problems.&amp;nbsp; First is code duplication.&amp;nbsp; Any class that is lazy loaded first does a check to see if its been loaded and then makes a call to the database to actually load the object.&amp;nbsp; Ideally this&amp;nbsp;sequence should be refactored and reused among all objects that are lazy loaded.&lt;/p&gt; &lt;p&gt;The second problem with this code is that&amp;nbsp;domain objects become littered with&amp;nbsp;code&amp;nbsp;unrelated to&amp;nbsp;their&amp;nbsp;sole responsibility.&amp;nbsp;&amp;nbsp;This impacts the communicability&amp;nbsp;and simplicity of domain objects and makes&amp;nbsp;the code more difficult to maintain.&amp;nbsp; I prefer my domain objects to be pure.&amp;nbsp; They should have no knowledge about how or when they or their data gets reconstituted.&lt;/p&gt; &lt;p&gt;Ideally, I would like an object that looks like a Customer (and can, in fact, be used as a Customer object) to sit as a proxy in the getter of my&amp;nbsp;aggregate root.&amp;nbsp; This proxy can be passed around and will remain an empty shell of a Customer until&amp;nbsp;one of the properties is requested at which point the proxy would fetch the data to reconstitute the Customer.&lt;/p&gt; &lt;p&gt;This flavour of lazy loading is called Virtual Proxy and can be achieved in .NET with the help of the Sytem.Remoting.Proxies namespace.&lt;/p&gt; &lt;p&gt;Essentially we can derive a proxy class from RealProxy that will intercept calls to the getters and setters of our domain object.&amp;nbsp; When the LazyLoadProxy receives a message for a getter it will first&amp;nbsp;load the class from the database then continue to invoke the getter.&amp;nbsp; If the LazyLoadProxy receives a message for a setter it will first load the domain object then continue to invoke the setter.&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public class LazyLoadProxy : RealProxy&lt;br&gt;{&lt;br&gt;private readonly IGhostable subject; &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;[PermissionSet(SecurityAction.LinkDemand)]&lt;br&gt;public LazyLoadProxy(int id, Type type) : base(type)&lt;br&gt;{&lt;br&gt;&amp;nbsp; subject = (IGhostable) Activator.CreateInstance(type);&lt;br&gt;&amp;nbsp; InitializeAsGhost(id);&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;private void InitializeAsGhost(int id)&lt;br&gt;{&lt;br&gt;&amp;nbsp; subject.Id = id;&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]&lt;br&gt;public override IMessage Invoke(IMessage msg)&lt;br&gt;{&lt;br&gt;&amp;nbsp; MethodCallMessageWrapper wrappedMessage = new MethodCallMessageWrapper((IMethodCallMessage) msg);&lt;br&gt;&amp;nbsp; MethodInfo invokedMethodInfo = (MethodInfo)&amp;nbsp;&amp;nbsp; wrappedMessage.MethodBase;&lt;br&gt;InvokeStrategy strategy = GetInvokeStrategy(invokedMethodInfo);&lt;br&gt;return strategy.InvokeMethod(subject, invokedMethodInfo, wrappedMessage);&lt;br&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font color="#000000"&gt;You'll notice in the above code that I've introduced the term Ghost.&amp;nbsp; Ghosting is just another flavour of lazy loading where you have a real object without any data (or partial data).&amp;nbsp; In this case, our&amp;nbsp;ghost object knows its Id.&amp;nbsp; When it comes time&amp;nbsp;to reconstitute the domain object our ghost object can do so by passing the&amp;nbsp;Id into the&amp;nbsp;persistence&amp;nbsp;object.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font color="#000000"&gt;Now, we will implement the IGhostable interface&amp;nbsp;in the domain objects we want to lazy load.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public interface IGhostable&lt;br&gt;{&lt;br&gt;int Id { get; set; }&lt;br&gt;bool IsGhost { get; }&lt;br&gt;bool IsLoading { get; }&lt;br&gt;void MarkLoaded();&lt;br&gt;void MarkLoading();&lt;br&gt;void Load(IGhostable subject);&lt;br&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;But to&amp;nbsp;prevent duplication bloat in&amp;nbsp;our code we can inherit from this Layer Supertype called GhostObject.&amp;nbsp; The GhostObject class is responsible for knowing when to reconstitute the subclass.&amp;nbsp; It looks a little like:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public abstract class GhostObject&amp;lt;T&amp;gt; : MarshalByRefObject where T : IGhostable&lt;br&gt;{&lt;br&gt;private readonly object GHOST = new object();&lt;br&gt;private readonly object LOADING = new object();&lt;br&gt;private readonly object LOADED = new object();&lt;br&gt;private object status; &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;protected GhostObject()&lt;br&gt;{&lt;br&gt;status = GHOST;&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public bool IsGhost&lt;br&gt;{&lt;br&gt;get { return status == GHOST; }&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public bool IsLoading&lt;br&gt;{&lt;br&gt;get { return status == LOADING; }&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public void MarkLoaded()&lt;br&gt;{&lt;br&gt;status = LOADED;&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public void MarkLoading()&lt;br&gt;{&lt;br&gt;status = LOADING;&lt;br&gt;}&lt;br&gt;public void Load(IGhostable subject)&lt;br&gt;{&lt;br&gt;ILoader&amp;lt;T&amp;gt; loader = LoaderRegistry.GetLoader&amp;lt;T&amp;gt;();&lt;br&gt;subject.MarkLoading();&lt;br&gt;loader.Load((T) subject);&lt;br&gt;subject.MarkLoaded(); &lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public static T CreateGhost(int id)&lt;br&gt;{&lt;br&gt;return (T) new LazyLoadProxy(id, typeof(T)).GetTransparentProxy();&lt;br&gt;} &lt;br&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Verdana" color="#000000" size="2"&gt;You should be aware of two things.&amp;nbsp; First, the GhostObject class inherits from MarshalByRefObject.&amp;nbsp; This is a prerequisite for the class to be used by RealProxy.&amp;nbsp; Also&amp;nbsp;note that&amp;nbsp;the GetTransparentProxy() method of RealProxy is getting called from the CreateGhost method.&amp;nbsp; This is what generates a class that is a proxy for the subclass.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Now,&amp;nbsp;our Customer&amp;nbsp;achieves a pure state of domain object zen.&amp;nbsp; As for me, having lost the clutter of lazy initialization from this object I can once again sleep soundly at night.&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public class Customer: GhostObject&amp;lt;Customer&amp;gt;, IGhostable&lt;br&gt;{&lt;br&gt;private int id;&lt;br&gt;private string name;&lt;br&gt;private string description; &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public int Id&lt;br&gt;{&lt;br&gt;get { return id; }&lt;br&gt;set { id = value; }&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public string Name&lt;br&gt;{&lt;br&gt;get { return name; }&lt;br&gt;set { name = value; }&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;public string Description&lt;br&gt;{&lt;br&gt;get { return description; }&lt;br&gt;set { description = value; }&lt;br&gt;}&lt;br&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;And pulling it all together results in a test that looks like:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;[TestFixture]&lt;br&gt;public class CustomerTest&lt;br&gt;{&lt;br&gt;private Mockery mockery;&lt;br&gt;private ILoader&amp;lt;Customer&amp;gt; loader; &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;[SetUp]&lt;br&gt;public void SetUp()&lt;br&gt;{&lt;br&gt;mockery = new Mockery();&lt;br&gt;loader = (ILoader&amp;lt;Customer&amp;gt;) mockery.NewMock(typeof (ILoader&amp;lt;Customer&amp;gt;));&lt;br&gt;LoaderRegistry.RegisterLoader(loader);&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;[Test]&lt;br&gt;public void ShouldMarkAsNotGhost()&lt;br&gt;{&lt;br&gt;Customer Customer = Customer.CreateGhost(99);&lt;br&gt;Expect.Once.On(loader).Method("Load").With(Is.EqualTo(Customer)).Will(new LoadCustomerAction());&lt;br&gt;Assert.IsNotNull(Customer);&lt;br&gt;Assert.AreEqual("My Customer", Customer.Name);&lt;br&gt;Assert.IsFalse(Customer.IsGhost);&lt;br&gt;Assert.IsFalse(Customer.IsLoading);&lt;br&gt;mockery.VerifyAllExpectationsHaveBeenMet();&lt;br&gt;} &lt;/font&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;[Test]&lt;br&gt;public void ShouldMarkAsLoadedWhenPropertySet()&lt;br&gt;{&lt;br&gt;Customer Customer = Customer.CreateGhost(99);&lt;br&gt;Customer.Name = "Fred"&lt;br&gt;Assert.IsFalse(Customer.IsGhost);&lt;br&gt;Assert.IsFalse(Customer.IsLoading);&lt;br&gt;Assert.AreEqual("Fred", Customer.Name);&lt;br&gt;mockery.VerifyAllExpectationsHaveBeenMet();&lt;br&gt;} &lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier" color="#0000ff" size="1"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;As you can see from the test, to create a customer that will lazy load you just have to call the factory method Customer.CreateGhost().&amp;nbsp; So to achieve performance gains I can replace all reconstitutions of aggregate objects in the persistence layer with calls to DomainObject.CreateGhost() and have them lazy load automatically.&lt;/p&gt; &lt;p&gt;If you would like my sample code, complete with NUnit tests just drop me a line.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-115657780366435910?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/08/lazy-loading-using-net-remoting-proxy.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">21</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-115406216982173596</guid><pubDate>Fri, 28 Jul 2006 04:33:00 +0000</pubDate><atom:updated>2006-07-27T22:50:47.866-06:00</atom:updated><title>Edmonton Code Camp (Sept 30)</title><description>So, I'll be presenting a topic during the Edmonton .NET Code Camp on Sept 30.  My topic is geared towards the mass of VB6 developers currently making the migration to .NET.  We all love that you're making the switch, but please, please, leave your dirty VB6 habits at the door.  To help make the .NET transition smoother for all involved parties I'll be presenting some basic OO concepts along with some helpful OO design patterns.  Of course it will all be test driven, so I'll touch on that as well.&lt;br /&gt;&lt;br /&gt;Specifically I'd like to delve into Encapsulation, Inheritance and Polymorphism as well as some patterns such as Strategy, Command and perhaps Observer.  Although I'm not sure the problem domain yet, I'll be starting with some code that looks like it could have been written by a VB6 developer and we'll refactor it iteratively into a simple, maintanable OO solution using each of the aformentioned concepts.  It will be not only fun, and educational but will make you more attractive to the opposite sex.&lt;br /&gt;&lt;br /&gt;If you're in town on the 30th of Sept stop by, say hi, buy me beer.&lt;br /&gt;&lt;br /&gt;For more information about the Edmonton Code Camp do that clicky thing here --&gt; &lt;a href="http://www.edmontoncodecamp.com/"&gt;http://www.edmontoncodecamp.com/&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-115406216982173596?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/07/edmonton-code-camp-sept-30.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-115396545401693967</guid><pubDate>Thu, 27 Jul 2006 01:41:00 +0000</pubDate><atom:updated>2006-07-30T14:01:55.633-06:00</atom:updated><title>Clumsy .NET Decorators</title><description>The &lt;a href="http://dofactory.com/Patterns/PatternDecorator.aspx"&gt;decorator pattern&lt;br /&gt;&lt;/a&gt; is one of my favourite design patterns to assist in adherence to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.  I especially enjoy the pattern in Ruby. &lt;br /&gt;&lt;br /&gt;In .NET, however, the pattern loses alot of it's elegance.  The necessity of gratuitous virtual delegating methods or overriding with the new keyword means that a decorator can not masquerade as the object it is decorating unless a significant amount of code has been added to the decorator class itself.  When I've been faced with a task that could have been solved simply with a thin decorator, I'm often dissuaded from implementing the pattern because of the homely looking mess of a class that results.&lt;br /&gt;&lt;br /&gt;This leads me to believe that an elegant solution exists by using reflection and Type Descriptors to create a Rubyish decorator that can dynamically delegate to base class methods.  Stay tuned...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-115396545401693967?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/07/clumsy-net-decorators.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-115344273111232145</guid><pubDate>Thu, 20 Jul 2006 23:40:00 +0000</pubDate><atom:updated>2006-07-26T19:58:44.733-06:00</atom:updated><title>Click Once deployment using NAnt</title><description>Click Once publishing using Visual Studio 2005 is an easy, quick and painless way to manage deployments.  Painless, that is, if you are a cowboy developer who's stomach doesn't quiver at the thought of throwing non-integration tested code into production or if you're a coding monkey who enjoys the mindless monotony of manual builds.&lt;br /&gt;&lt;br /&gt;For those of us enlightened in the art of automated builds and continuous integration, however, Click Once is a nightmare.  Fortunately, there is a way to deploy Click once applications without becoming a Visual Studio monkey.  Here are my lessons learned for Click Once deployements using NAnt.&lt;br /&gt;&lt;br /&gt;First you must configure IIS with your deployment folder.  For example I set up a virtual directory called http://server/clienttest to C:\Development\Deploy\ClickOnce on my deployment server.  We have a windows file share pointing to C:\Development called \\server\Development.  In NAnt the &lt;span style="font-size:100%;"&gt;&lt;span style="font-style: italic;"&gt;${clickonce.dir}&lt;/span&gt; &lt;/span&gt;points to \\server\Development\Deploy\ClickOnce while the &lt;span style="font-style: italic;"&gt;${deploy.dir}&lt;/span&gt; variable points to a local folder containing the deployable outputs from the build.&lt;br /&gt;&lt;br /&gt;Now, if you don't already have one you will have to create a certificate with which to sign the manifests.  I used the &lt;span style="font-weight: bold;"&gt;makecert&lt;/span&gt; tool for this ensuring that I the private key was exported.  I store this key in the file indicated in the &lt;span style="font-style: italic;"&gt;${certificate.filename}&lt;/span&gt; NAnt variable.  Read &lt;a href="http://msdn2.microsoft.com/en-us/library/bfsktky3.aspx"&gt;http://msdn2.microsoft.com/en-us/library/bfsktky3.aspx&lt;/a&gt; for more info on makecert.&lt;br /&gt;&lt;br /&gt;Next I have a NAnt build target that creates a new directory for the build version then copies the client dlls into that directory:&lt;br /&gt;&lt;br /&gt;(pardon my obtuse XML, just can't figure out how to get the angle brackets and indentation on this site)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;blockquote&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;  (target name="&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;create.clickonce.dir&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;")&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(51, 51, 153);font-size:85%;" &gt;  (delete dir="${clickonce.dir}\${version}" failonerror="false" /)&lt;br /&gt;(mkdir dir="${clickonce.dir}\${version}"  /)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;  (copy todir="${clickonce.dir}\${version}" flatten="true" )&lt;br /&gt;(fileset basedir="${deploy.dir}\client")&lt;br /&gt;  (include name="*.exe" /)&lt;br /&gt;  (include name="&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(51, 51, 153);font-size:85%;" &gt;Dependant&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;span style="color: rgb(51, 51, 153);"&gt;*.dll" /)     &lt;/span&gt;&lt;br /&gt;(/fileset)&lt;br /&gt;(/copy)&lt;/span&gt;&lt;/blockquote&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Essentially, Click Once deployment is the creation of two manifest files.  The first describes the contents of a specific version of your application the second describes the contents of your deployment and specifies the most recent version.  Fortunately, Microsoft has a utility called &lt;span style="font-weight: bold;"&gt;mage&lt;/span&gt; that assists in generating and signing these manifests.  In order to create these files as part of the build process I created a NAnt task that calls mage.&lt;br /&gt;&lt;br /&gt;I had to do this in two steps.  The first step is to create both the application manifest and the deployment manifest.  For example I ended up adding a target that looks something like this:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;  (target name="create.clickonce")&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;(call target="&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;create.clickonce.dir" /)&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;br /&gt;(exec program="mage" commandline="-New Application&lt;br /&gt;-t ${&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;clickonce.dir&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}\&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;${version}&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;\Client.exe.manifest&lt;br /&gt;-fd  ${deploy.dir}\client  -Name Client" /)&lt;br /&gt;(exec program="mage" commandline="-Sign ${&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;clickonce.dir&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}\${version}\Client.exe.manifest&lt;br /&gt;-CertFile &lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;${certificate.filename} -Password ${certificate.password}&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;span style="color: rgb(51, 51, 153);"&gt;" /)&lt;/span&gt;&lt;br /&gt;(exec program="mage" commandline="-New Deployment&lt;br /&gt;-t ${&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;clickonce.dir&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}\Client.application&lt;br /&gt;-pu http://server/clienttest/Client.application&lt;br /&gt;-n Alpha -i false&lt;br /&gt;-appm ${&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;clickonce.dir&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}\${version}\Client.exe.manifest" /)&lt;br /&gt;(exec program="mage" commandline="-Sign ${&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;clickonce.dir&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}\Alpha.application&lt;br /&gt;-CertFile &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;span style="color: rgb(51, 51, 153);"&gt;${certificate.filename} &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;-Password &lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;${certificate.&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;password&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;&lt;span style="color: rgb(51, 51, 153);"&gt;" /)&lt;/span&gt;&lt;br /&gt;(/target)&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(51, 51, 153);font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;This target need only be called when you want to create your deployment manifest.  Typically it will only be called once, but I like to throw it in the build script in case I ever need to rebuild my deployment server.&lt;br /&gt;&lt;br /&gt;The second step is to create the application manifest for the most recent build and then update the deployment manifest.  The following target copies the&lt;br /&gt;&lt;blockquote style="color: rgb(51, 51, 153);"&gt;&lt;span style="font-size:85%;"&gt;  (target name="deploy.clickonce")&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;(call target="&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;create.clickonce.dir" /)&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;(exec program="mage" commandline="-New Application -t ${clickonce.dir}\${version}\Client.exe.manifest -fd ${deploy.dir}\client -Name Client -Version ${version}" /)&lt;br /&gt;(exec program="mage" commandline="-Sign ${clickonce.dir}\${version}\Client.exe.manifest -CertFile ${certificate.filename} -Password &lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;${certificate.&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;password&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;span style="font-size:85%;"&gt;" /)&lt;br /&gt;(exec program="mage" commandline="-Update ${clickonce.dir}\Alpha.application -AppManifest ${clickonce.dir}\${version}\Alpha.exe.manifest -Version ${version}" /)&lt;br /&gt;(exec program="mage" commandline="-Sign ${clickonce.dir}\Alpha.application -CertFile ${certificate.filename} -Password &lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;${certificate.&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;password&lt;/span&gt;&lt;span style="color: rgb(51, 51, 153); font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;span style="font-size:85%;"&gt;" /)&lt;br /&gt;(/target)&lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;For more information on manual Click Once deployments have a read through &lt;a href="http://msdn2.microsoft.com/en-us/library/xc3tc5xx.aspx"&gt;http://msdn2.microsoft.com/en-us/library/xc3tc5xx.aspx&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-115344273111232145?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/07/click-once-deployment-using-nant.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-26108077.post-115328068929245100</guid><pubDate>Wed, 19 Jul 2006 02:43:00 +0000</pubDate><atom:updated>2006-07-18T21:45:02.303-06:00</atom:updated><title>Got Mission?</title><description>Being a skeptic of mandated doctrine the typical corporate mission statement is about as appealing to me as ketchup on cottage cheese.  In my experiences coporate mission statements have been vague, broad and cliche.  Until now, I've never been asked to participate in creating a new mission statement nor has my opinion been solicited in improving an existing one.  Lacking in the authentic spirit of a team these works of bland prose garner little support from the very people they are supposed to inspire.  So people tend to ignore them.  Reading through them as part of the new hire literature and then shelving them with the instructions to the voice mail system.&lt;br /&gt;&lt;br /&gt;Although I think that most mission statements are useless because they are done by the wrong people for the wrong reasons I do believe in the power of an effective mission statement written by the people who are to be guided by it.  So when our PM announced a team meeting to come up with a mission statement, I'll admit I was a little excited.&lt;br /&gt;&lt;br /&gt;The session was accepted by all the developers and everyone had their own ideas to contribute into the mix.  Our PM did a great job of controlling the brainstorming sessions and making it a fun excersize.  The meeting took just under an hour and the end result was a single sentence (consisting of just 4 words, actually) that conveys the quintessential values and goals of our team in addition to half a dozen sentences that describe it a little deeper.&lt;br /&gt;&lt;br /&gt;The breakdown of the brainstorming was as follows:&lt;br /&gt;1.  List of things that we want to accomplish (the Whats)  &lt;br /&gt;2.  List of ways we will accomplish them (the Hows)&lt;br /&gt;3.  Words or a sentence that describes a lofty yet attainable goal considering the context of the prior lists&lt;br /&gt;&lt;br /&gt;Of course there's some categorization nebula between steps 1 and 2 that can be solved with brainstorming patterns.  For example, words that fall under What and How might be broken down into two or more words which can be placed across categories.&lt;br /&gt;&lt;br /&gt;Interestingly for our team, some of the fundamental Agile/XP values seemed to pop out on the whiteboard.  Not necessarily expressed using these words, the concepts of Communication, Feedback, Simplicity, Responding to Change, Customer Collaboration emerged.  Sadly, there was no Courage ;-)&lt;br /&gt;&lt;br /&gt;So, what was our mission?  The end result the following statement: &lt;span style="font-style:italic;"&gt;Positively impact every project.&lt;/span&gt; Meaning that our team's lofty goal is to have our fingers in every project and ensure that we are providing a positive impact in each of those engagements.&lt;br /&gt;&lt;br /&gt;So, does your team have mission?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26108077-115328068929245100?l=blog.gravityfree.ca' alt='' /&gt;&lt;/div&gt;</description><link>http://blog.gravityfree.ca/2006/07/got-mission.html</link><author>noreply@blogger.com (Neil Bourgeois)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item></channel></rss>
