<?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:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>brendan forster</title>
    <description>adventures in software development and other shenanigans</description>
    <link>http://www.brendanforster.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 1.5.1.10</generator>
    <language>en-AU</language>
    <blogChannel:blogRoll>http://www.brendanforster.com/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Brendan Forster</dc:creator>
    <dc:title>brendan forster</dc:title>
    <geo:lat>0.000000</geo:lat>
    <geo:long>0.000000</geo:long>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/BrendanForster" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="brendanforster" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>MEF Monday #1 – Interesting Resources</title>
      <description>&lt;p&gt;Following up from my talk on MEF at SLCC on the weekend – where I ran through the fundamentals of MEF, developing applications in Silverlight, and answering questions about using MEF in applications -&amp;#160; I thought I’d share some of the resources that I’ve found valuable when I started with MEF. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://mef.codeplex.com/"&gt;MEF on Codeplex&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A great starting place for anyone looking into MEF. Installers, documentation, blogs and more.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://mef.codeplex.com/Thread/List.aspx"&gt;MEF Forums&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Have an issue? Trying something and its not working as you expect? Have a look here, someone else may have come across the same issue and found a fix.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/08/16/should-i-use-mef-for-my-general-ioc-needs.aspx"&gt;MEF and Dependency Injection – How they Differ&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I got the “Is this Dependency Injection?” question a few times over the weekend, and &lt;a href="http://twitter.com/gblock"&gt;Glenn Block&lt;/a&gt; has a good list here of what MEF v1 does not support when you compare it to popular DI Containers such as Ninject, StructureMap or Unity.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://blogs.msdn.com/gblock/archive/2009/12/02/mef-and-prism-to-be-or-not-to-be.aspx"&gt;MEF and Prism – How They Relate&lt;/a&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Another interesting question that was raised is how MEF and Prism relate. While they may have inadvertently encroached on eachother’s turf, the feature parity and directions differ greatly. I expect some synergy between the two down the track (perhaps MEF support within Prism), but currently Prism has the specialised UI composition features (EventAggregator, Composite Commands) out-of-the-box that MEF doesn’t support in v1.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://nblumhardt.com/2010/01/the-relationship-zoo/"&gt;The Relationship Zoo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;How do we identify and represent the relationships between components, especially as applications become more intricate? &lt;/p&gt;  &lt;p&gt;Nicholas Blumhardt, maintainer of the &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt; project, did a great job of dissecting the patterns for defining relationships between components, and throws around some ideas about scenarios and future plans for this space.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://microsoftpdc.com/Sessions/FT24"&gt;Building Extensible Rich Internet Applications with MEF&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here is a session from PDC last year where Glenn talked about using MEF in Silverlight, including some of the Silverlight-specific features, such as PackageCatalogs and asynchronous downloading of XAP files.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;PS: I’ve got some of my own thoughts and samples on the way, as well as some ideas about applying MEF to application scenarios.&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2010/02/01/MEF-Monday-1-Interesting-Resources.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2010/02/01/MEF-Monday-1-Interesting-Resources.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=76906bf9-81bb-460e-9eb5-59db762eb4c0</guid>
      <pubDate>Mon, 01 Feb 2010 14:20:10 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=76906bf9-81bb-460e-9eb5-59db762eb4c0</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=76906bf9-81bb-460e-9eb5-59db762eb4c0</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2010/02/01/MEF-Monday-1-Interesting-Resources.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=76906bf9-81bb-460e-9eb5-59db762eb4c0</wfw:commentRss>
    </item>
    <item>
      <title>Why even bother?</title>
      <description>&lt;p&gt;Ever since PDC, I’ve been struggling to figure out how a community project can compete in the Twitter space in this day and age. Up until recently, &lt;a href="http://www.tweetdeck.com/beta/"&gt;Tweetdeck&lt;/a&gt; was the defacto Twitter client, with a few people going on about &lt;a href="http://www.atebits.com/tweetie-iphone/"&gt;Tweetie&lt;/a&gt; on something called an iPhone. A lot of others were around the place, fighting for airtime. But then Seesmic gets a few moments on stage during a keynote, scoring a bunch of free publicity for their app – which was a preview demonstrating the fact that they rewrote it as a native Windows application.&lt;/p&gt;  &lt;h4&gt;Where was the innovation? &lt;/h4&gt;  &lt;p&gt;This may come across as me venting at Seesmic, but its more a vent in general at how someone can differentiate themselves in such a noisy industry – an industry where an update for a product client generates an &lt;a href="http://www.google.com.au/search?q=Tweetie+Iphone&amp;amp;hl=en&amp;amp;tbo=1&amp;amp;tbs=qdr:w"&gt;absurd amount of news articles&lt;/a&gt; on top of the Twitter noise. After all, this isn’t something I do for a day-job. Its a side-project that a few friends work on whenever they have spare time. How can we even compete against full-time employees, venture capitalists, and tech influencers for mindshare?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;What project am I talking about? &lt;a href="http://mahtweets.com/"&gt;MahTweets&lt;/a&gt;. A extensible client in WPF for microblogging and uploading content that started out as something we wrote for ourselves, and is something we are sharing with the community. Sounds like just about every other client out there, I bet.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let me discuss how we differentiate ourselves from the rest.&lt;/p&gt;  &lt;h4&gt;A Truly Open-Source Client&lt;/h4&gt;  &lt;p&gt;Anyone can download the source, explore its internals, and customise it to suit their needs. We’ve had upstream patches submitted from people who want to tweak the user interface, (theming is something that we support for plugins, the full UI is a lower priority).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;A client that is free as in beer is one thing, but ultimately you’re at the mercy of the company resolving any issues you encounter. If you’ve got some developer skills (or are just curious about code) why not download the code and browse it yourself?&lt;/p&gt;  &lt;h4&gt;Plugin Support Available Today&lt;/h4&gt;  &lt;p&gt;MahTweets supports Twitter and Facebook out of the box, along with support for a number of other syndication services (Flickr, 12seconds, Twitpic, RSS). We’d love to talk about what is in the pipeline, but we’re hard at work polishing the user experience and the plugin architecture for the next release, so that other people can come in and write their own plugins.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;One of the most interesting features we have (and have barely scratched the surface of using) was the Status Handler pipeline. No other platform client I’ve come across has good support for writing handlers. We did a proof-of-concept version for TechEd AU that had support for Ruby plugins using the upcoming Dynamic Language Runtime, which translated status updates into LOLwords - I thoroughly enjoyed hearing people see their tweets turned into something out of ICanHazCheezburger.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;A Better (and more personal) List Implementation&lt;/h4&gt;  &lt;p&gt;One of the use cases that Loic uses as a motivator for plugin-based architecture is support for custom behaviour. He uses an example of an “temporary unfollow” to ignore the noise that someone is already. Of course, the user wants it specifically for Twitter.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;We identified this frustration early on and built what we call “streams” – giving the user full control over what appears in each list, so they can make changes on-the-fly. The streams are persisted when you close the app, so there’s no requirement for you to use Twitter or use Facebook. This is our own implementation – you can filter based on account, message, contact or any combination.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=vNext-Streams.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="vNext-Streams" border="0" alt="vNext-Streams" src="http://www.brendanforster.com/image.axd?picture=vNext-Streams_thumb.png" width="725" height="172" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p align="center"&gt;&lt;strong&gt;Clients don’t seems to care about good filtering, yet its something everyone wants&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;We’re making it for you (any incidental fame is just a bonus)&lt;/h4&gt;  &lt;p&gt;The current incarnation of MahTweets started out over a few tweets between myself and &lt;a href="http://twitter.com/aeoth"&gt;Paul&lt;/a&gt; – discussing how we might build a client which supports plugins. We wanted to have plugins at the core so that others could take our initial work and go further. It really sucks that all the others are finally “getting” this desire for people to customise their client, when this was something that we wanted from the start. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;A lot of what we’ve done in terms of features has been driven from our users. Little things can often make or break the experience for a user (we’ve seen both sides of that) and its what drives me to do more. Here’s some recent ones I’ve found on Twitter:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;“Did I just see mahtweets do geotagging? NICE!” - &lt;a href="http://twitter.com/joshnunn/statuses/6036818506"&gt;source&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;“Now that I'm getting used to it, I'm beginning to dig #MahTweets. Slick interface and nifty animations.” - &lt;a href="http://twitter.com/latringo/statuses/6073267786"&gt;source&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;“I like &lt;a href="http://twitter.com/search?q=%23mahtweets"&gt;#mahtweets&lt;/a&gt; and its use of &lt;a href="http://twitter.com/search?q=%23MEF"&gt;#MEF&lt;/a&gt; - I have written an extension myself.” – &lt;a href="http://twitter.com/georgeck/statuses/6189323372"&gt;source&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;“@airliebee you running it on a personal machine or a work one? If it's yours, give @aeoth's MahTweets a spin. Best Windows client evar! :)” – &lt;a href="http://twitter.com/rob_caporetto/statuses/5993913106"&gt;source&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;Wrapup&lt;/h4&gt;  &lt;p&gt;This was never about being #1 in popularity in the Twitter space. I wanted to see the guys behind MahTweets get some recognition for their hard work over the past few months. I know Paul has looked at a shitload of Twitter clients over the past months, to see what everyone else is doing and where they fall down, and he’s done an incredible amount of work on turning out something incredible (why lie - he’s the overlord and the main driver of this).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So how can we push the competition further and get a bigger voice in the field when we’re struggling to be heard above the noise of the interwebs? I’d love to see Tweetdeck or Seesmic move in a direction which is more driven by plugins – they have been talking about this, but I don’t believe they’re close to the flexibility and control that we have currently.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;What are your thoughts?&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/11/30/why-even-bother.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/11/30/why-even-bother.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=782fd3a7-99e3-46d3-865a-f786ca5cc028</guid>
      <pubDate>Mon, 30 Nov 2009 16:18:07 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=782fd3a7-99e3-46d3-865a-f786ca5cc028</pingback:target>
      <slash:comments>4</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=782fd3a7-99e3-46d3-865a-f786ca5cc028</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/11/30/why-even-bother.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=782fd3a7-99e3-46d3-865a-f786ca5cc028</wfw:commentRss>
    </item>
    <item>
      <title>.NET Code Contracts – What’s the best way to use it?</title>
      <description>&lt;p&gt;This is part of a series of articles covering not only the technical capabilities of .NET &lt;a href="http://research.microsoft.com/en-us/projects/contracts/"&gt;Code Contracts&lt;/a&gt; being added in v4.0 of the .NET Framework, but also the benefits of this technology and how developers can leverage the technology in their projects.&lt;/p&gt;  &lt;h4&gt;The Million Dollar Question&lt;/h4&gt;  &lt;p&gt;In the &lt;a href="http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Overview.aspx"&gt;previous entry&lt;/a&gt;, I introduced the Code Contracts concept and demonstrated how the tooling works. With so many different ways to write software, is Code Contracts something that we can integrate into a developer’s workflow?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Ultimately, the process of writing software is made up of a number of tasks. And before writing any code, you should have an idea of what it is you’re designing. Let’s use the scenario of owning a vehicle (there’s more to it than this, but I’ll focus on a few key scenarios) and define a number of behaviours which must be adhered to:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;A vehicle can have a number of occupants. &lt;/p&gt;  &lt;p&gt;An unregistered vehicle cannot be driven.&lt;/p&gt;  &lt;p&gt;The vehicle needs at least one occupant before it can be driven.&lt;/p&gt;  &lt;p&gt;An occupant should only leave a vehicle when it is stationary (unless the vehicle is in an action movie, of course!).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The key about this is that it can be applied to different types of vehicles – cars, motorbikes, trucks.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;Defining the Interface&lt;/h4&gt;  &lt;p&gt;To cover the key features of the class, this code snippet should suffice:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippet" class="csharpcode"&gt;     &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IVehicle&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;int&lt;/span&gt; Wheels { get; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;int&lt;/span&gt; Occupants { get; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsRegistered { get; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsMoving { get; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; PersonEntersVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; PersonLeavesVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; StopDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; RegisterVehicle(Country country);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; Country&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    Australia,&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    UnitedStates,&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    UnitedKingdom&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h4&gt;&lt;/h4&gt;

&lt;h4&gt;Declearing the Contract&lt;/h4&gt;

&lt;p&gt;Typically, the next step is to implement this IVehicle interface and test against that. But what we can do here is write the contract statements in a separate class, so that all implementations of IVehicle will use the same contract.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Note: To ensure the contract is detected for interface implementations of IVehicle, VehicleContract must &lt;em&gt;explicitly&lt;/em&gt; implement IVehicle.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;[ContractClassFor(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IVehicle))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; VehicleContract : IVehicle&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Wheels { get { &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException(); } }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Occupants { get { &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException(); } }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsRegistered { get { &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException(); } }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsMoving { get { &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException(); } }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    [ContractInvariantMethod]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; ObjectInvariant()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Invariant(Occupants &amp;gt;= 0);    &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; IVehicle.PersonEntersVehicle()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Requires(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(Occupants &amp;gt;= 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(Contract.OldValue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(Occupants) + 1 == Occupants);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(Occupants &amp;gt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; IVehicle.PersonLeavesVehicle()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Requires(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(Occupants &amp;gt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(Contract.OldValue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(Occupants) - 1 == Occupants);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(Occupants &amp;gt;= 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; IVehicle.StartDriving()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Requires(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(Occupants &amp;gt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(Contract.OldValue(Occupants) == Occupants);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; IVehicle.StopDriving()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Requires(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Requires(IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(Contract.OldValue(Occupants) == Occupants);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; IVehicle.RegisterVehicle(Country country)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Requires(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(Contract.OldValue(Occupants) == Occupants);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;[ContractClass(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(VehicleContract))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IVehicle&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// interface here&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The two attributes on the interface and the contract class link the contracts to the interface, enabling the Code Contracts rewriter to verify the implementations of the interface against the contract.&lt;/p&gt;

&lt;p&gt;This may seem like a lot of work, but the major benefit of having contracts defined in a separate class is that the contracts will be slipstreamed into any class which implements IVehicle.&lt;/p&gt;

&lt;h4&gt;Implementing the Interface&lt;/h4&gt;

&lt;p&gt;With all the core components in place, we can now go ahead and write the code for the class. While one might feel that you should be checking that the state of the class is valid (and throwing exceptions otherwise), this is handled by the static and runtime checkers.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Car : IVehicle&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Wheels { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Occupants { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsRegistered { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsMoving { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Car(&lt;span class="kwrd"&gt;int&lt;/span&gt; wheels)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Requires(wheels &amp;gt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(!IsMoving);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(Occupants &amp;gt;= 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Contract.Ensures(Wheels == wheels);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.Ensures(!IsRegistered);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.Wheels = wheels;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.Occupants = 0;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.IsMoving = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.IsRegistered = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PersonEntersVehicle()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Occupants++;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PersonLeavesVehicle()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        Occupants--;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; StartDriving()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.IsMoving = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; StopDriving()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.IsMoving = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RegisterVehicle(Country country)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.IsRegistered = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="rem"&gt;// TODO: track the country information&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;I’ve added in some additional contracts in the constructor, so that the Car is initially configured correctly. Aside from that, all the contract information is driven from the VehicleContract class.&lt;/p&gt;

&lt;h4&gt;Catching the Exceptional Behaviour with Static Verification&lt;/h4&gt;

&lt;p&gt;Here’s a quick console app that tries to use the Car in strange ways. I’ve highlighted the lines where static verification throws up warnings (see Part 1 for how this appears in Visual Studio).&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    Car car1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(4);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="rem"&gt;// test #1 - no people in the car&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    car1.RegisterVehicle(Country.Australia);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    car1.StartDriving(); &lt;span class="rem"&gt;// requires unproven: Occupants &amp;gt; 0&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    Car car2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(3);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// test #2 - person leaves moving car&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    car2.RegisterVehicle(Country.Australia);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    car2.PersonEntersVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    car2.StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    car2.PersonLeavesVehicle(); &lt;span class="rem"&gt;// requires unproven: !IsMoving&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// lets use it properly&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    Car car3 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(5);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    car3.RegisterVehicle(Country.Australia);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    car3.PersonEntersVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    car3.StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    car3.StopDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    car3.PersonLeavesVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h4&gt;Automated Testing of Contract Behaviours&lt;/h4&gt;

&lt;p&gt;For the adventurous, they might be wondering if there is a value proposition in automated testing instead of detecting warnings during compilation. As the Contracts will generate an exception, the test runner should be capable to detect the exception and deal with it.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;There are a couple of hurdles to jump to get this working:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hurdle 1: the ContractException class:&lt;/strong&gt; this class is marked sealed and internal, to ensure developers don’t try to catch the exception. Which I totally appreciate, because the contracts should be raised so that it isn’t handled by the function. This is probably a discussion for another topic – while I like how they’ve implemented handling exceptions, it could be taken further.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;[TestFixture]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CarTests&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    [TestFixture]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CarTests&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        [Test]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        [ExpectedException(ExpectedMessage = &lt;span class="str"&gt;&amp;quot;Precondition failed: IsRegistered&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; StartDriving_WithNoRegistration_ThrowsException()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            Car car1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(4);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car1.StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            Assert.Inconclusive(&lt;span class="str"&gt;&amp;quot;This should raise a contract exception&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        [Test]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        [ExpectedException(ExpectedMessage = &lt;span class="str"&gt;&amp;quot;Precondition failed: Occupants &amp;gt; 0&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; StartDriving_WithNoOccupants_ThrowsException()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            Car car1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(4);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            car1.RegisterVehicle(Country.Australia);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car1.StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            Assert.Inconclusive(&lt;span class="str"&gt;&amp;quot;This should raise a contract exception&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        [Test]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        [ExpectedException(ExpectedMessage = &lt;span class="str"&gt;&amp;quot;Precondition failed: !IsMoving&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PersonLeavesVehicle_WhileDriving_ThrowsException()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            Car car2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(3);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// bad test #2 - person leaves moving car&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car2.RegisterVehicle(Country.Australia);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            car2.PersonEntersVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car2.StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            car2.PersonLeavesVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            Assert.Inconclusive(&lt;span class="str"&gt;&amp;quot;This should raise a contract exception&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        [Test]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Occupant_EntersDrivesAndLeavesVehicle_HasNoOccupants()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            &lt;span class="rem"&gt;// good test - correct setup and usage&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            Car car3 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car(5);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car3.RegisterVehicle(Country.Australia);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            car3.PersonEntersVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car3.StartDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            car3.StopDriving();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;            car3.PersonLeavesVehicle();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;            Assert.AreEqual(0, car3.Occupants);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;We’ve brought over the scenarios from the console application. As ContractException is not accessible directly, we need to inspect the Message property of the exception to ensure the correct precondition is throwing the exception – its not perfect, but it works.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hurdle 2 - Exception Handling: &lt;/strong&gt;Code Contracts uses event handlers to allow applications to handle contract exceptions, so that applications can handle issues globally by listening to the ContractFailed event. In addition to getting the exception information, the handler can either unwind the stack, or mark the event as handled.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;To demonstrate the available options, I’ve added this code to the test class to set up the test:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;[TestFixture]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CarTests&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    EventHandler&amp;lt;ContractFailedEventArgs&amp;gt; errorHandle = (s, e) =&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="rem"&gt;// signal that the issue is sorted and no exception should be raised&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="rem"&gt;//e.SetHandled();&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="rem"&gt;// revert the code to before the method was entered and throw the exception&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="rem"&gt;//e.SetUnwind(); &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    [SetUp]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.ContractFailed += errorHandle;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    [TearDown]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TearDown()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        Contract.ContractFailed -= errorHandle;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// tests&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If I set the ContractFailed event as handled – by calling &lt;strong&gt;e.SetHandled()&lt;/strong&gt; - the exception will be consumed and the application will continue along its merry way. Except that’s not a good thing – I want to ensure that the test throws an exception. I’ve structured the failing tests so that they will assert the test is inconclusive (it shouldn’t reach the end if the contract is correct) because it should never complete.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This is what I see from the NUnit system runner:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=FailedTests.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="NUnit Test Runner Fail" border="0" alt="NUnit Test Runner Fail" src="http://www.brendanforster.com/image.axd?picture=FailedTests_thumb.png" width="699" height="178" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Ruh-roh. I consumed the exception when I shouldn’t have. &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If I set the ContractFailed event to wind back the stack – by calling &lt;strong&gt;e.SetUniwnd() – &lt;/strong&gt;the application will roll its state back to before it entered the problem method, and the exception will be raised.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The test runner now gives me this information:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=PassedTests.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="NUnit Test Runner Passed" border="0" alt="NUnit Test Runner Passed" src="http://www.brendanforster.com/image.axd?picture=PassedTests_thumb.png" width="705" height="182" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Ah, that’s better. Green pastures.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The benefit of pushing this stuff out to a testing framework is that you can reduce compilation overhead:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;1. Writing the code (opt-in to check the contracts).&lt;/p&gt;

&lt;p&gt;2. Commit the code and run an automated build containing a test suite against the contracts (mandatory check).&lt;/p&gt;

&lt;p&gt;3. Finally, deploy the application without the contracts to boost performance&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The tests can be structured around identifying the expected behaviour for valid or invalid scenarios. Ultimately, this becomes another type of testing you can do during development, and this can be integrated easily into your automated build processes using existing .NET testing frameworks.&lt;/p&gt;

&lt;h4&gt;Summary&lt;/h4&gt;

&lt;p&gt;Code Contracts can be added to existing code, but one of the main benefits I’ve encountered is that code contracts can become a step between defining the interface and writing the implementation. The compilation tools allow a clean separation between interfaces, contracts and implementations for clarity and consistency, while still allowing verification of contracts within the IDE, by merging contracts with the interface implementation during compilation.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Code contracts can also be verified at runtime by leveraging unit testing frameworks, to ensure that a contract change (much like changing the implementation of a class) will raise errors if the test scenario breaks. I also demonstrated some of the configuration options about handling contract exceptions, but this will be the focus of an upcoming entry.&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Workflow.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Workflow.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=3cae1cb0-2553-4edd-89dd-1041fde20b25</guid>
      <pubDate>Thu, 19 Nov 2009 05:57:14 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=3cae1cb0-2553-4edd-89dd-1041fde20b25</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=3cae1cb0-2553-4edd-89dd-1041fde20b25</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Workflow.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=3cae1cb0-2553-4edd-89dd-1041fde20b25</wfw:commentRss>
    </item>
    <item>
      <title>What’s All This? An Overview of .NET Code Contracts</title>
      <description>&lt;p&gt;This is part of a series of articles covering not only the technical capabilities of .NET &lt;a href="http://research.microsoft.com/en-us/projects/contracts/"&gt;Code Contracts&lt;/a&gt; being added in v4.0 of the .NET Framework, but also the benefits of this technology and how developers can leverage the technology in their projects.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;What is Code Contracts?&lt;/h4&gt;  &lt;p&gt;Code Contracts is a direct descendant of the &lt;a href="http://research.microsoft.com/en-us/projects/specsharp/"&gt;Spec#&lt;/a&gt; project – which was a research project designed to add design-by-contract functionality to the C# language. While its syntax was quite similar to Eiffel (which was the first to implement DbC), it was always intended to be a research project. Most of the team moved over to work on Code Contracts, which applied this knowledge with the goal of providing better integration with the .NET Framework and tooling support within Visual Studio.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The Base Class Library team is using Code Contracts to verify the core libraries of .NET 4.0 as it heads towards being released next year – with the longer-term goal being that contracts can be used as first-class citizens by third party developers building applications and components on top of .NET. These contracts are included when you download the Code Contracts tools, so you can see how the BCL team is using Code Contracts.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;What is Design By Contract?&lt;/h4&gt;  &lt;p&gt;For those who are unfamiliar with &lt;a href="http://en.wikipedia.org/wiki/Design_by_contract"&gt;design-by-contract&lt;/a&gt;, its a pattern for specifying how components in an application behave. I’ll defer to Wikipedia here, because I think this is the best summary out there about the focus of design-by-contract:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;What does it expect? &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;What does it guarantee? &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;What does it maintain? &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You’re probably rolling your eyes at this point, so lets stop handwaving and use a practical example. I’ll drill down a bit deeper on the concept itself in another entry, but this should be a good starting point.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;A Tale of Consumers and Suppliers&lt;/h4&gt;  &lt;p&gt;In a typical contract, there are two participants – for simplicity’s sake, we’ll call these entities the &lt;strong&gt;consumer&lt;/strong&gt; and the &lt;strong&gt;supplier.&lt;/strong&gt; In the real world, the supplier would be the developer writing the component, and the consumer could be a different developer using the component in their application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Say I have implemented a collection, and this is the method for adding an item:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippet" class="csharpcode"&gt;     &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Add(T item)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (item == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="str"&gt;&amp;quot;item&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="rem"&gt;// ... add the item to a collection&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Count&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    get {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;        &lt;span class="rem"&gt;// implementation&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    set {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;        &lt;span class="rem"&gt;// implementation&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/div&gt;

&lt;div&gt;For this method above, we have an implied expectation that the consumer will provide a non-null item as the parameter. The method will throw an exception otherwise, to scold the consumer about doing something silly.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;How can the consumer understand this requirement when writing code against this class? &lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;Personally, I’d suggest Reflector here (a godsend and should be in every .NET developer’s toolset), but that’s hardly ideal to do for every class and library you deal with. In a utopian developer society, we should be able to find this information easily and quickly.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;There is also a guarantee here – can you spot it? Think about what is being managed internally by the collection and is being exposed to the consumer. &lt;/div&gt;

&lt;div&gt;There is a guarantee around the behaviour of the Count variable – the supplier must ensure that the Count value is updated when Add is finished. Should we take it for granted that this will be incremented?&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;How can we verify that Count is correct at the end of the Add method?&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;This depends on the implementation of the collection. You could write a unit test to achieve this, but we can specify this as part of the contract, and use the static checking tools to verify the behaviour.&lt;/div&gt;

&lt;h3&gt;What does a contract look like?&lt;/h3&gt;

&lt;div&gt;This depends on the implementation of the language - Eiffel has DbC available natively, other languages have contract available via third party libraries – and .NET 4 will have Code Contracts built into the core. How these contracts are integrated into assemblies will be discussed in a later entry.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;So, to rewrite the above section to use Code Contracts, this is how it would behave:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics.Contracts;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span class="rem"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Add(T item)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;    Contract.Requires(item != &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    Contract.Ensures(Contract.OldValue(Count) + 1 == Count);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;    &lt;span class="rem"&gt;// ... add the item to a collection&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Count { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;So we’ve taken out the exception and included two statements which form the contract:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;The expectation of the consumer: &lt;strong&gt;I require that ‘item’ is not null.&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;The guarantee of the supplier: &lt;strong&gt;I ensure that ‘Count’ will be incremented by this method.&lt;/strong&gt;&lt;/div&gt;

&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are other contract statements available (Assert and Assume) but these are only verified at runtime – rather than the all-round awesomeness of static verification.&lt;/p&gt;

&lt;div&gt;
  &lt;br /&gt;&lt;/div&gt;

&lt;h4&gt;&lt;/h4&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h4&gt;How does this work in Visual Studio?&lt;/h4&gt;

&lt;p&gt;Code Contracts are not available by default in Visual Studio. You need to install the tools from &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx"&gt;the project site&lt;/a&gt; to get the integration – this is available for 2008 or 2010. Not sure on whether this is going to be something that will be available&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This is how I’ve set up my project. There’s a fair bit of control over what can be tested here. &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=ContractSettings.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Code Contracts settings in Visual Studio" border="0" alt="Code Contracts settings in Visual Studio" src="http://www.brendanforster.com/image.axd?picture=ContractSettings_thumb.png" width="588" height="393" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Sidenote: &lt;/strong&gt;There are two versions of Code Contracts – Standard and VSTS Edition. The only difference between the two is that the Static Checking is available with VSTS – which I think is the most important feature in terms of supporting design-by-contract. Insert sadface here. Guess its going to become like FxCop – a nice tool available and used by the percentage of the community who already do this sort of thing. Oh well. Back to regular transmission.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Having all that set up, I create an instance of my custom collection and try to add null to it (because I’m silly and trying to make a point). No compilation errors are raised, but I get a harsh warning message.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="200"&gt;
        &lt;p align="center"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=ContractFailed1.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Contract Failed in the application" border="0" alt="Contract Failed in the application" src="http://www.brendanforster.com/image.axd?picture=ContractFailed1_thumb.png" width="359" height="448" /&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/td&gt;

      &lt;td valign="top" width="200"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=ContractFailed2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Here&amp;#39;s the contract that failed" border="0" alt="Here&amp;#39;s the contract that failed" align="right" src="http://www.brendanforster.com/image.axd?picture=ContractFailed2_thumb.png" width="375" height="447" /&gt;&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So I have two choices here: ignore the warning or fix it. Of course, I’m trying to make a point here, so I’m going to ignore it. Pressing F5 runs the app, but then I ...&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=StackTrace.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="A Runtime Exception" border="0" alt="A Runtime Exception" src="http://www.brendanforster.com/image.axd?picture=StackTrace_thumb.png" width="304" height="508" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Oh crap, that’s a lot of stack trace. I’ve highlighted the stack which is managed by Code Contracts,&amp;#160; but this also gives us the option to debug the code.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;a href="http://www.brendanforster.com/image.axd?picture=Debugger1.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Debugging the Exception" border="0" alt="Debugging the Exception" src="http://www.brendanforster.com/image.axd?picture=Debugger1_thumb.png" width="759" height="208" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The error that I ignored. Well, I guess I should fix it because the user’s not going to like that message either. &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=ContractSucceeded.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="All Assertions Passed!" border="0" alt="All Assertions Passed!" src="http://www.brendanforster.com/image.axd?picture=ContractSucceeded_thumb.png" width="406" height="339" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p align="center"&gt;&lt;strong&gt;And the verification passes. Sweet.&amp;#160; &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If the shoe was on the other foot, and the supplier was at fault, you’d see a similar scenario.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=Contract1.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Supplier is at fault!" border="0" alt="Supplier is at fault!" src="http://www.brendanforster.com/image.axd?picture=Contract1_thumb.png" width="303" height="402" /&gt;&lt;/a&gt; &lt;/td&gt;

      &lt;td valign="top"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=Contract2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The contract which the supplier broke" border="0" alt="The contract which the supplier broke" src="http://www.brendanforster.com/image.axd?picture=Contract2_thumb.png" width="442" height="211" /&gt;&lt;/a&gt; &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" colspan="2"&gt;
        &lt;p align="center"&gt;&lt;b&gt;Oh! That's what that line was supposed to do!&lt;/b&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h4&gt;&lt;/h4&gt;

&lt;h4&gt;The Summary (people who have a short attention span, read this)&lt;/h4&gt;

&lt;p&gt;Code Contracts is a feature that will be available in .NET 4.0 that will allow developers to specify contracts in their code, which are exposed when the library is used by a third party. The tooling in Visual Studio is progressing along nicely (with enough guidance to help resolve issues without getting in the way) but the restriction on the static checking tools is something that will prevent many from utilizing Code Contracts to its fullest.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;With the ongoing evangelism of practices to improve software development, I hope that this becomes another tool in the belt for good developers to build quality code – it is a different pattern to TDD, but I believe they can work side-by-side. It will be interested to see how this gets picked up by the community as .NET 4.0 heads towards going gold in March.&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Overview.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Overview.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=ae2450d8-bde9-460b-b758-27fa6bacea6f</guid>
      <pubDate>Thu, 19 Nov 2009 05:21:15 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=ae2450d8-bde9-460b-b758-27fa6bacea6f</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=ae2450d8-bde9-460b-b758-27fa6bacea6f</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/11/19/Code-Contracts-Overview.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=ae2450d8-bde9-460b-b758-27fa6bacea6f</wfw:commentRss>
    </item>
    <item>
      <title>Thoughts on hashDB – 140 characters just isn’t enough</title>
      <description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Long Zheng (who makes me tired just following his twitter feed) has, in the space of a few hours, taken an idea from &lt;a href="http://www.istartedsomething.com/20090706/windows-7-iso-verifier/"&gt;a fun side project&lt;/a&gt; and turned it into a full-on site. It revolves around a centralised repository of hashes, which are managed by the community and accessible through a web service. He’s got a brief &lt;a href="http://www.hashdb.net/"&gt;summary of his idea&lt;/a&gt; on the site and is looking for feedback.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The idea is solid (I’d use it, which is a good indicator) but I’d probably add a few things:&lt;/p&gt;  &lt;h4&gt;Trusted Users&lt;/h4&gt;  &lt;p&gt;While users can register and submit their own hashes, I’d like to streamline the hash input process by allowing people who already provide the correct hashes (that is, providing verification files with the download) to be permitted to directly upload the correct hash. Cut out the moderation, call them &lt;strong&gt;Trusted Users&lt;/strong&gt; for the applications they provide, and focus on the files/downloads which do not already provide the mechanism to verify files.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;Choice of Format&lt;/h4&gt;  &lt;p&gt;SHA-1 is the best choice for the moment – it doesn’t suffer from collisions like MD5 and is not computationally intensive. But what if things change? This probably falls into the “future-proofing” category but I’d like the site to be able to switch to format XYZ in the future, because the advancement (and security analysis) of the cryptography and digital signature field is rapidly changing. It wasn’t so long ago that MD5 was considered “safe”.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;Descriptors + Metadata&lt;/h4&gt;  &lt;p&gt;Not sure what Long has in mind, but I’d like to see a number of optional information associated with each hash.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Malware Hazard: &lt;/strong&gt;Invalid hashes are fine, but if a known hash is associated with some malware – think of those Win7 Beta ISOs which were freely available but included nasty surprises when you installed it – I’d like to warn others about it. Once it becomes apparent that the ISO I have is malicious, I’d like a mechanism to submit a comment about this hash to the site. It may not be an immediate change, but after a number of responses (other users may see the comment and verify it themselves) the moderators should flip the warning switch and consider it a serious risk.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Versioning: &lt;/strong&gt;While you can have a file which is called ApplicationName.X.Y.Z.exe, what if I have an installer called setup.exe (I still get this a lot, particularly with small apps). What I’d love is to see information about the source of the file (who provided it), the actual version (date, number, whatever) and perhaps some release notes (I’m pushing my luck, I know).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Application History: &lt;/strong&gt;With all this rich metadata (related products, versioning, comments), why not throw in some functionality to search for information about a specific application (assuming we’re linking hashes from related applications) that the site is tracking. The scenario for this feature may be like this: “I have application ABC and am getting some crashes when I follow the same series of steps. Google’s no help with a solution, the support process for the application is lacking (forums/phone/nonexistent/whatever) and I’m at my wits end. If I can point my installer at the DB and obtain a history of the application, I can quickly decide whether to try (and get my hands on) a different version or hit the bottle and tackle the problem tomorrow. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;It could be as simple as a link to the product’s Downloads page, but it could also be a direct link if that is at all possible. This may not work as well (Codeplex for example comes to mind) for some sites, but many applications provide public FTP access to official releases, plus beta and nightly drops.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’ll definitely keep an eye on how this turns out – he’s already looking for help with getting something out there. Good luck!&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/07/07/Thoughts-on-hashDB-e28093-140-characters-just-isne28099t-enough.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/07/07/Thoughts-on-hashDB-e28093-140-characters-just-isne28099t-enough.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=a2726db1-1458-4ecc-8b67-8325c39aa143</guid>
      <pubDate>Tue, 07 Jul 2009 18:30:08 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=a2726db1-1458-4ecc-8b67-8325c39aa143</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=a2726db1-1458-4ecc-8b67-8325c39aa143</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/07/07/Thoughts-on-hashDB-e28093-140-characters-just-isne28099t-enough.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=a2726db1-1458-4ecc-8b67-8325c39aa143</wfw:commentRss>
    </item>
    <item>
      <title>Why Current Operating Systems Suck: the Mouse</title>
      <description>&lt;p&gt;&lt;strong&gt;Background: &lt;/strong&gt;Long Zheng mentioned an offer &lt;a href="http://www.istartedsomething.com/20090703/get-paid-talk-about-pc-operating-systems/"&gt;inviting people to get paid and provide feedback&lt;/a&gt; about their current experiences with operating systems. I can’t fault him for working to bridge the gap between OS makers and users (that’s gotta be the end result out of any research, otherwise its about as useful as shouting at a canyon), but I thought I’d go in another direction and vent directly about what is currently broken with the current crop of operating system. Partly because 20 minutes is not enough time to outline years of frustration and tired concepts, but mostly because the issues I have also have some historical context.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;[All of the following should not be considered as fact. This is just the musings of a jaded geek. – Ed]&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;The Computer Mouse as a Concept &lt;/h4&gt;  &lt;p&gt;The current form of the mouse is a concept that come out of XEROX PARC in the early 70s. It had a metal ball underneath and three buttons on the top. There were some earlier versions which were much more radical (one even used a bowling ball as a component) but the essence of the mouse remains unchanged.&lt;/p&gt;  &lt;h6 align="center"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=AltoMouse.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="AltoMouse" border="0" alt="AltoMouse" src="http://www.brendanforster.com/image.axd?picture=AltoMouse_thumb.png" width="216" height="244" /&gt;&lt;/a&gt;I am glad that the mouse style improved, however. Source: &lt;a href="http://www.digibarn.com/collections/devices/alto-mouse/index.html"&gt;DigiBarn Museum&lt;/a&gt;&lt;/h6&gt;  &lt;p&gt;So, why change something that has been around for 30+ years and is attached to just about every computer out there? After playing around with other types of inputs which are designed to better represent natural gestures, such as stylus, &lt;a href="http://www.hp.com/united-states/campaigns/touchsmart/alt/tsalt.html"&gt;touchscreens&lt;/a&gt;, &lt;a href="http://www.microsoft.com/surface/"&gt;big damn tables&lt;/a&gt;, and even &lt;a href="http://www.youtube.com/watch?v=I9tmr8VDqN8"&gt;tyre-changing addons&lt;/a&gt;, &lt;strong&gt;I can’t help but feel that it is time to stop building operating systems and software designed specifically for the mouse, and focus on progressing towards user interfaces.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You want specific reasons? Read on.&lt;/p&gt; &lt;break /&gt;  &lt;h4&gt;Fitts’ Law&lt;/h4&gt;  &lt;p&gt;Ever get frustrated when using a mouse that you have to carefully move so that you don’t overshoot a target on the screen (for example, the button to close a popup that’s appeared and distracted you)? That’s &lt;a href="http://en.wikipedia.org/wiki/Fitts%27s_law"&gt;Fitts’ Law&lt;/a&gt; in action.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Having come from a Computer Science background, I was introduced to this theory in a subject called Event Driven Computing (which had a fair bit of work around user interfaces). Basically, this law states that the time to position the mouse cursor to a specific target is proportional to two things. The first is the distance (D) to travel, and the second is the width (W) of the target.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The law was first published in 1954, and has proven to be remarkable accurate in subsequent studies. &lt;/p&gt;  &lt;h6&gt;&lt;strong&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=FittsLaw.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="FittsLaw" border="0" alt="FittsLaw" src="http://www.brendanforster.com/image.axd?picture=FittsLaw_thumb.png" width="323" height="113" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/h6&gt;  &lt;h6 align="center"&gt;Apologies for the math. It won’t happen again (until the next time).&lt;/h6&gt;  &lt;p&gt;It became apparent when the graphical user interfaces started to become popular that the issue needed to be addressed. The most common fix for this is to put menus and commonly-used items at the edge of the screen, so that the user cannot overshoot the target area. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’ve included a screenshot of how the position affects the “target region” when comparing Safari on OS X and Windows. OS X works around Fitts’ Law by moving the menu to the top of the screen (just don’t try and close the window itself). Windows just ignores the issue and forces the user to aim for a small region.&lt;/p&gt;  &lt;h6 align="center"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=TargetAreaComparison.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="TargetAreaComparison" border="0" alt="TargetAreaComparison" src="http://www.brendanforster.com/image.axd?picture=TargetAreaComparison_thumb.png" width="276" height="393" /&gt;&lt;/a&gt;Safari for OS X (top) and Windows 7 (bottom)&lt;/h6&gt;  &lt;p&gt;Some operating systems work around Fitts’ Law better than others, but real estate at the edge of the screen is always at a premium (and if you have multiple monitors you sacrifice some of real estate to get a bigger desktop space), so I view this as a hack rather than a solution. Plus, most applications cannot push all their interactive elements to the outer (unless you’re doing something trivial like a document viewer, perhaps) so they need to design their applications so that the mouse gestures are optimised. That has lead to conventions like menus.&lt;/p&gt;  &lt;h6&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=MenuStructure.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="MenuStructure" border="0" alt="MenuStructure" src="http://www.brendanforster.com/image.axd?picture=MenuStructure_thumb.png" width="222" height="244" /&gt;&lt;/a&gt;&lt;/h6&gt;  &lt;h6 align="center"&gt;Sure, menus organise a lot of functionality. But can it be done better?&lt;/h6&gt;  &lt;p&gt;See all those keyboard shortcuts? That’s a good indicator that, after a while, users get sick of navigating menus with the mouse. Some applications that have a lot of functionality (this is from Visual Studio - you need to know shortcuts to maintain your sanity with using it) need to resort to using menus.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;[&lt;strong&gt;Ed&lt;/strong&gt;: I want to do some testing of whether Fitts’ Law applies to touch input – while it has some similarity to mouse input, I feel that it will perform better than using mouse input. Stay tuned.]&lt;/p&gt;  &lt;h4&gt;Why no Multi-User Interactions?&lt;/h4&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Ever get frustrated when you plug in a second mouse and expect (or desire) to see a second cursor appear?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Your average operating system is a single user experience. These days you can log in as different users (like in Unix and its derivatives) and you can have access multiple desktops running concurrently (eg. Remote Desktop). But at the desktop level, &lt;strong&gt;the standard is to have a single cursor and a single active window.&lt;/strong&gt; That may have been something more suitable in yesterday’s world - where 640K was enough for anybody - but not for today’s world.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;These days, I find myself wanting more. I find myself doing a fair bit of pair programming these days – whether it is throwing around ideas for implementing features or solving particular issues - but sharing a computer between two users is a tedious experience. Either we have one person coaching the other or we throw the keyboard and mouse between each other until the task is done.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;To be honest, I’m not sure what I want specifically. But I do know that I want more freedom. I’m sure there’s some human-computer interaction rules that are violated by having multiple users being active at once, but that is beside the point.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;Touch Gesture != Mouse Click&lt;/h4&gt;  &lt;p&gt;The &lt;a href="http://blogs.msdn.com/e7/archive/2009/03/25/touching-windows-7.aspx"&gt;Engineering Windows 7 blog&lt;/a&gt; has a nice overview of how touch gestures in Windows 7 behave - but they underlying vibe I get from the gestures is that they’re focusing too much on making touch behave like mouse clicks, instead of deliberately separating these features out. Things like this:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“Drag – Touch and slide your finger on screen. Like a dragging with a mouse, this moves icons around the desktop, moves windows, selects text (by dragging left or right), etc. &lt;strong&gt;This works everywhere.&lt;/strong&gt;“&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“Flicks – Flick left or right to navigate back and forward in a browser and other apps. This works in most applications that support back and forward.”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You’d think that these gestures are mutually exclusive, but what I’ve found while building applications is that the Flick is really specific (move X distance within a specific period of time), and the workaround of listening for a Drag on the window (and calling the navigation event you were expecting originally) introduces other issues – like FlowDocumentPageViewer text being highlighted. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“Zoom – Pinch two fingers together or apart to zoom in or out on a document. This comes in handy when looking at photos or reading documents on a small laptop. &lt;strong&gt;This works in applications that support mouse wheel zooming.&lt;/strong&gt;“&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This may seem silly, but when I manipulate objects on a touch surface, I perform the opposite gesture. This is also opposite to how the Surface ScatterView items behave&amp;#160; - two fingers moving away from eachother will increase the size of the item, two fingers moving towards eachother will reduce the size of the item. [&lt;strong&gt;Ed:&lt;/strong&gt; Here’s &lt;a href="http://www.microsoft.com/australia/remix/videos/video.aspx?vid=v27#mixVideoPlayer"&gt;a quick video&lt;/a&gt; introducing the Surface concept that came up at REMIX Australia] &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So which is it? I hope I’m wrong here (we ended up using our own code, rather than leaning on the default behaviours, so I can’t confirm that this is the default behaviour) but it shows that even a simple gesture such as zoom can be misinterpreted.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;My personal favourite:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“Press-and-hold – Hold your finger on screen for a moment and release &lt;strong&gt;after the animation to get a right-click&lt;/strong&gt;. This works everywhere. &lt;/em&gt;&lt;em&gt;Or, press-and-tap with a second finger – to get right-click, just like you would click the right button on a mouse or trackpad. This works everywhere.”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;1) You took my Hold Gesture (which is supposed to be a left-click) and &lt;strong&gt;turned it into a different mouse button after an arbitrary period of time&lt;/strong&gt;.&lt;strong&gt; &lt;/strong&gt;The only way I saw to get around this is to move your finger before the animation shows up, and that’s introducing a Drag gesture instead.&lt;/p&gt;  &lt;p&gt;2) This is not intuitive. I found the first way of doing this by accident, and I didn’t even know about the 2nd way until I read the article. And don’t give me this whole “Macs were doing it for ages” excuse either – if its supposed to be a touch gesture, don’t map it to a right-click. Call it a context menu event or something similar.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’m going to follow up on this particular topic as I’ve been throwing around some ideas with my boss (well, this was a while ago, I need to look back at what we actually discussed) about making gestures more discoverable. Surface is a great example of this – how do you guide the user through the functionality available without interfering with the user experience or requiring an instruction book to get started?&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;&amp;#160;&lt;/h4&gt;  &lt;h4&gt;Got any solutions, angry guy?&lt;/h4&gt;  &lt;p&gt;As I said before, stay tuned. I want to delve a bit deeper into the current touch situation for Windows developers (I haven’t had a chance to try WPF 4.0 yet, I want to delve deeper into the Windows 7 Touch stuff once it RTMs, I want &lt;a href="http://solutions.3m.com/wps/portal/3M/en_US/3MTouchSystems/TS/Technologies/Multi-touch/?WT.mc_id=www.3M.com/multitouch"&gt;one of these&lt;/a&gt; to play around with) and I want to go into some greater detail about building applications that don’t rely on a mouse – and there are some tricky things like text selection and menu navigation that you’d think “No mouse? You must be mad!”.&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/07/05/Why-Current-Operating-Systems-Suck-the-Mouse.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/07/05/Why-Current-Operating-Systems-Suck-the-Mouse.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=9585e0a8-ca90-4e07-8ddc-82ddddbe471d</guid>
      <pubDate>Sun, 05 Jul 2009 12:28:40 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=9585e0a8-ca90-4e07-8ddc-82ddddbe471d</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=9585e0a8-ca90-4e07-8ddc-82ddddbe471d</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/07/05/Why-Current-Operating-Systems-Suck-the-Mouse.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=9585e0a8-ca90-4e07-8ddc-82ddddbe471d</wfw:commentRss>
    </item>
    <item>
      <title>Side Note: Silverlight 2 Windowless Browser Support</title>
      <description>&lt;p&gt;I was doing some site updates during the week (stuff unrelated to Silverlight actually) and came across this simple and intriguing issue. I thought I’d blog about this really specific issue because its been frustrating to say the least (and I have no idea when this will be resolved) and it was a stark reminder for me that Silverlight development is not at the point where it is a ubiquitous experience for some browsers.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Updated 9/6&lt;/strong&gt; – Re-tested with Safari 4 RTW.&lt;/p&gt; &lt;break /&gt;  &lt;h3&gt;Windowless Silverlight Content&lt;/h3&gt;  &lt;p&gt;One of the perks of using Silverlight plugins on a website is having the ability to blend XAML content and HTML content. I’ll use a simple example of a control that is semi-transparent to show how this blending works and where it falls down. The screenshots below show the XAML code of the plugin and and HTML code used to embed the plugin on the page. A photo is used for the HTML background.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="748"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="404"&gt;&lt;strong&gt;XAML Code&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="342"&gt;&lt;strong&gt;HTML Code&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="404"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=XAMLCode.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="XAMLCode" border="0" alt="XAMLCode" src="http://www.brendanforster.com/image.axd?picture=XAMLCode_thumb.png" width="392" height="270" /&gt;&lt;/a&gt; &lt;/td&gt;        &lt;td valign="top" width="342"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=HTMLCode.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="HTMLCode" border="0" alt="HTMLCode" src="http://www.brendanforster.com/image.axd?picture=HTMLCode_thumb.png" width="336" height="131" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;First off, a test of the officially supported browsers:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="701"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="349"&gt;&lt;strong&gt;IE8&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="350"&gt;&lt;strong&gt;Firefox 3.0.10&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="349"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=Plugin-IE8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Plugin-IE8" border="0" alt="Plugin-IE8" src="http://www.brendanforster.com/image.axd?picture=Plugin-IE8_thumb.png" width="344" height="252" /&gt;&lt;/a&gt; &lt;/td&gt;        &lt;td valign="top" width="350"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=Plugin-FF.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Plugin-FF" border="0" alt="Plugin-FF" src="http://www.brendanforster.com/image.axd?picture=Plugin-FF_thumb.png" width="344" height="252" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;All good so far. The supported browsers work - IE6 and IE7 are dead to me for the purposes of this test. And in general. Hell, even my dad uses Firefox these days. &lt;a href="http://www.stoplivinginthepast.com/"&gt;Stop using IE6&lt;/a&gt;. Please.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Checking MSDN shows a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.silverlightcontrols.silverlight.windowless(VS.95).aspx"&gt;laundry list&lt;/a&gt; of limitations about using windowless plugins. This doesn’t mean “never use windowless mode” but what I read as &lt;strong&gt;TEST YOUR PLUGIN IN OTHER BROWSERS.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So, I did. And what I found was not good. To ensure this is just a snapshot in time, I’ve included browser versions that I’ve tested this on – the Silverlight plugin I’m using is v2.0.40115.0.&lt;/p&gt;  &lt;h3&gt;Safari 4 for Windows &lt;/h3&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I tested this with the public Beta (released 12 May). What do you get? To use a non-technical term, bupkis. I kid you not, nothing gets rendered. Fantastic, here’s a screenshot of what I get. Note that if you right-click the region where the plugin should be, nothing happens.&amp;#160; No Silverlight context menu, no Safari HTML menu, bupkis.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=Plugin-Safari.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Plugin-Safari" border="0" alt="Plugin-Safari" src="http://www.brendanforster.com/image.axd?picture=Plugin-Safari_thumb.png" width="454" height="355" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The annoying part of Safari 4 for Windows is that the Mac version supports windowless content (side note: it renders all content as windowless irrespective of what the plugin specifies – funky!) and renders the plugin content fine (I don’t have a Mac handy – yet). This may be just an issue with the beta (only been available for a couple of weeks). Safari 3 works fine with Silverlight 2 plugins – so I’m sure that it’ll be resolved between now and then.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;EDIT 9/6&lt;/strong&gt; – Safari 4 for Windows came out of beta to celebrate WWDC. Problem still occurring.&lt;/p&gt;  &lt;h3&gt;Google Chrome and Chromium&lt;/h3&gt;  &lt;p&gt;The new browser on the block (and its open-source brother) started out with no support for Silverlight at all. So how does it handle this plugin now? This is using Chrome v2.0.172.30. As the screenshot below shows, the experience is better, but the transparency is being ignored. The same thing occurs on the latest Chromium build I could find (build 17842).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=Plugin-Chrome.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Plugin-Chrome" border="0" alt="Plugin-Chrome" src="http://www.brendanforster.com/image.axd?picture=Plugin-Chrome_thumb.png" width="454" height="338" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;The Moral of the Story&lt;/h3&gt;  &lt;p&gt;Things I’ve learned from this:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;1. Avoid using Windowless plugins unless required. This hinges on whether or not a large percentage of your visitors use Safari or Chrome, as well as on the numerous gotchas to keep in mind, such as CPU overhead, mouse events, full-screen mode and others. The situation may also dictate it.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;2. Wait for Silverlight 3.0 for additional supported browser. I found a &lt;a href="http://code.google.com/p/chromium/issues/list?can=1&amp;amp;q=silverlight+windowless&amp;amp;colspec=ID+Stars+Pri+Area+Type+Status+Summary+Modified+Owner+Mstone&amp;amp;x=mstone&amp;amp;y=area&amp;amp;cells=tiles"&gt;few threads&lt;/a&gt; on the Chrome boards about Silverlight 2.0 issues - most of them are resolved but &lt;a href="http://code.google.com/p/chromium/issues/detail?id=301"&gt;this one&lt;/a&gt; (see last comment) makes me think that we may have to wait until Silverlight 3 to get full Silverlight 2.0 support in Chrome (which doesn’t sound right unless there are upstream issues). &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;3. Safari for Windows &lt;a href="http://msdn.microsoft.com/en-us/library/dd229102(VS.95).aspx"&gt;isn’t officially supported&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;4. There isn’t much documentation around what Silverlight features work/don’t work in unsupported browsers (I haven’t even looked at Opera for this). The MSDN docs are vague, bug reports are sporadic, and most discussions I’ve seen on the Silverlight forums end with a “It doesn’t work” summary. Or have I missed some magic fountain of information about this? &lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/06/07/Side-Note-Silverlight-2-Windowless-Browser-Support.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/06/07/Side-Note-Silverlight-2-Windowless-Browser-Support.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=345b335e-8805-4c9a-9a9d-08458cd28838</guid>
      <pubDate>Sun, 07 Jun 2009 19:53:26 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=345b335e-8805-4c9a-9a9d-08458cd28838</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=345b335e-8805-4c9a-9a9d-08458cd28838</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/06/07/Side-Note-Silverlight-2-Windowless-Browser-Support.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=345b335e-8805-4c9a-9a9d-08458cd28838</wfw:commentRss>
    </item>
    <item>
      <title>Microsoft Extensibility Framework – Introduction and Sample</title>
      <description>&lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;MEF is new a library that enables extensibility to be added to an application.&amp;#160; Extensibility isn’t a new concept, but MEF is an attempt by Microsoft to provide a single architecture across the .NET platform to enable flexible development of extensibility without having to roll your own code, or having to learn a completely new architecture when writing a plug-in for another application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The other thing to note is that MEF is more than a CodePlex project – it is slated to be a part of .NET Framework v4. The latest previews have focused on making the libraries more developer friendly. The preview versions are available from &lt;a href="http://mef.codeplex.com" target="_blank"&gt;the Codeplex site&lt;/a&gt;. &lt;/p&gt; &lt;break /&gt;  &lt;h3&gt;Using MEF in your Application&lt;/h3&gt;  &lt;p&gt;To get started using MEF, grab the latest drop from &lt;a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=25797" target="_blank"&gt;this page&lt;/a&gt; (Preview 5 at the time of writing this) and add a reference to &lt;strong&gt;System.ComponentModel.Composition.dll &lt;/strong&gt;in your project.&lt;/p&gt;  &lt;h4&gt;Declaring Extensions&lt;/h4&gt;  &lt;p&gt;As a minimum, the class needs the &lt;strong&gt;Export&lt;/strong&gt; attribute and a reference to the MEF assembly. The code snippet below shows the current contract attributes available (more than one Export attributes can be used on a class).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippet" class="csharpcode"&gt;     &lt;pre class="alt"&gt;&lt;span id="lnum1" class="lnum"&gt;   1:&lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.ComponentModel.Composition;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum2" class="lnum"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum3" class="lnum"&gt;   3:&lt;/span&gt; [Export]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum4" class="lnum"&gt;   4:&lt;/span&gt; [Export(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum5" class="lnum"&gt;   5:&lt;/span&gt; [Export(&lt;span class="str"&gt;&amp;quot;ExportTypeFoo&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum6" class="lnum"&gt;   6:&lt;/span&gt; [Export(&lt;span class="str"&gt;&amp;quot;ExportTypeFoo&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum7" class="lnum"&gt;   7:&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyExtension : IExtension&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum8" class="lnum"&gt;   8:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum9" class="lnum"&gt;   9:&lt;/span&gt;     &lt;span class="rem"&gt;// stuff here&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum10" class="lnum"&gt;  10:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Use the first attribute if you are looking to use this type specifically. I’m not as sold on the value of this, as it seems excessive to use MEF to find something I already have referenced – perhaps I haven’t found the right use for it yet.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The second attribute is good if you have a defined interface which all extensions should implement. If you arrange your application so that the core (interfaces, business logic) is separate from the UI, then you can write extensions in a separate assembly which references the core (or a subset containing the interfaces), and then add the extensions assembly into a predefined folder to make it available at runtime. &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The third attribute uses a string to define the contract type. This still suffers from the problems that come from using the first attribute, but I suppose its there for those times when you just don’t want to define interfaces.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The fourth attribute is for interfaces with a specific contract. For example, if you have an editor which allows extensions to be written to manipulate a text file. The editor supports right-click on selected text to run all available extensions, but also has a menu item with sections to group the extensions (eg Format, Transform, Validate). Rather than having multiple interfaces for each menu type, the grouping name and the interface becomes the contract type.&lt;/p&gt;

&lt;h4&gt;Consuming Extensions&lt;/h4&gt;

&lt;p&gt;One of my habits with consuming extensions is to wrap the functionality in a separate class. The code below shows the minimum that you need to configure a list of extensions:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span id="lnum1" class="lnum"&gt;   1:&lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.ComponentModel.Composition;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum2" class="lnum"&gt;   2:&lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.ComponentModel.Composition.Hosting;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum3" class="lnum"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum4" class="lnum"&gt;   4:&lt;/span&gt; &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BasicExtensionManager&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum5" class="lnum"&gt;   5:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum6" class="lnum"&gt;   6:&lt;/span&gt;     [ImportMany(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum7" class="lnum"&gt;   7:&lt;/span&gt;     &lt;span class="kwrd"&gt;private&lt;/span&gt; List&amp;lt;IExtension&amp;gt; allExtensions = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;IExtension&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum8" class="lnum"&gt;   8:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum9" class="lnum"&gt;   9:&lt;/span&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compose()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum10" class="lnum"&gt;  10:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum11" class="lnum"&gt;  11:&lt;/span&gt;         allExtensions.Clear();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum12" class="lnum"&gt;  12:&lt;/span&gt;         var catalog = &lt;span class="kwrd"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension)));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum13" class="lnum"&gt;  13:&lt;/span&gt;         var container = &lt;span class="kwrd"&gt;new&lt;/span&gt; CompositionContainer(catalog);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum14" class="lnum"&gt;  14:&lt;/span&gt;         var batch = &lt;span class="kwrd"&gt;new&lt;/span&gt; CompositionBatch();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum15" class="lnum"&gt;  15:&lt;/span&gt;         batch.AddPart(&lt;span class="kwrd"&gt;this&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum16" class="lnum"&gt;  16:&lt;/span&gt;         batch.AddExportedObject&amp;lt;AssemblyCatalog&amp;gt;(catalog);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum17" class="lnum"&gt;  17:&lt;/span&gt;         container.Compose(batch);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum18" class="lnum"&gt;  18:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum19" class="lnum"&gt;  19:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum20" class="lnum"&gt;  20:&lt;/span&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;IExtension&amp;gt; Extensions&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum21" class="lnum"&gt;  21:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum22" class="lnum"&gt;  22:&lt;/span&gt;         get&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum23" class="lnum"&gt;  23:&lt;/span&gt;         {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum24" class="lnum"&gt;  24:&lt;/span&gt;             &lt;span class="kwrd"&gt;return&lt;/span&gt; allExtensions;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum25" class="lnum"&gt;  25:&lt;/span&gt;         }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum26" class="lnum"&gt;  26:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum27" class="lnum"&gt;  27:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;I’ve used the &lt;strong&gt;ImportMany&lt;/strong&gt; attribute here to obtain multiple extensions (previous versions had this as an implicit behaviour of &lt;strong&gt;Import &lt;/strong&gt;but this has been separated out with Preview 5). You also have access to the &lt;strong&gt;Import&lt;/strong&gt; attribute if a single entity is required (for example, the catalog contains multiple items and you allow the user to select one, which is loaded into the application).&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;A few terms to note in the above snippet:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Catalog:&lt;/strong&gt; the catalog can be an &lt;strong&gt;AssemblyCatalog&lt;/strong&gt;, &lt;strong&gt;DirectoryCatalog&lt;/strong&gt; or &lt;strong&gt;TypeCatalog. &lt;/strong&gt;The catalog searches any found assemblies for the Export declarations mentioned above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CompositionBatch: &lt;/strong&gt;the Composition Batch is used to represent the “session” when using extensions. In this case I supplied the extension manager (which has a property with the Import declaration) and the catalog containing the extensions (which have the Export attributes as shown above). If the catalog is updated, the batch handles this (previously this had to be handled manually).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CompositionContainer:&lt;/strong&gt; handles the mapping of import and export objects. You can query the exported items here, or simply pass a &lt;strong&gt;CompositionBatch&lt;/strong&gt; and do it automatically.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The code to use this extension manager is shown below:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span id="lnum1" class="lnum"&gt;   1:&lt;/span&gt; BasicExtensionManager mgr = &lt;span class="kwrd"&gt;new&lt;/span&gt; BasicExtensionManager();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum2" class="lnum"&gt;   2:&lt;/span&gt; mgr.Compose();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum3" class="lnum"&gt;   3:&lt;/span&gt; &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var e &lt;span class="kwrd"&gt;in&lt;/span&gt; mgr.Extensions)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum4" class="lnum"&gt;   4:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum5" class="lnum"&gt;   5:&lt;/span&gt;     Console.WriteLine(e.ToString());&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum6" class="lnum"&gt;   6:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;Demo: Single Instance Plugins&lt;/h3&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;To drill down deeper on the importing of extensions, I thought I’d implement an extension manager that a) provides information about the types imported and b) creates instances of each available type for an application to use. For this sample, I have two extensions which are added to an application. &lt;strong&gt;ExtensionA&lt;/strong&gt; can be created in multiple instances, but &lt;strong&gt;ExtensionB &lt;/strong&gt;can only be used in a single instance. But how do we set this behaviour with MEF? &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The extension author has control over how their extension is used, so they can specify whether or not their code can exist in multiple instances. The trick is to use the new &lt;strong&gt;PartCreationPolicy&lt;/strong&gt; attribute. &lt;/p&gt;

&lt;p&gt;The options to provide with this attribute are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NonShared:&lt;/strong&gt; A new instance is created for each type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shared:&lt;/strong&gt; Only one instance of this type should exist at any time.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span id="lnum1" class="lnum"&gt;   1:&lt;/span&gt; [Export(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum2" class="lnum"&gt;   2:&lt;/span&gt; [PartCreationPolicy(CreationPolicy.NonShared)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum3" class="lnum"&gt;   3:&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ExtensionA : IExtension&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum4" class="lnum"&gt;   4:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum5" class="lnum"&gt;   5:&lt;/span&gt;     &lt;span class="rem"&gt;// implementation&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum6" class="lnum"&gt;   6:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum7" class="lnum"&gt;   7:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum8" class="lnum"&gt;   8:&lt;/span&gt; [Export(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum9" class="lnum"&gt;   9:&lt;/span&gt; [PartCreationPolicy(CreationPolicy.Shared)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum10" class="lnum"&gt;  10:&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ExtensionB : IExtension&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum11" class="lnum"&gt;  11:&lt;/span&gt; {    &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum12" class="lnum"&gt;  12:&lt;/span&gt;     &lt;span class="rem"&gt;// implementation&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum13" class="lnum"&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The consumer can also specify the creation policy for any imported extensions. This code snippet shows how the import can request extensions that are only single-instance:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span id="lnum1" class="lnum"&gt;   1:&lt;/span&gt; [ImportMany(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IExtension), RequiredCreationPolicy=CreationPolicy.Shared)]&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;To test this behaviour I used a Guid property on each extension. This is generated the first time it is read (there are other ways create a unique identifier, the Guid was was quick and relatively straightforward). I wrote a different implementation of the Extension Manager that uses this code snippet to create a new instance of a specific type. The container contains the catalog with the available extensions.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span id="lnum1" class="lnum"&gt;   1:&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; IExtension CreateNewInstance(Type t)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum2" class="lnum"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum3" class="lnum"&gt;   3:&lt;/span&gt;     var result = container.GetExportedObjects&amp;lt;IExtension&amp;gt;()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum4" class="lnum"&gt;   4:&lt;/span&gt;                     .Where(c =&amp;gt; c.GetType() == t).SingleOrDefault();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum5" class="lnum"&gt;   5:&lt;/span&gt;     &lt;span class="rem"&gt;// may get null if you pass an invalid type&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alteven"&gt;&lt;span id="lnum6" class="lnum"&gt;   6:&lt;/span&gt;     &lt;span class="kwrd"&gt;return&lt;/span&gt; result;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre class="alt"&gt;&lt;span id="lnum7" class="lnum"&gt;   7:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Now, when I run through the test suite and request multiple instances of each extension type, I get this resulting output. Extension A has a different identifier for each instance, and each instance of Extension B is simply a reference to the first instance.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;&lt;a href="http://www.brendanforster.com/image.axd?picture=MEF-Sample1.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="MEF-Sample1" border="0" alt="MEF-Sample1" src="http://www.brendanforster.com/image.axd?picture=MEF-Sample1_thumb.png" width="712" height="114" /&gt;&lt;/a&gt;&amp;#160;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Hooray! &lt;/p&gt;

&lt;h3&gt;Try it out&lt;/h3&gt;

&lt;p&gt;The sample I have included below has both extension managers and the tests used above. I’m aiming to have something more complex up next (need to polish the concept some more and do some refactoring of the application itself) but hopefully this is something that can be used as a starting point. The MEF assembly is included with this zip file.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;iframe style="border-bottom: #dde5e9 1px solid; border-left: #dde5e9 1px solid; padding-bottom: 0px; background-color: #ffffff; margin: 3px; padding-left: 0px; width: 240px; padding-right: 0px; height: 66px; border-top: #dde5e9 1px solid; border-right: #dde5e9 1px solid; padding-top: 0px" marginheight="0" src="http://cid-6cb1aeffbddc751b.skydrive.live.com/embedrowdetail.aspx/Code%20Samples/MEFDemo.zip" frameborder="0" marginwidth="0" scrolling="no"&gt;&lt;/iframe&gt;</description>
      <link>http://www.brendanforster.com/post/2009/05/09/Microsoft-Extensibility-Framework-e28093-Introduction-and-Sample.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/05/09/Microsoft-Extensibility-Framework-e28093-Introduction-and-Sample.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=3a12fb5e-fb47-4548-aed0-a8eb176dc06f</guid>
      <pubDate>Sat, 09 May 2009 15:35:56 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=3a12fb5e-fb47-4548-aed0-a8eb176dc06f</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=3a12fb5e-fb47-4548-aed0-a8eb176dc06f</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/05/09/Microsoft-Extensibility-Framework-e28093-Introduction-and-Sample.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=3a12fb5e-fb47-4548-aed0-a8eb176dc06f</wfw:commentRss>
    </item>
    <item>
      <title>Introduction</title>
      <description>&lt;p&gt;Welcome to my little place on the internet. Up until now I’ve been on the “consumer” side of blogging, so this is new territory for me (side note: I actually loathe the word “blog”, and I’ll always endeavour to use a better word in its place). If something requires clarification, or you have feedback to provide, just leave a note on the site or email me directly.&lt;/p&gt;  &lt;h3&gt;About Me&lt;/h3&gt;  &lt;p&gt;After growing up in Adelaide, I graduated from university with a Bachelor of Engineering (Computer Systems) and Bachelor of Maths and Computer Sciences in mid-2008. I’m now living in Sydney and currently working for nsquared solutions. The other details aren’t important right now, but if you want to beat the details out of me you’ll need to buy me a beer at a social event or something.&lt;/p&gt;  &lt;h3&gt;Background&lt;/h3&gt;  &lt;p&gt;Right now I’m wearing a .NET developer hat – this may change in the future, but for now that’s where the bulk of my experience lies, so I’m going to start off this blog (ugh, last time, I swear) by sharing my experiences in the .NET world. I mostly did Java through uni, with a bit of C and C++, perhaps I might touch on that a bit too, depending on the rust levels.&amp;#160; My professional .NET adventures started when I did some part-time ASP.NET work while at uni, and while ASP.NET remains my core competency I’ve branched out recently to do work with Live Services, WPF and Silverlight.&lt;/p&gt;  &lt;h3&gt;Goals&lt;/h3&gt;  &lt;p&gt;A lot of the software developers I know and read recommend having your own blog space – writing skills are a completely different domain to writing code, and demonstrating a concept or API function to someone else is completely different to understanding it yourself – and I want to give back to the community and contribute to the body of knowledge available regarding .NET and software development. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;There’s a whole bunch of new and updated tools and technologies coming out of Redmond recently, and with Win7 and .NET 4.0 on the horizon this is a very exciting time for anyone who is interested in playing around with new stuff. Specifically, I’ve been playing around with the &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;Microsoft Extensibility Framework&lt;/a&gt; a bit over the last few days (got some basic samples, but needs some polish to finally be an awesome F5-style demo) and &lt;a href="http://www.asp.net/mvc/" target="_blank"&gt;ASP.NET MVC&lt;/a&gt; continues to catch my eye (after wrapping up a WebForms project I’m going to put some time into that). &lt;a href="http://silverlight.net/" target="_blank"&gt;Silverlight&lt;/a&gt; (in particular some of the v3 features) look to be blurring the lines between web, RIA and client applications. And &lt;a href="http://dev.live.com/virtualearth/" target="_blank"&gt;Virtual Earth&lt;/a&gt; and &lt;a href="http://dev.live.com/liveframework/" target="_blank"&gt;Live Framework&lt;/a&gt; continue to evolve and grow, with updates and new features that seem to have been lost in the noise of other product announcements.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In addition to talking about .NET I hope to branch out more into the practices and concepts that are associated with software development, software engineering or whatever-you-wish-to-call-it-when-people-build-software. This is a young field, which is rapidly evolving, and there are many, many ways to get from A to B. I do not have all the answers right now. But over time, hopefully I can pick the brains of others in the industry, dissect opinions and articles from others, and add my insight and opinions to the process wherever appropriate.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’ll also include some non-work stuff as it happens – because all work and no play makes Jack a dull boy. &lt;/p&gt;  &lt;h3&gt;Code Samples&lt;/h3&gt;  &lt;p&gt;While I agree with the sentiments of others - that we need to do more to improve the quality of code in use everywhere - many of the samples that will appear here will not demonstrate best practices &lt;em&gt;initially&lt;/em&gt; – most of the ideas I have in place currently are around proof-of-concept work and demonstrations of specific features. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As concepts grow and evolve, my personal goal is to turn these samples into standalone applications - wherever there is a genuine interest in the community. Of course, some of these may be purely for fun or for mad-scientist-esque purposes, so not all of these will achieve greatness (or even mediocrity). So if something you see here fills a niche, let me know and perhaps we can organise some collaborate so that we can critique and refine and continue to drive interest and make the world a better place, one line of code at a time.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As others have said &lt;a href="http://haacked.com/archive/2009/04/16/code-sample-taxonomy.aspx#feedback" target="_blank"&gt;far more eloquently&lt;/a&gt; in a recent discussion on this, we need to do more around outlining what the sample includes and, more importantly, emphasize what the sample does not cover. Is there value in giving a sample &lt;a href="http://www.straightdope.com/columns/read/2026/whats-the-origin-of-to-treat-with-kid-gloves" target="_blank"&gt;the kid gloves treatment&lt;/a&gt; to protect that someone who does the F5-oh-look-it-works trick before using it elsewhere? Where is the point where the sample needs to satisfy the necessary requirements that production code is held to? Is there a happy medium? Can we even define succinctly what a “good sample” should constitute? &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The only thing I’m sure of is that a samples should clearly define what it is not demonstrating&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;</description>
      <link>http://www.brendanforster.com/post/2009/05/03/Introduction.aspx</link>
      <author>Brendan Forster</author>
      <comments>http://www.brendanforster.com/post/2009/05/03/Introduction.aspx#comment</comments>
      <guid>http://www.brendanforster.com/post.aspx?id=c53c8e10-80d6-49b3-9fa5-9e786f13533e</guid>
      <pubDate>Sun, 03 May 2009 10:09:00 +0600</pubDate>
      <dc:publisher>Administrator</dc:publisher>
      <pingback:server>http://www.brendanforster.com/pingback.axd</pingback:server>
      <pingback:target>http://www.brendanforster.com/post.aspx?id=c53c8e10-80d6-49b3-9fa5-9e786f13533e</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.brendanforster.com/trackback.axd?id=c53c8e10-80d6-49b3-9fa5-9e786f13533e</trackback:ping>
      <wfw:comment>http://www.brendanforster.com/post/2009/05/03/Introduction.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.brendanforster.com/syndication.axd?post=c53c8e10-80d6-49b3-9fa5-9e786f13533e</wfw:commentRss>
    </item>
  </channel>
</rss>

