<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Los Techies</title><link>http://www.lostechies.com/blogs/</link><description>LosTechies.com was originally discussed a few years ago, over a couple of adult beverages whose name sounds very similar to l(D)os t(E)quies. Anyway the thought was to create a public forum where technical ideas and thoughts can be shared in the same way we all get together around a good meal and drinks. Ideas and thoughts are cultivated in discussion, and brought to fruition through professional debate and laughter. Sounds good in theory, well read our thoughts and ideas, take part in our debates and rejoice in our laughter.</description><dc:language>en-US</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/LosTechies" type="application/rss+xml" /><item><title>Announcing: the Nashville ALT.NET User Group</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/357052950/announcing-the-nashville-alt-net-user-group.aspx</link><pubDate>Wed, 06 Aug 2008 03:42:36 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4362</guid><dc:creator>hoffe</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;This has been a long time coming, but it&amp;#39;s finally here!&lt;/p&gt; &lt;p&gt;Join the &lt;a href="http://groups.google.com/group/nashvillealtnet" target="_blank"&gt;announcements list&lt;/a&gt; and jump in on &lt;a href="http://groups.google.com/group/nashvillealtnet-discuss" target="_blank"&gt;the discussion&lt;/a&gt;.&amp;nbsp; We&amp;#39;ll plan our first meeting for later this month via the discussion list.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4362" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=4iZafK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=4iZafK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=w02Qpk"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=w02Qpk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=hgkZTK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=hgkZTK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/357052950" height="1" width="1"/&gt;</description><feedburner:origLink>http://www.lostechies.com/blogs/evan_hoff/archive/2008/08/05/announcing-the-nashville-alt-net-user-group.aspx</feedburner:origLink></item><item><title>Disconnected - Channels Of Communication</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/356042114/disconnected-channels-of-communication.aspx</link><pubDate>Tue, 05 Aug 2008 05:05:59 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4343</guid><dc:creator>mo.khan</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;A few weeks ago I started feeling a little over whelmed by the volume of interest in what I was up to. After reading a chapter from Tim Ferris&amp;#39; book, I decided to disconnect. It was the most effective advice I could have ever received. I went &lt;a href="http://en.wikipedia.org/wiki/Cold_turkey"&gt;cold turkey&lt;/a&gt;. I turned off my phone and put it in a drawer. I completely stopped checking my email, and wouldn&amp;#39;t allow myself to &amp;quot;surf&amp;quot; the net. &lt;/p&gt;  &lt;p&gt;The result after a couple of weeks, I feel liberated... and refreshed! &lt;/p&gt;  &lt;table&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://www.amazon.com/4-Hour-work-Week-Escape-Anywhere/dp/0786158964%3FSubscriptionId%3D0525E2PQ81DD7ZTWTK82%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0786158964"&gt;&lt;img src="http://ecx.images-amazon.com/images/I/41cND5RQsLL._SL75_.jpg" border="1" alt="" /&gt;&lt;/a&gt;&lt;/td&gt;        &lt;td&gt;&lt;b&gt;The 4-Hour work Week: Escape 9-5, Live Anywhere, and Join the New Rich&lt;/b&gt;           &lt;br /&gt;by Timothy Ferris           &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://www.amazon.com/4-Hour-work-Week-Escape-Anywhere/dp/0786158964%3FSubscriptionId%3D0525E2PQ81DD7ZTWTK82%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0786158964"&gt;Read more about this book...&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The first couple of days were hard, I had the itch. I kept wondering... &lt;strong&gt;&amp;quot;what if an emergency happens and someone needs to get a hold of me?&amp;quot;&lt;/strong&gt; There was no emergency, and the best part no shackles. When I finally checked my email, I spent 5 minutes scanning the email that seemed to contain &amp;quot;information&amp;quot; that was important to me. It was amazing how much &amp;quot;noise&amp;quot; I was able to filter out. This is something that Tim describes as a &lt;a href="http://www.fourhourworkweek.com/blog/category/low-information-diet-and-selective-ignorance/"&gt;&amp;quot;Low Information Diet.&amp;quot;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I&amp;#39;m toying with the idea of completely disconnecting my phone and I&amp;#39;m currently checking my email once a week (Mondays). &lt;/p&gt;  &lt;p&gt;I looked back at a post that made remarkable difference to me when I first read it last year. It was JP&amp;#39;s &lt;a href="http://blog.jpboodhoo.com/TipsForBecomingAMoreEffectiveDeveloper.aspx"&gt;tips on becoming a more effective developer&lt;/a&gt;. In it he told us to limit the amount of instant messaging that we do during the day. Today I feel that instant messaging has been replaced by mailing lists, twitter, texting and RSS feeds. All of this can consume a good portion of your day, and for me causes me to lose focus, quickly. It&amp;#39;s important to be selective about what information is important to keep you focused and to filter out what can wait. &lt;/p&gt;  &lt;p&gt;I&amp;#39;m not saying this is for everyone, but the Low Information Diet is working for me, and my daughter is loving the extra focused attention she gets from her daddy (likewise for her daddy).&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4343" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=ZvA5RK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=ZvA5RK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=R356ak"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=R356ak" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=qMA5yK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=qMA5yK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/356042114" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/mokhan/archive/tags/Books/default.aspx">Books</category><feedburner:origLink>http://www.lostechies.com/blogs/mokhan/archive/2008/08/04/disconnected-channels-of-communication.aspx</feedburner:origLink></item><item><title>Rails notes, from the .NET side</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/355949600/rails-notes-from-the-net-side.aspx</link><pubDate>Tue, 05 Aug 2008 03:09:10 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4341</guid><dc:creator>bogardj</dc:creator><slash:comments>12</slash:comments><description>&lt;p&gt;I&amp;#39;m starting a small experiment to try out Rails, and I&amp;#39;m keeping notes as I go.&amp;nbsp; From the first day, here&amp;#39;s what I have so far:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Rails easy to install&lt;/li&gt; &lt;ul&gt; &lt;li&gt;First MySQL (easy)&lt;/li&gt; &lt;li&gt;Ruby, another MSI and go&lt;/li&gt; &lt;li&gt;Rails - from gem&lt;/li&gt; &lt;li&gt;All easy to install, basically a one-click installer for all&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;gem installer is crazy nuts&lt;/li&gt; &lt;li&gt;Don&amp;#39;t forget to actually start the MySQL service (duh)&lt;/li&gt; &lt;li&gt;rake is also nuts&lt;/li&gt; &lt;ul&gt; &lt;li&gt;rake db:migrate is my friend&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;The generators trump ridiculous wizards any day of the week&lt;/li&gt; &lt;ul&gt; &lt;li&gt;We as .NET developers seem to be rather infatuated with projects and wizards&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Ridiculous how little code is needed&lt;/li&gt; &lt;li&gt;The Rails console script is nice to try things out (it&amp;#39;s an interactive Ruby session)&lt;/li&gt; &lt;li&gt;I can&amp;#39;t tell when I need to restart the console to pick up changes&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;On that last point, it&amp;#39;s unbelievably ridiculous how little code is needed.&amp;nbsp; When using Active Record, I don&amp;#39;t have to create properties and business as I would with any other ORM.&amp;nbsp; This comes from the ingenuity of Rails, combined with the power of Ruby.&amp;nbsp; Literally, an Active Record class could be just two lines of code, with as many properties as you wanted.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Convention over configuration, or &amp;quot;the Rails way&amp;quot;, took exactly 5 minutes to accept&lt;/li&gt; &lt;li&gt;e is a nice text editor, which I need to remember to pay for someday&lt;/li&gt; &lt;li&gt;I need to find a good color template for e&lt;/li&gt; &lt;li&gt;Remember when using e, to set the tab size to 2, and use soft tabs&lt;/li&gt; &lt;li&gt;I&amp;#39;m surprised how little I care that I&amp;#39;m not in VS when developing in Rails&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I&amp;#39;m pretty much primarily using VS right now for a handful of features:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Compile, run, debug&lt;/li&gt; &lt;li&gt;Host ReSharper, TD.NET and VisualSVN (thanks Jeremy for reminding me on the last two)&lt;/li&gt; &lt;li&gt;An explorer-ish window to see my files&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I got rid of all the other items quite a while back.&amp;nbsp; I don&amp;#39;t use the Toolbox window, the Server explorer, or the designer (the biggest waste).&lt;/p&gt; &lt;p&gt;I&amp;#39;m not trading my career for a Rails path, not by a long shot, but I&amp;#39;m really curious to see what other communities have come up with.&amp;nbsp; More soon...&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4341" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=HK9OMK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=HK9OMK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=6oLLQk"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=6oLLQk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=GynUlK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=GynUlK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/355949600" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/Rails/default.aspx">Rails</category><feedburner:origLink>http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/04/rails-notes-from-the-net-side.aspx</feedburner:origLink></item><item><title>Ooops...</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/355511788/ooops.aspx</link><pubDate>Mon, 04 Aug 2008 16:55:16 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4337</guid><dc:creator>mo.khan</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;a href="http://mrlasseter.blogspot.com/"&gt;Mike&lt;/a&gt; left a comment on my last post on &lt;a href="http://www.lostechies.com/blogs/mokhan/archive/2008/08/02/windows-forms-data-binding.aspx"&gt;Windows Forms Databinding&lt;/a&gt; asking:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;What do the tests look like?&lt;/p&gt;    &lt;p&gt;On the ComboBox binding, why aren&amp;#39;t you using adding the binding through DataBinding.Add?&amp;#160; With the way you have it now if you change the value the combobox is bound too it doesn&amp;#39;t get pushed back to the screen.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Well Mr. Mike, on the view implementation there were no tests... *hang my head in shame* Yup, we went at it trying to understand how Windows Forms Data bindings works, but if we had gone at it test first, we would have found that leveraging the built-in data bindings are not very testable. It requires having a BindingContext setup, and in some cases the controls have to actually be displayed for the bindings to actually kick in. Second, if we had gone test first, we would have noticed the issue the Mike brought up in regards to the ComboBox.&lt;/p&gt;  &lt;p&gt;Feeling a little guilty about publishing code that wasn&amp;#39;t well thought out, I decided to go at it again, with a test first approach. The test started off very high level. I knew the API that I wanted to work with, in this case a fluent interface for defining a binding to a control. The end result was quite different..&lt;/p&gt;  &lt;pre class="code"&gt;    [&lt;span&gt;Concern&lt;/span&gt;(&lt;span&gt;typeof&lt;/span&gt; (&lt;span&gt;Create&lt;/span&gt;))]
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;when_binding_a_property_from_an_object_to_a_combo_box&lt;/span&gt; : &lt;span&gt;context_spec&lt;/span&gt; {
        [&lt;span&gt;Test&lt;/span&gt;]
        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; should_initialize_the_combo_box_with_the_current_value_of_the_property() {
            combo_box.SelectedItem.should_be_equal_to(baby_girl);
        }

        &lt;span&gt;protected&lt;/span&gt; &lt;span&gt;override&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; under_these_conditions() {
            combo_box = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;ComboBox&lt;/span&gt;();
            thing_to_bind_to = Dependency&amp;lt;&lt;span&gt;IAnInterface&lt;/span&gt;&amp;gt;();
            baby_girl = Dependency&amp;lt;&lt;span&gt;IAnInterface&lt;/span&gt;&amp;gt;();
            baby_boy = Dependency&amp;lt;&lt;span&gt;IAnInterface&lt;/span&gt;&amp;gt;();

            combo_box.Items.Add(baby_boy);
            combo_box.Items.Add(baby_girl);

            thing_to_bind_to
                .setup_result_for(t =&amp;gt; t.Child)
                .Return(baby_girl);
        }

        &lt;span&gt;protected&lt;/span&gt; &lt;span&gt;override&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; because_of() {
            &lt;span&gt;Create
&lt;/span&gt;                .BindingFor(thing_to_bind_to)
                .BindToProperty(t =&amp;gt; t.Child)
                .BoundToControl(combo_box);
        }

        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;ComboBox&lt;/span&gt; combo_box;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;IAnInterface&lt;/span&gt; thing_to_bind_to;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;IAnInterface&lt;/span&gt; baby_girl;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;IAnInterface&lt;/span&gt; baby_boy;
    }&lt;/pre&gt;

&lt;p&gt;The end result doesn&amp;#39;t leverage the Windows Forms databindings at all. It registers event handlers for events on the controls.&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;ComboBoxPropertyBinding&lt;/span&gt;&amp;lt;TypeToBindTo, PropertyType&amp;gt; : &lt;span&gt;IPropertyBinding&lt;/span&gt;&amp;lt;PropertyType&amp;gt; {
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;IPropertyBinder&lt;/span&gt;&amp;lt;TypeToBindTo, PropertyType&amp;gt; binder;

        &lt;span&gt;public&lt;/span&gt; ComboBoxPropertyBinding(&lt;span&gt;ComboBox&lt;/span&gt; control, &lt;span&gt;IPropertyBinder&lt;/span&gt;&amp;lt;TypeToBindTo, PropertyType&amp;gt; binder) {
            &lt;span&gt;this&lt;/span&gt;.binder = binder;
            control.SelectedItem = binder.CurrentValue();
            control.SelectedIndexChanged +=
                &lt;span&gt;delegate&lt;/span&gt; { binder.ChangeValueOfPropertyTo(control.SelectedItem.ConvertedTo&amp;lt;PropertyType&amp;gt;()); };
        }

        &lt;span&gt;public&lt;/span&gt; PropertyType CurrentValue() {
            &lt;span&gt;return&lt;/span&gt; binder.CurrentValue();
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you&amp;#39;re interested in the rest of the source code download the source &lt;a href="http://mokhan.ca/blog/content/binary/notepad.net.2008.08.03.zip"&gt;here&lt;/a&gt;. The moral of the story... Don&amp;#39;t become complacent and take off your TDD hat, prematurely. In most cases it can, and should be, tested. Your design will probably come out much cleaner then going at the problem head on without tests to back you up. Not only that, but tests also give you extension points for making changes, and dealing with different contexts you probably wouldn&amp;#39;t have thought of right off the bat.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4337" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=dq9B6K"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=dq9B6K" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=F14Jkk"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=F14Jkk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=eDjufK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=eDjufK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/355511788" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/mokhan/archive/tags/Windows+Forms/default.aspx">Windows Forms</category><feedburner:origLink>http://www.lostechies.com/blogs/mokhan/archive/2008/08/04/ooops.aspx</feedburner:origLink></item><item><title>Query Objects with Repository Pattern Part 2</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/353359069/query-objects-with-repository-pattern-part-2.aspx</link><pubDate>Sat, 02 Aug 2008 06:29:27 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4303</guid><dc:creator>chadmyers</dc:creator><slash:comments>17</slash:comments><description>&lt;p&gt;As promised in &lt;a href="http://www.lostechies.com/blogs/chad_myers/archive/2008/08/01/query-objects-with-the-repository-pattern.aspx"&gt;my previous post&lt;/a&gt;, I’m going to make our query object a little more flexible and dynamic.&lt;/p&gt;  &lt;p&gt;First, this is what I really want to be able to do something like this:&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;var customers = repo.FindBy(
                &lt;span class="kwrd"&gt;new&lt;/span&gt; TopCustomersWithLowDiscountQuery()
                    .IncludePreferred()
                    .BelowDiscountThreshold(3)
                    .WithMoreSalesThan(500)
                    .As_BLOCKED EXPRESSION
);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Better yet, maybe even something like this:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;var customers = repo.FindBy(
        &lt;span class="kwrd"&gt;new&lt;/span&gt; TopCustomersWithLowDiscountQuery()
            .IncludePreferred()
            .BelowDiscountThreshold(3)
            .WithMoreSalesThan(500)
        .AndAlso(
            &lt;span class="kwrd"&gt;new&lt;/span&gt; DeliquentCustomersQuery()
                .WithDebtOver(9999))
            
);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Strongly typed query objects, completely testable outside of the repository, chain-able together with other like-typed query objects using AndAlso or OrElse.&lt;/p&gt;

&lt;p&gt;Ok, now how do we do it?&lt;/p&gt;

&lt;h2&gt;Expression Tree Helper (Naive)&lt;/h2&gt;

&lt;p&gt;First, I started with an extension method class to make dealing with some of the Expression&amp;lt;Func&amp;lt;ENTITY, bool&amp;gt;&amp;gt; expressions (which can get old to type) easier.&amp;#160; What I needed was the ability to take two expressions and AndAlso or OrElse them together.&amp;#160; AndAlso (&amp;amp;&amp;amp;) and OrElse (||) are both binary expressions represented by the BinaryExpression class in System.Linq.Expressions.&amp;#160; You can combine two expressions together with any binary expression type by using the Expression.MakeBinary() method.&amp;#160; One problem though is that both Expressions start with a different parameter (i.e. the ‘c’ in (c=&amp;gt;c.AnnualSales &amp;gt; 999)).&amp;#160; So you can’t just join them together because, unfortunately, the Expression Tree compiler will get the ‘c’ parameters jumbled and it won’t work. &lt;/p&gt;

&lt;p&gt;The way I found to deal with this problem is to basically wrap an ‘Invoke()’ expression around the other lambda using the first lambda’s parameter.&amp;#160; In C# parlance, it’s the difference between these two things:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;c =&amp;gt; c.AnnualSales &amp;gt; 99 &amp;amp;&amp;amp; c.Discount &amp;lt; 4
    -- VS.--
c =&amp;gt; c.AnnualSales &amp;gt; 99 &amp;amp;&amp;amp; &lt;span class="kwrd"&gt;new&lt;/span&gt; Func&amp;lt;Customer, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;(x=&amp;gt; x.Discount &amp;lt; 4)(c));&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;See how the second one actually involves wrapping the other Lambda (.Discount &amp;lt; 4) with a function and then invokes it?&amp;#160; I’m not sure if that’s EXACTLY what goes on when you use Expression.Invoke(), but that’s what I like to tell myself when I’m working with Expression Trees. It also helps to keep me from ending up in the corner in the fetal position drooling and babbling incoherently which is, unfortunately, a frequent occurrence when dealing with Expression Trees.&lt;/p&gt;

&lt;p&gt;You may notice that I put the condition “naive” on this section. This is because my expression tree helper is very naive and doesn’t account for a lot of the crazy things you can do with Expression Trees. This means that you will probably bump against its limitations and have problems. Sorry in advance for this, but I’m stretching the limits of my knowledge here and doing well to write coherently about it. If you have problems, let me know and maybe we can work it out together.&lt;/p&gt;

&lt;p&gt;Anyhow, once you’ve invoked the other lambda, you can join them together with whatever binary expression you want, and then you have to re-wrap them in a Lambda again in order to continue working with it.&amp;#160; Without further ado, here’s my expression helper extension methods:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ExpressionHelpers
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; AndAlso&amp;lt;T&amp;gt;(
        &lt;span class="kwrd"&gt;this&lt;/span&gt; Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; left,
        Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; right)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; BinaryOnExpressions(left, ExpressionType.AndAlso, right);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; OrElse&amp;lt;T&amp;gt;(
        &lt;span class="kwrd"&gt;this&lt;/span&gt; Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; left,
        Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; right)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; BinaryOnExpressions(left, ExpressionType.OrElse, right);
    }
        

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; BinaryOnExpressions&amp;lt;T&amp;gt;(
        &lt;span class="kwrd"&gt;this&lt;/span&gt; Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; left,
        ExpressionType binaryType,
        Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; right)
    {
        &lt;span class="rem"&gt;// Invoke that lambda with my parameter and give me the bool back, KKTHX&lt;/span&gt;
        var rightInvoke = Expression.Invoke(right, left.Parameters.Cast&amp;lt;Expression&amp;gt;());

        &lt;span class="rem"&gt;// make a binary expression between the results (i.e. AndAlso(&amp;amp;&amp;amp;), OrElse(||), etc)&lt;/span&gt;
        var binExpression = Expression.MakeBinary(binaryType, left.Body, rightInvoke);

        &lt;span class="rem"&gt;// Wrap it in a lambda and send it back&lt;/span&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; Expression.Lambda&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt;(binExpression, left.Parameters);
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;With that out of the way, we can get on to the less complicated stuff which is chaining all these things together.&amp;#160; The next thing I did was to create a simple abstract base class for my query objects (I’m sure there’s a million better ways to do this, but to get things running, this was the simplest thing that worked for right now).&lt;/p&gt;

&lt;h2&gt;Query Base Class&lt;/h2&gt;

&lt;p&gt;The query base is quite simple, actually. It just shuffles around the expressions and provides some convenience methods for you to chain them together:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; QueryBase&amp;lt;ENTITY&amp;gt;
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; Expression&amp;lt;Func&amp;lt;ENTITY, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; _curExpression;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Expression&amp;lt;Func&amp;lt;ENTITY, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; AsBLOCKED EXPRESSION
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _curExpression;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Expression&amp;lt;Func&amp;lt;ENTITY, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; AndAlso(QueryBase&amp;lt;ENTITY&amp;gt; otherQuery)
    {
        AsBLOCKED EXPRESSION.AndAlso(otherQuery.AsBLOCKED EXPRESSION;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Expression&amp;lt;Func&amp;lt;ENTITY, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; OrElse(QueryBase&amp;lt;ENTITY&amp;gt; otherQuery)
    {
        AsBLOCKED EXPRESSION.OrElse(otherQuery.AsBLOCKED EXPRESSION;
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; addCriteria(Expression&amp;lt;Func&amp;lt;ENTITY, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; nextExpression)
    {
        _curExpression = (_curExpression == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
                            ? nextExpression
                            : _curExpression.AndAlso(nextExpression);
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;You can AND and OR two queries together (must be of the same type, for now).&amp;#160; Sub-classes can add their own expressions in a nice, easy-to-use lambda expression style.&lt;/p&gt;

&lt;h2&gt;Query Implementation&lt;/h2&gt;

&lt;p&gt;And now, on to one of the actual query classes. Remember our ridiculously named and implemented TopCustomersWithLowDiscountQuery from my last post?&amp;#160; Here it is in its more simplified form complete with Fluent-API bonus material:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TopCustomersWithLowDiscountQuery : QueryBase&amp;lt;Customer&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; TopCustomersWithLowDiscountQuery IncludePreferred()
    {
        addCriteria(c =&amp;gt; c.Preferred);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; TopCustomersWithLowDiscountQuery BelowDiscountThreshold(&lt;span class="kwrd"&gt;decimal&lt;/span&gt; discountThresh)
    {
        addCriteria(c =&amp;gt; c.Discount &amp;lt; discountThresh);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; TopCustomersWithLowDiscountQuery WithMoreSalesThan(&lt;span class="kwrd"&gt;int&lt;/span&gt; salesThresh)
    {
        addCriteria(c =&amp;gt; c.AnnualSales &amp;gt; salesThresh);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To use it, just chain the methods together. Consider this test case:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;[Test]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; low_discount_high_sales_customers_should_be_selected()
{
    _query = &lt;span class="kwrd"&gt;new&lt;/span&gt; TopCustomersWithLowDiscountQuery()
        .BelowDiscountThreshold(3)
        .WithMoreSalesThan(500);

    var high = 15m;
    var low = 1m;

    _customers.AddRange(&lt;span class="kwrd"&gt;new&lt;/span&gt;[]
    {
        &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 1, Discount = low, AnnualSales = 200},
        &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 2, Discount = low, AnnualSales = 800},
        &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 3, Discount = high, AnnualSales = 1000}
    });

    Results.Count().ShouldEqual(1);
    Results.ElementAt(0).Id.ShouldEqual(2);
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If you want to chain them together, just use the AndAlso or OrElse methods.&amp;#160; Consider this other test case which uses OrElse:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;[Test]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; preferred_customers_that_are_also_bad()
{
    _query = &lt;span class="kwrd"&gt;new&lt;/span&gt; TopCustomersWithLowDiscountQuery()
        .IncludePreferred();

    var otherQuery = &lt;span class="kwrd"&gt;new&lt;/span&gt; DeliquentCustomersQuery()
        .WithPendingLitigation()
        .WithDebtOver(999);

    _customers.AddRange(&lt;span class="kwrd"&gt;new&lt;/span&gt;[]
    {
        &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 1, Preferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;, PendingLitigation = &lt;span class="kwrd"&gt;false&lt;/span&gt;, OutstandingDebts = 4000},
        &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 2, Preferred = &lt;span class="kwrd"&gt;false&lt;/span&gt;},
        &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 3, Preferred = &lt;span class="kwrd"&gt;false&lt;/span&gt;,  PendingLitigation = &lt;span class="kwrd"&gt;true&lt;/span&gt;, OutstandingDebts = 4000}
    });

    var results = _customers
        .AsQueryable()
        .Where(_query.OrElse(otherQuery));

    results.Count().ShouldEqual(2);
    results.ElementAt(0).Id.ShouldEqual(1);
    results.ElementAt(1).Id.ShouldEqual(3);
}&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4303" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=mhToOK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=mhToOK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=B8tgck"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=B8tgck" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=kcbsdK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=kcbsdK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/353359069" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/chad_myers/archive/tags/LINQ/default.aspx">LINQ</category><feedburner:origLink>http://www.lostechies.com/blogs/chad_myers/archive/2008/08/02/query-objects-with-repository-pattern-part-2.aspx</feedburner:origLink></item><item><title>Windows Forms Data Binding</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/353333155/windows-forms-data-binding.aspx</link><pubDate>Sat, 02 Aug 2008 05:36:34 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4300</guid><dc:creator>mo.khan</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;A couple of weeks ago, &lt;a href="http://www.linkedin.com/pub/3/933/425"&gt;Adam&lt;/a&gt; and I were pairing on a new screen in a windows forms application. He started showing me some stuff that he had learned about windows forms data bindings. I showed him a little bit of what &lt;a href="http://jpboodhoo.com"&gt;JP&lt;/a&gt; tried to teach me, back in the &lt;a href="http://blog.jpboodhoo.com/NothinButNetAustinRecap.aspx"&gt;Austin Nothin&amp;#39; But .NET boot camp&lt;/a&gt;, about Expressions and we decided to try a different way of binding domain object to screen elements in our application. The following is a method on the view that&amp;#39;s invoked from a presenter. It&amp;#39;s given an object from our model to display. &lt;/p&gt;  &lt;pre class="code"&gt;        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Display(&lt;span&gt;IActionPlan&lt;/span&gt; actionPlan)
        {
            &lt;span&gt;Create
&lt;/span&gt;                .BindingFor(actionPlan)
                .BindToProperty(a =&amp;gt; a.RecommendedAction)
                .BoundToControl(uxRecommendedAction);

            &lt;span&gt;Create
&lt;/span&gt;                .BindingFor(actionPlan)
                .BindToProperty(a =&amp;gt; a.AccountablePerson)
                .BoundToControl(uxAccoutablePerson);

            &lt;span&gt;Create
&lt;/span&gt;                .BindingFor(actionPlan)
                .BindToProperty(a =&amp;gt; a.EstimatedCompletionDate)
                .BoundToControl(uxEstimatedCompletionDate);

            &lt;span&gt;Create
&lt;/span&gt;                .BindingFor(actionPlan)
                .BindToProperty(a =&amp;gt; a.EstimatedStartDate)
                .BoundToControl(uxEstimatedStartDate);

            &lt;span&gt;Create&lt;/span&gt;.BindingFor(actionPlan)
                .BindToProperty(a =&amp;gt; a.RequiredResources)
                .BoundToControl(uxResourcesRequired);

            &lt;span&gt;Create&lt;/span&gt;.BindingFor(actionPlan)
                .BindToProperty(a =&amp;gt; a.Priority)
                .BoundToControl(uxPriority);
        }&lt;/pre&gt;

&lt;p&gt;Each of our controls are prefixed with &amp;quot;ux&amp;quot;. What we did was bind different types of controls to property&amp;#39;s on the object to display. This immediately changed that state of the object as the user filled out information on the screen. The BindToPropery() method is given the property on the object to bind too. The following was the implementation we came up with.&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Create
&lt;/span&gt;    {
        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IBinding&lt;/span&gt;&amp;lt;T&amp;gt; BindingFor&amp;lt;T&amp;gt;(T object_to_bind_to)
        {
            &lt;span&gt;return&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;ControlBinder&lt;/span&gt;&amp;lt;T&amp;gt;(object_to_bind_to);
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;interface&lt;/span&gt; &lt;span&gt;IBinding&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt;
    {
        &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt; BindToProperty&amp;lt;T&amp;gt;(&lt;span&gt;Expression&lt;/span&gt;&amp;lt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;TypeToBindTo, T&amp;gt;&amp;gt; property_to_bind_to);
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;interface&lt;/span&gt; &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;TypeOfDomainObject&amp;gt;
    {
        &lt;span&gt;string&lt;/span&gt; NameOfTheProperty { &lt;span&gt;get&lt;/span&gt;; }
        TypeOfDomainObject InstanceToBindTo { &lt;span&gt;get&lt;/span&gt;; }
    }&lt;/pre&gt;

&lt;p&gt;The implementation of the BindToProperty method takes in an input argument of type Expression&amp;lt;Func&amp;lt;TypeOfDomainObject&amp;gt;&amp;gt;. This allows us to inspect the expression to parse out the name of the property the binding is for. It&amp;#39;s like treating code as data. The IControlBinder implements two interfaces. One that&amp;#39;s issued to client components (IBinding) which restricts what they can do with the type. (see above in the Create class) The second interface exposes enough information for extension methods to pull from to build bindings for specific windows forms controls.&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;interface&lt;/span&gt; &lt;span&gt;IControlBinder&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt; : &lt;span&gt;IBinding&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt;, &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt;
    {
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;ControlBinder&lt;/span&gt;&amp;lt;TypeOfDomainObject&amp;gt; : &lt;span&gt;IControlBinder&lt;/span&gt;&amp;lt;TypeOfDomainObject&amp;gt;
    {
        &lt;span&gt;public&lt;/span&gt; ControlBinder(TypeOfDomainObject instance_to_bind_to)
        {
            InstanceToBindTo = instance_to_bind_to;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;TypeOfDomainObject&amp;gt; BindToProperty&amp;lt;TypeOfPropertyToBindTo&amp;gt;(
            &lt;span&gt;Expression&lt;/span&gt;&amp;lt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;TypeOfDomainObject, TypeOfPropertyToBindTo&amp;gt;&amp;gt; property_to_bind_to)
        {
            &lt;span&gt;var&lt;/span&gt; expression = property_to_bind_to.Body &lt;span&gt;as&lt;/span&gt; &lt;span&gt;MemberExpression&lt;/span&gt;;
            NameOfTheProperty = expression.Member.Name;
            &lt;span&gt;return&lt;/span&gt; &lt;span&gt;this&lt;/span&gt;;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; NameOfTheProperty { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;private&lt;/span&gt; &lt;span&gt;set&lt;/span&gt;; }

        &lt;span&gt;public&lt;/span&gt; TypeOfDomainObject InstanceToBindTo { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;private&lt;/span&gt; &lt;span&gt;set&lt;/span&gt;; }
    }&lt;/pre&gt;

&lt;p&gt;The BoundToControl overloads were put into extension methods, allowing others to create new implementations of bindings without having to modify the Control binder itself. The extension methods....&lt;/p&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;ControlBindingExtensions&lt;/span&gt; {
        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IControlBinding&lt;/span&gt; BoundToControl&amp;lt;TypeOfDomainObject&amp;gt;(
            &lt;span&gt;this&lt;/span&gt; &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;TypeOfDomainObject&amp;gt; binder,
            &lt;span&gt;TextBox&lt;/span&gt; control) {
            &lt;span&gt;var&lt;/span&gt; property_binder = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;TextPropertyBinding&lt;/span&gt;&amp;lt;TypeOfDomainObject&amp;gt;(
                control,
                binder.NameOfTheProperty,
                binder.InstanceToBindTo);
            property_binder.Bind();
            &lt;span&gt;return&lt;/span&gt; property_binder;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IControlBinding&lt;/span&gt; BoundToControl&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;T&amp;gt; binder, &lt;span&gt;RichTextBox&lt;/span&gt; box1) {
            &lt;span&gt;var&lt;/span&gt; property_binder = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;TextPropertyBinding&lt;/span&gt;&amp;lt;T&amp;gt;(box1,
                                                             binder.NameOfTheProperty,
                                                             binder.InstanceToBindTo);
            property_binder.Bind();
            &lt;span&gt;return&lt;/span&gt; property_binder;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IControlBinding&lt;/span&gt; BoundToControl&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;T&amp;gt; binder, &lt;span&gt;ComboBox&lt;/span&gt; box1) {
            &lt;span&gt;var&lt;/span&gt; property_binder = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;ComboBoxBinding&lt;/span&gt;&amp;lt;T&amp;gt;(box1,
                                                         binder.NameOfTheProperty,
                                                         binder.InstanceToBindTo);
            property_binder.Bind();
            &lt;span&gt;return&lt;/span&gt; property_binder;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IControlBinding&lt;/span&gt; BoundToControl&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IBinder&lt;/span&gt;&amp;lt;T&amp;gt; binder, &lt;span&gt;DateTimePicker&lt;/span&gt; box1) {
            &lt;span&gt;var&lt;/span&gt; property_binder = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;DatePickerBinding&lt;/span&gt;&amp;lt;T&amp;gt;(box1,
                                                           binder.NameOfTheProperty,
                                                           binder.InstanceToBindTo);
            property_binder.Bind();
            &lt;span&gt;return&lt;/span&gt; property_binder;
        }
    }&lt;/pre&gt;

&lt;p&gt;For completeness... the control bindings...&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;TextPropertyBinding&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt; : &lt;span&gt;IControlBinding&lt;/span&gt; {
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;Control&lt;/span&gt; control_to_bind_to;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; name_of_the_propery_to_bind;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; TypeToBindTo instance_of_the_object_to_bind_to;

        &lt;span&gt;public&lt;/span&gt; TextPropertyBinding(
            &lt;span&gt;Control&lt;/span&gt; control_to_bind_to,
            &lt;span&gt;string&lt;/span&gt; name_of_the_propery_to_bind,
            TypeToBindTo instance_of_the_object_to_bind_to
            ) {
            &lt;span&gt;this&lt;/span&gt;.control_to_bind_to = control_to_bind_to;
            &lt;span&gt;this&lt;/span&gt;.name_of_the_propery_to_bind = name_of_the_propery_to_bind;
            &lt;span&gt;this&lt;/span&gt;.instance_of_the_object_to_bind_to = instance_of_the_object_to_bind_to;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Bind() {
            control_to_bind_to.DataBindings.Clear();
            control_to_bind_to.DataBindings.Add(
                &lt;span&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;,
                instance_of_the_object_to_bind_to,
                name_of_the_propery_to_bind);
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;ComboBoxBinding&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt; : &lt;span&gt;IControlBinding&lt;/span&gt; {
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;ComboBox&lt;/span&gt; control_to_bind_to;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; name_of_the_propery_to_bind;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; TypeToBindTo instance_of_the_object_to_bind_to;

        &lt;span&gt;public&lt;/span&gt; ComboBoxBinding(&lt;span&gt;ComboBox&lt;/span&gt; control_to_bind_to,
                               &lt;span&gt;string&lt;/span&gt; name_of_the_propery_to_bind,
                               TypeToBindTo instance_of_the_object_to_bind_to) {
            &lt;span&gt;this&lt;/span&gt;.control_to_bind_to = control_to_bind_to;
            &lt;span&gt;this&lt;/span&gt;.name_of_the_propery_to_bind = name_of_the_propery_to_bind;
            &lt;span&gt;this&lt;/span&gt;.instance_of_the_object_to_bind_to = instance_of_the_object_to_bind_to;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Bind() {
            control_to_bind_to.SelectedIndexChanged +=
                &lt;span&gt;delegate&lt;/span&gt; {
                    &lt;span&gt;typeof&lt;/span&gt; (TypeToBindTo)
                        .GetProperty(name_of_the_propery_to_bind)
                        .SetValue(
                        instance_of_the_object_to_bind_to,
                        control_to_bind_to.Items[control_to_bind_to.SelectedIndex],
                        &lt;span&gt;null&lt;/span&gt;);
                };
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;DatePickerBinding&lt;/span&gt;&amp;lt;TypeToBindTo&amp;gt; : &lt;span&gt;IControlBinding&lt;/span&gt; {
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;DateTimePicker&lt;/span&gt; control_to_bind_to;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; name_of_the_propery_to_bind;
        &lt;span&gt;private&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; TypeToBindTo instance_of_the_object_to_bind_to;

        &lt;span&gt;public&lt;/span&gt; DatePickerBinding(&lt;span&gt;DateTimePicker&lt;/span&gt; control_to_bind_to,
                                 &lt;span&gt;string&lt;/span&gt; name_of_the_propery_to_bind,
                                 TypeToBindTo instance_of_the_object_to_bind_to) {
            &lt;span&gt;this&lt;/span&gt;.control_to_bind_to = control_to_bind_to;
            &lt;span&gt;this&lt;/span&gt;.name_of_the_propery_to_bind = name_of_the_propery_to_bind;
            &lt;span&gt;this&lt;/span&gt;.instance_of_the_object_to_bind_to = instance_of_the_object_to_bind_to;
        }

        &lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Bind() {
            control_to_bind_to.DataBindings.Clear();
            control_to_bind_to.DataBindings.Add(
                &lt;span&gt;&amp;quot;Value&amp;quot;&lt;/span&gt;,
                instance_of_the_object_to_bind_to,
                name_of_the_propery_to_bind);
        }
    }&lt;/pre&gt;

&lt;p&gt;We found that using the fluent interface for creating bindings was pretty easy and made screen synchronization a breeze, however, our implementation wasn&amp;#39;t the easiest thing to test. So far it&amp;#39;s been good to us. &lt;/p&gt;

&lt;p&gt;As a side note... go &lt;a href="http://www.jpboodhoo.com/training.oo"&gt;register for the Las Vegas course&lt;/a&gt;, it may cause you to love your job! Also, if you&amp;#39;ve already attended a boot camp, and you think you already know what the course is about, you have no idea, it keeps getting &lt;a href="http://mokhan.ca/blog/2007/11/13/Photos+From+The+Nothin+But+NET+Boot+Camp.aspx"&gt;better&lt;/a&gt; and &lt;a href="http://mokhan.ca/blog/2008/04/14/Nothin+But+NET+Retrospective+Austin+Texas+Style.aspx"&gt;better&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4300" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=JzfpzK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=JzfpzK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=fq43tk"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=fq43tk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=aBzLjK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=aBzLjK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/353333155" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/mokhan/archive/tags/Windows+Forms/default.aspx">Windows Forms</category><feedburner:origLink>http://www.lostechies.com/blogs/mokhan/archive/2008/08/01/windows-forms-data-binding.aspx</feedburner:origLink></item><item><title>Query Objects with the Repository Pattern</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/353278576/query-objects-with-the-repository-pattern.aspx</link><pubDate>Sat, 02 Aug 2008 04:17:46 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4299</guid><dc:creator>chadmyers</dc:creator><slash:comments>7</slash:comments><description>&lt;p&gt;&lt;a href="http://kohari.org/"&gt;Nate Kohari&lt;/a&gt; (whose primate brain is far too small to comprehend this post [inside joke, he’s actually really sharp]) was asking on Twitter today about how to structure his repositories: Per aggregate root, Per entity, or just one repository for everything?&lt;/p&gt;  &lt;p&gt;The two suggestions that rose to the top were the per-aggregate and one repository approaches. Jeremy and I use a the one repository approach at work and it’s working well for us so far.&amp;#160; The was one compelling argument for the per-aggregate repository and that is that you could encapsulate your aggregate-specific queries inside your aggregate-specific repositories.&lt;/p&gt;  &lt;p&gt;Thinking about this a little more, I felt it had a little smell to it, namely the potential for business logic (also known as ‘where’ clauses) to creep into the repository which would be &lt;a href="http://en.wikipedia.org/wiki/Proton_pack#Crossing_the_Streams"&gt;bad&lt;/a&gt;. Perhaps a better alternative would be to encapsulate the specific logic of a given query into an object. You could then have this object produce something that the repository could (blindly, decoupled) use to query on. &lt;/p&gt;  &lt;p&gt;This approach allows you to maintain the one repository approach, yet still have encapsulated domain-specific queries. Plus, you can test your queries independently of the repository which is a huge benefit.&lt;/p&gt;  &lt;h2&gt;Creating and Passing Around Expression Trees&lt;/h2&gt;  &lt;p&gt;What might the query object produce that the repository could blindly use?&amp;#160; In the .NET 3.5 world, you could use expression trees!&amp;#160; These come in quite handy and play well with LINQ-style (IQueryable) behavior of Linq2NHibernate, Linq2Objects, and Linq2JustAboutAnythingThatInvolvesAListOfThings.&lt;/p&gt;  &lt;p&gt;Producing an expression tree is actually quite simple:&amp;#160; Have a method or property that returns Expression&amp;lt;Func&amp;lt;T,bool&amp;gt;&amp;gt;. Yep. That’s it. We can all go home now.&amp;#160; In case you actually wanted to see some code, here’s a simple, extremely naive example of what I mean. Let’s say we wanted to find all our customers that have significant sales with our company, but whose discount is really low (for some reason).&amp;#160; We’d like to find these customers and give them a better discount since they do so much business with us. Your query probably (wouldn’t) look something like this:&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TopCustomersWithLowDiscountQuery
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IncludePreferred { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt; DiscountThreshold { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; SalesThreshold { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Expression&amp;lt;Func&amp;lt;Customer, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; AsBLOCKED EXPRESSION
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (c =&amp;gt;
                c.Preferred == IncludePreferred
                &amp;amp;&amp;amp; c.Discount &amp;lt; DiscountThreshold
                &amp;amp;&amp;amp; c.AnnualSales &amp;gt; SalesThreshold);
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Using this query object, we can still get all the benefits of delayed execution that LINQ/IQueryable provides us without forcing us to sprinkle little surprises of business logic everywhere in LINQ queries.&lt;/p&gt;

&lt;h2&gt;Using the Expression Tree to Query&lt;/h2&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;To actually use the expression tree, you can call the Where() method on any IEnumerable&amp;lt;T&amp;gt; or IQueryable&amp;lt;T&amp;gt;.&amp;#160; Consider this silly/contrived example:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;var query = &lt;span class="kwrd"&gt;new&lt;/span&gt; TopCustomersWithLowDiscountQuery
{
    IncludePreferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;,
    DiscountThreshold = 3,
    SalesThreshold = 100000
};

var customerList = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Customer&amp;gt;
{
    &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 1, Preferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;},
    &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 2, Preferred = &lt;span class="kwrd"&gt;false&lt;/span&gt;},
    &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 3, Preferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;}
};

var filteredCustomers = 
    customerList
        .AsQueryable()
        .Where(query.AsBLOCKED EXPRESSION;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Once GetEnumerator() is called on filteredCustomers, the magic happens and you’ll end up with an IEnumerable&amp;lt;T&amp;gt; that has only 2 elements in id (ID=1, and ID=3).&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;Full Code Example&lt;/h2&gt;

&lt;p&gt;Here’s the full code of the test fixture I was using for these examples. Yes, I know it’s not very realistic and there are lots of potential problems with the logic in the query object, but the point was to illustrate how you might go about encapsulating LINQ where clauses, so please go easy on me :)&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;[TestFixture]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TopCustomersWithLowDiscountQueryTester
    {
        [Test]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; preferred_customers_should_be_selected_when_IncludePreferred_is_true()
        {
            _customers.AddRange(&lt;span class="kwrd"&gt;new&lt;/span&gt;[]
            {
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 1, Preferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;},
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 2, Preferred = &lt;span class="kwrd"&gt;false&lt;/span&gt;},
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 3, Preferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;}
            });

            Results.Count().ShouldEqual(2);
            Results.ElementAt(0).Id.ShouldEqual(1);
            Results.ElementAt(1).Id.ShouldEqual(3);
        }

        [Test]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; high_discount_customers_should_not_be_selected()
        {
            _query.IncludePreferred = &lt;span class="kwrd"&gt;false&lt;/span&gt;;

            var high = 15m;
            var low = 1m;

            _customers.AddRange(&lt;span class="kwrd"&gt;new&lt;/span&gt;[]
            {
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 1, Discount = high},
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 2, Discount = low},
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {Id = 3, Discount = high}
            });

            Results.Count().ShouldEqual(1);
            Results.ElementAt(0).Id.ShouldEqual(2);
        }

        [SetUp]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetUp()
        {
            _query = &lt;span class="kwrd"&gt;new&lt;/span&gt; TopCustomersWithLowDiscountQuery
            {
                IncludePreferred = &lt;span class="kwrd"&gt;true&lt;/span&gt;,
                DiscountThreshold = 3,
                SalesThreshold = 100000
            };

            _customers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Customer&amp;gt;();
            _resultsCached = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
        }

        &lt;span class="kwrd"&gt;private&lt;/span&gt; TopCustomersWithLowDiscountQuery _query;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; List&amp;lt;Customer&amp;gt; _customers;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; IEnumerable&amp;lt;Customer&amp;gt; _resultsCached;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Customer&amp;gt; Results
        {
            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _resultsCached ?? _customers.AsQueryable().Where(_query.AsBLOCKED EXPRESSION; }
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SpecificationExtensions
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ShouldEqual(&lt;span class="kwrd"&gt;this&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; actual, &lt;span class="kwrd"&gt;object&lt;/span&gt; expected)
        {
            Assert.AreEqual(actual, expected);
        }
    }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Coming up…&lt;/h2&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Stay tuned: I’m going to do a follow-on post on how you can make the query object code a little more elegant, as well as chain them together with AndAlso and OrElse expressions.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4299" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=1oBrlK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=1oBrlK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=ahpYnk"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=ahpYnk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=hg8vxK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=hg8vxK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/353278576" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/chad_myers/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://www.lostechies.com/blogs/chad_myers/archive/tags/Repositories/default.aspx">Repositories</category><feedburner:origLink>http://www.lostechies.com/blogs/chad_myers/archive/2008/08/01/query-objects-with-the-repository-pattern.aspx</feedburner:origLink></item><item><title>StuctureMap: Advanced-level Usage Scenarios (Part 1: Type/Convention Scanners)</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/353207943/stucturemap-advanced-level-usage-scenarios-part-1-type-convention-scanners.aspx</link><pubDate>Sat, 02 Aug 2008 01:51:58 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4298</guid><dc:creator>chadmyers</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;I’ll start with my strong hand on the advanced StructureMap-foo and go straight to the type scanners (a.k.a. convention scanners).&amp;#160; Thankfully, there was &lt;a href="http://groups.google.com/group/structuremap-users/browse_thread/thread/5f3c5c6824d380ad"&gt;a thread&lt;/a&gt; on the &lt;a href="http://groups.google.com/group/structuremap-users"&gt;StructureMap Users mailing list&lt;/a&gt; with just the kind of problem these things solve. I’ll use that problem as the strawman for this post.&lt;/p&gt;  &lt;h2&gt;The Problem&lt;/h2&gt;  &lt;p&gt;Let’s say you had an IRepository&amp;lt;T&amp;gt; interface implemented by an abstract base class (RepositoryBase&amp;lt;T&amp;gt;) which, in turn, is implemented by many classes – one for each entity.&amp;#160; Let’s say your implementations looked something like this:&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IRepository&amp;lt;T&amp;gt;
{
    &lt;span class="rem"&gt;// intersting stuff here    &lt;/span&gt;
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RepositoryBase&amp;lt;T&amp;gt; : IRepository&amp;lt;T&amp;gt;
{
    &lt;span class="rem"&gt;// interesting abstract base stuff here&lt;/span&gt;
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; UserRepository : RepositoryBase&amp;lt;User&amp;gt;
{
        &lt;span class="rem"&gt;// interesting abstract base stuff here&lt;/span&gt;
}

// ... a bunch more of these CustomerRepository, SaleRepository, etc&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And you you may add more in the future and you’d rather not have to register them all individually in StructureMap like this:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// You don&amp;#39;t have to do this!&lt;/span&gt;
StructureMapConfiguration
    .ForRequestedType&amp;lt;IRepositor&amp;lt;User&amp;gt;&amp;gt;.TheDefaultIsConcreteType&amp;lt;UserRepository&amp;gt;()
    .ForRequestedType&amp;lt;IRepositor&amp;lt;Customer&amp;gt;&amp;gt;.TheDefaultIsConcreteType&amp;lt;CustomerRepository&amp;gt;()
    .ForRequestedType&amp;lt;IRepositor&amp;lt;Sale&amp;gt;&amp;gt;.TheDefaultIsConcreteType&amp;lt;SaleRepository&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;What would be nice is if StructureMap could automatically figure my convention out. Well, unfortunately it won’t do that. BUT you can give it enough of a hint so that it CAN do it using your own ITypeScanner implementation. ITypeScanner has one method on it: Process(Type, Registry).&amp;#160; StructureMap will call Process() for each Type it finds when scanning (using ScanAssemblies).&amp;#160; Your scanner can, if it wants, add things to the container (through the ‘Registry’ parameter).&amp;#160; This allows you to do all sorts of cool things like automatically find any types Foo which implement an interface named IFoo (or Bar/IBar, Something/ISomething, etc) and register them for you.&amp;#160; In fact, this last example is done for you using &lt;a href="http://structuremap.svn.sourceforge.net/viewvc/structuremap/trunk/Source/StructureMap/Graph/ITypeScanner.cs?view=markup"&gt;StructureMap.Graph.DefaultConventionScanner&lt;/a&gt;. Here, let me show you:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;ScanAssemblies().IncludeTheCallingAssembly()
    .IncludeTheCallingAssembly()
    .With&amp;lt;DefaultConventionScanner&amp;gt;()&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Implementing our Repository Convention ITypeScanner&lt;/h2&gt;

&lt;p&gt;First, create a class that implements ITypeScanner. Let’s call it RepositoryConvention.&lt;/p&gt;

&lt;p&gt;The next thing we’ll need is a method that will, given a type, see if it’s the type of generic type we’re looking for (i.e. RepositoryBase&amp;lt;T&amp;gt;), and return what the T parameter is (i.e. &amp;lt;User&amp;gt;). With this, we can register an IRepository&amp;lt;User&amp;gt; in StructureMap for that class (UserRepository, for example). It might look something like this:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Type GetGenericParamFor(Type typeToInspect, Type genericType)
{
    var baseType = typeToInspect.BaseType;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (baseType != &lt;span class="kwrd"&gt;null&lt;/span&gt;
        &amp;amp;&amp;amp; baseType.IsGenericType
        &amp;amp;&amp;amp; baseType.GetGenericTypeDefinition().Equals(genericType))
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; baseType.GetGenericArguments()[0];
    }

    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;p&gt;This will take a given type (i.e. UserRepository) and check it’s base type (i.e. RepositoryBase&amp;lt;User&amp;gt;) to see if it’s generic and if it’s the generic type we’re looking for. If so, it’ll return the “User” portion of RepositorBase&amp;lt;User&amp;gt;, for example.&lt;/p&gt;

  &lt;p&gt;Now, our process method looks something like:&lt;/p&gt;

  &lt;div class="csharpcode-wrapper"&gt;
    &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Process(Type type, Registry registry)
{
    Type entityType = GetGenericParamFor(type, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(RepositoryBase&amp;lt;&amp;gt;));

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (entityType != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
    {
        var genType = &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IRepository&amp;lt;&amp;gt;).MakeGenericType(entityType);
        registry.ForRequestedType(genType).AddInstance(&lt;span class="kwrd"&gt;new&lt;/span&gt; ConfiguredInstance(type));
    }
}&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;We try to get the entityType (i.e. User). If present, then we create a new specific type from the generic IRepository&amp;lt;T&amp;gt; and register it. In our UserRepository case, it’ll register IRepository&amp;lt;User&amp;gt; with the default concrete type of UserRepository. Our whole class now looks like this:&lt;/p&gt;

  &lt;div class="csharpcode-wrapper"&gt;
    &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RepositoryConvention : ITypeScanner
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Process(Type type, Registry registry)
    {
        Type entityType = GetGenericParamFor(type, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(RepositoryBase&amp;lt;&amp;gt;));

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (entityType != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            var genType = &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IRepository&amp;lt;&amp;gt;).MakeGenericType(entityType);
            registry.ForRequestedType(genType).AddInstance(&lt;span class="kwrd"&gt;new&lt;/span&gt; ConfiguredInstance(type));
        }
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Type GetGenericParamFor(Type typeToInspect, Type genericType)
    {
        var baseType = typeToInspect.BaseType;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (baseType != &lt;span class="kwrd"&gt;null&lt;/span&gt;
            &amp;amp;&amp;amp; baseType.IsGenericType
            &amp;amp;&amp;amp; baseType.GetGenericTypeDefinition().Equals(genericType))
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; baseType.GetGenericArguments()[0];
        }

        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    }
}&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;Now, we need to let StructureMap know about it…&lt;/p&gt;

  &lt;h2&gt;Configuring StructureMap with the new Scanner&lt;/h2&gt;

  &lt;p&gt;Now, somewhere in your bootstrapping/startup routines for your app, add the With() call to your ScanAssemblies configuration. It should end up looking like this:&lt;/p&gt;

  &lt;div class="csharpcode-wrapper"&gt;
    &lt;pre class="csharpcode"&gt;StructureMapConfiguration
   .ScanAssemblies()
   .IncludeTheCallingAssembly()
    &lt;span class="rem"&gt;// maybe other assemblies, too?&lt;/span&gt;
   .With(&lt;span class="kwrd"&gt;new&lt;/span&gt; RepositoryConvention());  // add our convention &lt;span class="kwrd"&gt;for&lt;/span&gt; wiring up the repos&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;As StructureMap mines through all the types in your assemblies, it’ll pass them to your Process() method where you can evaluate whether they meet your Repository convention and register them accordingly.&lt;/p&gt;

  &lt;h2&gt;Using the Repository&lt;/h2&gt;

  &lt;p&gt;Finally, to grab one of your newly configured repositories, you can simple make a normal ObjectFactory GetInstance call:&lt;/p&gt;

  &lt;div class="csharpcode-wrapper"&gt;
    &lt;pre class="csharpcode"&gt;var userRepo = ObjectFactory.GetInstance&amp;lt;IRepository&amp;lt;User&amp;gt;&amp;gt;();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Also, it’ll work with for normal constructor injection scenarios. For example:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SomeService
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; IRepository&amp;lt;User&amp;gt; _userRepo;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; SomeService(IRepository&amp;lt;User&amp;gt; userRepo)
    {
        _userRepo = userRepo;
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;What else can you do?&lt;/h2&gt;

&lt;p&gt;I hope as you come up with clever uses for this convention technique you let us know about them!&amp;#160; As a teaser, here’s something Jeremy and I are doing:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ControllerConvention : TypeRules, ITypeScanner
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Process(Type type, Registry registry)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (CanBeCast(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (IController), type))
        {
            &lt;span class="kwrd"&gt;string&lt;/span&gt; name = type.Name.Replace(&lt;span class="str"&gt;&amp;quot;Controller&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;).ToLower();
            registry.AddInstanceOf(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (IController), &lt;span class="kwrd"&gt;new&lt;/span&gt; ConfiguredInstance(type).WithName(name));
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;See if you can guess what that’s for… ;)&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4298" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=e9dkiK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=e9dkiK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=wJ9O9k"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=wJ9O9k" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=GF7stK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=GF7stK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/353207943" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/chad_myers/archive/tags/StructureMap/default.aspx">StructureMap</category><feedburner:origLink>http://www.lostechies.com/blogs/chad_myers/archive/2008/08/01/stucturemap-advanced-level-usage-scenarios-part-1-type-convention-scanners.aspx</feedburner:origLink></item><item><title>Recursive Command</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/352737408/recursive-command.aspx</link><pubDate>Fri, 01 Aug 2008 15:38:55 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4297</guid><dc:creator>mo.khan</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note&lt;/b&gt;: this entry &lt;a title="New location at Clarius" href="http://mokhan.ca/blog/2008/08/01/Recursive+Command.aspx"&gt;has moved&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;When building up a tree view that represents the directory structure of a file system, like the windows explorer, my first reaction was to use recursion to traverse the file system and build up a tree. I quickly found that doing something like that is a time consuming process, and required some optimization. &lt;/p&gt;  &lt;p&gt;I came up with what I like to call the recursive command. Each Tree Node item on a tree view is bound to a command to execute. The command looks like this...&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ITreeNodeClickedCommand &lt;/span&gt;{
    &lt;span style="color:blue;"&gt;void &lt;/span&gt;Execute(&lt;span style="color:#2b91af;"&gt;ITreeNode &lt;/span&gt;node);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;When the command is executed, the command gets an opportunity to modify the state of the tree node that was clicked. In this case I wanted to lazy load the sub directories of a node that was clicked. The command implementation looks like this...&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IAddFoldersCommand &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ITreeNodeClickedCommand &lt;/span&gt;{}

&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;AddFoldersCommand &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IAddFoldersCommand &lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DirectoryInfo &lt;/span&gt;the_current_directory;
    &lt;span style="color:blue;"&gt;private bool &lt;/span&gt;has_executed;

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;AddFoldersCommand(&lt;span style="color:#2b91af;"&gt;DirectoryInfo &lt;/span&gt;the_current_directory) {
        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.the_current_directory = the_current_directory;
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Execute(&lt;span style="color:#2b91af;"&gt;ITreeNode &lt;/span&gt;node) {
        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!has_executed) {
            &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;directory &lt;span style="color:blue;"&gt;in &lt;/span&gt;the_current_directory.GetDirectories()) {
                node.Add(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;TreeNodeItem&lt;/span&gt;(directory.Name, &lt;span style="color:#2b91af;"&gt;ApplicationIcons&lt;/span&gt;.Folder, &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;AddFoldersCommand&lt;/span&gt;(directory)));
            }
        }
        has_executed = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This command is executed each time the tree node that it is bound too is clicked, but will only build up the child tree node items once. Each of the child tree nodes are bound to a new instance of the same command. Hence, what I like to call the recursive command.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/RecursiveCommand_8561/recursive_command_2.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="484" alt="recursive_command" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/RecursiveCommand_8561/recursive_command_thumb.png" width="628" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h6&gt;&lt;/h6&gt;

&lt;p&gt;For more information on the command pattern check out &lt;a href="http://en.wikipedia.org/wiki/Command_pattern"&gt;WikiPedia&amp;#39;s write up&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4297" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=NUwetK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=NUwetK" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=WRGgHk"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=WRGgHk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=LxWtDK"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=LxWtDK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/352737408" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/mokhan/archive/tags/Design+Patterns/default.aspx">Design Patterns</category><feedburner:origLink>http://www.lostechies.com/blogs/mokhan/archive/2008/08/01/recursive-command.aspx</feedburner:origLink></item><item><title>Parents, talk to your kids about Linux...</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/352260258/parents-talk-to-your-kids-about-linux.aspx</link><pubDate>Fri, 01 Aug 2008 03:58:02 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4294</guid><dc:creator>Jason Meridth</dc:creator><slash:comments>0</slash:comments><description>Saw this today on &lt;a href="http://xkcd.com/456/" target="_blank"&gt;xkcd.com&lt;/a&gt;:

I wish this wasn&amp;#39;t true, but I&amp;#39;ve seen it happen.

&lt;img src="http://imgs.xkcd.com/comics/cautionary.png" title="This really is a true story, and she doesn&amp;#39;t know I put it in my comic because her wifi hasn&amp;#39;t worked for weeks." alt="Cautionary" border="0" /&gt;
&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4294" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=tsWd0J"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=tsWd0J" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=FWUsJj"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=FWUsJj" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=hVUm7J"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=hVUm7J" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/352260258" height="1" width="1"/&gt;</description><feedburner:origLink>http://www.lostechies.com/blogs/jason_meridth/archive/2008/07/31/parents-talk-to-your-kids-about-linux.aspx</feedburner:origLink></item><item><title>Trusted Subsystem, WCF and IIS 5 - revisited</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/352154176/trusted-subsystem-wcf-and-iis-5-revisited.aspx</link><pubDate>Fri, 01 Aug 2008 01:26:00 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4291</guid><dc:creator>bogardj</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;In my last post, I tried to get the following scenario to work:&lt;/p&gt; &lt;p&gt;&lt;img src="http://grabbagoftimg.s3.amazonaws.com/trusted_subsystem.PNG" alt="" /&gt;&lt;/p&gt; &lt;p&gt;One thing I &lt;em&gt;didn&amp;#39;t&lt;/em&gt; add was that I&amp;#39;m running IIS in Windows XP, in IIS 5.&amp;nbsp; In this &lt;a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=Intranet%20%u2013%20Web%20to%20Remote%20WCF%20Using%20Transport%20Security%20%28Trusted%20Subsystem%2c%20HTTP%29&amp;amp;referringTitle=Application%20Scenarios"&gt;article on CodePlex&lt;/a&gt;, which I originally modeled my solution after and a couple of folks pointed out, it recommends setting both the ASP.NET identity (outside the Trusted Zone) and the App Pool identity to the Service account identity.&lt;/p&gt; &lt;p&gt;Now, in my case, I don&amp;#39;t care about who calls my service, nor do I want any requirement on their side to provide any transport security.&amp;nbsp; The article also recommends this, but I don&amp;#39;t need it.&lt;/p&gt; &lt;p&gt;The real problem came from me running in IIS 5.&amp;nbsp; In IIS 5, the aspnet_wp process runs as MACHINE\ASPNET, which examining WindowsIdentity.GetCurrent() confirmed.&amp;nbsp; Even though I set the identity in IIS to use the service account as anonymous user, WCF &lt;strong&gt;by default&lt;/strong&gt; uses the process identity, not any other identity.&amp;nbsp; WCF has no knowledge of the host environment, by default, so it doesn&amp;#39;t know it&amp;#39;s hosted in ASP.NET or IIS normally.&lt;/p&gt; &lt;p&gt;To do a Trusted Subsystem model under IIS 5, I had to make the following changes:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Set the anonymous account to the service account, turn off any other security (like Integrated Windows Security)&lt;/li&gt; &lt;li&gt;Turn on impersonation in the web.config (&amp;lt;identity impersonate=&amp;quot;true&amp;quot; /&amp;gt;)&lt;/li&gt; &lt;li&gt;Turn on ASP.NET compatibility for the service hosting environment and each individual service&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The last part is critical, and detailed in &lt;a href="http://msdn.microsoft.com/en-us/library/aa702682.aspx"&gt;this MSDN article&lt;/a&gt;.&amp;nbsp; I&amp;#39;ve had to do that before, when designing JSON services for an AJAX application, where the services needed access to HttpContext.Session.&lt;/p&gt; &lt;p&gt;Once ASP.NET compatibility is turned on, WCF requests now go through the ASP.NET pipeline, which means the normal ASP.NET impersonation model.&amp;nbsp; From the article:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;WCF services run using the current identity of the ASP.NET impersonated thread, which may be different than the IIS process identity if ASP.NET impersonation has been enabled for the application.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;With the full ASP.NET impersonation set up, and the correct identity set up in IIS, WCF could now take the identity of the ASP.NET impersonated thread.&amp;nbsp; Even though Thread.CurrentPrincipal reflected the service identity, WCF won&amp;#39;t use it unless I set up the compatibility mode.&lt;/p&gt; &lt;p&gt;Of course, if I had been developing in a Workstation 2008 machine, or something with IIS 6 or 7, this wouldn&amp;#39;t be a problem.&amp;nbsp; Setting the App Pool identity is the process identity.&amp;nbsp; In IIS 5, the identity is configured elsewhere.&amp;nbsp; In any case, it&amp;#39;s all working now locally.&amp;nbsp; I guess this means it&amp;#39;s finally time to upgrade to Workstation 2008.&lt;/p&gt; &lt;p&gt;Thanks to everyone for all the pointers!&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4291" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=dA3RQJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=dA3RQJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=RHMyXj"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=RHMyXj" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=EOoMUJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=EOoMUJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/352154176" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/WCF/default.aspx">WCF</category><feedburner:origLink>http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/07/31/trusted-subsystem-wcf-and-iis-5-revisited.aspx</feedburner:origLink></item><item><title>Trusted Subsystem, WCF and IIS</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/351175267/trusted-subsystem-wcf-and-iis.aspx</link><pubDate>Thu, 31 Jul 2008 03:43:39 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4282</guid><dc:creator>bogardj</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;I&amp;#39;ve just about pulled my hair out on this one.&amp;nbsp; This used to be very easy with ASMX:&lt;/p&gt; &lt;p&gt;&lt;img src="http://grabbagoftimg.s3.amazonaws.com/trusted_subsystem.PNG" alt="" /&gt; &lt;/p&gt; &lt;p&gt;Basically, I have IIS running as a trusted user, &amp;quot;Service&amp;quot;.&amp;nbsp; I want WCF to run as this user for connecting to databases, etc.&amp;nbsp; I don&amp;#39;t care who&amp;#39;s calling me, I&amp;#39;m in an intranet environment, and this service is open to the world.&amp;nbsp; Unfortunately, all I can ever get is &amp;quot;user not associated with a trusted connection&amp;quot;, no matter what I do.&amp;nbsp; I suspect it&amp;#39;s due to a network hop issue, or something similar.&amp;nbsp; I can get it to work by flowing identity down, but I don&amp;#39;t want to do that, it&amp;#39;s not Trusted Subsystem.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve spent about a day on two separate occasions trying to get this to work, but all examples seem to force me to set the service account on the ASP.NET side.&amp;nbsp; But I don&amp;#39;t want to force clients to do any kind of security, that defeats the purpose.&lt;/p&gt; &lt;p&gt;The quickest way to Trusted Subsystem now is to use SQL Server authentication.&amp;nbsp; With ASMX, I used ASP.NET configuration, along with IIS security configuration to set the identity, and it worked just fine.&amp;nbsp; WCF works outside the ASP.NET stack, so I don&amp;#39;t have that luxury.&amp;nbsp; Security in WCF is tough, kids, don&amp;#39;t let anyone tell you any different.&lt;/p&gt; &lt;p&gt;Boo.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4282" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=VKK0lJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=VKK0lJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=PVoFWj"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=PVoFWj" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=LOiMiJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=LOiMiJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/351175267" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/WCF/default.aspx">WCF</category><feedburner:origLink>http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/07/30/trusted-subsystem-wcf-and-iis.aspx</feedburner:origLink></item><item><title>Integrating StructureMap with WCF</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/350132633/integrating-structuremap-with-wcf.aspx</link><pubDate>Wed, 30 Jul 2008 03:44:00 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4272</guid><dc:creator>bogardj</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;When developing with an IoC container like StructureMap, eventually some place in your code you will need to call the &lt;a href="http://martinfowler.com/eaaCatalog/registry.html"&gt;registry&lt;/a&gt; to instantiate your classes with their dependencies.&amp;nbsp; With StructureMap, this means a call to ObjectFactory.GetInstance.&amp;nbsp; Ideally, we&amp;#39;d like to limit the number of places the registry is called, so that we don&amp;#39;t have a lot of StructureMap concerns sprinkled throughout our application.&lt;/p&gt; &lt;p&gt;Suppose we have the following WCF service:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;CustomerSearchService &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ICustomerSearchService
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ICustomerRepository &lt;/span&gt;_customerRepository;
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ICustomerSummaryMapper &lt;/span&gt;_customerSummaryMapper;

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;CustomerSearchService(&lt;span style="color:#2b91af;"&gt;ICustomerRepository &lt;/span&gt;customerRepository, &lt;span style="color:#2b91af;"&gt;ICustomerSummaryMapper &lt;/span&gt;customerSummaryMapper)
    {
        _customerRepository = customerRepository;
        _customerSummaryMapper = customerSummaryMapper;
    }

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;CustomerSearchResult &lt;/span&gt;FindCustomerByName(&lt;span style="color:blue;"&gt;string &lt;/span&gt;fullName)
    {
        &lt;span style="color:#2b91af;"&gt;Customer &lt;/span&gt;customer = _customerRepository.FindCustomerByName(fullName);

        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(customer == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:blue;"&gt;return new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;CustomerSearchResult
                       &lt;/span&gt;{
                           IsSuccessful = &lt;span style="color:blue;"&gt;false&lt;/span&gt;,
                           FailureReasons = &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {&lt;span style="color:#a31515;"&gt;&amp;quot;Customer not found.&amp;quot;&lt;/span&gt;}
                       };
        }

        &lt;span style="color:#2b91af;"&gt;CustomerSummary &lt;/span&gt;summary = _customerSummaryMapper.MapFrom(customer);

        &lt;span style="color:blue;"&gt;return  new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;CustomerSearchResult &lt;/span&gt;{IsSuccessful = &lt;span style="color:blue;"&gt;true&lt;/span&gt;, Result = summary};
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Nothing too exciting, just a search service that returns customer summary information from a name search.&amp;nbsp; Now, if we just try to use this service as is, we get a fun exception:&lt;/p&gt;
&lt;p&gt;&lt;i&gt;The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;That makes sense, as WCF is in charge of instantiating my service class (CustomerSearchService), but it only knows how to call a no-args constructor.&amp;nbsp; For a quick fix, we can add this constructor:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;CustomerSearchService()
    : &lt;span style="color:blue;"&gt;this&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ObjectFactory&lt;/span&gt;.GetInstance&amp;lt;&lt;span style="color:#2b91af;"&gt;ICustomerRepository&lt;/span&gt;&amp;gt;(), 
        &lt;span style="color:#2b91af;"&gt;ObjectFactory&lt;/span&gt;.GetInstance&amp;lt;&lt;span style="color:#2b91af;"&gt;ICustomerSummaryMapper&lt;/span&gt;&amp;gt;())
{
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Each of our WCF services would need to the same thing, have a bunch of ObjectFactory calls to set up the dependencies appropriately.&amp;nbsp; But we can find a better way, and have all of our services wired up automatically, with only one call to StructureMap in the entire application.&amp;nbsp; To do this, we&amp;#39;ll need to plug in to a few WCF extension points to make it happen.&lt;/p&gt;
&lt;h3&gt;A custom instance provider&lt;/h3&gt;
&lt;p&gt;WCF provides an interface just for this purpose, &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx"&gt;IInstanceProvider&lt;/a&gt;, that allows us to create custom instantiation behavior.&amp;nbsp; This interface has basically two methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CreateInstance - needs to return the right service&lt;/li&gt;
&lt;li&gt;ReleaseInstance - if we have some custom cleanup to do&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Just creating the IInstanceProvider isn&amp;#39;t enough, we&amp;#39;ll have to tell WCF to use our instance provider, instead of its own default instance provider.&amp;nbsp; This requires a custom endpoint behavior.&amp;nbsp; We can&amp;#39;t configure the instance provider directly, through configuration or other means.&amp;nbsp; Instead, we&amp;#39;ll use a custom service behavior through the &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iservicebehavior.aspx"&gt;IServiceBehavior&lt;/a&gt; interface.&amp;nbsp; Here&amp;#39;s the implementation of our custom service behavior:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapServiceBehavior &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IServiceBehavior
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ApplyDispatchBehavior(&lt;span style="color:#2b91af;"&gt;ServiceDescription &lt;/span&gt;serviceDescription, &lt;span style="color:#2b91af;"&gt;ServiceHostBase &lt;/span&gt;serviceHostBase)
    {
        &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ChannelDispatcherBase &lt;/span&gt;cdb &lt;span style="color:blue;"&gt;in &lt;/span&gt;serviceHostBase.ChannelDispatchers)
        {
            &lt;span style="color:#2b91af;"&gt;ChannelDispatcher &lt;/span&gt;cd = cdb &lt;span style="color:blue;"&gt;as &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ChannelDispatcher&lt;/span&gt;;
            &lt;span style="color:blue;"&gt;if &lt;/span&gt;(cd != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;EndpointDispatcher &lt;/span&gt;ed &lt;span style="color:blue;"&gt;in &lt;/span&gt;cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = 
                        &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapInstanceProvider&lt;/span&gt;(serviceDescription.ServiceType);
                }
            }
        }
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;AddBindingParameters(&lt;span style="color:#2b91af;"&gt;ServiceDescription &lt;/span&gt;serviceDescription, &lt;span style="color:#2b91af;"&gt;ServiceHostBase &lt;/span&gt;serviceHostBase, &lt;span style="color:#2b91af;"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;ServiceEndpoint&lt;/span&gt;&amp;gt; endpoints, &lt;span style="color:#2b91af;"&gt;BindingParameterCollection &lt;/span&gt;bindingParameters)
    {
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Validate(&lt;span style="color:#2b91af;"&gt;ServiceDescription &lt;/span&gt;serviceDescription, &lt;span style="color:#2b91af;"&gt;ServiceHostBase &lt;/span&gt;serviceHostBase)
    {
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;For all of the endpoints in all of the channels, we need to give WCF our custom instance provider.&amp;nbsp; Also, since StructureMap needs to know what type to create, we have to pass that information along to our instance provider.&amp;nbsp; This comes from the ServiceDescription passed in above.&lt;/p&gt;
&lt;p&gt;With our service behavior done, let&amp;#39;s create the actual instance provider, which will call StructureMap:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapInstanceProvider &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IInstanceProvider
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;_serviceType;

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;StructureMapInstanceProvider(&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;serviceType)
    {
        _serviceType = serviceType;
    }

    &lt;span style="color:blue;"&gt;public object &lt;/span&gt;GetInstance(&lt;span style="color:#2b91af;"&gt;InstanceContext &lt;/span&gt;instanceContext)
    {
        &lt;span style="color:blue;"&gt;return &lt;/span&gt;GetInstance(instanceContext, &lt;span style="color:blue;"&gt;null&lt;/span&gt;);
    }

    &lt;span style="color:blue;"&gt;public object &lt;/span&gt;GetInstance(&lt;span style="color:#2b91af;"&gt;InstanceContext &lt;/span&gt;instanceContext, &lt;span style="color:#2b91af;"&gt;Message &lt;/span&gt;message)
    {
        &lt;span style="color:blue;"&gt;return &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ObjectFactory&lt;/span&gt;.GetInstance(_serviceType);
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ReleaseInstance(&lt;span style="color:#2b91af;"&gt;InstanceContext &lt;/span&gt;instanceContext, &lt;span style="color:blue;"&gt;object &lt;/span&gt;instance)
    {
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Pretty straightforward, we just capture the service type (passed in from our service behavior earlier), then use ObjectFactory to create an instance of that service when asked.&amp;nbsp; There&amp;#39;s no information on the InstanceContext about the service type, or we could have just used it instead.&lt;/p&gt;
&lt;p&gt;Now that we have our IInstanceProvider and IServiceBehavior, it&amp;#39;s time to hook up this new service behavior to the rest of WCF.&amp;nbsp; We have a few choices:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Attributes&lt;/li&gt;
&lt;li&gt;Custom service host&lt;/li&gt;
&lt;li&gt;Configuration&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;With attributes, we&amp;#39;d decorate all of our services with something like [StructureMapServiceBehavior] or something similar, so that WCF would know to attach our IServiceBehavior to the ServiceDescription behaviors.&amp;nbsp; Attributes are okay, but again, we&amp;#39;d have to put something on all of our services.&amp;nbsp; Since the whole point of this exercise was to reduce the footprint, let&amp;#39;s go the custom service host route.&lt;/p&gt;
&lt;p&gt;As for configuration, I can&amp;#39;t stand XML, so let&amp;#39;s just pretend that one doesn&amp;#39;t exist.&lt;/p&gt;
&lt;h3&gt;A custom service host&lt;/h3&gt;
&lt;p&gt;By going the custom ServiceHost route, we&amp;#39;ll just need to plug in to the right event to add our custom service behavior to the mix.&amp;nbsp; Here&amp;#39;s what we wind up creating:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapServiceHost &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ServiceHost
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;StructureMapServiceHost()
    {
    }

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;StructureMapServiceHost(&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color:blue;"&gt;params &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Uri&lt;/span&gt;[] baseAddresses)
        : &lt;span style="color:blue;"&gt;base&lt;/span&gt;(serviceType, baseAddresses)
    {
    }

    &lt;span style="color:blue;"&gt;protected override void &lt;/span&gt;OnOpening()
    {
        Description.Behaviors.Add(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapServiceBehavior&lt;/span&gt;());
        &lt;span style="color:blue;"&gt;base&lt;/span&gt;.OnOpening();
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Nothing too exciting, we just add our custom service behavior to the ServiceDescription Behaviors collection, right before the service host is opened in the OnOpening method.&lt;/p&gt;
&lt;p&gt;Next, to plug in our custom service host, we&amp;#39;ll need a custom service host factory:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapServiceHostFactory &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ServiceHostFactory
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;StructureMapServiceHostFactory()
    {
        &lt;span style="color:#2b91af;"&gt;StructureMapConfiguration
            &lt;/span&gt;.ScanAssemblies()
            .IncludeTheCallingAssembly()
            .With&amp;lt;&lt;span style="color:#2b91af;"&gt;DefaultConventionScanner&lt;/span&gt;&amp;gt;();
    }

    &lt;span style="color:blue;"&gt;protected override &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceHost &lt;/span&gt;CreateServiceHost(&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color:#2b91af;"&gt;Uri&lt;/span&gt;[] baseAddresses)
    {
        &lt;span style="color:blue;"&gt;return new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;StructureMapServiceHost&lt;/span&gt;(serviceType, baseAddresses);
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The overridden method makes sense, override the CreateServiceHost to...create our custom service host.&amp;nbsp; However, there&amp;#39;s quite a bit going on in the constructor.&lt;/p&gt;
&lt;p&gt;At this point, it will start to matter how you&amp;#39;d like StructureMap to be configured:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Attributes&lt;/li&gt;
&lt;li&gt;XML config&lt;/li&gt;
&lt;li&gt;Code&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I&amp;#39;m liking the code way, as we don&amp;#39;t have to have crazy XML or a bunch of attributes everywhere.&amp;nbsp; Which way you configure is up to you, but if you go the code route, you&amp;#39;ll need to put the StructureMap configuration in the constructor here.&lt;/p&gt;
&lt;p&gt;Finally, we&amp;#39;ll need to configure our WCF service to use this service host factory.&amp;nbsp; I&amp;#39;m using IIS to host, so I&amp;#39;ll just need to change my .svc file:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background:#ffee62;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;@ &lt;/span&gt;&lt;span style="color:#a31515;"&gt;ServiceHost &lt;/span&gt;&lt;span style="color:red;"&gt;Language&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;Debug&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;true&amp;quot; 
    &lt;/span&gt;&lt;span style="color:red;"&gt;Service&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;SMExample.Wcf.CustomerSearchService&amp;quot; 
    &lt;/span&gt;&lt;span style="color:red;"&gt;Factory&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;SMExample.Wcf.StructureMapServiceHostFactory&amp;quot; &lt;/span&gt;&lt;span style="background:#ffee62;"&gt;%&amp;gt;
&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Other hosting solutions will use different ways of using the custom service host factory.&lt;/p&gt;
&lt;h3&gt;Recap&lt;/h3&gt;
&lt;p&gt;We wanted to use StructureMap with WCF, but this initially presented us with some problems.&amp;nbsp; WCF requires a no-args constructor, which means we&amp;#39;ll have to use a lot of ObjectFactory.GetInstance calls on every service implementation.&lt;/p&gt;
&lt;p&gt;Instead, we can use WCF extension points to use StructureMap to create the service for us, without needing that messy constructor.&amp;nbsp; We created two extensions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;IInstanceProvider&lt;/li&gt;
&lt;li&gt;IServiceBehavior&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Once we had our custom instance provider and service behavior, we needed to decide how our custom service behavior would get attached to our service.&amp;nbsp; We chose the custom service host route, which meant two more custom WCF implementations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ServiceHost&lt;/li&gt;
&lt;li&gt;ServiceHostFactory&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The ServiceHostFactory implementation forced us to decide how our dependencies should be configured in StructureMap, and we went the code route.&amp;nbsp; Finally, we configured our service host in IIS to use the right service host factory.&lt;/p&gt;
&lt;p&gt;With all this in place, none of our service classes nor their implementations need to know anything about StructureMap, allowing their design to grow and change outside of WCF construction concerns.&amp;nbsp; We also reduced the footprint of StructureMap in our application, where we now had exactly one call to the StructureMap infrastructure, ObjectFactory.GetInstance.&amp;nbsp; With this one place StructureMap is used, it will be easier to swap out IoC container implementations (just kidding, &lt;a href="http://codebetter.com/blogs/jeremy.miller/"&gt;Jeremy&lt;/a&gt;).&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4272" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=9iOL0J"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=9iOL0J" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=I6813j"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=I6813j" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=8lcFdJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=8lcFdJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/350132633" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/Tools/default.aspx">Tools</category><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/WCF/default.aspx">WCF</category><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/StructureMap/default.aspx">StructureMap</category><feedburner:origLink>http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/07/29/integrating-structuremap-with-wcf.aspx</feedburner:origLink></item><item><title>StructureMap: Interlude</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/346793677/structuremap-interlude.aspx</link><pubDate>Sat, 26 Jul 2008 17:54:53 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4257</guid><dc:creator>chadmyers</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I&amp;#39;m trying to wrap up the &amp;quot;StructureMap: Advanced Scenarios Usage&amp;quot; post. In the meantime, I wanted to make you aware of a few things:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;I just updated my &lt;a href="http://chadmyers.lostechies.com/archive/2008/07/15/structuremap-basic-scenario-usage.aspx"&gt;StructureMap: Basic Scenario Usage&lt;/a&gt; post with a correction. I was mistaken about being able to set properties on configured objects. Currently, you cannot configure properties on configured objects unless the property has the [SetterProperty] attribute defined.&amp;nbsp; This was on purpose since there is a good argument that property injection is &amp;#39;bad&amp;#39; (i.e. leads to problems later, can complicate configuration and testing, etc).&amp;nbsp; There are, however, scenarios where property injection is acceptable so being able to configure properties without using the attribute is being seriously considered for the StructureMap 2.5 release. &lt;br /&gt;&lt;/li&gt; &lt;li&gt;Derik Whitaker posted &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/07/24/using-your-app-config-web-config-to-store-your-structuremap-settings.aspx"&gt;about how to use StructureMap in an MSTest situation&lt;/a&gt; to work around some of MSTest&amp;#39;s quirks. He used StructureMap&amp;#39;s ability to configure itself through XML in your web.config/app.config file rather than having a separate XML file. Or you could, of course, save half your time and tons of complexity and just use NUnit/MBUnit/XUnit and not deal with the hassle. But it&amp;#39;s good for those people required to use MSTest to still be able to take advantage of StructureMap.&lt;br /&gt;&lt;/li&gt; &lt;li&gt;You should know that &lt;a href="http://flimflan.com/blog/"&gt;Joshua Flanagan&lt;/a&gt; was the one who contributed that feature (the app.config configuration stuff) to StructureMap which is useful for a variety of reasons -- not just the MSTest reason.&amp;nbsp; The reason you should know of him is because...&lt;br /&gt;&lt;/li&gt; &lt;li&gt;The same Joshua just posted a great post on &lt;a href="http://flimflan.com/blog/HelloStructureMap.aspx"&gt;how to get started from the most basic level with StructureMap&lt;/a&gt;. It&amp;#39;s a &amp;quot;Hello World&amp;quot; type app, but shows off some of the ways StructureMap works. Of course it&amp;#39;s overdone as a Hello World app, but that wasn&amp;#39;t the point.&amp;nbsp; The point is to show how to get started with StructureMap and I think it does a really good job at that.&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4257" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=VONXjJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=VONXjJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=G21h1j"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=G21h1j" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=fgMepJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=fgMepJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/346793677" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/chad_myers/archive/tags/StructureMap/default.aspx">StructureMap</category><feedburner:origLink>http://www.lostechies.com/blogs/chad_myers/archive/2008/07/26/structuremap-interlude.aspx</feedburner:origLink></item><item><title>Arrange Act Assert and BDD specifications</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/344599960/arrange-act-assert-and-bdd-specifications.aspx</link><pubDate>Thu, 24 Jul 2008 13:18:04 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4241</guid><dc:creator>bogardj</dc:creator><slash:comments>13</slash:comments><description>&lt;p&gt;With Rhino Mocks 3.5 just around the corner, I&amp;#39;ve started using it to create much more readable tests.&amp;nbsp; One of the things that always bothered me with Expect.Call, constraints and the like was that it mixed in the Arrange with Assert.&amp;nbsp; For those that haven&amp;#39;t heard of AAA, it&amp;#39;s a pattern for authoring unit tests:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Arrange - set up the unit under test&lt;/li&gt; &lt;li&gt;Act - exercise the unit under test, capturing any resulting state&lt;/li&gt; &lt;li&gt;Assert - verify the behavior through assertions&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;As I moved towards BDD context/specification style tests, working with Rhino Mocks didn&amp;#39;t fit the picture very well.&amp;nbsp; But with the new AAA syntax of Rhino Mocks 3.5, I can very cleanly separate out the behavior I want to observe from the mechanics of setting up the test.&lt;/p&gt; &lt;p&gt;Here&amp;#39;s a normal unit test, as I would have written it about a year ago:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color:#2b91af;"&gt;Fact&lt;/span&gt;]
&lt;span style="color:blue;"&gt;public void &lt;/span&gt;Should_send_email_when_order_is_over_200()
{
    &lt;span style="color:#2b91af;"&gt;MockRepository &lt;/span&gt;repo = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MockRepository&lt;/span&gt;();

    &lt;span style="color:green;"&gt;//Arrange
    &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISmtpClient &lt;/span&gt;mockClient = repo.DynamicMock&amp;lt;&lt;span style="color:#2b91af;"&gt;ISmtpClient&lt;/span&gt;&amp;gt;();
    &lt;span style="color:#2b91af;"&gt;IOrderSpec &lt;/span&gt;stubSpec = repo.DynamicMock&amp;lt;&lt;span style="color:#2b91af;"&gt;IOrderSpec&lt;/span&gt;&amp;gt;();
    &lt;span style="color:#2b91af;"&gt;MailMessage &lt;/span&gt;actual = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;

    &lt;span style="color:#2b91af;"&gt;Order &lt;/span&gt;order = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Order&lt;/span&gt;();
    order.Total = 201.0m;

    &lt;span style="color:blue;"&gt;using &lt;/span&gt;(repo.Record())
    {
        mockClient.Send(&lt;span style="color:blue;"&gt;null&lt;/span&gt;);

        &lt;span style="color:green;"&gt;// Also assert?
        &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;LastCall
            &lt;/span&gt;.IgnoreArguments()
            .Do(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;MailMessage&lt;/span&gt;&amp;gt;(message =&amp;gt; actual = message));

        &lt;span style="color:#2b91af;"&gt;SetupResult
            &lt;/span&gt;.For(stubSpec.IsMatch(order))
            .Return(&lt;span style="color:blue;"&gt;true&lt;/span&gt;);
    }

    &lt;span style="color:#2b91af;"&gt;OrderProcessor &lt;/span&gt;pr = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OrderProcessor&lt;/span&gt;(mockClient, stubSpec);

    &lt;span style="color:green;"&gt;// Act
    &lt;/span&gt;pr.PlaceOrder(order);

    &lt;span style="color:green;"&gt;// Assert
    &lt;/span&gt;actual.ShouldNotBeNull();
    actual.To.Count.ShouldEqual(1);
    actual.To[0].Address.ShouldEqual(&lt;span style="color:#a31515;"&gt;&amp;quot;salesdude@email.com&amp;quot;&lt;/span&gt;);

    repo.VerifyAll();
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;It&amp;#39;s a really long test, but the basic idea is that an email needs to be sent out to the sales guy when big orders get placed.&amp;nbsp; The sales guy wanted to follow up immediately, to try and sell more (we think).&amp;nbsp; I see many issues with this test:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It&amp;#39;s frickin&amp;#39; huge&lt;/li&gt;
&lt;li&gt;I can&amp;#39;t tell what the point of it is at first glance&lt;/li&gt;
&lt;li&gt;It&amp;#39;s really hard to tell what&amp;#39;s being tested&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Moving towards the context/specification style, but still with Rhino Mocks improved things somewhat, but it&amp;#39;s still quite awkward:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;When_placing_a_large_order 
    &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ContextSpecification
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MockRepository &lt;/span&gt;_repo;
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OrderProcessor &lt;/span&gt;_orderProcessor;
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Order &lt;/span&gt;_order;
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MailMessage &lt;/span&gt;_actual;

    &lt;span style="color:blue;"&gt;protected override void &lt;/span&gt;EstablishContext()
    {
        _repo = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MockRepository&lt;/span&gt;();

        &lt;span style="color:#2b91af;"&gt;ISmtpClient &lt;/span&gt;mockClient = _repo.DynamicMock&amp;lt;&lt;span style="color:#2b91af;"&gt;ISmtpClient&lt;/span&gt;&amp;gt;();
        &lt;span style="color:#2b91af;"&gt;IOrderSpec &lt;/span&gt;stubSpec = _repo.DynamicMock&amp;lt;&lt;span style="color:#2b91af;"&gt;IOrderSpec&lt;/span&gt;&amp;gt;();
        _actual = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;

        _order = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Order&lt;/span&gt;();
        _order.Total = 201.0m;

        &lt;span style="color:blue;"&gt;using &lt;/span&gt;(_repo.Record())
        {
            mockClient.Send(&lt;span style="color:blue;"&gt;null&lt;/span&gt;);

            &lt;span style="color:green;"&gt;// Also assert?
            &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;LastCall
                &lt;/span&gt;.IgnoreArguments()
                .Do(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;MailMessage&lt;/span&gt;&amp;gt;(message =&amp;gt; _actual = message));

            &lt;span style="color:#2b91af;"&gt;SetupResult
                &lt;/span&gt;.For(stubSpec.IsMatch(_order))
                .Return(&lt;span style="color:blue;"&gt;true&lt;/span&gt;);
        }

        _orderProcessor = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OrderProcessor&lt;/span&gt;(mockClient, stubSpec);
    }

    &lt;span style="color:blue;"&gt;protected override void &lt;/span&gt;Because()
    {
        _orderProcessor.PlaceOrder(_order);
    }

    [&lt;span style="color:#2b91af;"&gt;Test&lt;/span&gt;]
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Should_send_the_email_out()
    {
        _actual.ShouldNotBeNull();
    }

    [&lt;span style="color:#2b91af;"&gt;Test&lt;/span&gt;]
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Email_sent_should_be_addressed_to_the_sales_guy()
    {
        _actual.To.Count.ShouldEqual(1);
        _actual.To[0].Address.ShouldEqual(&lt;span style="color:#a31515;"&gt;&amp;quot;salesdude@email.com&amp;quot;&lt;/span&gt;);
    }

    [&lt;span style="color:#2b91af;"&gt;Test&lt;/span&gt;]
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Should_verify_all_expectations()
    {
        _repo.VerifyAll();
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Again, I have to do some strange things to capture the output, and the record/replay model doesn&amp;#39;t jive well with BDD-style specifications.&amp;nbsp; I always had this one observation that said, &amp;quot;Should verify all expectations&amp;quot;.&amp;nbsp; Not very interesting, and not descriptive of the behavior I want to observe.&amp;nbsp; It doesn&amp;#39;t describe any behavior, just some cleanup assertion for the MockRepository.&lt;/p&gt;
&lt;p&gt;Finally, let&amp;#39;s see how the AAA syntax of Rhino Mocks 3.5 clears things up:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;When_placing_a_large_order 
    &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ContextSpecification
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OrderProcessor &lt;/span&gt;_orderProcessor;
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Order &lt;/span&gt;_order;
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISmtpClient &lt;/span&gt;_mockClient;

    &lt;span style="color:blue;"&gt;protected override void &lt;/span&gt;EstablishContext()
    {
        _mockClient = Dependency&amp;lt;&lt;span style="color:#2b91af;"&gt;ISmtpClient&lt;/span&gt;&amp;gt;();
        &lt;span style="color:#2b91af;"&gt;IOrderSpec &lt;/span&gt;stubSpec = Stub&amp;lt;&lt;span style="color:#2b91af;"&gt;IOrderSpec&lt;/span&gt;&amp;gt;();

        _order = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Order&lt;/span&gt;();
        _order.Total = 201.0m;

        stubSpec.Stub(x =&amp;gt; x.IsMatch(_order)).Return(&lt;span style="color:blue;"&gt;true&lt;/span&gt;);

        _orderProcessor = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OrderProcessor&lt;/span&gt;(_mockClient, stubSpec);
    }

    &lt;span style="color:blue;"&gt;protected override void &lt;/span&gt;Because()
    {
        _orderProcessor.PlaceOrder(_order);
    }

    [&lt;span style="color:#2b91af;"&gt;Test&lt;/span&gt;]
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Should_send_the_email_to_the_sales_guy()
    {
        _mockClient.Expect(x =&amp;gt; x.Send(&lt;span style="color:#2b91af;"&gt;Arg&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;MailMessage&lt;/span&gt;&amp;gt;.Matches(msg =&amp;gt; msg.To[0].Address == &lt;span style="color:#a31515;"&gt;&amp;quot;salesdude@email.com&amp;quot;&lt;/span&gt;)));
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;That&amp;#39;s a lot smaller!&amp;nbsp; I clearly separate the Arrange (EstablishContext) from Act (Because) and the Assert, which is my actual observation.&amp;nbsp; To stub the indirect input of the IOrderSpec, I can use the Stub extension method provided by Rhino Mocks 3.5.&lt;/p&gt;
&lt;p&gt;But the best aspect of the new AAA syntax is that I can finally create readable specifications that use Rhino Mocks.&amp;nbsp; Before, all of the noise of the Record/Replay and the MockRepository obscured the intention of the specification.&amp;nbsp; I had to rely on test spies earlier to capture the output of the ISmtpClient.Send call, as the old constraint model would have mixed in the Assert with the Arrange (i.e., I would have to put the constraints in the record section.&amp;nbsp; Not pretty.)&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve found that without the distractions of the old Rhino Mocks syntax, I can better focus on the behavior I&amp;#39;m trying to observe.&amp;nbsp; It&amp;#39;s now just one line to set up indirect inputs with stubs, and one line to verify interactions and indirect outputs.&lt;/p&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=4241" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=BFMumJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=BFMumJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=zDA8wj"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=zDA8wj" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/LosTechies?a=3rCPSJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/LosTechies?i=3rCPSJ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LosTechies/~4/344599960" height="1" width="1"/&gt;</description><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/Tools/default.aspx">Tools</category><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/BDD/default.aspx">BDD</category><category domain="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/Behavior-Driven+Development/default.aspx">Behavior-Driven Development</category><feedburner:origLink>http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/07/24/arrange-act-assert-and-bdd-specifications.aspx</feedburner:origLink></item><item><title>Running a x64 build server. Challenges overcome.</title><link>http://feeds.feedburner.com/~r/LosTechies/~3/342587148/running-a-x64-build-server-challenges-overcome.aspx</link><pubDate>Tue, 22 Jul 2008 14:02:10 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:4222</guid><dc:creator>erichexter</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;We just setup a new build server on my current project and with it came some of those little hiccups which made the setup take longer than planned.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;strike&gt;Cruise Control&lt;/strike&gt; Team Foundation Client&amp;#160; issues&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. Although our continuous integration server (Cruise Control.Ne