<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" 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/"><channel><title>Christopher Bennage : Featured</title><link>http://devlicio.us/blogs/christopher_bennage/archive/tags/Featured/default.aspx</link><description>Tags: Featured</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Using Lambdas to get at properties</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2008/07/16/using-lambdas-to-get-at-properties.aspx</link><pubDate>Thu, 17 Jul 2008 00:36:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:41324</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>9</slash:comments><description>&lt;p&gt;This is a somewhat made-up scenario based on a recent project. My client has not given me permission to discuss the specifics of the application (yet), so I apologize for keeping it vague. I hope you don&amp;#39;t give up before the payoff.&lt;/p&gt; &lt;h3&gt;The Problem&lt;/h3&gt; &lt;p&gt;Imagine an application, where a user can create &amp;quot;styles&amp;quot; and those styles can be applied to shapes. The application uses WPF to visualize the shapes with the styles applied.&amp;nbsp; (Perhaps it&amp;#39;s a design tool meant for Hanselman&amp;#39;s &lt;a href="http://www.hanselman.com/blog/CategoryView.aspx?category=BabySmash"&gt;BabySmash&lt;/a&gt;?) Let&amp;#39;s say that there are three kinds of shapes: a line, a circle, and a bull&amp;#39;s eye.&lt;/p&gt; &lt;p&gt;&lt;a href="http://devlicious.com/blogs/christopher_bennage/WindowsLiveWriter/1a84ff9474f3_108BF/styled_shapes_2.gif"&gt;&lt;img height="225" alt="styled_shapes" src="http://devlicious.com/blogs/christopher_bennage/WindowsLiveWriter/1a84ff9474f3_108BF/styled_shapes_thumb.gif" width="160" align="right" border="0" /&gt;&lt;/a&gt; Each shape has specific attributes that can be styled, and the these attributes are unique to each shape.&amp;nbsp; Here&amp;#39;s a simple listing of possible attributes:&lt;/p&gt; &lt;div align="center"&gt; &lt;table style="border-right:#dddddd 1px solid;border-top:#dddddd 1px solid;border-left:#dddddd 1px solid;width:300px;border-bottom:#dddddd 1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0" align="center"&gt;  &lt;tr&gt; &lt;th&gt;Line&lt;/th&gt; &lt;th&gt;Circle&lt;/th&gt; &lt;th&gt;Bull&amp;#39;s Eye&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;p align="center"&gt;Stroke&lt;/p&gt;&lt;/td&gt; &lt;td&gt; &lt;p align="center"&gt;Fill&lt;/p&gt;&lt;/td&gt; &lt;td&gt; &lt;p align="center"&gt;Outer Fill&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;p align="center"&gt;Thickness&lt;/p&gt;&lt;/td&gt; &lt;td&gt; &lt;p align="center"&gt;Stroke&lt;/p&gt;&lt;/td&gt; &lt;td&gt; &lt;p align="center"&gt;Inner Fill&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt; &lt;div align="center"&gt;&amp;nbsp;&lt;/div&gt; &lt;p&gt;Now, we have three classes to represent the styles: LineStyle, CircleStyle, BullsEyeStyle. The Fill and Stroke attributes are all of type &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.media.brush.aspx"&gt;Brush&lt;/a&gt;. Furthermore, all three classes derive from ShapeStyle.&lt;/p&gt; &lt;p&gt;Now, imagine that we have to construct instances of these styles for some data that comes in the form of an API that we don&amp;#39;t control. The constructor for LineStyle might look like this:&lt;/p&gt;&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public class LineStyle : ShapeStyle
{
    public LineStyle(ForeignLineStyle style)
    {
        Thickness = style.StrokeThickness;
        Stroke = style.StrokeBrush;
    }

    public Brush Stroke { get; set; }
    public double Thickness { get; set; }
}&lt;/pre&gt;
&lt;p&gt;That&amp;#39;s simple enough, but remember the properties are of type Brush, and we don&amp;#39;t really want our instances of LineStyle to share &lt;em&gt;the same&lt;/em&gt; Brush with instances of ForeignLineStyle.&amp;nbsp; Luckily, Brush has a Clone method! Yeah!&amp;nbsp; Only, what if the Brush is null? Okay, we&amp;#39;ll change it to this:&lt;/p&gt;&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public class LineStyle : ShapeStyle
{
    public LineStyle(ForeignLineStyle style)
    {
        Thickness = style.StrokeThickness;
        Stroke = (style.StrokeBrush != null)
                     ? style.StrokeBrush.Clone()
                     : null;
    }

    public Brush Stroke { get; set; }
    public double Thickness { get; set; }
}&lt;/pre&gt;
&lt;p&gt;Now, every time I have a property of type Brush on one of these style classes the pattern is the same.&amp;nbsp; I&amp;nbsp; start copying and pasting. (&lt;a href="http://en.wikipedia.org/wiki/Code_smell"&gt;Do you smell something?&lt;/a&gt;)&amp;nbsp; Hmm, what if I have lot of Brush properties? Many more than I initially listed in the table. I sure feel like I am &lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;repeating myself&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;At this point, my instinct is to extract this pattern into a method. But how do I do this? The properties are unique to each class, and using reflection would be way overkill.&lt;/p&gt;
&lt;h3&gt;Lambda Magic&lt;/h3&gt;
&lt;p&gt;What I want is a method that takes the brush from ForeignLineStyle, and the setter for a property of type Brush. Using a lambda for the setter, I am able to create:&lt;/p&gt;&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public static void SetBrush(Brush brush, Action&amp;lt;Brush&amp;gt; property)
{
    var value = (brush != null)
            ? brush.Clone()
            : null;

    property.Invoke(value);
}&lt;/pre&gt;
&lt;p&gt;For this to make sense, you need to recognize that a setter is a essentially a method with a signature like this:&lt;/p&gt;&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;delegate void PropertySetter&amp;lt;T&amp;gt;(T value);&lt;/pre&gt;
&lt;p&gt;That signature is the same as &lt;a href="http://msdn.microsoft.com/en-us/library/018hxwa8.aspx"&gt;Action&amp;lt;T&amp;gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now, I can modify LineStyle to look like this:&lt;/p&gt;&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public class LineStyle : ShapeStyle
{
    public LineStyle(ForeignLineStyle style)
    {
        Thickness = style.StrokeThickness;
        SetBrush(style.StrokeBrush, brush =&amp;gt; Stroke = brush);
    }

    public Brush Stroke { get; set; }
    public double Thickness { get; set; }
}&lt;/pre&gt;
&lt;p&gt;Notice that the second parameter is :&lt;/p&gt;&lt;pre&gt;brush =&amp;gt; Stroke = brush&lt;/pre&gt;
&lt;p&gt;and not simply&lt;/p&gt;&lt;pre&gt;()=&amp;gt; Stroke&lt;/pre&gt;
&lt;p&gt;The latter is for the getter and would match &lt;a href="http://msdn.microsoft.com/en-us/library/bb534960.aspx"&gt;Func&amp;lt;T&amp;gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;After Thoughts&lt;/h3&gt;
&lt;p&gt;Arguably, this doesn&amp;#39;t accomplish much.&amp;nbsp; The repetitive code had a small footprint, and the SetBrush method is likely to throw off other developers. &lt;/p&gt;
&lt;p&gt;It&amp;#39;s up to you to decide it&amp;#39;s applicability, but a least it&amp;#39;s one more tool in the box.&lt;/p&gt;
&lt;p&gt;(Thanks to &lt;a title="some say that he&amp;#39;s just that crazy!" href="http://devlicio.us/blogs/rob_eisenberg/default.aspx"&gt;Rob Eisenberg&lt;/a&gt;, who wrote some code that planted this ideas in my head.) &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=41324" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/WPF/default.aspx">WPF</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Tips+_2600_amp_3B00_+Tricks/default.aspx">Tips &amp;amp; Tricks</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/lambdas/default.aspx">lambdas</category></item><item><title>TemplateBinding: a bridge between styles and templates</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2008/07/04/templatebinding-a-bridge-between-styles-and-templates.aspx</link><pubDate>Fri, 04 Jul 2008 04:30:09 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:41205</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;This is partially a follow-up to my &lt;a href="http://devlicio.us/blogs/christopher_bennage/archive/2008/06/27/contrasting-control-templates-amp-styles-in-wpf.aspx" target="_blank"&gt;last post&lt;/a&gt; contrasting control templates and styles. Like I mentioned before I&amp;#39;ve been working on an application that has required a custom theme. In addition to styling the standard controls like text boxes and expanders, we&amp;#39;ve had to create a number of custom controls as well. That has added up to a lot of control templates.&lt;/p&gt; &lt;p&gt;This recent work has emphasized the importance of the template binding. (Beatriz Costa first helped me get a handle on template binding &lt;a title="you&amp;#39;ll need to scroll down a bit" href="http://www.beacosta.com/blog/?m=200611" target="_blank"&gt;here&lt;/a&gt;.)&amp;nbsp; &lt;a href="http://msdn.microsoft.com/en-us/library/ms742882.aspx" target="_blank"&gt;TemplateBinding&lt;/a&gt; is a special markup extension specifically designed for binding values in a template. (That&amp;#39;s a bit of a circular definition, isn&amp;#39;t it?) TemplateBinding is the mechanism that allows us to &lt;em&gt;inject&lt;/em&gt; values into our templates, or to parameterize them.&lt;/p&gt; &lt;h4&gt;An Example&lt;/h4&gt; &lt;p&gt;First, imagine that we have a resource dictionary where we have defined a set of brushes that we&amp;#39;ll use as the palette for our application. We might go about building a control template for the &lt;a title="an overview of the ToolTip" href="http://msdn.microsoft.com/en-us/library/ms754034.aspx" target="_blank"&gt;ToolTip&lt;/a&gt; control like this:&lt;/p&gt;&lt;pre class="xml:nocontrols:nogutter" name="code"&gt;&amp;lt;ControlTemplate x:Key=&amp;quot;ToolTipTemplate&amp;quot;
                 TargetType=&amp;quot;ToolTip&amp;quot;&amp;gt;
    &amp;lt;Grid&amp;gt;
        &amp;lt;Border x:Name=&amp;quot;DropShadow&amp;quot;
                Focusable=&amp;quot;False&amp;quot;
                CornerRadius=&amp;quot;3&amp;quot;
                Background=&amp;quot;Black&amp;quot;
                Opacity=&amp;quot;0.4&amp;quot;
                Margin=&amp;quot;3 3 0 0&amp;quot; /&amp;gt;
        &amp;lt;Border Background=&amp;quot;{DynamicResource NormalControlBackground}&amp;quot;
                BorderBrush=&amp;quot;{DynamicResource NormalControlBorder}&amp;quot;
                BorderThickness=&amp;quot;1&amp;quot;
                Margin=&amp;quot;0 0 3 3&amp;quot;
                CornerRadius=&amp;quot;3&amp;quot;
                Padding=&amp;quot;6&amp;quot;&amp;gt;
            &amp;lt;ContentPresenter TextBlock.Foreground=&amp;quot;{DynamicResource NormalControlText}&amp;quot; /&amp;gt;
        &amp;lt;/Border&amp;gt;
    &amp;lt;/Grid&amp;gt;
&amp;lt;/ControlTemplate&amp;gt;&lt;/pre&gt;
&lt;p&gt;This control template is too rigid. What if another team member needs to create a special ToolTip in just one place with a background color other than NormalControlBackground?&amp;nbsp; They would probably write some markup like this:&lt;/p&gt;&lt;pre class="xml:nocontrols:nogutter" name="code"&gt;&amp;lt;Button Content=&amp;quot;History Eraser Button&amp;quot;&amp;gt;
    &amp;lt;Button.ToolTip&amp;gt;
        &amp;lt;ToolTip Background=&amp;quot;Red&amp;quot; 
                 Foreground=&amp;quot;White&amp;quot;
                 Content=&amp;quot;Don&amp;#39;t press it!&amp;quot; /&amp;gt;
    &amp;lt;/Button.ToolTip&amp;gt;
&amp;lt;/Button&amp;gt;&lt;/pre&gt;
&lt;p&gt;Sadly, they would discover that it wouldn&amp;#39;t work. The control template doesn&amp;#39;t know anything about the properties we set on the ToolTip in the markup. Everything is defined in the control template and it&amp;#39;s self contained.&amp;nbsp; We really want the template to be able to use the properties that are set on the control. The template above can be rewritten to do that:&lt;/p&gt;&lt;pre class="xml:nocontrols:nogutter" name="code"&gt;&amp;lt;ControlTemplate x:Key=&amp;quot;ToolTipTemplate&amp;quot;
                 TargetType=&amp;quot;ToolTip&amp;quot;&amp;gt;
    &amp;lt;Grid&amp;gt;
        &amp;lt;Border x:Name=&amp;quot;DropShadow&amp;quot;
                Focusable=&amp;quot;False&amp;quot;
                CornerRadius=&amp;quot;3&amp;quot;
                Background=&amp;quot;Black&amp;quot;
                Opacity=&amp;quot;0.4&amp;quot;
                Margin=&amp;quot;3 3 0 0&amp;quot; /&amp;gt;
        &amp;lt;Border Background=&amp;quot;{TemplateBinding Background}&amp;quot;
                BorderBrush=&amp;quot;{TemplateBinding BorderBrush}&amp;quot;
                BorderThickness=&amp;quot;{TemplateBinding BorderThickness}&amp;quot;
                Margin=&amp;quot;0 0 3 3&amp;quot;
                CornerRadius=&amp;quot;3&amp;quot;
                Padding=&amp;quot;{TemplateBinding Padding}&amp;quot;&amp;gt;
            &amp;lt;ContentPresenter TextBlock.Foreground=&amp;quot;{TemplateBinding Foreground}&amp;quot; /&amp;gt;
        &amp;lt;/Border&amp;gt;
    &amp;lt;/Grid&amp;gt;
&amp;lt;/ControlTemplate&amp;gt;&lt;/pre&gt;
&lt;p&gt;Using the template bindings allows the control template to pick up the values that are supplied directly on the element, or on the style applied to the element. That means that the Button markup above would work, or we could even create a style for the special case tool tip that looks like this:&lt;/p&gt;&lt;pre class="xml:nocontrols:nogutter" name="code"&gt;&amp;lt;Style x:Key=&amp;quot;WarningStyle&amp;quot; TargetType=&amp;quot;ToolTip&amp;quot;&amp;gt;
    &amp;lt;Setter Property=&amp;quot;Background&amp;quot; Value=&amp;quot;Red&amp;quot; /&amp;gt;
    &amp;lt;Setter Property=&amp;quot;Foreground&amp;quot; Value=&amp;quot;White&amp;quot; /&amp;gt;
&amp;lt;/Style&amp;gt;&lt;/pre&gt;
&lt;h4&gt;Handling the Default Values&lt;/h4&gt;
&lt;p&gt;If you use a control template with template binding, and don&amp;#39;t provide any values on the control, what happens? The default values of the bound properties are used. There are many cases where this is &lt;em&gt;not&lt;/em&gt; what you want though. If your intent was to have a global look for tool tips based on our first example, then you want the template to use some specific brushes as well a few other things.&lt;/p&gt;
&lt;p&gt;In this case, you can use a style to set the desired defaults. To apply a style to a control for an entire application, set its TargetType and &lt;em&gt;don&amp;#39;t &lt;/em&gt;provide a key. The style will also need to be added to (or merged into) the resource dictionary of application. The style is also used to set the custom template for the control (otherwise you&amp;#39;d have to set the Template property on every instance of the control.)&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s an example style to accompany our template:&lt;/p&gt;&lt;pre class="xml:nocontrols:nogutter" name="code"&gt;&amp;lt;Style TargetType=&amp;quot;ToolTip&amp;quot;&amp;gt;
    &amp;lt;Setter Property=&amp;quot;Background&amp;quot; Value=&amp;quot;{DynamicResource NormalControlBackground}&amp;quot; /&amp;gt;
    &amp;lt;Setter Property=&amp;quot;BorderBrush&amp;quot; Value=&amp;quot;{DynamicResource NormalControlBorder}&amp;quot; /&amp;gt;
    &amp;lt;Setter Property=&amp;quot;BorderThickness&amp;quot; Value=&amp;quot;1&amp;quot; /&amp;gt;
    &amp;lt;Setter Property=&amp;quot;Foreground&amp;quot; Value=&amp;quot;{DynamicResource NormalControlText}&amp;quot; /&amp;gt;
    &amp;lt;Setter Property=&amp;quot;Padding&amp;quot; Value=&amp;quot;6&amp;quot; /&amp;gt;
    &amp;lt;Setter Property=&amp;quot;Template&amp;quot; Value=&amp;quot;{DynamicResource ToolTipTemplate}&amp;quot; /&amp;gt;
&amp;lt;/Style&amp;gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;m interested in feedback. If you find this useful, or too basic, or whatever, please let me know!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=41205" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/WPF/default.aspx">WPF</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/control+templates/default.aspx">control templates</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/data+binding/default.aspx">data binding</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/styles/default.aspx">styles</category></item><item><title>Thoughts on ALT.NET</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2008/03/15/thoughts-on-alt-net.aspx</link><pubDate>Sat, 15 Mar 2008 21:39:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39678</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;I just listened to &lt;a href="http://www.hanselminutes.com/default.aspx?showID=122" target="_blank"&gt;Scott Hanselman interviewing Dave Laribee&lt;/a&gt; regarding &lt;a href="http://altdotnet.org/" target="_blank"&gt;ALT.NET&lt;/a&gt;. I really like what Dave had to say, and I encourage everyone to go and listen.&amp;nbsp; I&amp;#39;ve been reticent about the ALT.NET movement (aside from my initial surge of enthusiasm.) I&amp;#39;m a bit shy when it comes to controversy, and even though I have strong evangelistic tendencies, I am also quick to shut up. ALT.NET has had its &lt;a href="http://altnetpursefight.blogspot.com/" target="_blank"&gt;share&lt;/a&gt; of &lt;a href="http://samgentile.com/blogs/samgentile/archive/2007/10/06/goodbye-codebetter-and-alt-net.aspx" target="_blank"&gt;controversy&lt;/a&gt;. &lt;/p&gt; &lt;p&gt;&lt;a href="http://devlicious.com/blogs/christopher_bennage/WindowsLiveWriter/ThoughtsonALT.NET_EF75/RWS2-Big_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="92" alt="Now we&amp;#39;re an institution!" src="http://devlicious.com/blogs/christopher_bennage/WindowsLiveWriter/ThoughtsonALT.NET_EF75/RWS2-Big_thumb.png" width="240" align="right" border="0" /&gt;&lt;/a&gt;I really like that Dave said &lt;em&gt;it&amp;#39;s not about the tools, it&amp;#39;s about the principles&lt;/em&gt;.&amp;nbsp; I am frequently asked about &amp;quot;good design&amp;quot; and &amp;quot;best practices&amp;quot;.&amp;nbsp;&amp;nbsp; This is why I&amp;#39;m ALT.NET. (In fact, I have a series of posts on this topic that I intend to start after we&amp;#39;re done with the &lt;a title="Sams Teach Yourself WPF in 24 Hours -- preorder yours today! :-)" href="http://www.amazon.com/Sams-Teach-Yourself-WPF-Hours/dp/0672329859" target="_blank"&gt;book&lt;/a&gt;.)&lt;/p&gt; &lt;p&gt;There&amp;#39;s an item in the interview that I&amp;#39;d like to comment on.&lt;/p&gt; &lt;p&gt;Scott asks (pardon my paraphrasing) why would the hypothetical &lt;a title="Is he (or she) a real person?" href="http://www.nfs.unl.edu/" target="_blank"&gt;Chief Architect of the Nebraska Department of Forestry&lt;/a&gt; have any interest in ALT.NET? The mythical Mort has pressing concerns, he just needs to get work done, why would he care about these conversations, discussions, and principles? Listen to the podcast for Dave&amp;#39;s answer.&amp;nbsp; However, my answer is this: &lt;em&gt;he probably doesn&amp;#39;t care and that&amp;#39;s okay&lt;/em&gt;. I think that ALT.NET is about bringing good design and principle to the forefront.&amp;nbsp; However, good ideas take a while to be adopted, to be democratized.&amp;nbsp; Those who are hungry, we&amp;#39;ll feed.&amp;nbsp; Those who aren&amp;#39;t, we&amp;#39;ll wish them well.&amp;nbsp; No hard feelings. Eventually, the good ideas will be institutionalized and they&amp;#39;ll trickle down. (Have you noticed the &amp;quot;refactor&amp;quot; menu in Visual Studio?) I&amp;#39;m more concerned about convincing the institutions and the thought leaders.&amp;nbsp; (Perhaps &amp;quot;convincing&amp;quot; is the wrong word, ALT.NET is more about &amp;quot;conversing&amp;quot; to me.) That&amp;#39;s why it&amp;#39;s good that ALT.NET is conversing with Microsoft, and that&amp;#39;s also why I&amp;#39;m encouraged to see Microsoft paying attention to things like open source (though I know many people are angry about the kind of attention being paid).&lt;/p&gt; &lt;p&gt;Now go learn &lt;a title="One-Click Ruby Installer for Windows" href="http://rubyinstaller.rubyforge.org/wiki/wiki.pl" target="_blank"&gt;Ruby&lt;/a&gt;!&amp;nbsp; :-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39678" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Agile/default.aspx">Agile</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Musings/default.aspx">Musings</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/ALT.NET/default.aspx">ALT.NET</category></item><item><title>TDD Example: Querying a Repository</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2007/08/26/tdd-example-querying-a-repository.aspx</link><pubDate>Mon, 27 Aug 2007 03:07:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:38280</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>7</slash:comments><description>&lt;p&gt;I&amp;#39;m always a little intimidated when I post code examples.&amp;nbsp; There are a lot of smart (and opinionated) people out there reading blogs.&amp;nbsp; Fear won&amp;#39;t get us anywhere though.&amp;nbsp; That said, I&amp;#39;m posting this example and asking for criticism.&lt;/p&gt; &lt;p&gt;Here&amp;#39;s the story.&amp;nbsp; A school of music needs to schedule auditions for incoming students. The faculty need to be able to view the schedule for the next two upcoming terms, and some of staff needs to be able to edit the audition schedule.&lt;/p&gt; &lt;p&gt;The &amp;quot;schedule&amp;quot; is really a set of rooms set aside on certain dates for certain types of students.&amp;nbsp; For example, room&amp;nbsp;1874 will be used for violins on September 21. Thus, a ScheduledRoom is a certain on a certain date, with rules about who can audition&amp;nbsp;there. &amp;nbsp;Right now, we&amp;#39;re just concerned with retrieving the list of rooms for a given Term, which is a unique combination of year and semester. &lt;/p&gt; &lt;p&gt;We have a repository for handling audition schedules, and a service that talks to the repository.&amp;nbsp; The service is in turn accessed by a desktop client, and a web client.&amp;nbsp; I&amp;#39;m going to focus on just the repository here.&lt;/p&gt;&lt;pre class="c-sharp:nogutter:nocontrols" name="code"&gt;namespace Specifications.Integrated.The_repository_for
{
    [TestFixture]
    public class the_audition_schedule
    {
        private AuditionScheduleRepository _repository;

        [SetUp]
        public void SetUp
        {
            _repository = new AuditionScheduleRepository();
        }

        [Test, RollBack]
        public void can_retrieve_the_list_of_scheduled_rooms_for_a_given_term()
        {
            //setup data for test
            Term fall2000 = a_term_for_Fall_2000();
            Term spring2001 = a_term_for_Spring_2001();

            schedule_a_room_for_(fall2000);
            schedule_a_room_for_(fall2000);
            schedule_a_room_for_(fall2000);
            schedule_a_room_for_(spring2001);

            //here&amp;#39;s the actual code under test
            IList&amp;lt;ScheduledRoom&amp;gt; rooms = _repository.GetRooms(fall2000);

            //verify the results
            Assert.AreEqual(3, rooms.Count);
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;The methods &lt;b&gt;a_term_for_Fall_2000()&lt;/b&gt;, &lt;b&gt;a_term_for_Spring_2001()&lt;/b&gt;, and &lt;b&gt;schedule_a_room_for_()&lt;/b&gt; are helper methods that do nothing more than&amp;nbsp;persist some test data.&amp;nbsp; In my [Setup], I create a new instance of _repository, so that it&amp;#39;s fresh for every test.&lt;/p&gt;
&lt;p&gt;What am I really testing here?&amp;nbsp; An important question, &lt;a title="Los Techies" href="http://www.lostechies.com/blogs/joeydotnet/default.aspx" target="_blank"&gt;Joey Beninghove&lt;/a&gt; has a good post on the topic &lt;a title="Unit Testing NHibernate DALs - What Are You *Really* Testing?" href="http://www.lostechies.com/blogs/joeydotnet/archive/2007/05/17/unit-testing-nhibernate-dals-what-are-you-really-testing.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I want to ensure that GetRooms() &lt;i&gt;behaves&lt;/i&gt; the way I expect.&amp;nbsp; I expect it to return 3 out of the 4 terms I have persisted. The actual implementation for GetRooms() builds an NHibernate.ICriteria and call its List&amp;lt;&amp;gt;() method.&lt;/p&gt;
&lt;p&gt;Thoughts? Comments?&amp;nbsp; More examples to come...&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://devlicious.com/blogs/christopher_bennage/archive/2007/08/26/tdd-example-querying-a-repository.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://devlicious.com/blogs/christopher_bennage/archive/2007/08/26/tdd-example-querying-a-repository.aspx" border="0" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=38280" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/TDD/default.aspx">TDD</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Featured/default.aspx">Featured</category></item></channel></rss>