<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>IIrrelevant - C#</title>
    <link>http://www.milkcarton.com/blog/</link>
    <description>Irrelevant musings about software development</description>
    <language>en-us</language>
    <copyright>Dan Morphis</copyright>
    <lastBuildDate>Mon, 05 Jul 2010 16:00:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@milkcarton.com</managingEditor>
    <webMaster>blog@milkcarton.com</webMaster>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=334639e4-7e22-4df6-8239-e7d5a4433b65</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,334639e4-7e22-4df6-8239-e7d5a4433b65.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,334639e4-7e22-4df6-8239-e7d5a4433b65.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=334639e4-7e22-4df6-8239-e7d5a4433b65</wfw:commentRss>
      <title>Databinding to an NSTableView with a MonoMac app</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,334639e4-7e22-4df6-8239-e7d5a4433b65.aspx</guid>
      <link>http://www.milkcarton.com/blog/2010/07/05/Databinding+To+An+NSTableView+With+A+MonoMac+App.aspx</link>
      <pubDate>Mon, 05 Jul 2010 16:00:00 GMT</pubDate>
      <description>&lt;p&gt;
I’m working on my presentation for the &lt;a href="http://akdotnet.ning.com/"&gt;Alaska
.NET Users Group&lt;/a&gt;. My goal is to build an app that consumes a web service, and
displays the data. After getting the web service piece going, the next step was to
display that data in a NSTableView.
&lt;/p&gt;
&lt;p&gt;
I battled for hours and hours trying to figure out how to create a NSTableViewDataSource,
and it turns out I was making things way to complicated. I did some searching on the
web, but couldn’t really find anything except a &lt;a href="http://lists.ximian.com/pipermail/mono-osx/2010-June/002965.html"&gt;post&lt;/a&gt; by &lt;a href="http://twitter.com/moshakis"&gt;John
Moshakis&lt;/a&gt; on the Mono-osx mailing list which gave me some hope that someone had
figured it out.
&lt;/p&gt;
&lt;p&gt;
I emailed John, and he was gracious enough to send me some source code to work from.
It turns out, I what I had wasn’t far off the mark, I just needed a few pointers.
&lt;/p&gt;
&lt;h3&gt;The DataSource Code
&lt;/h3&gt;
&lt;div style="font-family: consolas,; font-size: small" class="csharp" #ffffff;?="#ffffff;?" background-color:="background-color:" monospace;="monospace;" courier,="courier," New?,="New?," Courier="Courier"&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;using&lt;/span&gt; &lt;span style="color: #008080"&gt;System&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;using&lt;/span&gt; &lt;span style="color: #008080"&gt;System.Collections&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;using&lt;/span&gt; &lt;span style="color: #008080"&gt;MonoMac.Foundation&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;using&lt;/span&gt; &lt;span style="color: #008080"&gt;MonoMac.AppKit&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;using&lt;/span&gt; &lt;span style="color: #008080"&gt;MovieTimes.BusinessLogic&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;using&lt;/span&gt; &lt;span style="color: #008080"&gt;System.Collections.Generic&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;[&lt;/span&gt;Register&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #666666"&gt;&amp;quot;MyItemDataSource&amp;quot;&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;]&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;class&lt;/span&gt; MyItemDataSource &lt;span style="color: #008000"&gt;:&lt;/span&gt; NSTableViewDataSource
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; List&lt;span style="color: #008000"&gt;&amp;lt;&lt;/span&gt;MyItem&lt;span style="color: #008000"&gt;&amp;gt;&lt;/span&gt; MyItems &lt;span style="color: #008000"&gt;{&lt;/span&gt; get&lt;span style="color: #008000"&gt;;&lt;/span&gt; set&lt;span style="color: #008000"&gt;;&lt;/span&gt; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;static&lt;/span&gt; MyItemDataSource &lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;[&lt;/span&gt;Export&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #666666"&gt;&amp;quot;numberOfRowsInTableView:&amp;quot;&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;]&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;int&lt;/span&gt; numberOfRowsInTableView &lt;span style="color: #008000"&gt;(&lt;/span&gt;NSTableView
aTableView&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;MyItems &lt;span style="color: #008000"&gt;==&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #ff0000"&gt;0&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; MyItems&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Count&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;[&lt;/span&gt;Export&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #666666"&gt;&amp;quot;tableView:objectValueForTableColumn:row:&amp;quot;&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;]&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; NSObject
objectValueForTableColumn &lt;span style="color: #008000"&gt;(&lt;/span&gt;NSTableView aTableView,
NSTableColumn aTableColumn, &lt;span style="color: #6666cc; font-weight: bold"&gt;int&lt;/span&gt; rowIndex&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;a href="http://www.google.com/search?q=new+msdn.microsoft.com"&gt;&lt;span style="color: #008000"&gt;new&lt;/span&gt;&lt;/a&gt; NSString&lt;span style="color: #008000"&gt;(&lt;/span&gt;MyItems&lt;span style="color: #008000"&gt;[&lt;/span&gt;rowIndex&lt;span style="color: #008000"&gt;]&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Name&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;void&lt;/span&gt; Items&lt;span style="color: #008000"&gt;(&lt;/span&gt;List&lt;span style="color: #008000"&gt;&amp;lt;&lt;/span&gt;MyItem&lt;span style="color: #008000"&gt;&amp;gt;&lt;/span&gt; items&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
MyItems &lt;span style="color: #008000"&gt;=&lt;/span&gt; items&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;class&lt;/span&gt; MyItemDataSourceDelegate &lt;span style="color: #008000"&gt;:&lt;/span&gt; NSTableViewDelegate &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;event&lt;/span&gt; EventHandler&lt;span style="color: #008000"&gt;&amp;lt;&lt;/span&gt;MyItemChangedEventArgs&lt;span style="color: #008000"&gt;&amp;gt;&lt;/span&gt; MyItemChanged&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; MyItemDataSourceDelegate &lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;:&lt;/span&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;base&lt;/span&gt;&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&amp;#160; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;override&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;void&lt;/span&gt; SelectionDidChange&lt;span style="color: #008000"&gt;(&lt;/span&gt;NSNotification
notification&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
var table &lt;span style="color: #008000"&gt;=&lt;/span&gt; notification&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #6666cc; font-weight: bold"&gt;Object&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;as&lt;/span&gt; NSTableView&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
var ds &lt;span style="color: #008000"&gt;=&lt;/span&gt; table&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;DataSource&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;as&lt;/span&gt; MyItemDataSource&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
var rowNum &lt;span style="color: #008000"&gt;=&lt;/span&gt; table&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;SelectedRow&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;rowNum &lt;span style="color: #008000"&gt;&amp;gt;=&lt;/span&gt; &lt;span style="color: #ff0000"&gt;0&lt;/span&gt; &lt;span style="color: #008000"&gt;&amp;amp;&amp;amp;&lt;/span&gt; rowNum &lt;span style="color: #008000"&gt;&amp;lt;&lt;/span&gt; ds&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;MyItems&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Count&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
OnMyItemChanged&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;a href="http://www.google.com/search?q=new+msdn.microsoft.com"&gt;&lt;span style="color: #008000"&gt;new&lt;/span&gt;&lt;/a&gt; MyItemChangedEventArgs&lt;span style="color: #008000"&gt;(&lt;/span&gt;ds&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;MyItems&lt;/span&gt;&lt;span style="color: #008000"&gt;[&lt;/span&gt;rowNum&lt;span style="color: #008000"&gt;]&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;protected&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;void&lt;/span&gt; OnMyItemChanged&lt;span style="color: #008000"&gt;(&lt;/span&gt;MyItemChangedEventArgs
e&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;MyItemChanged &lt;span style="color: #008000"&gt;!=&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
MyItemChanged&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;this&lt;/span&gt;,
e&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;class&lt;/span&gt; MyItemChangedEventArgs &lt;span style="color: #008000"&gt;:&lt;/span&gt; EventArgs &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; MyItem
MyItem &lt;span style="color: #008000"&gt;{&lt;/span&gt; get&lt;span style="color: #008000"&gt;;&lt;/span&gt; set&lt;span style="color: #008000"&gt;;&lt;/span&gt; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; MyItemChangedEventArgs&lt;span style="color: #008000"&gt;(&lt;/span&gt;MyItem
i&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
MyItem &lt;span style="color: #008000"&gt;=&lt;/span&gt; i&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The code is kind of long, and I’ve removed all the normal guard code you would use
for the sake of brevity. But lets dissect the code. The first part, lines 8 – 31 is
the actual data source. It simply listens for a few messages (numberOfRowsInTableView
and objectValueForTableColumn) and allows items to be added to the data source (line
28). The next part, lines 33 – 54 fire off an event when the selected item has changed
in the NSTableView instance.
&lt;/p&gt;
&lt;h3&gt;The Window Code
&lt;/h3&gt;
&lt;div style="font-family: consolas,; font-size: small" class="csharp" #ffffff;?="#ffffff;?" background-color:="background-color:" monospace;="monospace;" courier,="courier," New?,="New?," Courier="Courier"&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;override&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;void&lt;/span&gt; WindowDidLoad &lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;base&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;WindowDidLoad&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;
var myDel &lt;span style="color: #008000"&gt;=&lt;/span&gt; &lt;a href="http://www.google.com/search?q=new+msdn.microsoft.com"&gt;&lt;span style="color: #008000"&gt;new&lt;/span&gt;&lt;/a&gt; MyItemDataSourceDelegate&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;
myDel&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;ItemChanged&lt;/span&gt; &lt;span style="color: #008000"&gt;+=&lt;/span&gt; ItemChanged&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;someTable&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #6666cc; font-weight: bold"&gt;Delegate&lt;/span&gt; &lt;span style="color: #008000"&gt;=&lt;/span&gt; myDel&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;someTable&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;DataSource&lt;/span&gt; &lt;span style="color: #008000"&gt;=&lt;/span&gt; &lt;a href="http://www.google.com/search?q=new+msdn.microsoft.com"&gt;&lt;span style="color: #008000"&gt;new&lt;/span&gt;&lt;/a&gt; MyItemDataSource&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;p&gt;
In the window controller, paste the above code to have code run when you window loads.
For WinForms people, public override void WindowDidLoad is the equivilent of protected
override void OnLoad().
&lt;/p&gt;
&lt;p&gt;
So what does the window controller code do? First off, we create our delegate (note,
delegates in Cocoa are &lt;strong&gt;not&lt;/strong&gt; the same thing as in C#), and wire up
to the changed event, then we assign the newly created delegate and a data source
to our table.
&lt;/p&gt;
&lt;p&gt;
Any questions, thoughts or feedback? Hit me up in the comments, or &lt;a href="http://twitter.com/akcoder"&gt;follow
me on twitter&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=334639e4-7e22-4df6-8239-e7d5a4433b65" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,334639e4-7e22-4df6-8239-e7d5a4433b65.aspx</comments>
      <category>C#</category>
      <category>Mac OS X</category>
      <category>Mono</category>
      <category>MonoMac</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=454b3531-266d-40ee-821d-f86b18758acc</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,454b3531-266d-40ee-821d-f86b18758acc.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,454b3531-266d-40ee-821d-f86b18758acc.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=454b3531-266d-40ee-821d-f86b18758acc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When Miguel De Icaza <a href="http://tirania.org/blog/archive/2010/Apr-19.html">first
announced MonoMac</a> back in April I was pretty excited! Its been my goal for the
past two years to write a half-decent, <strong>native</strong> looking, Mac OSX application
with .NET. I’ve looked at the various Cocoa binding strategies for Mono and always
found them lacking. Some like <a href="http://www.mono-project.com/CocoaSharp">CocoaSharp</a> haven’t
been updated in ages, and the WinForms stuff on Mac looked plain fugly. Like Windows
95 fugly.
</p>
        <p>
I found <a href="http://mjhutchinson.com/journal/2010/06/09/monomac_in_monodevelop">Michael
Hutchinson’s quick-start post</a> on writing a simple .NET app using MonoMac. Because
I had previous experience using Interface Builder, I was able to muddle my way through
building his simple app. But at the end, I wanted something more. “Hello World” is
great to show you the most basic syntax of a language, and maybe how to use the compiler,
but it honestly does nothing for me.
</p>
        <p>
So, I set out to write real, although somewhat contrived image viewing application.
Having an extensive background writing WinForms, ASP.NET and some WPF apps, the hardest
part for me was trying to figure out the Cocoa equivalent to things like PictureBox,
and FolderBrowserDialog. But Google as always was my friend.
</p>
        <p>
I’m not going to dive into how to write the app tonight, but I will post a screen
shot, and the <a href="http://www.milkcarton.com/blog/content/binary/ImageViewer.zip">ImageViewer
source!</a><br /><a href="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/MyfirstMonoMacapplicationanditsnotHelloW_144FC/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/MyfirstMonoMacapplicationanditsnotHelloW_144FC/image_thumb.png" width="458" height="439" /></a></p>
        <h4>Nits
</h4>
        <p>
Yes Cocoa is based on an MVC architecture, and yes I should have put some of the code
into a business class, but I didn’t. 
</p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=454b3531-266d-40ee-821d-f86b18758acc" />
      </body>
      <title>My first MonoMac application, and its not “Hello World”</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,454b3531-266d-40ee-821d-f86b18758acc.aspx</guid>
      <link>http://www.milkcarton.com/blog/2010/07/02/My+First+MonoMac+Application+And+Its+Not+Hello+World.aspx</link>
      <pubDate>Fri, 02 Jul 2010 15:22:00 GMT</pubDate>
      <description>&lt;p&gt;
When Miguel De Icaza &lt;a href="http://tirania.org/blog/archive/2010/Apr-19.html"&gt;first
announced MonoMac&lt;/a&gt; back in April I was pretty excited! Its been my goal for the
past two years to write a half-decent, &lt;strong&gt;native&lt;/strong&gt; looking, Mac OSX application
with .NET. I’ve looked at the various Cocoa binding strategies for Mono and always
found them lacking. Some like &lt;a href="http://www.mono-project.com/CocoaSharp"&gt;CocoaSharp&lt;/a&gt; haven’t
been updated in ages, and the WinForms stuff on Mac looked plain fugly. Like Windows
95 fugly.
&lt;/p&gt;
&lt;p&gt;
I found &lt;a href="http://mjhutchinson.com/journal/2010/06/09/monomac_in_monodevelop"&gt;Michael
Hutchinson’s quick-start post&lt;/a&gt; on writing a simple .NET app using MonoMac. Because
I had previous experience using Interface Builder, I was able to muddle my way through
building his simple app. But at the end, I wanted something more. “Hello World” is
great to show you the most basic syntax of a language, and maybe how to use the compiler,
but it honestly does nothing for me.
&lt;/p&gt;
&lt;p&gt;
So, I set out to write real, although somewhat contrived image viewing application.
Having an extensive background writing WinForms, ASP.NET and some WPF apps, the hardest
part for me was trying to figure out the Cocoa equivalent to things like PictureBox,
and FolderBrowserDialog. But Google as always was my friend.
&lt;/p&gt;
&lt;p&gt;
I’m not going to dive into how to write the app tonight, but I will post a screen
shot, and the &lt;a href="http://www.milkcarton.com/blog/content/binary/ImageViewer.zip"&gt;ImageViewer
source!&lt;/a&gt; 
&lt;br /&gt;
&lt;a href="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/MyfirstMonoMacapplicationanditsnotHelloW_144FC/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/MyfirstMonoMacapplicationanditsnotHelloW_144FC/image_thumb.png" width="458" height="439" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;h4&gt;Nits
&lt;/h4&gt;
&lt;p&gt;
Yes Cocoa is based on an MVC architecture, and yes I should have put some of the code
into a business class, but I didn’t. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=454b3531-266d-40ee-821d-f86b18758acc" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,454b3531-266d-40ee-821d-f86b18758acc.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>Mac OS X</category>
      <category>Mono</category>
      <category>MonoMac</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=52104a75-5e30-4ee5-b966-b44e6cf7840f</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,52104a75-5e30-4ee5-b966-b44e6cf7840f.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,52104a75-5e30-4ee5-b966-b44e6cf7840f.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=52104a75-5e30-4ee5-b966-b44e6cf7840f</wfw:commentRss>
      <title>Sometimes, you have to go back to the pointers</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,52104a75-5e30-4ee5-b966-b44e6cf7840f.aspx</guid>
      <link>http://www.milkcarton.com/blog/2010/06/29/Sometimes+You+Have+To+Go+Back+To+The+Pointers.aspx</link>
      <pubDate>Tue, 29 Jun 2010 14:45:00 GMT</pubDate>
      <description>&lt;p&gt;
A while back, I was tasked with solving a defect in our system. The defect was “Changes
to an image can be reverted only one time.”
&lt;/p&gt;
&lt;p&gt;
As I started to look into the code, I saw something like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre style="font-family: consolas,; font-size: small" class="csharp" #ffffff;? background-color: monospace; courier, New?, Courier&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;private&lt;/span&gt; Image
_originalImage&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;private&lt;/span&gt; Image
_image&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;void&lt;/span&gt; Show&lt;span style="color: #008000"&gt;(&lt;/span&gt;Image
image&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;
_image &lt;span style="color: #008000"&gt;=&lt;/span&gt; image&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;
_originalImage &lt;span style="color: #008000"&gt;=&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;Image&lt;span style="color: #008000"&gt;)&lt;/span&gt; image&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Clone&lt;/span&gt;&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;private&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;void&lt;/span&gt; Revert&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;
_image &lt;span style="color: #008000"&gt;=&lt;/span&gt; _originalImage&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px"&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/pre&gt;&amp;nbsp;&gt;
&lt;p&gt;
Looking at the code, I suspected I knew what was wrong, but I wanted to confirm it.
So I ran the app, made modifications to the image, clicked revert, made modifications
to the image again, set a break point and clicked revert again. Once Visual Studio
broke, I popped over to the immediate window and typed this: 
&lt;/p&gt;
&lt;pre style="background-color: rgb(244,244,244)"&gt;&amp;amp;_image
Which returned: 0x1234abd0
&amp;amp;_originalImage
Which also returned: 0x1234abd0&lt;/pre&gt;
&lt;p&gt;
Now I knew what the issue was, the original image was getting clobbered due to a missed
.Clone() on line 10. A simple _image = (Image)_originalImage.Clone(); fixed the issue.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=52104a75-5e30-4ee5-b966-b44e6cf7840f" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,52104a75-5e30-4ee5-b966-b44e6cf7840f.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=ed3efa4a-022a-4af7-b75a-17a66803088c</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,ed3efa4a-022a-4af7-b75a-17a66803088c.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,ed3efa4a-022a-4af7-b75a-17a66803088c.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=ed3efa4a-022a-4af7-b75a-17a66803088c</wfw:commentRss>
      <title>How to discover if your UserControl is designer hosted</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,ed3efa4a-022a-4af7-b75a-17a66803088c.aspx</guid>
      <link>http://www.milkcarton.com/blog/2010/06/29/How+To+Discover+If+Your+UserControl+Is+Designer+Hosted.aspx</link>
      <pubDate>Tue, 29 Jun 2010 01:45:00 GMT</pubDate>
      <description>&lt;p&gt;
The other day, Visual Studio 2008 kept crashing due to an issue with one of our user
controls. When I opened a WinForm which contained the control, VS would initialize
everything, get to an exception and crash. The exception was caused by the user control
running code that it should not have in the designer. A quick search turned up &lt;a href="http://stackoverflow.com/questions/34664/designmode-with-controls"&gt;this
post on stackoverflow&lt;/a&gt;
&lt;/p&gt;
&lt;div style="font-family: consolas,; font-size: small" class="csharp" #ffffff;?="#ffffff;?" background-color:="background-color:" monospace;="monospace;" courier,="courier," New?,="New?," Courier="Courier"&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;LicenseManager&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;UsageMode&lt;/span&gt; &lt;span style="color: #008000"&gt;==&lt;/span&gt; LicenseUsageMode&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Runtime&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160; &lt;span style="color: #008000"&gt;...&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;p&gt;
This code works in most scenarios, but failed for me. More searching turned up a &lt;a href="http://decav.com/blogs/andre/archive/2007/04/18/1078.aspx"&gt;post
by Andre de Cavaignac&lt;/a&gt;
&lt;/p&gt;
&lt;div style="font-family: consolas,; font-size: small" class="csharp" #ffffff;?="#ffffff;?" background-color:="background-color:" monospace;="monospace;" courier,="courier," New?,="New?," Courier="Courier"&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;bool&lt;/span&gt; IsDesignerHosted &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
get &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;
Control ctrl &lt;span style="color: #008000"&gt;=&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;ctrl &lt;span style="color: #008000"&gt;!=&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;ctrl&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Site&lt;/span&gt; &lt;span style="color: #008000"&gt;==&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;false&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;ctrl&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Site&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;DesignMode&lt;/span&gt; &lt;span style="color: #008000"&gt;==&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;true&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;true&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
ctrl &lt;span style="color: #008000"&gt;=&lt;/span&gt; ctrl&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Parent&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;false&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;p&gt;
I found that mostly worked as well. I ended up having to combine them to detect correctly
100% of the time if the control was designer hosted. I created it as an extension
method, and here is the result
&lt;/p&gt;
&lt;div style="font-family: consolas,; font-size: small" class="csharp" #ffffff;?="#ffffff;?" background-color:="background-color:" monospace;="monospace;" courier,="courier," New?,="New?," Courier="Courier"&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="font-style: italic; color: #008080"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="font-style: italic; color: #008080"&gt;///
Used to detect whether the control is being hosted on the surface of a form at design-time,&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="font-style: italic; color: #008080"&gt;///
since the DesignMode property no longer works once the control has been compiled and&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="font-style: italic; color: #008080"&gt;///
hosted on the surface of another design-time control.&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="font-style: italic; color: #008080"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;static&lt;/span&gt; &lt;span style="color: #6666cc; font-weight: bold"&gt;bool&lt;/span&gt; IsDesignerHosted&lt;span style="color: #008000"&gt;(&lt;/span&gt;&lt;span style="color: #0600ff; font-weight: bold"&gt;this&lt;/span&gt; Control
ctrl&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
get &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;ctrl &lt;span style="color: #008000"&gt;!=&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt; &lt;span style="color: #008000"&gt;{&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;ctrl&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Site&lt;/span&gt; &lt;span style="color: #008000"&gt;==&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;false&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;ctrl&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Site&lt;/span&gt;&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;DesignMode&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0600ff; font-weight: bold"&gt;true&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
ctrl &lt;span style="color: #008000"&gt;=&lt;/span&gt; ctrl&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Parent&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0600ff; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #008000"&gt;(&lt;/span&gt;LicenseManager&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;UsageMode&lt;/span&gt; &lt;span style="color: #008000"&gt;==&lt;/span&gt; LicenseUsageMode&lt;span style="color: #008000"&gt;.&lt;/span&gt;&lt;span style="color: #0000ff"&gt;Designtime&lt;/span&gt;&lt;span style="color: #008000"&gt;)&lt;/span&gt;&lt;span style="color: #008000"&gt;;&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&amp;#160; &lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #f4f4f4"&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 1em/1.2em monospace; background: none transparent scroll repeat 0% 0%; vertical-align: top; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=ed3efa4a-022a-4af7-b75a-17a66803088c" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,ed3efa4a-022a-4af7-b75a-17a66803088c.aspx</comments>
      <category>C#</category>
      <category>WinForms</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=bcd01da5-498e-410c-a687-acc721220457</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=bcd01da5-498e-410c-a687-acc721220457</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Background
</h3>
        <p>
For a project I'm doing, I have a task model for the various pieces. In the beginning,
I was manually creating a List&lt;ITask&gt;. As I kept adding tasks to run, I started
thinking about hacking some code together to rifle through my assembly and pull back
all the classes which implement ITask.
</p>
        <p>
Then I remember hearing about Managed Extensibility Framework (MEF). I did some searching,
found the <a href="http://www.codeplex.com/MEF/" target="_blank">MEF home page</a>,
and even read the <a href="http://mef.codeplex.com/Wiki/View.aspx?title=Overview&amp;referringTitle=Home" target="_blank">MEF
overview</a>. But none of that told me what I really wanted to know, what's the fastest
way to get started using MEF as a component loader?
</p>
        <p>
I did some more searching and found the <a href="http://dnrtv.com/" target="_blank">dnrTV</a> episode
"<a href="http://www.dnrtv.com/default.aspx?showNum=130" target="_blank">Glenn Block
on MEF, the Managed Extensibility Framework</a>" and after 20-30 minutes they finally
got down to how to create a plugin for your app.
</p>
        <p>
But what I really wanted, and I bet a lot of others, is a quick start guide for creating
a plugin.
</p>
        <h3>Solution
</h3>
        <p>
          <a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22313" target="_blank">Download
the latest version of MEF</a>, as of this writing its Preview 4. Grab the System.ComponentModel.Composition.dll
from the bin folder and stash it somewhere. Make a reference to said dll in your project.
</p>
        <p>
On your plugin class, add Export attribute:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[Export(<span style="color: #0000ff">typeof</span>(IPlugin))] <span style="color: #0000ff">public</span><span style="color: #0000ff">class</span> Foo
: IPlugin { ... }</pre>
        </div>
        <p>
In your plugin consumer, create a property to hold your plugins, and add the Import
attribute:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[Import(<span style="color: #0000ff">typeof</span>(IPlugin))] <span style="color: #0000ff">internal</span> IList&lt;IPlugin&gt;
_myPlugins { get; set; }</pre>
        </div>
        <p>
Now, tell MEF where to get the plugins at (line 2), and where you want MEF to fulfill
any plugins (line 5):
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <p>
              <span style="color: #0000ff">private</span>
              <span style="color: #0000ff">void</span> LoadPlugins()
{ var catalog = <span style="color: #0000ff">new</span> AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = <span style="color: #0000ff">new</span> CompositionContainer(catalog);
var batch = <span style="color: #0000ff">new</span> CompositionBatch(); batch.AddPart(<span style="color: #0000ff">this</span>);
container.Compose(batch); } 
</p>
            <p>
 
</p>
          </pre>
        </div>
        <p>
I put my call to the LoadPlugins method in the constructor.
</p>
        <p>
Now, spin through your plugins and do the work:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">Console.WriteLine(<span style="color: #006080">"Found
{0} plugins"</span>, _myPlugins.Count); <span style="color: #0000ff">foreach</span> (var
plugin <span style="color: #0000ff">in</span> _myPlugins) { Console.WriteLine(plugin.Name);
}</pre>
        </div>
        <p>
 
</p>
        <p>
          <a href="/blog/content/binary/5_Minute_MEF_Tutorial.zip" target="_blank">Download
the complete source</a> to this (really, only about 10 extra lines to glue things
together) and have fun!
</p>
        <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx">
          <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx" border="0" alt="kick it on DotNetKicks.com" />
        </a>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=bcd01da5-498e-410c-a687-acc721220457" />
      </body>
      <title>5 Minute Tutorial on Managed Extensibility Framework (MEF)</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</guid>
      <link>http://www.milkcarton.com/blog/2009/04/04/5+Minute+Tutorial+On+Managed+Extensibility+Framework+MEF.aspx</link>
      <pubDate>Sat, 04 Apr 2009 05:37:33 GMT</pubDate>
      <description>&lt;h3&gt;Background
&lt;/h3&gt;
&lt;p&gt;
For a project I'm doing, I have a task model for the various pieces. In the beginning,
I was manually creating a List&amp;lt;ITask&amp;gt;. As I kept adding tasks to run, I started
thinking about hacking some code together to rifle through my assembly and pull back
all the classes which implement ITask.
&lt;/p&gt;
&lt;p&gt;
Then I remember hearing about Managed Extensibility Framework (MEF). I did some searching,
found the &lt;a href="http://www.codeplex.com/MEF/" target="_blank"&gt;MEF home page&lt;/a&gt;,
and even read the &lt;a href="http://mef.codeplex.com/Wiki/View.aspx?title=Overview&amp;amp;referringTitle=Home" target="_blank"&gt;MEF
overview&lt;/a&gt;. But none of that told me what I really wanted to know, what's the fastest
way to get started using MEF as a component loader?
&lt;/p&gt;
&lt;p&gt;
I did some more searching and found the &lt;a href="http://dnrtv.com/" target="_blank"&gt;dnrTV&lt;/a&gt; episode
"&lt;a href="http://www.dnrtv.com/default.aspx?showNum=130" target="_blank"&gt;Glenn Block
on MEF, the Managed Extensibility Framework&lt;/a&gt;" and after 20-30 minutes they finally
got down to how to create a plugin for your app.
&lt;/p&gt;
&lt;p&gt;
But what I really wanted, and I bet a lot of others, is a quick start guide for creating
a plugin.
&lt;/p&gt;
&lt;h3&gt;Solution
&lt;/h3&gt;
&lt;p&gt;
&lt;a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22313" target="_blank"&gt;Download
the latest version of MEF&lt;/a&gt;, as of this writing its Preview 4. Grab the System.ComponentModel.Composition.dll
from the bin folder and stash it somewhere. Make a reference to said dll in your project.
&lt;/p&gt;
&lt;p&gt;
On your plugin class, add Export attribute:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;[Export(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IPlugin))] &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Foo
: IPlugin { ... }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In your plugin consumer, create a property to hold your plugins, and add the Import
attribute:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;[Import(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IPlugin))] &lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; IList&amp;lt;IPlugin&amp;gt;
_myPlugins { get; set; }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Now, tell MEF where to get the plugins at (line 2), and where you want MEF to fulfill
any plugins (line 5):
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
&lt;p&gt;
&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; LoadPlugins()
{ var catalog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(catalog);
var batch = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionBatch(); batch.AddPart(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
container.Compose(batch); } 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
I put my call to the LoadPlugins method in the constructor.
&lt;/p&gt;
&lt;p&gt;
Now, spin through your plugins and do the work:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Console.WriteLine(&lt;span style="color: #006080"&gt;"Found
{0} plugins"&lt;/span&gt;, _myPlugins.Count); &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var
plugin &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; _myPlugins) { Console.WriteLine(plugin.Name);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="/blog/content/binary/5_Minute_MEF_Tutorial.zip" target="_blank"&gt;Download
the complete source&lt;/a&gt; to this (really, only about 10 extra lines to glue things
together) and have fun!
&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=bcd01da5-498e-410c-a687-acc721220457" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>HowTo</category>
      <category>MEF</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A long time, and many many moons ago I took wrote some code to interface our build
server with a <a href="http://www.epowerswitch.com/uk/p-4g_guard.htm" target="_blank" rel="nofollow">network
power switch</a> we had laying around the office. We used this to turn on and off
lava lamps to indicate the status of the build. Some might ask why we didn't use the
X10 support that is already in CCNET, and the answer mostly is cost, and the fact
that X10 wouldn't work in our environment.
</p>
        <p>
That was 2.5 years ago. Since then, our team has become more distributed. We have
one guy working in Ann Arbor, MI, and occasionally have others telecommuting. So not
everyone can see the status of the lamps. Also in the 2.5 years since that code was
written, a little thing called <a href="http://twitter.com/" target="_blank">Twitter</a> has
become very popular. I did some research, and found <a href="http://techblog.tomfanning.eu/2008/03/twitter-task-for-nant-sms-notification.html" target="_blank">Tom
Fannings nAnt Twitter task</a> and briefly considered using it.
</p>
        <p>
But in the end, I just couldn't resist adding my own <a href="http://www.codinghorror.com/blog/archives/000150.html" target="_blank">developer
gold plating</a> and thought it would be neat if we could also issue commands to the
build server via tweets. So with that feature in mind, I had to write it myself.
</p>
        <p>
To start out with, I used <a href="http://devblog.yedda.com/index.php/2007/05/16/twitter-c-library/" target="_blank">Yedda's
C# Twitter library</a>. The Yedda library is a pleasure to work with, it makes sending
a tweet as simple as
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <span style="color: #0000ff">new</span> Twitter().UpdateAsXML(_username,
password, messsage);</pre>
        </div>
        <p>
One thing the Yedda library didn't have, was the ability to query for your Twitter
replies. A quick look through the source, and the <a href="http://apiwiki.twitter.com/" target="_blank">Twitter
API</a> docs and I realized this would be trivial. The details of how I did it aren't
important to this post, but if your curious, you can look at lines 567 - 627 of the
Yedda source included with this post.
</p>
        <p>
I'm not going to dive to much into how the whole project works, but here is a high
level. The software runs as a Windows Service, leverages the CCTrayLib assembly for
Cruise Control.NET to do all the heavy lifting. It polls the Cruise Control.NET server
every 5 seconds, and fires events when things happen. The two events we want are the
Polled and BuildOccurred events.
</p>
        <p>
These events allow us to intern kick off our own events based on the state of the
build. Based on the state, we grab the appropriate actions to run as defined in the
BuildActions.xml file. This maps a build state to a set of actions. In the case of
a "Building" action, we send a Twitter, with a message template of "{PROJECT} is building",
and turn ports 1 on, and 2 off on our ePower switch. Easy enough.
</p>
        <p>
But how do we take in commands? I pondered this for a minute than realized it would
be trivial to leverage the Twitter replies API for this. But what about security,
we can't have just anyone sending commands to our build server. This is where the
Twitter friends API comes in handy. In order to issue commands to our build server,
the account our build server uses has to have you as a friend, not just a follower.
</p>
        <p>
The first action I implemented was the force build command. The idea for the grammar
came from a <a href="http://twitter.com/orand/status/1319154695" target="_blank">joke
reply</a><a href="http://twitter.com/orand" target="_blank">@orand</a> sent our Twitter
bot. After that, I thought it might be nice to be able to get the list of projects,
get a projects status, and ask for help. So that leaves us with a total of 4 commands.
</p>
        <h3>A small bunny trail
</h3>
        <p>
When I first wrote the command parser, it looked something like this:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <span style="color: #0000ff">if</span> (msg.StartsWith(<span style="color: #006080">"force
build "</span>)) ProcessForceBuildCommand(msg, user); <span style="color: #0000ff">else</span><span style="color: #0000ff">if</span> (msg.StartsWith(<span style="color: #006080">"get
projects"</span>)) ProcessProjectListsCommand(msg, user); ...</pre>
        </div>
        <p>
I thought about it for a while, and thought there had to be an easier way. We are
using .NET 3.5 after all, with all its lamba, LINQ and new and improved delegate goodness.
I did some research and came upon the Action&lt;T&gt; (and Func&lt;T&gt;) delegate
type. And came up with this implementation for registering commands
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">var commands = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">string</span>,
Action&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt;&gt;
{ {<span style="color: #006080">"force build "</span>, ProcessForceBuildCommand},
{<span style="color: #006080">"get projects"</span>, ProcessProjectListsCommand},
{<span style="color: #006080">"get project status "</span>, ProcessProjectStatusCommand},
{<span style="color: #006080">"help"</span>, ProcessHelpCommand} };</pre>
        </div>
        <p>
Once we have all the commands registered, we can use some lambda and LINQ magic to
act upon the commands issued
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">var key = _commands.Keys.Where(msg.StartsWith).First();
<span style="color: #0000ff">if</span> (!<span style="color: #0000ff">string</span>.IsNullOrEmpty(key))
_commands[key].Invoke(msg, user); <span style="color: #0000ff">else</span> SendTweet(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">"@{0}
I'm sorry, I don't understand you. Maybe you should ask for help"</span>, user)); </pre>
        </div>
        <h3>Get to the point, I want to see the source code
</h3>
        <p>
The code overall is fairly well structured (if I do say so myself), although there
is one area where I did violate the separation of concerns rule, the TwitterManager
class knows more about Cruise Control.NET than it should. But, given that this is
a very simple internal project, and not for public consumption, I'm mostly OK with
that :)
</p>
        <p>
I've included the source to our entire build monitor, I hope you find it useful. 
We are using a very old version (1.1) of Cruise Control.NET in our environment. If
your using a more recent version, you will probably need to swap out the Cruise Control
CCTrayLib and Remote assemblies for something more recent, and invert some of the
commented out code in the SetupCruiseControl method of BuildServerMonitor.cs
</p>
        <p>
If you have any questions, or find/fix any bugs, please feel free to leave a comment,
or send me a tweet, my username on Twitter is <a href="http://twitter.com/akcoder" target="_blank">akcoder</a>.
</p>
        <p>
          <a href="http://files.milkcarton.com/Afhcan.BuildMonitor.zip" target="_blank">Download
the Afhcan.BuildMonitor</a>
        </p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a" />
      </body>
      <title>Does your Cruise Control.NET server Twitter?</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a.aspx</guid>
      <link>http://www.milkcarton.com/blog/2009/03/16/Does+Your+Cruise+ControlNET+Server+Twitter.aspx</link>
      <pubDate>Mon, 16 Mar 2009 04:59:22 GMT</pubDate>
      <description>&lt;p&gt;
A long time, and many many moons ago I took wrote some code to interface our build
server with a &lt;a href="http://www.epowerswitch.com/uk/p-4g_guard.htm" target="_blank" rel="nofollow"&gt;network
power switch&lt;/a&gt; we had laying around the office. We used this to turn on and off
lava lamps to indicate the status of the build. Some might ask why we didn't use the
X10 support that is already in CCNET, and the answer mostly is cost, and the fact
that X10 wouldn't work in our environment.
&lt;/p&gt;
&lt;p&gt;
That was 2.5 years ago. Since then, our team has become more distributed. We have
one guy working in Ann Arbor, MI, and occasionally have others telecommuting. So not
everyone can see the status of the lamps. Also in the 2.5 years since that code was
written, a little thing called &lt;a href="http://twitter.com/" target="_blank"&gt;Twitter&lt;/a&gt; has
become very popular. I did some research, and found &lt;a href="http://techblog.tomfanning.eu/2008/03/twitter-task-for-nant-sms-notification.html" target="_blank"&gt;Tom
Fannings nAnt Twitter task&lt;/a&gt; and briefly considered using it.
&lt;/p&gt;
&lt;p&gt;
But in the end, I just couldn't resist adding my own &lt;a href="http://www.codinghorror.com/blog/archives/000150.html" target="_blank"&gt;developer
gold plating&lt;/a&gt; and thought it would be neat if we could also issue commands to the
build server via tweets. So with that feature in mind, I had to write it myself.
&lt;/p&gt;
&lt;p&gt;
To start out with, I used &lt;a href="http://devblog.yedda.com/index.php/2007/05/16/twitter-c-library/" target="_blank"&gt;Yedda's
C# Twitter library&lt;/a&gt;. The Yedda library is a pleasure to work with, it makes sending
a tweet as simple as
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Twitter().UpdateAsXML(_username,
password, messsage);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
One thing the Yedda library didn't have, was the ability to query for your Twitter
replies. A quick look through the source, and the &lt;a href="http://apiwiki.twitter.com/" target="_blank"&gt;Twitter
API&lt;/a&gt; docs and I realized this would be trivial. The details of how I did it aren't
important to this post, but if your curious, you can look at lines 567 - 627 of the
Yedda source included with this post.
&lt;/p&gt;
&lt;p&gt;
I'm not going to dive to much into how the whole project works, but here is a high
level. The software runs as a Windows Service, leverages the CCTrayLib assembly for
Cruise Control.NET to do all the heavy lifting. It polls the Cruise Control.NET server
every 5 seconds, and fires events when things happen. The two events we want are the
Polled and BuildOccurred events.
&lt;/p&gt;
&lt;p&gt;
These events allow us to intern kick off our own events based on the state of the
build. Based on the state, we grab the appropriate actions to run as defined in the
BuildActions.xml file. This maps a build state to a set of actions. In the case of
a "Building" action, we send a Twitter, with a message template of "{PROJECT} is building",
and turn ports 1 on, and 2 off on our ePower switch. Easy enough.
&lt;/p&gt;
&lt;p&gt;
But how do we take in commands? I pondered this for a minute than realized it would
be trivial to leverage the Twitter replies API for this. But what about security,
we can't have just anyone sending commands to our build server. This is where the
Twitter friends API comes in handy. In order to issue commands to our build server,
the account our build server uses has to have you as a friend, not just a follower.
&lt;/p&gt;
&lt;p&gt;
The first action I implemented was the force build command. The idea for the grammar
came from a &lt;a href="http://twitter.com/orand/status/1319154695" target="_blank"&gt;joke
reply&lt;/a&gt; &lt;a href="http://twitter.com/orand" target="_blank"&gt;@orand&lt;/a&gt; sent our Twitter
bot. After that, I thought it might be nice to be able to get the list of projects,
get a projects status, and ask for help. So that leaves us with a total of 4 commands.
&lt;/p&gt;
&lt;h3&gt;A small bunny trail
&lt;/h3&gt;
&lt;p&gt;
When I first wrote the command parser, it looked something like this:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (msg.StartsWith(&lt;span style="color: #006080"&gt;"force
build "&lt;/span&gt;)) ProcessForceBuildCommand(msg, user); &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (msg.StartsWith(&lt;span style="color: #006080"&gt;"get
projects"&lt;/span&gt;)) ProcessProjectListsCommand(msg, user); ...&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
I thought about it for a while, and thought there had to be an easier way. We are
using .NET 3.5 after all, with all its lamba, LINQ and new and improved delegate goodness.
I did some research and came upon the Action&amp;lt;T&amp;gt; (and Func&amp;lt;T&amp;gt;) delegate
type. And came up with this implementation for registering commands
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;var commands = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;,
Action&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;
{ {&lt;span style="color: #006080"&gt;"force build "&lt;/span&gt;, ProcessForceBuildCommand},
{&lt;span style="color: #006080"&gt;"get projects"&lt;/span&gt;, ProcessProjectListsCommand},
{&lt;span style="color: #006080"&gt;"get project status "&lt;/span&gt;, ProcessProjectStatusCommand},
{&lt;span style="color: #006080"&gt;"help"&lt;/span&gt;, ProcessHelpCommand} };&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Once we have all the commands registered, we can use some lambda and LINQ magic to
act upon the commands issued
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;var key = _commands.Keys.Where(msg.StartsWith).First();
&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(key))
_commands[key].Invoke(msg, user); &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; SendTweet(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #006080"&gt;"@{0}
I'm sorry, I don't understand you. Maybe you should ask for help"&lt;/span&gt;, user)); &lt;/pre&gt;
&lt;/div&gt;
&lt;h3&gt;Get to the point, I want to see the source code
&lt;/h3&gt;
&lt;p&gt;
The code overall is fairly well structured (if I do say so myself), although there
is one area where I did violate the separation of concerns rule, the TwitterManager
class knows more about Cruise Control.NET than it should. But, given that this is
a very simple internal project, and not for public consumption, I'm mostly OK with
that :)
&lt;/p&gt;
&lt;p&gt;
I've included the source to our entire build monitor, I hope you find it useful.&amp;nbsp;
We are using a very old version (1.1) of Cruise Control.NET in our environment. If
your using a more recent version, you will probably need to swap out the Cruise Control
CCTrayLib and Remote assemblies for something more recent, and invert some of the
commented out code in the SetupCruiseControl method of BuildServerMonitor.cs
&lt;/p&gt;
&lt;p&gt;
If you have any questions, or find/fix any bugs, please feel free to leave a comment,
or send me a tweet, my username on Twitter is &lt;a href="http://twitter.com/akcoder" target="_blank"&gt;akcoder&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://files.milkcarton.com/Afhcan.BuildMonitor.zip" target="_blank"&gt;Download
the Afhcan.BuildMonitor&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,0f2afcce-d7c6-4d53-93b7-c3d9bf87ab2a.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>HowTo</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=65541c8f-4900-4677-bec2-557da84e1ecb</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=65541c8f-4900-4677-bec2-557da84e1ecb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>The Problem
</h3>
        <p>
For the last few days I've been trying to figure out how to do this SQL in linq:
</p>
        <div>
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 1:</span>
              <span style="color: #0000ff">select</span>
              <span style="color: #0000ff">from</span> customers <span style="color: #0000ff">where</span><span style="color: #0000ff">substring</span>(customers.Name,
1, 1) <span style="color: #0000ff">IN</span> (<span style="color: #006080">'a'</span>, <span style="color: #006080">'b'</span>, <span style="color: #006080">'c'</span>, <span style="color: #006080">'d'</span>);</pre>
          </div>
The problem is the list (a, b, c, d) is variable, on one page I need all the results
where foo.Name starts with a or b, on another it could be b, c, d, e and f. 
This is trivial to do in SQL, you just break out a little dynamic sql and your done. 
But being a noob to LINQ, I'm not so sure how to go about doing this.
</div>
        <p>
I had a lot of failed queries, and failed code, before giving up and just looping
through the letters for that particular page and manually building up what I wanted.
</p>
        <p>
But that's kind of a hack. OK, its not kind of a hack, its a big hack.
</p>
        <h3>The Solution
</h3>
        <p>
This LINQ goodness (using the Customers db in <a href="http://www.linqpad.net/">LINQPad</a>)
will pull back all the customers who's names begin with a t, d, or j (note the .ToList()
on line 4, I'll discuss it next).
</p>
        <div>
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 1:</span>
              <span style="color: #0000ff">string</span>[]
letters = { <span style="color: #006080">"t"</span>, <span style="color: #006080">"d"</span>, <span style="color: #006080">"j"</span> }; </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 3:</span> var
customers = (from c <span style="color: #0000ff">in</span> Customers</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 4:</span> select
c).ToList(); </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 5:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 6:</span> var
results = from c <span style="color: #0000ff">in</span> customers</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 7:</span> join
letter <span style="color: #0000ff">in</span> letters on c.Name.First().ToString().ToUpper()
equals letter.ToUpper()</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 8:</span> select
c; </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 9:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 10:</span> customers.Dump(<span style="color: #006080">"Customers"</span>);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 11:</span> results.Dump(<span style="color: #006080">"Filtered
by first letter"</span>);</pre>
          </div>
        </div>
        <p>
And LINQPad renders this for us:
</p>
        <p>
          <a href="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_2.png">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_thumb.png" width="176" border="0" />
          </a>
        </p>
        <h3>
Limitations
</h3>
        <p>
LINQ to SQL doesn't support querying a SQL database with if one of your sources is
an in-memory store, except if you use the Contains operator.  So, to work around
that, you have to pull back ALL the results from the db, and then convert it to a
List before you use it in the above query.
</p>
        <p>
I'm my opinion, this is kind of a painful limitation, but for my particular purpose,
I'm willing to live with it because my particular database will only ever have around
100 or so rows, so pulling everything back in memory isn't such a big deal...
</p>
        <p>
P.S. if you are reading this in a feed reader, let me know how it renders, I've updated
my code snippet plugin to try and fix the previous rendering problems.
</p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=65541c8f-4900-4677-bec2-557da84e1ecb" />
      </body>
      <title>LINQ Goodness</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</guid>
      <link>http://www.milkcarton.com/blog/2008/05/09/LINQ+Goodness.aspx</link>
      <pubDate>Fri, 09 May 2008 09:13:08 GMT</pubDate>
      <description>&lt;h3&gt;The Problem
&lt;/h3&gt;
&lt;p&gt;
For the last few days I've been trying to figure out how to do this SQL in linq:
&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;select&lt;/span&gt; &lt;span style="color: #0000ff"&gt;from&lt;/span&gt; customers &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; &lt;span style="color: #0000ff"&gt;substring&lt;/span&gt;(customers.Name,
1, 1) &lt;span style="color: #0000ff"&gt;IN&lt;/span&gt; (&lt;span style="color: #006080"&gt;'a'&lt;/span&gt;, &lt;span style="color: #006080"&gt;'b'&lt;/span&gt;, &lt;span style="color: #006080"&gt;'c'&lt;/span&gt;, &lt;span style="color: #006080"&gt;'d'&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
The problem is the list (a, b, c, d) is variable, on one page I need all the results
where foo.Name starts with a or b, on another it could be b, c, d, e and f.&amp;nbsp;
This is trivial to do in SQL, you just break out a little dynamic sql and your done.&amp;nbsp;
But being a noob to LINQ, I'm not so sure how to go about doing this.
&lt;/div&gt;
&lt;p&gt;
I had a lot of failed queries, and failed code, before giving up and just looping
through the letters for that particular page and manually building up what I wanted.
&lt;/p&gt;
&lt;p&gt;
But that's kind of a hack. OK, its not kind of a hack, its a big hack.
&lt;/p&gt;
&lt;h3&gt;The Solution
&lt;/h3&gt;
&lt;p&gt;
This LINQ goodness (using the Customers db in &lt;a href="http://www.linqpad.net/"&gt;LINQPad&lt;/a&gt;)
will pull back all the customers who's names begin with a t, d, or j (note the .ToList()
on line 4, I'll discuss it next).
&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[]
letters = { &lt;span style="color: #006080"&gt;"t"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"d"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"j"&lt;/span&gt; }; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; var
customers = (from c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Customers&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 4:&lt;/span&gt; select
c).ToList(); &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 5:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 6:&lt;/span&gt; var
results = from c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; customers&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 7:&lt;/span&gt; join
letter &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; letters on c.Name.First().ToString().ToUpper()
equals letter.ToUpper()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 8:&lt;/span&gt; select
c; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 9:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 10:&lt;/span&gt; customers.Dump(&lt;span style="color: #006080"&gt;"Customers"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 11:&lt;/span&gt; results.Dump(&lt;span style="color: #006080"&gt;"Filtered
by first letter"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
And LINQPad renders this for us:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_thumb.png" width="176" border="0"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;h3&gt;
Limitations
&lt;/h3&gt;
&lt;p&gt;
LINQ to SQL doesn't support querying a SQL database with if one of your sources is
an in-memory store, except if you use the Contains operator.&amp;nbsp; So, to work around
that, you have to pull back ALL the results from the db, and then convert it to a
List before you use it in the above query.
&lt;/p&gt;
&lt;p&gt;
I'm my opinion, this is kind of a painful limitation, but for my particular purpose,
I'm willing to live with it because my particular database will only ever have around
100 or so rows, so pulling everything back in memory isn't such a big deal...
&lt;/p&gt;
&lt;p&gt;
P.S. if you are reading this in a feed reader, let me know how it renders, I've updated
my code snippet plugin to try and fix the previous rendering problems.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=65541c8f-4900-4677-bec2-557da84e1ecb" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>LINQ</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=aa751f74-30f0-4d38-aaf7-36e2c75ea382</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,aa751f74-30f0-4d38-aaf7-36e2c75ea382.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,aa751f74-30f0-4d38-aaf7-36e2c75ea382.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=aa751f74-30f0-4d38-aaf7-36e2c75ea382</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Background Information
</h3>
        <p>
Aspect Oriented Programming has been on my list of things to read up on for almost
a year now.  It likely would have stayed on my list of things to do well into
the future if it hadn't been for a recent <a href="http://www.dotnetrocks.com/">.NET
Rocks</a> episode.  I can't recall if it was the <a href="http://www.dotnetrocks.com/default.aspx?showNum=289">Pablo
Castro on Astoria</a>, or the <a href="http://www.dotnetrocks.com/default.aspx?showNum=290">Tim
Sneath and Ian Ellison</a> show, but one of them off-hand mentioned <a href="http://www.postsharp.org/">PostSharp</a>. 
As I usually do when links are thrown about on the show, I scribbled it on my hand
for later review. <b>Update</b><a href="http://www.dotnetrocks.com/default.aspx?showNum=298">DNR
just released a show with Gael Fraiteur of PostSharp</a> fame!<br /></p>
        <p>
Once I got to the PostSharp site, I found their <a href="http://www.postsharp.org/documentation/simple-trace-aspect-tutorial">sample
video on creating a trace AOP attribute</a>.  They had me hooked.  I wasn't
so much interested in doing a logging or trace attribute, but was more interested
in doing a few validation attributes.  More specifically a NotNullAttribute.
</p>
        <h3>The Problem
</h3>
        <p>
What got me interested in doing the NotNullAttribute was thinking back to how many
times in our code base we would have something like this:
</p>
        <pre>
          <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:412e8b34-6570-4246-b54c-2d1eec69d82e" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false">
            <pre style="overflow: auto; background-color: rgb(255, 255, 128);">
              <div>
                <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
                <span style="color: rgb(0, 128, 128);"> 1</span>
                <span style="color: rgb(0, 0, 255);">public</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">void</span>
                <span style="color: rgb(0, 0, 0);"> AddUser(Organization
org, User user) </span>
                <span style="color: rgb(0, 128, 128);"> 2</span>
                <span style="color: rgb(0, 0, 0);">{ </span>
                <span style="color: rgb(0, 128, 128);"> 3</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">if</span>
                <span style="color: rgb(0, 0, 0);"> (org </span>
                <span style="color: rgb(0, 0, 0);">==</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">null</span>
                <span style="color: rgb(0, 0, 0);">) </span>
                <span style="color: rgb(0, 128, 128);"> 4</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">throw</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">new</span>
                <span style="color: rgb(0, 0, 0);"> ArgumentNullException(</span>
                <span style="color: rgb(128, 0, 0);">"</span>
                <span style="color: rgb(128, 0, 0);">org</span>
                <span style="color: rgb(128, 0, 0);">"</span>
                <span style="color: rgb(0, 0, 0);">); </span>
                <span style="color: rgb(0, 128, 128);"> 5</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">if</span>
                <span style="color: rgb(0, 0, 0);"> (user </span>
                <span style="color: rgb(0, 0, 0);">==</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">null</span>
                <span style="color: rgb(0, 0, 0);">) </span>
                <span style="color: rgb(0, 128, 128);"> 6</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">throw</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 0, 255);">new</span>
                <span style="color: rgb(0, 0, 0);"> ArgumentNullException(</span>
                <span style="color: rgb(128, 0, 0);">"</span>
                <span style="color: rgb(128, 0, 0);">user</span>
                <span style="color: rgb(128, 0, 0);">"</span>
                <span style="color: rgb(0, 0, 0);">); </span>
                <span style="color: rgb(0, 128, 128);"> 7</span>
                <span style="color: rgb(0, 0, 0);">
                </span>
                <span style="color: rgb(0, 128, 128);"> 8</span>
                <span style="color: rgb(0, 0, 0);"> org.AddUser(user); </span>
                <span style="color: rgb(0, 128, 128);"> 9</span>
                <span style="color: rgb(0, 0, 0);"> _ordDal.Save(org); </span>
                <span style="color: rgb(0, 128, 128);">10</span>
                <span style="color: rgb(0, 0, 0);">}</span>
              </div>
            </pre>
            <!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com -->
          </div>
        </pre>
        <p>
        </p>
        <p>
Granted this is a somewhat short method, but we have methods which take 6 parameters,
and only 2 actual lines of code.  That's 12 lines of code taken up for argument
checking, and 2 lines of code.  That would put our crap to code ratio at 6/1.
</p>
        <h3>The Solution
</h3>
        <p>
Wouldn't it be much nicer if you could do something like this:
</p>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b14b6321-8fde-41c3-a4c5-e50fc4fd2847" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false">
          <pre style="overflow: auto; background-color: rgb(255, 255, 128);">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: rgb(0, 128, 128);">1</span>
              <span style="color: rgb(0, 0, 255);">public</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">void</span>
              <span style="color: rgb(0, 0, 0);"> AddUser([NotNull]
Organization org, [NotNull] User user) </span>
              <span style="color: rgb(0, 128, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">{ </span>
              <span style="color: rgb(0, 128, 128);">3</span>
              <span style="color: rgb(0, 0, 0);"> org.AddUser(user); </span>
              <span style="color: rgb(0, 128, 128);">4</span>
              <span style="color: rgb(0, 0, 0);"> _ordDal.Save(org); </span>
              <span style="color: rgb(0, 128, 128);">5</span>
              <span style="color: rgb(0, 0, 0);">}</span>
            </div>
          </pre>
          <!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com -->
        </div>
        <p>
        </p>
        <p>
 
</p>
        <p>
PostSharp is almost there to let you write code just like that.  Currently, there
is no support for an OnParameterAspectBoundaryAttribute in PostSharp, so we have to
do things just slightly differently:
</p>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:497ca957-7091-4b07-84f7-3f49ed150dad" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false">
          <pre style="overflow: auto; background-color: rgb(255, 255, 128);">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: rgb(0, 0, 0);">[NotNullHelper] </span>
              <span style="color: rgb(0, 0, 255);">public</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">void</span>
              <span style="color: rgb(0, 0, 0);"> AddUser([NotNull]
Organization org, [NotNull] User user) { org.AddUser(user); _ordDal.Save(org); }</span>
            </div>
          </pre>
          <!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com -->
        </div>
        <p>
        </p>
        <p>
We have to have a helper class were all the actual code lives.
</p>
        <h3>How PostSharp Works
</h3>
        <p>
PostSharp works by weaving all your AOP attributes with your source code after the
compiler compiles your code.  This <a href="http://www.codeproject.com/useritems/ps-custom-attributes-2.asp">article
by Gael Fraiteur describes in more detail how the PostSharp process works</a>. 
After the code is post-compiled by PostSharp, you end up with something very ugly
that looks like this:
</p>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ec455986-ad9f-4f97-aeba-2727cc90f106" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false">
          <pre style="overflow: auto; background-color: rgb(255, 255, 128);">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: rgb(0, 0, 255);">public</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">void</span>
              <span style="color: rgb(0, 0, 0);"> AddUser([NotNull]
Organization org, [NotNull] User user) { MethodExecutionEventArgs </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">; </span>
              <span style="color: rgb(0, 0, 255);">try</span>
              <span style="color: rgb(0, 0, 0);"> { </span>
              <span style="color: rgb(0, 0, 255);">object</span>
              <span style="color: rgb(0, 0, 0);">[] </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">arguments</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">1</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 0);">=</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">new</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">object</span>
              <span style="color: rgb(0, 0, 0);">[]
{ org, user }; </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 0);">=</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">new</span>
              <span style="color: rgb(0, 0, 0);"> MethodExecutionEventArgs(methodof(OrgManager.AddUser,
OrgManager), </span>
              <span style="color: rgb(0, 0, 255);">this</span>
              <span style="color: rgb(0, 0, 0);">, </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">arguments</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">1</span>
              <span style="color: rgb(0, 0, 0);">); </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">PostSharp</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Laos</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Implementation.NotNullHelperAttribute</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">3</span>
              <span style="color: rgb(0, 0, 0);">.OnEntry(</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">); </span>
              <span style="color: rgb(0, 0, 255);">if</span>
              <span style="color: rgb(0, 0, 0);"> (</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">.FlowBehavior </span>
              <span style="color: rgb(0, 0, 0);">!=</span>
              <span style="color: rgb(0, 0, 0);"> FlowBehavior.Return)
{ org.AddUser(user); _orgDal.Save(org); </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">PostSharp</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Laos</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Implementation.NotNullHelperAttribute</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">3</span>
              <span style="color: rgb(0, 0, 0);">.OnSuccess(</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">);
} } </span>
              <span style="color: rgb(0, 0, 255);">catch</span>
              <span style="color: rgb(0, 0, 0);"> (Exception </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">exception</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">0</span>
              <span style="color: rgb(0, 0, 0);">)
{ </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">.Exception </span>
              <span style="color: rgb(0, 0, 0);">=</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">exception</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">0</span>
              <span style="color: rgb(0, 0, 0);">; </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">PostSharp</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Laos</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Implementation.NotNullHelperAttribute</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">3</span>
              <span style="color: rgb(0, 0, 0);">.OnException(</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">); </span>
              <span style="color: rgb(0, 0, 255);">switch</span>
              <span style="color: rgb(0, 0, 0);"> (</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">.FlowBehavior)
{ </span>
              <span style="color: rgb(0, 0, 255);">case</span>
              <span style="color: rgb(0, 0, 0);"> FlowBehavior.Continue: </span>
              <span style="color: rgb(0, 0, 255);">case</span>
              <span style="color: rgb(0, 0, 0);"> FlowBehavior.Return: </span>
              <span style="color: rgb(0, 0, 255);">return</span>
              <span style="color: rgb(0, 0, 0);">;
} </span>
              <span style="color: rgb(0, 0, 255);">throw</span>
              <span style="color: rgb(0, 0, 0);">;
} </span>
              <span style="color: rgb(0, 0, 255);">finally</span>
              <span style="color: rgb(0, 0, 0);"> { </span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">PostSharp</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Laos</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">Implementation.NotNullHelperAttribute</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">3</span>
              <span style="color: rgb(0, 0, 0);">.OnExit(</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(0, 0, 0);">laosEventArgs</span>
              <span style="color: rgb(0, 0, 0);">~</span>
              <span style="color: rgb(128, 0, 128);">2</span>
              <span style="color: rgb(0, 0, 0);">);
} }</span>
            </div>
          </pre>
          <!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com -->
        </div>
        <p>
        </p>
        <p>
I don't know about you, but I would have one HELL of a time trying to debug this,
thankfully PostSharp touches up the PDB files, so when your debugging, you only see
and debug the source code you've written, both your attribute, and your business logic.
</p>
        <h3>The Drawbacks
</h3>
        <p>
PostSharp modifies your code! Well, not directly, but it is still modifying your code
(as seen above), but thankfully it touches up the PDB files so all the mess under
the hood is hidden from you.
</p>
        <h3>Conclusion
</h3>
        <p>
This is but one of the many things you can do with AOP and PostSharp.  Next up,
I plan to write some validation attributes.  Look for those in the next few weeks. 
In the mean time, <a href="http://www.milkcarton.com/blog/content/binary/NotNullAttribute.cs.txt">download
the source code for the NotNullAttribute</a>.
</p>
        <p>
I'm sorry if this post looks terrible in your feed reader, I still haven't been able
to figure out why the code looks terrible.  If anyone has any suggestions, let
me know.  I'm using DasBlogCE as my blog engine.
</p>
        <a href="http://www.dotnetkicks.com/kick/?url=http://www.milkcarton.com/blog/2007/11/26/Decrapify+Your+Code+Base+With+AOP+Using+PostSharp.aspx">
          <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.milkcarton.com/blog/2007/11/26/Decrapify+Your+Code+Base+With+AOP+Using+PostSharp.aspx" border="0" />
        </a>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=aa751f74-30f0-4d38-aaf7-36e2c75ea382" />
      </body>
      <title>De-crapify your code base with AOP using PostSharp</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,aa751f74-30f0-4d38-aaf7-36e2c75ea382.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/11/26/Decrapify+Your+Code+Base+With+AOP+Using+PostSharp.aspx</link>
      <pubDate>Mon, 26 Nov 2007 05:37:23 GMT</pubDate>
      <description>&lt;h3&gt;Background Information
&lt;/h3&gt;
&lt;p&gt;
Aspect Oriented Programming has been on my list of things to read up on for almost
a year now.&amp;nbsp; It likely would have stayed on my list of things to do well into
the future if it hadn't been for a recent &lt;a href="http://www.dotnetrocks.com/"&gt;.NET
Rocks&lt;/a&gt;&amp;nbsp;episode.&amp;nbsp; I can't recall if it was the &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=289"&gt;Pablo
Castro on Astoria&lt;/a&gt;, or the &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=290"&gt;Tim
Sneath and Ian Ellison&lt;/a&gt;&amp;nbsp;show, but one of them off-hand mentioned &lt;a href="http://www.postsharp.org/"&gt;PostSharp&lt;/a&gt;.&amp;nbsp;
As I usually do when links are thrown about on the show, I scribbled it on my hand
for later review. &lt;b&gt;Update&lt;/b&gt; &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=298"&gt;DNR
just released a show with Gael Fraiteur of PostSharp&lt;/a&gt; fame!&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Once I got to the PostSharp site, I found their &lt;a href="http://www.postsharp.org/documentation/simple-trace-aspect-tutorial"&gt;sample
video on creating a trace AOP attribute&lt;/a&gt;.&amp;nbsp; They had me hooked.&amp;nbsp; I wasn't
so much interested in doing a logging or trace attribute, but was more interested
in doing a few validation attributes.&amp;nbsp; More specifically a NotNullAttribute.
&lt;/p&gt;
&lt;h3&gt;The Problem
&lt;/h3&gt;
&lt;p&gt;
What got me interested in doing the NotNullAttribute was thinking back to how many
times in our code base we would have something like this:
&lt;/p&gt;
&lt;pre&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:412e8b34-6570-4246-b54c-2d1eec69d82e" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AddUser(Organization
org, User user) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;{ &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (org &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;org&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (user &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 6&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 7&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 8&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; org.AddUser(user); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 9&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; _ordDal.Save(org); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;10&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Granted this is a somewhat short method, but we have methods which take 6 parameters,
and only 2 actual&amp;nbsp;lines of code.&amp;nbsp; That's 12 lines of code taken up for argument
checking, and 2 lines of code.&amp;nbsp; That would put our crap to code ratio at 6/1.
&lt;/p&gt;
&lt;h3&gt;The Solution
&lt;/h3&gt;
&lt;p&gt;
Wouldn't it be much nicer if you could do something like this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b14b6321-8fde-41c3-a4c5-e50fc4fd2847" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AddUser([NotNull]
Organization org, [NotNull] User user) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;{ &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; org.AddUser(user); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; _ordDal.Save(org); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
PostSharp is almost there to let you write code just like that.&amp;nbsp; Currently, there
is no support for an OnParameterAspectBoundaryAttribute in PostSharp, so we have to
do things just slightly differently:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:497ca957-7091-4b07-84f7-3f49ed150dad" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[NotNullHelper] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AddUser([NotNull]
Organization org, [NotNull] User user) { org.AddUser(user); _ordDal.Save(org); }&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
We have to have a helper class were all the actual code lives.
&lt;/p&gt;
&lt;h3&gt;How&amp;nbsp;PostSharp Works
&lt;/h3&gt;
&lt;p&gt;
PostSharp works by weaving all your AOP attributes with your source code after the
compiler compiles your code.&amp;nbsp; This &lt;a href="http://www.codeproject.com/useritems/ps-custom-attributes-2.asp"&gt;article
by Gael Fraiteur describes in more detail how the PostSharp process works&lt;/a&gt;.&amp;nbsp;
After the code is post-compiled by PostSharp, you end up with something very ugly
that looks like this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ec455986-ad9f-4f97-aeba-2727cc90f106" style="margin: 0px; padding: 0px; display: inline; float: none;" contenteditable="false"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AddUser([NotNull]
Organization org, [NotNull] User user) { MethodExecutionEventArgs &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;arguments&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[]
{ org, user }; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MethodExecutionEventArgs(methodof(OrgManager.AddUser,
OrgManager), &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;arguments&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;PostSharp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Laos&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Implementation.NotNullHelperAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.OnEntry(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.FlowBehavior &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; FlowBehavior.Return)
{ org.AddUser(user); _orgDal.Save(org); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;PostSharp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Laos&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Implementation.NotNullHelperAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.OnSuccess(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
} } &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (Exception &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;exception&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
{ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.Exception &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;exception&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;PostSharp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Laos&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Implementation.NotNullHelperAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.OnException(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;switch&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.FlowBehavior)
{ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;case&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; FlowBehavior.Continue: &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;case&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; FlowBehavior.Return: &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
} &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
} &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;finally&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;PostSharp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Laos&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Implementation.NotNullHelperAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.OnExit(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;laosEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;~&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
} }&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
I don't know about you, but I would have&amp;nbsp;one HELL of a time trying to debug this,
thankfully PostSharp touches up the PDB files, so when your debugging, you only see
and debug the source code you've written, both your attribute, and your business logic.
&lt;/p&gt;
&lt;h3&gt;The Drawbacks
&lt;/h3&gt;
&lt;p&gt;
PostSharp modifies your code! Well, not directly, but it is still modifying your code
(as seen above), but thankfully it touches up the PDB files so all the mess under
the hood is hidden from you.
&lt;/p&gt;
&lt;h3&gt;Conclusion
&lt;/h3&gt;
&lt;p&gt;
This is but one of the many things you can do with AOP and PostSharp.&amp;nbsp; Next up,
I plan to write some validation attributes.&amp;nbsp; Look for those in the next few weeks.&amp;nbsp;
In the mean time, &lt;a href="http://www.milkcarton.com/blog/content/binary/NotNullAttribute.cs.txt"&gt;download
the source code for the NotNullAttribute&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
I'm sorry if this post looks terrible in your feed reader, I still haven't been able
to figure out why the code looks terrible.&amp;nbsp; If anyone has any suggestions, let
me know.&amp;nbsp; I'm using DasBlogCE as my blog engine.
&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.milkcarton.com/blog/2007/11/26/Decrapify+Your+Code+Base+With+AOP+Using+PostSharp.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.milkcarton.com/blog/2007/11/26/Decrapify+Your+Code+Base+With+AOP+Using+PostSharp.aspx" border="0"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=aa751f74-30f0-4d38-aaf7-36e2c75ea382" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,aa751f74-30f0-4d38-aaf7-36e2c75ea382.aspx</comments>
      <category>.NET</category>
      <category>AOP</category>
      <category>C#</category>
      <category>PostSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=f1de834d-6c68-4cdf-bbd3-caa00629d883</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=f1de834d-6c68-4cdf-bbd3-caa00629d883</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you've read <a href="http://blogs.msdn.com/oldnewthing/">Raymond Chen's blog</a> long
enough, then you know trying to change system stuff directly in Windows registry
is discouraged, if not frowned upon.  So when I kept hacking away at the registry
trying to get some Windows Firewall exceptions for XP and Vista created, I decided
to take a step back and see what Windows's API's are out there to do this.
</p>
        <p>
Doing some Google searches doesn't reveal much (which is why I decided to blog this),
except these two hidden gems <a href="http://www.codeproject.com/cs/internet/Syslogd.asp">Syslog
daemon for Windows Eventlog</a>, and <a href="http://www.developmentnow.com/g/36_2004_12_0_0_31409/Adding-a-port-to-the-XP-Firewall.htm">Adding
a port to the XP Firewall</a>.  Both of these gave me pointers in the right direction
to create this gem:
</p>
        <pre>
          <span style="color: teal"> 1</span>
          <span style="color: blue">private</span>
          <span style="color: blue">static</span>
          <span style="color: blue">void</span> ExceptionToFirewall(<span style="color: blue">bool</span> add, <span style="color: blue">string</span> imageFileName, <span style="color: blue">string</span> name) <span style="color: teal"> 2</span> { <span style="color: teal"> 3</span> Type
netFwMgrType = Type.GetTypeFromProgID(<span style="color: maroon">"HNetCfg.FwMgr"</span>); <span style="color: teal"> 4</span> INetFwMgr
mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); <span style="color: teal"> 5</span><span style="color: teal"> 6</span> INetFwProfile
curProfile = mgr.LocalPolicy.CurrentProfile; <span style="color: teal"> 7</span><span style="color: blue">if</span> (add) <span style="color: teal"> 8</span> { <span style="color: teal"> 9</span> Type
NetFwAuthorizedApplicationType = Type.GetTypeFromProgID(<span style="color: maroon">"HNetCfg.FwAuthorizedApplication"</span>, <span style="color: maroon">false</span>); <span style="color: teal"> 10</span> INetFwAuthorizedApplication
app = (INetFwAuthorizedApplication)Activator.CreateInstance(NetFwAuthorizedApplicationType); <span style="color: teal"> 11</span><span style="color: teal"> 12</span> app.Name
= name; <span style="color: teal"> 13</span> app.ProcessImageFileName = imageFileName; <span style="color: teal"> 14</span> app.Enabled
= <span style="color: maroon">true</span>; <span style="color: teal"> 15</span> app.RemoteAddresses
= <span style="color: maroon">"*"</span>; <span style="color: teal"> 16</span> app.Scope
= NET_FW_SCOPE_.NET_FW_SCOPE_ALL; <span style="color: teal"> 17</span><span style="color: teal"> 18</span> curProfile.AuthorizedApplications.Add(app); <span style="color: teal"> 19</span> } <span style="color: teal"> 20</span><span style="color: blue">else</span><span style="color: teal"> 21</span> { <span style="color: teal"> 22</span> curProfile.AuthorizedApplications.Remove(imageFileName); <span style="color: teal"> 23</span> } <span style="color: teal"> 24</span> }</pre>
        <pre class="code"> </pre>
        <p>
To use this, you'll need to add a reference to COM component HNetCfg.FwMgr (Guid "{304CE942-6E39-40D8-943A-B913C40C9CD4}", file
path C:\windows\system32\hnetcfg.dll).
</p>
        <p>
One note, don't use the IpVersion property of INetFwAuthorizedApplication, under Windows
Vista it throws a NotImplimentedException.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx" border="0" />
          </a>  
</p>
        <div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:b8ea0826-2453-4df0-abd7-4f69de44893a" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">del.icio.us
Tags: <a href="http://del.icio.us/popular/C#" rel="tag">C#</a>, <a href="http://del.icio.us/popular/Windows" rel="tag">Windows</a>, <a href="http://del.icio.us/popular/Firewall" rel="tag">Firewall</a></div>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=f1de834d-6c68-4cdf-bbd3-caa00629d883" />
      </body>
      <title>Creating Windows Firewall Exceptions</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/10/08/Creating+Windows+Firewall+Exceptions.aspx</link>
      <pubDate>Mon, 08 Oct 2007 02:50:55 GMT</pubDate>
      <description>&lt;p&gt;
If you've read &lt;a href="http://blogs.msdn.com/oldnewthing/"&gt;Raymond Chen's blog&lt;/a&gt; long
enough, then you know&amp;nbsp;trying to change system stuff directly in Windows registry
is discouraged, if not frowned upon.&amp;nbsp; So when I kept hacking away at the registry
trying to get some Windows Firewall exceptions for XP and Vista created, I decided
to take a step back and see what Windows's API's are out there to&amp;nbsp;do this.
&lt;/p&gt;
&lt;p&gt;
Doing some Google searches doesn't reveal much (which is why I decided to blog this),
except these two&amp;nbsp;hidden&amp;nbsp;gems &lt;a href="http://www.codeproject.com/cs/internet/Syslogd.asp"&gt;Syslog
daemon for Windows Eventlog&lt;/a&gt;, and &lt;a href="http://www.developmentnow.com/g/36_2004_12_0_0_31409/Adding-a-port-to-the-XP-Firewall.htm"&gt;Adding
a port to the XP Firewall&lt;/a&gt;.&amp;nbsp; Both of these gave me pointers in the right direction
to create this gem:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: teal"&gt; 1&lt;/span&gt; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ExceptionToFirewall(&lt;span style="color: blue"&gt;bool&lt;/span&gt; add, &lt;span style="color: blue"&gt;string&lt;/span&gt; imageFileName, &lt;span style="color: blue"&gt;string&lt;/span&gt; name) &lt;span style="color: teal"&gt; 2&lt;/span&gt; { &lt;span style="color: teal"&gt; 3&lt;/span&gt; Type
netFwMgrType = Type.GetTypeFromProgID(&lt;span style="color: maroon"&gt;"HNetCfg.FwMgr"&lt;/span&gt;); &lt;span style="color: teal"&gt; 4&lt;/span&gt; INetFwMgr
mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); &lt;span style="color: teal"&gt; 5&lt;/span&gt; &lt;span style="color: teal"&gt; 6&lt;/span&gt; INetFwProfile
curProfile = mgr.LocalPolicy.CurrentProfile; &lt;span style="color: teal"&gt; 7&lt;/span&gt; &lt;span style="color: blue"&gt;if&lt;/span&gt; (add) &lt;span style="color: teal"&gt; 8&lt;/span&gt; { &lt;span style="color: teal"&gt; 9&lt;/span&gt; Type
NetFwAuthorizedApplicationType = Type.GetTypeFromProgID(&lt;span style="color: maroon"&gt;"HNetCfg.FwAuthorizedApplication"&lt;/span&gt;, &lt;span style="color: maroon"&gt;false&lt;/span&gt;); &lt;span style="color: teal"&gt; 10&lt;/span&gt; INetFwAuthorizedApplication
app = (INetFwAuthorizedApplication)Activator.CreateInstance(NetFwAuthorizedApplicationType); &lt;span style="color: teal"&gt; 11&lt;/span&gt; &lt;span style="color: teal"&gt; 12&lt;/span&gt; app.Name
= name; &lt;span style="color: teal"&gt; 13&lt;/span&gt; app.ProcessImageFileName = imageFileName; &lt;span style="color: teal"&gt; 14&lt;/span&gt; app.Enabled
= &lt;span style="color: maroon"&gt;true&lt;/span&gt;; &lt;span style="color: teal"&gt; 15&lt;/span&gt; app.RemoteAddresses
= &lt;span style="color: maroon"&gt;"*"&lt;/span&gt;; &lt;span style="color: teal"&gt; 16&lt;/span&gt; app.Scope
= NET_FW_SCOPE_.NET_FW_SCOPE_ALL; &lt;span style="color: teal"&gt; 17&lt;/span&gt; &lt;span style="color: teal"&gt; 18&lt;/span&gt; curProfile.AuthorizedApplications.Add(app); &lt;span style="color: teal"&gt; 19&lt;/span&gt; } &lt;span style="color: teal"&gt; 20&lt;/span&gt; &lt;span style="color: blue"&gt;else&lt;/span&gt; &lt;span style="color: teal"&gt; 21&lt;/span&gt; { &lt;span style="color: teal"&gt; 22&lt;/span&gt; curProfile.AuthorizedApplications.Remove(imageFileName); &lt;span style="color: teal"&gt; 23&lt;/span&gt; } &lt;span style="color: teal"&gt; 24&lt;/span&gt; }&lt;/pre&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;
To use this, you'll need to add a reference to COM component HNetCfg.FwMgr (Guid "{304CE942-6E39-40D8-943A-B913C40C9CD4}",&amp;nbsp;file
path&amp;nbsp;C:\windows\system32\hnetcfg.dll).
&lt;/p&gt;
&lt;p&gt;
One note, don't use the IpVersion property of INetFwAuthorizedApplication, under Windows
Vista it throws a NotImplimentedException.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp; 
&lt;div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:b8ea0826-2453-4df0-abd7-4f69de44893a" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;del.icio.us
Tags: &lt;a href="http://del.icio.us/popular/C#" rel="tag"&gt;C#&lt;/a&gt;, &lt;a href="http://del.icio.us/popular/Windows" rel="tag"&gt;Windows&lt;/a&gt;, &lt;a href="http://del.icio.us/popular/Firewall" rel="tag"&gt;Firewall&lt;/a&gt;
&lt;/div&gt;
&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=f1de834d-6c68-4cdf-bbd3-caa00629d883" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</comments>
      <category>C#</category>
      <category>Firewall</category>
      <category>Windows</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=9fe3849a-f364-43ea-acb4-b100e991e1cc</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9fe3849a-f364-43ea-acb4-b100e991e1cc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Usually I post solutions I've discovered or created to problems I've encountered. 
Now, I've run into a problem with the XmlSerializer and I need your help solving it.  Scroll
to the bottom for an update.
</p>
        <h3>The Problem
</h3>
        <p>
I have a class called Foobar.  Foobar inherits from List&lt;string&gt;, and contains
one member Title.  When I use the XmlSerializer to serialize and deserialize
Foobar, all the items in the List are make it through safely, but the member Title
does not.
</p>
        <h3>The Code
</h3>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd057b70-45a7-4b72-a7db-655bf0456e1b" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
          <pre style="background-color:#FFFF80;">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System; </span>
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System.Collections.Generic; </span>
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System.IO; </span>
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System.Xml.Serialization; </span>
              <span style="color: #0000FF; ">internal</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">class</span>
              <span style="color: #000000; "> Program
{ </span>
              <span style="color: #0000FF; ">private</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">static</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">void</span>
              <span style="color: #000000; "> Main()
{ Foobar f </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">new</span>
              <span style="color: #000000; "> Foobar();
f.Add(</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">1</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">);
f.Add(</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">2</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">);
f.Title </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">Title</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">;
MemoryStream mem </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">new</span>
              <span style="color: #000000; "> MemoryStream();
XmlSerializer xs </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">new</span>
              <span style="color: #000000; "> XmlSerializer(</span>
              <span style="color: #0000FF; ">typeof</span>
              <span style="color: #000000; ">(Foobar));
xs.Serialize(mem, f); mem.Seek(</span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">, </span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">);
mem.Seek(</span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">, </span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">);
Foobar deser </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; "> (Foobar)xs.Deserialize(mem);
Console.WriteLine(deser); } } [Serializable] </span>
              <span style="color: #0000FF; ">public</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">class</span>
              <span style="color: #000000; "> Foobar
: List</span>
              <span style="color: #000000; ">&lt;</span>
              <span style="color: #0000FF; ">string</span>
              <span style="color: #000000; ">&gt;</span>
              <span style="color: #000000; "> { </span>
              <span style="color: #0000FF; ">public</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">string</span>
              <span style="color: #000000; "> Title;
}</span>
            </div>
          </pre>
        </div>
        <h3>The Solution
</h3>
        <p>
One way I came up with to solve this problem is make Foobar not inherit from List&lt;string&gt;,
and make a member called Items that is a List&lt;string&gt;, but that's kind of hokey. 
I've searched Google and can't find a solution.  So I'm appealing to my dear
readers for a better solution.
</p>
        <h3>Update
</h3>
        <p>
Looks like I was being way to specific in my Google search, searching on "XmlSerializer
List" gives us this <a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;SiteID=1</a> as
the first item.  In the post, 
</p>
        <p>
Elena Kharitidi says "XmlSerializer does not serialize any members if a collection.
Only collection items get serialized. This is by design, basically a decision was
made to handle collections as arrays not as classes with multiple properties, so collections
should look like arrays on the wire, therefore they do not have any members other
then collection items, and can be “flattened” by adding the [XmlElement] to the member
of the ICollection type." 
</p>
        <p>
Long and short, I need to use the hokey solution I came up with (or something like
it), or write my own serializer.
</p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=9fe3849a-f364-43ea-acb4-b100e991e1cc" />
      </body>
      <title>Problems serializing class which inherits from a generic list</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/09/13/Problems+Serializing+Class+Which+Inherits+From+A+Generic+List.aspx</link>
      <pubDate>Thu, 13 Sep 2007 15:22:29 GMT</pubDate>
      <description>&lt;p&gt;
Usually I post solutions I've discovered or created to problems I've encountered.&amp;nbsp;
Now, I've run into a problem with the XmlSerializer and I need your help solving it.&amp;nbsp;&amp;nbsp;Scroll
to the bottom&amp;nbsp;for an update.
&lt;/p&gt;
&lt;h3&gt;The Problem
&lt;/h3&gt;
&lt;p&gt;
I have a class called Foobar.&amp;nbsp; Foobar inherits from List&amp;lt;string&amp;gt;, and contains
one member Title.&amp;nbsp; When I use the XmlSerializer to serialize and deserialize
Foobar, all the items in the List are make it through safely, but the member Title
does not.
&lt;/p&gt;
&lt;h3&gt;The Code
&lt;/h3&gt;
&lt;p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd057b70-45a7-4b72-a7db-655bf0456e1b" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color:#FFFF80;"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System.Collections.Generic; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System.IO; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System.Xml.Serialization; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;internal&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;class&lt;/span&gt;&lt;span style="color: #000000; "&gt; Program
{ &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;private&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;static&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;void&lt;/span&gt;&lt;span style="color: #000000; "&gt; Main()
{ Foobar f &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; Foobar();
f.Add(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;1&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
f.Add(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;2&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
f.Title &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Title&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;;
MemoryStream mem &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; MemoryStream();
XmlSerializer xs &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; XmlSerializer(&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;typeof&lt;/span&gt;&lt;span style="color: #000000; "&gt;(Foobar));
xs.Serialize(mem, f); mem.Seek(&lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;, &lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
mem.Seek(&lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;, &lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
Foobar deser &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; (Foobar)xs.Deserialize(mem);
Console.WriteLine(deser); } } [Serializable] &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;public&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;class&lt;/span&gt;&lt;span style="color: #000000; "&gt; Foobar
: List&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000; "&gt; { &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;public&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt; Title;
}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&gt;
&lt;h3&gt;The Solution
&lt;/h3&gt;
&lt;p&gt;
One way I came up with to solve this problem is make Foobar not inherit from List&amp;lt;string&amp;gt;,
and make a member called Items that is a List&amp;lt;string&amp;gt;, but that's kind of hokey.&amp;nbsp;
I've searched Google and can't find a solution.&amp;nbsp; So I'm appealing to my dear
readers for a better solution.
&lt;/p&gt;
&lt;h3&gt;Update
&lt;/h3&gt;
&lt;p&gt;
Looks like I was being way to specific in my Google search, searching on "XmlSerializer
List" gives us this &lt;a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;amp;SiteID=1"&gt;http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;amp;SiteID=1&lt;/a&gt;&amp;nbsp;as
the first item.&amp;nbsp; In the post, 
&lt;/p&gt;
&lt;p&gt;
Elena Kharitidi says "XmlSerializer does not serialize any members if a collection.
Only collection items get serialized. This is by design, basically a decision was
made to handle collections as arrays not as classes with multiple properties, so collections
should look like arrays on the wire, therefore they do not have any members other
then collection items, and can be “flattened” by adding the [XmlElement] to the member
of the ICollection type." 
&lt;p&gt;
Long and short, I need to use the hokey solution I came up with (or something like
it), or write my own serializer.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=9fe3849a-f364-43ea-acb4-b100e991e1cc" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>XmlSerializer</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=2eb48fa4-804c-4fca-8e50-ec9716611167</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,2eb48fa4-804c-4fca-8e50-ec9716611167.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,2eb48fa4-804c-4fca-8e50-ec9716611167.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=2eb48fa4-804c-4fca-8e50-ec9716611167</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've started work on the search piece of our application.  Searching (no pun
intended) for some inspiration on how users might want to search within our application,
I brought up the current WinForms version, and then the ASP.NET version.
</p>
        <p>
I realized that we need to make searching easier in WinForms client, currently when
searching for a patient in our WinForms client, you are presented with a separate
text box for first name, last name, SSN, date of birth, and health record number. 
Our ASP.NET client presents just one text box to search all of those fields.
</p>
        <h3>Tying the Domain Model to the Data Access Layer
</h3>
        <p>
So I started thinking about all the pieces in our domain model that we might want
to allow the user to search.  Then I realized that to allow searching across
all the fields in an entity would tie the data access layer (DAL) to 
the domain model ala something like this:
</p>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
        </p>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:78967905-c7a2-4f28-8968-c140689ab9b7" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;">
          <pre style="background-color: rgb(255, 255, 128);">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: rgb(0, 0, 255);">public</span>
              <span style="color: rgb(0, 0, 0);"> IList</span>
              <span style="color: rgb(0, 0, 0);">&lt;</span>
              <span style="color: rgb(0, 0, 0);">Patient</span>
              <span style="color: rgb(0, 0, 0);">&gt;</span>
              <span style="color: rgb(0, 0, 0);"> Search(</span>
              <span style="color: rgb(0, 0, 255);">string</span>
              <span style="color: rgb(0, 0, 0);"> text)
{ ICriteria criteria </span>
              <span style="color: rgb(0, 0, 0);">=</span>
              <span style="color: rgb(0, 0, 0);"> session.CreateCriteria(</span>
              <span style="color: rgb(0, 0, 255);">typeof</span>
              <span style="color: rgb(0, 0, 0);">(Patient));
Disjunction or </span>
              <span style="color: rgb(0, 0, 0);">=</span>
              <span style="color: rgb(0, 0, 0);"> Expression.Disjunction();
text </span>
              <span style="color: rgb(0, 0, 0);">=</span>
              <span style="color: rgb(0, 0, 0);">
              </span>
              <span style="color: rgb(0, 0, 255);">string</span>
              <span style="color: rgb(0, 0, 0);">.Format(</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">%{0}%</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">,
text); or.Add(Expression.Like(</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">FirstName</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">,
text)); or.Add(Expression.Like(</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">MiddleName</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">,
text)); or.Add(Expression.Like(</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">LastName</span>
              <span style="color: rgb(0, 0, 0);">"</span>
              <span style="color: rgb(0, 0, 0);">,
text)); crit.Add(or); </span>
              <span style="color: rgb(0, 0, 255);">return</span>
              <span style="color: rgb(0, 0, 0);"> criteria.List</span>
              <span style="color: rgb(0, 0, 0);">&lt;</span>
              <span style="color: rgb(0, 0, 0);">Patient</span>
              <span style="color: rgb(0, 0, 0);">&gt;</span>
              <span style="color: rgb(0, 0, 0);">();
} </span>
            </div>
          </pre>
        </div>
        <br />
Variations of this code would have to be repeated for every data access class
in our DAL.  You could create a string list of the properties on the entity to
search and pass them to a method that would build your criteria; but at the end of
the day, your still tying your domain model to your data access layer. 
<p></p><h3>Custom Attributes
</h3><p></p><p>
I started doing some thinking about how unit testing frameworks such as <a href="http://www.mbunit.com/" rel="tag">MbUnit</a> work
and realized I could use .NET custom attributes and some reflection to solve the problem.
</p><p>
I came up with an attribute called Search, currently it takes in one boolean parameter
called Enabled.
</p><p></p><div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5bc9a7f1-386c-4823-a314-bb79f545167d" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"><pre style="background-color: rgb(255, 255, 128);"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: rgb(0, 128, 128);"> 1</span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System; </span><span style="color: rgb(0, 128, 128);"> 2</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Reflection; </span><span style="color: rgb(0, 128, 128);"> 3</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);"> 4</span><span style="color: rgb(0, 0, 0);">[AttributeUsage(AttributeTargets.Property,
AllowMultiple </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">)] </span><span style="color: rgb(0, 128, 128);"> 5</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> SearchAttribute
: System.Attribute </span><span style="color: rgb(0, 128, 128);"> 6</span><span style="color: rgb(0, 0, 0);">{ </span><span style="color: rgb(0, 128, 128);"> 7</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> Enabled; </span><span style="color: rgb(0, 128, 128);"> 8</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);"> 9</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);">&lt;summary&gt;&lt;/summary&gt;</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);">&lt;param
name="Enabled"&gt;</span><span style="color: rgb(0, 128, 0);">if true, property will
be included in object-level searches</span><span style="color: rgb(128, 128, 128);">&lt;/param&gt;</span><span style="color: rgb(128, 128, 128);"></span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(128, 128, 128);"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> SearchAttribute(</span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> Enabled) </span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.Enabled </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Enabled; </span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"> } </span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 0, 0);">}</span></div></pre></div><p></p><p>
I could have written it as:
</p><p></p><div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5d4b8c1f-de81-4b94-b37f-5f65f59228d4" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"><pre style="background-color: rgb(255, 255, 128);"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: rgb(0, 128, 128);">1</span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System; </span><span style="color: rgb(0, 128, 128);">2</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Reflection; </span><span style="color: rgb(0, 128, 128);">3</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);">4</span><span style="color: rgb(0, 0, 0);">[AttributeUsage(AttributeTargets.Property,
AllowMultiple </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">)] </span><span style="color: rgb(0, 128, 128);">5</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> SearchAttribute
: System.Attribute { }</span></div></pre></div><p></p><p>
But I wanted the flexability of adding named parameters in the future.  Who knows,
maybe this is a lame reason, and I should just refactor the code if I want to add
parameters to it in the future.
</p><h3>Adding the Attribute to Your Domain Model
</h3><p>
The interesting thing about a custom attribute, is if it ends in Attribute (i.e.
SearchAttribute), its name gets changed to Search by the C# compiler(?), although
you could still use SearchAttribute as the name when you add it to your properties. 
If you look at the IL, its still called SearchAttribute:
</p><p>
.custom instance void MyNamespace.SearchAttribute::.ctor(bool) = ( bool(true) )
</p><p>
But if you change the attribute name to SearchAttrib, or SearchAttributes, then you
have to use the full name when decorating the properties in your class.  This
really isn't germane to the topic at hand, I just thought it was neat! :)
</p><p>
Example:
</p><div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d718b68-0d85-408d-851d-6e6460000af1" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"><pre style="background-color: rgb(255, 255, 128);"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: rgb(0, 128, 128);"> 1</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Patient </span><span style="color: rgb(0, 128, 128);"> 2</span><span style="color: rgb(0, 0, 0);">{ </span><span style="color: rgb(0, 128, 128);"> 3</span><span style="color: rgb(0, 0, 0);"> [Search(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">)] </span><span style="color: rgb(0, 128, 128);"> 4</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> FirstName </span><span style="color: rgb(0, 128, 128);"> 5</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 128, 128);"> 6</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> _firstName;
} </span><span style="color: rgb(0, 128, 128);"> 7</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> {
_firstName </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">value</span><span style="color: rgb(0, 0, 0);">;
} </span><span style="color: rgb(0, 128, 128);"> 8</span><span style="color: rgb(0, 0, 0);"> } </span><span style="color: rgb(0, 128, 128);"> 9</span><span style="color: rgb(0, 0, 0);">... </span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 0, 0);">}</span></div></pre></div><h3>Pulling it All Together
</h3><p>
The custom attribute is great and all, but it doesn't inherently buy us anything. 
We need to add a little bit more <a title="The word technology means magic!" href="http://www.homestarrunner.com/sbemail143.html" target="_blank" rel="tag">magic</a> to
make all this work.  This is a simplified version of what I ended up putting
in my DAL base class:
</p><p></p><div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9dae2aa7-48bc-4817-b207-3316ee795e41" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"><pre style="background-color: rgb(255, 255, 128);"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: rgb(0, 128, 128);"> 1</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> IList</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Patient</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> Search(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> text) </span><span style="color: rgb(0, 128, 128);"> 2</span><span style="color: rgb(0, 0, 0);">{ </span><span style="color: rgb(0, 128, 128);"> 3</span><span style="color: rgb(0, 0, 0);"> Type
type </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(Patient); </span><span style="color: rgb(0, 128, 128);"> 4</span><span style="color: rgb(0, 0, 0);"> ICriteria
criteria </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> _session.CreateCriteria(type); </span><span style="color: rgb(0, 128, 128);"> 5</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);"> 6</span><span style="color: rgb(0, 0, 0);"> Disjunction
or </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Expression.Disjunction(); </span><span style="color: rgb(0, 128, 128);"> 7</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">foreach</span><span style="color: rgb(0, 0, 0);"> (PropertyInfo
propInfo </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> type.GetProperties()) </span><span style="color: rgb(0, 128, 128);"> 8</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 128, 128);"> 9</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">SearchableAttribute
is AllowMultiple = false, so we only need the first item</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"> Attribute[]
attribs </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Attribute.GetCustomAttributes(propInfo, </span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(SearchAttribute)); </span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (attribs.Length </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (((SearchAttribute)attribs[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]).Enabled) </span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"> or.Add(Expression.InsensitiveLike(propInfo.Name, </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">.Format(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%{0}%</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,
text))); </span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 0, 0);"> } </span><span style="color: rgb(0, 128, 128);">17</span><span style="color: rgb(0, 0, 0);"> criteria.Add(or); </span><span style="color: rgb(0, 128, 128);">18</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> criteria.List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">(); </span><span style="color: rgb(0, 128, 128);">19</span><span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 128, 128);">20</span><span style="color: rgb(0, 0, 0);"></span></div></pre></div><p></p><p>
Again, this is a simplified version of what's in our DAL base class, the actual signature
looks more like this: private IList&lt;T&gt; Search&lt;T&gt;(string text) so that
I don't have to write a version of this for each class in our domain model I want
to enable searching for.
</p><p>
Everything above should be pretty self-explanatory unless you aren't familiar with
the NHibernate Criteria API; in which case the Expression.Disjunction bit on line
6 is how you do an OR (a OR b OR c)when searching.  You can also do Expression.Conjunction
if you want AND searching (a AND b AND c).
</p><p>
The original version of this code had a second foreach loop which spun through all
the attributes on each property looking for the SearchAttribute.  I thought to
myself that their had to be a better way and did a little bit of poking around, and
discovered much to my delight that the static method Attribute.GetCustomAttributes
allows you to specify what kind of attribute you are searching for!
</p><h3>Limitations
</h3><p>
In the code above, you'll get an Exception if you try to search against non-text columns
in your database so you will need to that into account when putting the Search
attribute in your Domain Model; or add some more smarts to the Search method to take
into account different data types.
</p><h3>Comments
</h3><p>
If you've read down this far, then I'd love to get a comment from you.  Is there
anything you think I could do a better job of explaining, am I an awesome guy? 
Or do I suck, either way, I'd like to know, so please leave me a comment!
</p><p>
If you like my article, please kick it at .NET Kicks (I have no idea why the kick
counter says I have zero kicks btw)!
</p><p><a href="http://www.dotnetkicks.com/kick?url=http://www.milkcarton.com/blog/2007/01/23/Using+Custom+Attributes+To+Enable+Quick+Searching+Of+Your+Domain+Entities+With+NHibernate.aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.milkcarton.com/blog/2007/01/23/Using+Custom+Attributes+To+Enable+Quick+Searching+Of+Your+Domain+Entities+With+NHibernate.aspx" border="0" /></a></p><img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=2eb48fa4-804c-4fca-8e50-ec9716611167" /></body>
      <title>Using Custom Attributes to Enable Quick Searching of Your Domain Entities With NHibernate</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,2eb48fa4-804c-4fca-8e50-ec9716611167.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/01/23/Using+Custom+Attributes+To+Enable+Quick+Searching+Of+Your+Domain+Entities+With+NHibernate.aspx</link>
      <pubDate>Tue, 23 Jan 2007 20:02:42 GMT</pubDate>
      <description>&lt;p&gt;
I've started work on the search piece of our application.&amp;nbsp; Searching (no pun
intended) for some inspiration on how users might want to search within our application,
I brought up the current WinForms version, and then&amp;nbsp;the ASP.NET version.
&lt;/p&gt;
&lt;p&gt;
I realized that we need to make searching easier in WinForms client, currently when
searching for a patient in our WinForms client, you are presented with a separate
text box for first name, last name, SSN, date of birth, and health record number.&amp;nbsp;
Our ASP.NET client presents just one text box to search all of those fields.
&lt;/p&gt;
&lt;h3&gt;Tying the Domain Model to the Data Access Layer
&lt;/h3&gt;
&lt;p&gt;
So I started thinking about all the pieces in our domain model that we might want
to allow the user to search.&amp;nbsp; Then I realized that to allow searching across
all the fields in an entity would tie&amp;nbsp;the data access layer (DAL)&amp;nbsp;to&amp;nbsp;
the domain model ala something like this:
&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:78967905-c7a2-4f28-8968-c140689ab9b7" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;&lt;pre style="background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IList&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Patient&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Search(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text)
{ ICriteria criteria &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; session.CreateCriteria(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(Patient));
Disjunction or &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Expression.Disjunction();
text &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.Format(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%{0}%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,
text); or.Add(Expression.Like(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FirstName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,
text)); or.Add(Expression.Like(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MiddleName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,
text)); or.Add(Expression.Like(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;LastName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,
text)); crit.Add(or); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; criteria.List&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Patient&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();
} &lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br&gt;
Variations of this code would have to be repeated for every&amp;nbsp;data access class
in our DAL.&amp;nbsp; You could create a string list of the properties on the entity to
search and pass them to a method that would build your criteria; but at the end of
the day, your still tying your domain model to your data access layer. 
&lt;p&gt;
&lt;/p&gt;
&lt;h3&gt;Custom Attributes
&lt;/h3&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
I started doing some thinking about how unit testing frameworks such as &lt;a href="http://www.mbunit.com/" rel="tag"&gt;MbUnit&lt;/a&gt;&amp;nbsp;work
and realized I could use .NET custom attributes and some reflection to solve the problem.
&lt;/p&gt;
&lt;p&gt;
I came up with an attribute called Search, currently it takes in one boolean parameter
called Enabled.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5bc9a7f1-386c-4823-a314-bb79f545167d" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;&lt;pre style="background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Reflection; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;[AttributeUsage(AttributeTargets.Property,
AllowMultiple &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)] &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SearchAttribute
: System.Attribute &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 6&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;{ &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 7&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enabled; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 8&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 9&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;10&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;11&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;param
name="Enabled"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;if true, property will
be included in object-level searches&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;12&lt;/span&gt; &lt;span style="color: rgb(128, 128, 128);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SearchAttribute(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enabled) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;13&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; { &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;14&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.Enabled &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enabled; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;15&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; } &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;16&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
I could have written it as:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5d4b8c1f-de81-4b94-b37f-5f65f59228d4" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;&lt;pre style="background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Reflection; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;[AttributeUsage(AttributeTargets.Property,
AllowMultiple &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)] &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SearchAttribute
: System.Attribute { }&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
But I wanted the flexability of adding named parameters in the future.&amp;nbsp; Who knows,
maybe this is a lame reason, and I should just refactor the code if I want to add
parameters to it in the future.
&lt;/p&gt;
&lt;h3&gt;Adding&amp;nbsp;the Attribute to Your Domain Model
&lt;/h3&gt;
&lt;p&gt;
The interesting thing about a custom&amp;nbsp;attribute, is if it ends in Attribute (i.e.
SearchAttribute), its name gets changed to Search by the C# compiler(?), although
you could still use SearchAttribute as the name when you add it to your properties.&amp;nbsp;
If you look at the IL, its still called SearchAttribute:
&lt;/p&gt;
&lt;p&gt;
.custom instance void MyNamespace.SearchAttribute::.ctor(bool) = ( bool(true) )
&lt;/p&gt;
&lt;p&gt;
But if you change the attribute name to SearchAttrib, or SearchAttributes, then you
have to use the full name when decorating the properties in your class.&amp;nbsp; This
really isn't germane to the topic at hand, I just thought it was neat! :)
&lt;/p&gt;
&lt;p&gt;
Example:
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d718b68-0d85-408d-851d-6e6460000af1" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;&lt;pre style="background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Patient &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;{ &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; [Search(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)] &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; FirstName &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; { &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 6&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _firstName;
} &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 7&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; {
_firstName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
} &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 8&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; } &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 9&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;... &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;10&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;h3&gt;Pulling it All Together
&lt;/h3&gt;
&lt;p&gt;
The custom attribute is great and all, but it doesn't inherently buy us anything.&amp;nbsp;
We need to add a little bit more &lt;a title="The word technology means magic!" href="http://www.homestarrunner.com/sbemail143.html" target="_blank" rel="tag"&gt;magic&lt;/a&gt; to
make all this work.&amp;nbsp; This is a simplified version of what I ended up putting
in my DAL base class:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9dae2aa7-48bc-4817-b207-3316ee795e41" contenteditable="false" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;&lt;pre style="background-color: rgb(255, 255, 128);"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IList&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Patient&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Search(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;{ &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; Type
type &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(Patient); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; ICriteria
criteria &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _session.CreateCriteria(type); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 6&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; Disjunction
or &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Expression.Disjunction(); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 7&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (PropertyInfo
propInfo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; type.GetProperties()) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 8&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; { &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt; 9&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;SearchableAttribute
is AllowMultiple = false, so we only need the first item&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;10&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Attribute[]
attribs &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Attribute.GetCustomAttributes(propInfo, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(SearchAttribute)); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;11&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (attribs.Length &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;12&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;continue&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;13&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;14&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (((SearchAttribute)attribs[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]).Enabled) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;15&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; or.Add(Expression.InsensitiveLike(propInfo.Name, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.Format(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%{0}%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,
text))); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;16&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; } &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;17&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; criteria.Add(or); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;18&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; criteria.List&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;19&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;20&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Again, this is a simplified version of what's in our DAL base class, the actual signature
looks more like this: private IList&amp;lt;T&amp;gt; Search&amp;lt;T&amp;gt;(string text) so that
I don't have to write a version of this for each class in our domain model I want
to enable searching for.
&lt;/p&gt;
&lt;p&gt;
Everything above should be pretty self-explanatory unless you aren't familiar with
the NHibernate Criteria API; in which case the Expression.Disjunction bit on line
6 is how you do an OR (a OR b OR c)when searching.&amp;nbsp; You can also do Expression.Conjunction
if you want&amp;nbsp;AND searching (a AND b AND c).
&lt;/p&gt;
&lt;p&gt;
The original version of this code had a second foreach loop which spun through all
the attributes on each property looking for the SearchAttribute.&amp;nbsp; I thought to
myself that their had to be a better way and did a little bit of poking around, and
discovered much to my delight that the static method Attribute.GetCustomAttributes
allows you to specify what kind of attribute you are searching for!
&lt;/p&gt;
&lt;h3&gt;Limitations
&lt;/h3&gt;
&lt;p&gt;
In the code above, you'll get an Exception if you try to search against non-text columns
in your database&amp;nbsp;so you will need to that into account when putting the Search
attribute in your Domain Model; or add some more smarts to the Search method to take
into account different data types.
&lt;/p&gt;
&lt;h3&gt;Comments
&lt;/h3&gt;
&lt;p&gt;
If you've read down this far, then I'd love to get a comment from you.&amp;nbsp; Is there
anything you think I could do a better job of explaining, am I an awesome guy?&amp;nbsp;
Or do I suck, either way, I'd like to know, so please leave me a comment!
&lt;/p&gt;
&lt;p&gt;
If you like my article, please kick it at .NET Kicks (I have no idea why the kick
counter says I have zero kicks btw)!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick?url=http://www.milkcarton.com/blog/2007/01/23/Using+Custom+Attributes+To+Enable+Quick+Searching+Of+Your+Domain+Entities+With+NHibernate.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.milkcarton.com/blog/2007/01/23/Using+Custom+Attributes+To+Enable+Quick+Searching+Of+Your+Domain+Entities+With+NHibernate.aspx" border="0"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=2eb48fa4-804c-4fca-8e50-ec9716611167" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,2eb48fa4-804c-4fca-8e50-ec9716611167.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>NHibernate</category>
      <category>ORM</category>
      <category>Searching</category>
    </item>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=faa5611c-38bb-4b97-b461-4b730286ec99</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,faa5611c-38bb-4b97-b461-4b730286ec99.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,faa5611c-38bb-4b97-b461-4b730286ec99.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=faa5611c-38bb-4b97-b461-4b730286ec99</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We are in the midst of doing a total rewrite of our Software, and one of the things
that has come up is date and time.  How do we do it, how do we store it, and
how do we ensure that we can compare DateTime from one timezone to DateTime in another
timezone.  After a lot of research, we settled on using UTC (or UCT depending
on your preference).  FxCop will take care of ensuring we use UTC (for the most
part).
</p>
        <p>
That solves the problem, or so we thought.  Turns out, when you create a DateTime
object either through the constructor, or through DateTime.Parse, its Kind defaults
to DateTimeKind.Unspecified.  We need a way to ensure that all DateTime objects
are always set to UTC.
</p>
        <h2>What are our options?
</h2>
        <p>
Because we are using NHibernate, we have a few options.  The three NHibernate
specific ones that immediately come to mind are using an Interceptor, a custom UserType
with a SQL datetime column, and a custom UserType with a SQL varchar column; and the
non-NHibernate specific one is creating our own DateTime container.  What are
the pros and cons of each of these?
</p>
        <h3>NHibernate Interceptor
</h3>
        <p>
Pro: very cross-cutting, can touch every object as it comes in and goes out to the
database; if there are other data types we need to monkey with, we already have
a framework in place.
</p>
        <p>
Con: Very cross-cutting, can be expensive because it's touching every property
on every entity as the entities are loaded and persisted
</p>
        <h3>UserType with SQL datetime column
</h3>
        <p>
Pro: Only touching the DateTime objects that we want it to
</p>
        <p>
Con: The type has to be specified for <strong>every</strong> DateTime object in every
mapping file; no meta-data along with the date to stamp in the timezone it was created
in
</p>
        <h3>UserType with SQL varchar column
</h3>
        <p>
Pro: Only touching the DateTime objects that we want it to; can store the timezone
and offset along with the date in the db
</p>
        <p>
Con: Same as above UserType; abusing SQL data types; datetimes created at the same
(relative) time in two different timezones won't be sorted correctly
</p>
        <h3>Custom DateTime container
</h3>
        <p>
Pro: We can do anything we want
</p>
        <p>
Con: Yuck! - I could write a whole paragraph on why this is yucky, but I'll leave
that to your imagination
</p>
        <p>
 
</p>
        <h4>
        </h4>
        <p>
After some thought, I decided on the Interceptor!  Here is the class I came up
with (you can also <a href="http://www.milkcarton.com/blog/content/binary/UtcDateTimeInterceptor.cs.txt">download
the complete UtcDateTimeInterceptor class</a>):
</p>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9f99d007-4145-4f43-81ec-aa3402facafe" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
          <pre style="background-color:#FFFFDD;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: auto;">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: #008080;"> 1</span>
              <span style="color: #0000FF;">class</span>
              <span style="color: #000000;"> UtcDateTimeInterceptor
: IInterceptor </span>
              <span style="color: #008080;"> 2</span>
              <span style="color: #000000;">{ </span>
              <span style="color: #008080;"> 3</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">public</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">bool</span>
              <span style="color: #000000;"> OnLoad(</span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;"> entity, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;"> id, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;">[]
state, </span>
              <span style="color: #0000FF;">string</span>
              <span style="color: #000000;">[]
propertyNames, IType[] types) </span>
              <span style="color: #008080;"> 4</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;"> 5</span>
              <span style="color: #000000;"> ConvertDatabaseDateTimeToUtc(state,
types); </span>
              <span style="color: #008080;"> 6</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">return</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">true</span>
              <span style="color: #000000;">; </span>
              <span style="color: #008080;"> 7</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;"> 8</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008080;"> 9</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">public</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">bool</span>
              <span style="color: #000000;"> OnSave(</span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;"> entity, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;"> id, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;">[]
state, </span>
              <span style="color: #0000FF;">string</span>
              <span style="color: #000000;">[]
propertyNames, IType[] types) </span>
              <span style="color: #008080;">10</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">11</span>
              <span style="color: #000000;"> ConvertLocalDateToUtc(state,
types); </span>
              <span style="color: #008080;">12</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">return</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">true</span>
              <span style="color: #000000;">; </span>
              <span style="color: #008080;">13</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">14</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008080;">15</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">public</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">bool</span>
              <span style="color: #000000;"> OnFlushDirty(</span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;"> entity, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;"> id, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;">[]
currentState, </span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;">[]
previousState, </span>
              <span style="color: #0000FF;">string</span>
              <span style="color: #000000;">[]
propertyNames, </span>
              <span style="color: #008080;">16</span>
              <span style="color: #000000;"> IType[]
types) </span>
              <span style="color: #008080;">17</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">18</span>
              <span style="color: #000000;"> ConvertLocalDateToUtc(currentState,
types); </span>
              <span style="color: #008080;">19</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">return</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">true</span>
              <span style="color: #000000;">; </span>
              <span style="color: #008080;">20</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">21</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008080;">22</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">private</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">void</span>
              <span style="color: #000000;"> ConvertLocalDateToUtc(</span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;">[]
state, IType[] types) </span>
              <span style="color: #008080;">23</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">24</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">int</span>
              <span style="color: #000000;"> index </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #800080;">0</span>
              <span style="color: #000000;">; </span>
              <span style="color: #008080;">25</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">foreach</span>
              <span style="color: #000000;"> (IType
type </span>
              <span style="color: #0000FF;">in</span>
              <span style="color: #000000;"> types) </span>
              <span style="color: #008080;">26</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">27</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">if</span>
              <span style="color: #000000;"> ((type.ReturnedClass </span>
              <span style="color: #000000;">==</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">typeof</span>
              <span style="color: #000000;">(DateTime)) </span>
              <span style="color: #000000;">&amp;&amp;</span>
              <span style="color: #000000;"> state[index] </span>
              <span style="color: #000000;">!=</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">null</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #000000;">&amp;&amp;</span>
              <span style="color: #000000;"> (((DateTime)state[index]).Kind </span>
              <span style="color: #000000;">==</span>
              <span style="color: #000000;"> DateTimeKind.Utc)) </span>
              <span style="color: #008080;">28</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">29</span>
              <span style="color: #000000;"> state[index] </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;"> ((DateTime)state[index]).ToUniversalTime(); </span>
              <span style="color: #008080;">30</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">31</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008080;">32</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #000000;">++</span>
              <span style="color: #000000;">index; </span>
              <span style="color: #008080;">33</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">34</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">35</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008080;">36</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">private</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">void</span>
              <span style="color: #000000;"> ConvertDatabaseDateTimeToUtc(</span>
              <span style="color: #0000FF;">object</span>
              <span style="color: #000000;">[]
state, IType[] types) </span>
              <span style="color: #008080;">37</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">38</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">int</span>
              <span style="color: #000000;"> index </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #800080;">0</span>
              <span style="color: #000000;">; </span>
              <span style="color: #008080;">39</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">foreach</span>
              <span style="color: #000000;"> (IType
type </span>
              <span style="color: #0000FF;">in</span>
              <span style="color: #000000;"> types) </span>
              <span style="color: #008080;">40</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">41</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">if</span>
              <span style="color: #000000;"> ((type.ReturnedClass </span>
              <span style="color: #000000;">==</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">typeof</span>
              <span style="color: #000000;">(DateTime)) </span>
              <span style="color: #000000;">&amp;&amp;</span>
              <span style="color: #000000;"> state[index] </span>
              <span style="color: #000000;">!=</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #0000FF;">null</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #000000;">&amp;&amp;</span>
              <span style="color: #000000;"> (((DateTime)state[index]).Kind </span>
              <span style="color: #000000;">==</span>
              <span style="color: #000000;"> DateTimeKind.Unspecified)) </span>
              <span style="color: #008080;">42</span>
              <span style="color: #000000;"> { </span>
              <span style="color: #008080;">43</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008000;">//</span>
              <span style="color: #008000;">Create
a new date and assume the value stored in the database is Utc</span>
              <span style="color: #008000;">
              </span>
              <span style="color: #008080;">44</span>
              <span style="color: #008000;">
              </span>
              <span style="color: #000000;"> DateTime
cur </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;"> (DateTime)state[index]; </span>
              <span style="color: #008080;">45</span>
              <span style="color: #000000;"> DateTime
result </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;"> DateTime.SpecifyKind(cur,
DateTimeKind.Local); </span>
              <span style="color: #008080;">46</span>
              <span style="color: #000000;"> state[index] </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;"> result; </span>
              <span style="color: #008080;">47</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">48</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #008080;">49</span>
              <span style="color: #000000;">
              </span>
              <span style="color: #000000;">++</span>
              <span style="color: #000000;">index; </span>
              <span style="color: #008080;">50</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">51</span>
              <span style="color: #000000;"> } </span>
              <span style="color: #008080;">52</span>
              <span style="color: #000000;">}</span>
            </div>
          </pre>
          <!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com -->
        </div>
        <p>
        </p>
        <h3>Loading The Entities
</h3>
        <p>
For the sake of brevity, I'm going to exclude the bits of the Interceptor interface
that aren't relevant to my posting.  With that said, the OnLoad event (line 3)
gets fired every time an entity is loaded from the database.  We can count on
the fact that all dates are stored as UTC in the database because of stuff we'll do
later, so we need to convert the DateTime that NHibernate generates (which has a DateTimeKind
of Unspecified) to a UTC date (line 36 - 48).  The types array holds the
CLR data type of each property in the entity, and each type in the array contains
both the internal NHibernate type, and the CLR type. But we don't really care about
how NHibernate maps the data types internally, so we are only interested in type.ReturnedClass.
</p>
        <p>
The first thing we need to do is see if the ReturnedClass is of type DateTime (line
41), if its a DateTime, then we need to see if its null, and finally double check
that the Kind on the DateTime object coming back from the database is Unspecified. 
This last check is a sanity check, in case this behavior changes in the future.
</p>
        <p>
After all these checks are passed, we need to create a new DateTime object from the
old one, and set its Kind to Utc (lines 44 and 45).  Thankfully, DateTime has
the built-in method SpecifyKind which will take care of building a DateTime of the
specified kind for us. And finally replace the existing DateTime object with our new
one (line 46).  Shampoo, rinse, repeat for all the DateTime values in the entity.
</p>
        <h3>Persisting The Entities
</h3>
        <p>
Now we can move on to the save and update NHibernate events (line 9, and 15 respectively). 
In these, we want to make sure the values being persisted to the datastore are UTC,
and that no Local times have slipped through the cracks.  If one were so inclined,
they could throw an error instead of converting the DateTime to UTC...
</p>
        <p>
The basic code for converting Local DateTime's to Utc (line 22- 34) is much the same
as above, but with a few exceptions.  When we do all our checks (line 27), this
time we make sure the DateTimeKind is Local before we perform a conversion operation
on it.  It is pointless to check if the Kind is Unspecified, because there is
no conversion operation we can really perform on it.  On line 29, we can use
the built in DateTime method ToUniversalTime() to convert a LocalTime to UTC.
</p>
        <h3>Finishing Up
</h3>
        <p>
How do we wire this all up?  When you open a session on your session factory,
you can pass in an Interceptor, this is where you would pass in the UtcDateTimeInterceptor. 
eg:
</p>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:c90243f1-a329-41f3-9467-b846d16c84de" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
          <pre style="background-color:#FFFFE1;;overflow: auto;">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: #000000;">ISession
openSession </span>
              <span style="color: #000000;">=</span>
              <span style="color: #000000;"> ourSessionFactory.OpenSession(</span>
              <span style="color: #0000FF;">new</span>
              <span style="color: #000000;"> UtcDateTimeInterceptor());</span>
            </div>
          </pre>
          <!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com -->
        </div>
        <p>
        </p>
        <p>
I want to give credit where credit is due, I got the actual idea of using an Interceptor
from <a href="http://www.ayende.com/Blog/2006/08/31/HandlingNullableDatetimeInNHibernateTheInterceptor.aspx" rel="tag">a
blog post by Ayende</a> where he grappled with DateTime, null, messages and web services. 
If anyone using NHibernate doesn't read <a href="http://www.ayende.com/Blog/" rel="tag">Ayende
Rahiens' blog</a>, I would highly encourage you to.  He is a very, very sharp
fellow; and prolific blogger.
</p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=faa5611c-38bb-4b97-b461-4b730286ec99" />
      </body>
      <title>NHibernate, DateTime and UTC</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,faa5611c-38bb-4b97-b461-4b730286ec99.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/01/19/NHibernate+DateTime+And+UTC.aspx</link>
      <pubDate>Fri, 19 Jan 2007 00:36:52 GMT</pubDate>
      <description>&lt;p&gt;
We are in the midst of doing a total rewrite of our Software, and one of the things
that has come up is date and time.&amp;nbsp; How do we do it, how do we store it, and
how do we ensure that we can compare DateTime from one timezone to DateTime in another
timezone.&amp;nbsp; After a lot of research, we settled on using UTC (or UCT depending
on your preference).&amp;nbsp; FxCop will take care of ensuring we use UTC (for the most
part).
&lt;/p&gt;
&lt;p&gt;
That solves the problem, or so we thought.&amp;nbsp; Turns out, when you create a DateTime
object either through the constructor, or through DateTime.Parse, its Kind&amp;nbsp;defaults
to DateTimeKind.Unspecified.&amp;nbsp; We need a way to ensure that all DateTime objects
are always set to UTC.
&lt;/p&gt;
&lt;h2&gt;What are our options?
&lt;/h2&gt;
&lt;p&gt;
Because we are using NHibernate, we have a few options.&amp;nbsp; The&amp;nbsp;three NHibernate
specific ones that immediately come to mind are using an Interceptor, a custom UserType
with a SQL datetime column, and a custom UserType with a SQL varchar column; and the
non-NHibernate specific one is creating our own DateTime container.&amp;nbsp; What are
the pros and cons of each of these?
&lt;/p&gt;
&lt;h3&gt;NHibernate Interceptor
&lt;/h3&gt;
&lt;p&gt;
Pro: very cross-cutting, can touch every object as it comes in and goes out to the
database; if there are other data types we need to monkey with, we already&amp;nbsp;have
a framework in place.
&lt;/p&gt;
&lt;p&gt;
Con: Very cross-cutting, can be expensive because it's touching every&amp;nbsp;property
on every entity as the entities are loaded and persisted
&lt;/p&gt;
&lt;h3&gt;UserType with SQL datetime column
&lt;/h3&gt;
&lt;p&gt;
Pro: Only touching the DateTime objects that we want it to
&lt;/p&gt;
&lt;p&gt;
Con: The type has to be specified for &lt;strong&gt;every&lt;/strong&gt; DateTime object in&amp;nbsp;every
mapping file; no meta-data along with the date to stamp in the timezone it was created
in
&lt;/p&gt;
&lt;h3&gt;UserType with SQL varchar column
&lt;/h3&gt;
&lt;p&gt;
Pro: Only touching the DateTime objects that we want it to; can store the timezone
and offset along with the date in the db
&lt;/p&gt;
&lt;p&gt;
Con: Same as above UserType; abusing SQL data types; datetimes created at the same
(relative) time in two different timezones won't be sorted correctly
&lt;/p&gt;
&lt;h3&gt;Custom DateTime container
&lt;/h3&gt;
&lt;p&gt;
Pro: We can do anything we want
&lt;/p&gt;
&lt;p&gt;
Con: Yuck! - I could write a whole paragraph on why this is yucky, but I'll leave
that to your imagination
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;h4&gt;
&lt;/h4&gt;
&lt;p&gt;
After some thought, I decided on the Interceptor!&amp;nbsp; Here is the class I came up
with (you can also &lt;a href="http://www.milkcarton.com/blog/content/binary/UtcDateTimeInterceptor.cs.txt"&gt;download
the complete UtcDateTimeInterceptor class&lt;/a&gt;):
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9f99d007-4145-4f43-81ec-aa3402facafe" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color:#FFFFDD;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: auto;"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080;"&gt; 1&lt;/span&gt; &lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; UtcDateTimeInterceptor
: IInterceptor &lt;/span&gt;&lt;span style="color: #008080;"&gt; 2&lt;/span&gt; &lt;span style="color: #000000;"&gt;{ &lt;/span&gt;&lt;span style="color: #008080;"&gt; 3&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; OnLoad(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; entity, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; id, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
state, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
propertyNames, IType[] types) &lt;/span&gt;&lt;span style="color: #008080;"&gt; 4&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt; 5&lt;/span&gt; &lt;span style="color: #000000;"&gt; ConvertDatabaseDateTimeToUtc(state,
types); &lt;/span&gt;&lt;span style="color: #008080;"&gt; 6&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008080;"&gt; 7&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt; 8&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt; 9&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; OnSave(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; entity, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; id, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
state, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
propertyNames, IType[] types) &lt;/span&gt;&lt;span style="color: #008080;"&gt;10&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;11&lt;/span&gt; &lt;span style="color: #000000;"&gt; ConvertLocalDateToUtc(state,
types); &lt;/span&gt;&lt;span style="color: #008080;"&gt;12&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008080;"&gt;13&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;14&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;15&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; OnFlushDirty(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; entity, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; id, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
currentState, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
previousState, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
propertyNames, &lt;/span&gt;&lt;span style="color: #008080;"&gt;16&lt;/span&gt; &lt;span style="color: #000000;"&gt; IType[]
types) &lt;/span&gt;&lt;span style="color: #008080;"&gt;17&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;18&lt;/span&gt; &lt;span style="color: #000000;"&gt; ConvertLocalDateToUtc(currentState,
types); &lt;/span&gt;&lt;span style="color: #008080;"&gt;19&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008080;"&gt;20&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;21&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;22&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; ConvertLocalDateToUtc(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
state, IType[] types) &lt;/span&gt;&lt;span style="color: #008080;"&gt;23&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;24&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; index &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008080;"&gt;25&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (IType
type &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; types) &lt;/span&gt;&lt;span style="color: #008080;"&gt;26&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;27&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; ((type.ReturnedClass &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(DateTime)) &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; state[index] &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; (((DateTime)state[index]).Kind &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; DateTimeKind.Utc)) &lt;/span&gt;&lt;span style="color: #008080;"&gt;28&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;29&lt;/span&gt; &lt;span style="color: #000000;"&gt; state[index] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ((DateTime)state[index]).ToUniversalTime(); &lt;/span&gt;&lt;span style="color: #008080;"&gt;30&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;31&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;32&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;index; &lt;/span&gt;&lt;span style="color: #008080;"&gt;33&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;34&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;35&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;36&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; ConvertDatabaseDateTimeToUtc(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[]
state, IType[] types) &lt;/span&gt;&lt;span style="color: #008080;"&gt;37&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;38&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; index &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008080;"&gt;39&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (IType
type &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; types) &lt;/span&gt;&lt;span style="color: #008080;"&gt;40&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;41&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; ((type.ReturnedClass &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(DateTime)) &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; state[index] &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; (((DateTime)state[index]).Kind &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; DateTimeKind.Unspecified)) &lt;/span&gt;&lt;span style="color: #008080;"&gt;42&lt;/span&gt; &lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #008080;"&gt;43&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Create
a new date and assume the value stored in the database is Utc&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;44&lt;/span&gt; &lt;span style="color: #008000;"&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; DateTime
cur &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (DateTime)state[index]; &lt;/span&gt;&lt;span style="color: #008080;"&gt;45&lt;/span&gt; &lt;span style="color: #000000;"&gt; DateTime
result &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; DateTime.SpecifyKind(cur,
DateTimeKind.Local); &lt;/span&gt;&lt;span style="color: #008080;"&gt;46&lt;/span&gt; &lt;span style="color: #000000;"&gt; state[index] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; result; &lt;/span&gt;&lt;span style="color: #008080;"&gt;47&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;48&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;49&lt;/span&gt; &lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;index; &lt;/span&gt;&lt;span style="color: #008080;"&gt;50&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;51&lt;/span&gt; &lt;span style="color: #000000;"&gt; } &lt;/span&gt;&lt;span style="color: #008080;"&gt;52&lt;/span&gt; &lt;span style="color: #000000;"&gt;}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;h3&gt;Loading The Entities
&lt;/h3&gt;
&lt;p&gt;
For the sake of brevity, I'm going to exclude the bits of the&amp;nbsp;Interceptor interface
that aren't relevant to my posting.&amp;nbsp; With that said, the OnLoad event (line 3)
gets fired every time an entity is loaded from the database.&amp;nbsp; We can count on
the fact that all dates are stored as UTC in the database because of stuff we'll do
later, so we need to convert the DateTime that NHibernate generates (which has a DateTimeKind
of Unspecified) to a UTC date (line&amp;nbsp;36 - 48).&amp;nbsp; The types array holds the
CLR data type&amp;nbsp;of each property in the entity, and each type in the array contains
both the internal NHibernate type, and the CLR type. But we don't really care about
how NHibernate maps the data types internally, so we are only interested in type.ReturnedClass.
&lt;/p&gt;
&lt;p&gt;
The first thing we need to do is see if the ReturnedClass is of type DateTime (line
41), if its a DateTime, then we need to see if its null, and finally double check
that the Kind on the DateTime object coming back from the database is Unspecified.&amp;nbsp;
This last check is a sanity check, in case this behavior changes in the future.
&lt;/p&gt;
&lt;p&gt;
After all these checks are passed, we need to create a new DateTime object from the
old one, and set its Kind to Utc (lines 44 and 45).&amp;nbsp; Thankfully, DateTime has
the built-in method SpecifyKind which will take care of building a DateTime of the
specified kind for us. And finally replace the existing DateTime object with our new
one (line 46).&amp;nbsp; Shampoo, rinse, repeat for all the DateTime values in the entity.
&lt;/p&gt;
&lt;h3&gt;Persisting The Entities
&lt;/h3&gt;
&lt;p&gt;
Now we can move on to the save and update NHibernate events (line 9, and 15 respectively).&amp;nbsp;
In these, we want to make sure the values being persisted to the datastore are UTC,
and that no Local times have slipped through the cracks.&amp;nbsp; If one were so inclined,
they could throw an error instead of converting the DateTime to UTC...
&lt;/p&gt;
&lt;p&gt;
The basic code for converting Local DateTime's to Utc (line 22- 34) is much the same
as above, but with a few exceptions.&amp;nbsp; When we do all our checks (line 27), this
time we make sure the DateTimeKind is Local before we perform a conversion operation
on it.&amp;nbsp; It is pointless to check if the Kind is Unspecified, because there is
no conversion operation we can really perform on it.&amp;nbsp; On line 29, we can use
the built in DateTime method ToUniversalTime() to convert a LocalTime to UTC.
&lt;/p&gt;
&lt;h3&gt;Finishing Up
&lt;/h3&gt;
&lt;p&gt;
How do we wire this all up?&amp;nbsp; When you open a session on your session factory,
you can pass in an Interceptor, this is where you would pass in the UtcDateTimeInterceptor.&amp;nbsp;
eg:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:c90243f1-a329-41f3-9467-b846d16c84de" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color:#FFFFE1;;overflow: auto;"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;ISession
openSession &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ourSessionFactory.OpenSession(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; UtcDateTimeInterceptor());&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
I want to give credit where credit is due, I got the actual idea of using an Interceptor
from&amp;nbsp;&lt;a href="http://www.ayende.com/Blog/2006/08/31/HandlingNullableDatetimeInNHibernateTheInterceptor.aspx" rel="tag"&gt;a
blog post by Ayende&lt;/a&gt; where he grappled with DateTime, null, messages and web services.&amp;nbsp;
If anyone using NHibernate&amp;nbsp;doesn't read &lt;a href="http://www.ayende.com/Blog/" rel="tag"&gt;Ayende
Rahiens' blog&lt;/a&gt;, I would highly encourage you to.&amp;nbsp; He is a very, very sharp
fellow; and prolific blogger.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=faa5611c-38bb-4b97-b461-4b730286ec99" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,faa5611c-38bb-4b97-b461-4b730286ec99.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>NHibernate</category>
      <category>ORM</category>
    </item>
  </channel>
</rss>