<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
    <channel>
        <title>AbstractCode</title>
        <link>http://abstractcode.com/abstractblog/Default.aspx</link>
        <description>Code too abstract to instantiate</description>
        <language>en-AU</language>
        <copyright>Colin David Scott</copyright>
        <generator>Subtext Version 2.1.0.5</generator>
        <image>
            <title>AbstractCode</title>
            <url>http://abstractcode.com/abstractblog/images/RSS2Image.gif</url>
            <link>http://abstractcode.com/abstractblog/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/Abstractcode" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
            <title>&amp;lsquo;as&amp;rsquo; is no safer than cast without a null check</title>
            <category>Development Practices</category>
            <category>C#</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/Te5fnP3jX6o/191.aspx</link>
            <description>&lt;p&gt;I’ve recently encountered code which looks something like:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:de23a1b8-588b-44d6-af99-b5487bb562cf" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;var someInstance &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; someParameter &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&lt;span style="color: #000000;"&gt; ISomething;
someInstance.DoSomething();
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The problem with this code is that it is wide open for a highly unhelpful &lt;em&gt;NullReferenceException&lt;/em&gt;. If &lt;em&gt;someParameter&lt;/em&gt; cannot be cast to &lt;em&gt;ISomething&lt;/em&gt; the &lt;em&gt;as&lt;/em&gt; operator will return null. But this code doesn’t check if &lt;em&gt;someInstance&lt;/em&gt; is null. It makes the assumption that &lt;em&gt;someParameter&lt;/em&gt; may be case to &lt;em&gt;ISomething&lt;/em&gt;, even though in practice this assumption may not hold.&lt;/p&gt;

&lt;p&gt;There are two solutions to this. You may replace the use of the &lt;em&gt;as&lt;/em&gt; operator with an explicit cast. This will throw an &lt;em&gt;InvalidCastException&lt;/em&gt; if the cast cannot be performed, which at least is more informative that the &lt;em&gt;NullReferenceException&lt;/em&gt;. It’s also going to be somewhat faster but the performance gain will be sufficiently trivial that this is unlikely to be a useful optimisation. It also makes the code more explicit in what it expects which aids readability.&lt;/p&gt;

&lt;p&gt;The preferred solution would be to check the result of the &lt;em&gt;as&lt;/em&gt; operator for null and handle this case appropriately. In many cases this would be to throw an exception, but this exception may be more informative than a generic &lt;em&gt;InvalidCastException&lt;/em&gt; or &lt;em&gt;NullReferenceException&lt;/em&gt;. In general you should throw a specific exception that provides meaningful information in the context, rather than a technical exception(what went wrong rather than how). In other cases there may be an appropriate alternate activity to be performed, or the method could simply do nothing in this case. Regardless of the appropriate behaviour it is important that it be considered and the code actually implement this behaviour.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/191.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/Te5fnP3jX6o" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/11/03/191.aspx</guid>
            <pubDate>Tue, 03 Nov 2009 07:44:17 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/191.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/11/03/191.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/191.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/11/03/191.aspx</feedburner:origLink></item>
        <item>
            <title>In which I review my old phone</title>
            <category>Misc</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/dVu5JRRE3g8/190.aspx</link>
            <description>&lt;p&gt;My phone has been annoying me for some time, as it’s been slow and buggy. I’ve therefore decided to do a review.&lt;/p&gt;  &lt;p&gt;To start with I need some tools:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0011_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0011" border="0" alt="IMAG0011" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0011_thumb.jpg" width="148" height="244" /&gt;&lt;/a&gt; &lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0010_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0010" border="0" alt="IMAG0010" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0010_thumb.jpg" width="148" height="244" /&gt;&lt;/a&gt;&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0012_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0012" border="0" alt="IMAG0012" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0012_thumb.jpg" width="148" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; Safety is of course paramount, so I’ll also need:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0009_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0009" border="0" alt="IMAG0009" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0009_thumb.jpg" width="148" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p /&gt;  &lt;p&gt;Reviewing can be dangerous. Kids, never review something without adult supervision.&lt;/p&gt;  &lt;p&gt;Before review:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0014_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0014" border="0" alt="IMAG0014" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0014_thumb.jpg" width="148" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here we see that in the event of attack by chisel the phone easily separates into two components, after which it ceases to function.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0015_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0015" border="0" alt="IMAG0015" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0015_thumb.jpg" width="464" height="772" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Further we can see that the device is vulnerable to chisel injection attacks:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0016_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMAG0016" border="0" alt="IMAG0016" src="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/WindowsLiveWriter/InwhichIreviewmyoldphone_AF60/IMAG0016_thumb.jpg" width="464" height="772" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Hopefully this will help you which your phone purchasing decisions in high-chisel environments.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/190.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/dVu5JRRE3g8" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/09/19/190.aspx</guid>
            <pubDate>Sat, 19 Sep 2009 04:29:00 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/190.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/09/19/190.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/190.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/09/19/190.aspx</feedburner:origLink></item>
        <item>
            <title>Slides from my presentation to the Perth .NET Community of Practice</title>
            <category>Development Community</category>
            <category>Development Tools</category>
            <category>Abstract2ion</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/o0kToSO0DG0/189.aspx</link>
            <description>Some people asked for the slides for my Continuous Integration presentation at the Perth .NET Community of Practice yesterday. Enjoy.&lt;br /&gt;
&lt;a href="http://abstractcode.com/abstractblog/images/abstractcode_com/abstractblog/Continuous%20Integration.ppt"&gt;&lt;br /&gt;
Download the Presentation here&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
This presentation was targetted at developers new to Continuous Integration. The examples use my Abstract2ion project which comes set up for Continuous Integration. If you are trying to set this up with TeamCity the table below has some of the configuration values necessary (TeamCity is pretty easy to use, so I'm not going to write a step by step guide):&lt;br /&gt;
&lt;br /&gt;
&lt;table width="" cellspacing="1" cellpadding="1" border="0" align="" summary=""&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Artifact paths:&lt;/td&gt;
            &lt;td&gt;Abstract2ion\Package\*.zip&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Build runner:&lt;/td&gt;
            &lt;td&gt;MSBuild&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Build File Path:&lt;/td&gt;
            &lt;td&gt;Abstract2ion\Abstract2ion.build&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;MSBuild Version&lt;/td&gt;
            &lt;td&gt;Microsoft .NET Framework 3.5&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;MSBuild ToolsVersion&lt;/td&gt;
            &lt;td&gt;3.5&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Targets&lt;/td&gt;
            &lt;td&gt;Test;Package&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Command Line Parameters&lt;/td&gt;
            &lt;td&gt;/property:Configuration=Release&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt; &lt;/td&gt;
            &lt;td&gt; &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/189.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/o0kToSO0DG0" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/09/04/189.aspx</guid>
            <pubDate>Fri, 04 Sep 2009 10:28:00 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/189.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/09/04/189.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/189.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/09/04/189.aspx</feedburner:origLink></item>
        <item>
            <title>Presenting at the Perth .NET Community of Practice</title>
            <category>Development Community</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/JarV3Y2wDdE/188.aspx</link>
            <description>&lt;p&gt;I’m doing a presentation on &lt;a href="http://perthdotnet.org/blogs/events/archive/2009/08/11/continuous-integration-improving-quality-and-reducing-cost-through-constructive-laziness.aspx"&gt;Continuous Integration&lt;/a&gt; at the Perth .NET Community of Practice Thursday 3rd September. This is a completely reworked version of a presentation I’ve given to colleagues aimed primarily at .NET developers looking to start using CI.&lt;/p&gt;  &lt;p&gt;Details:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font style="background-color: #ffffff"&gt;&lt;strong&gt;Date:&lt;/strong&gt; Thursday September 3rd, 5:30pm&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font style="background-color: #ffffff"&gt;&lt;strong&gt;Venue:&lt;/strong&gt; Excom, Ground Floor, 23 Barrack St, Perth&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font style="background-color: #ffffff"&gt;&lt;strong&gt;Cost:&lt;/strong&gt; Free&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/188.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/JarV3Y2wDdE" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/08/29/188.aspx</guid>
            <pubDate>Sat, 29 Aug 2009 09:03:14 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/188.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/08/29/188.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/188.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/08/29/188.aspx</feedburner:origLink></item>
        <item>
            <title>If how many times you scream at the screen in anger is the metric&amp;hellip;</title>
            <category>Misc</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/rs6jbqzCpCQ/187.aspx</link>
            <description>&lt;p&gt;…then The Howard Years is a much better documentary than Liberal Rule.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/187.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/rs6jbqzCpCQ" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/08/15/187.aspx</guid>
            <pubDate>Sat, 15 Aug 2009 10:49:14 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/187.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/08/15/187.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/187.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/08/15/187.aspx</feedburner:origLink></item>
        <item>
            <title>A small example of the value of separation of concerns</title>
            <category>Development Practices</category>
            <category>Application Architecture</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/1_K0JiU7heQ/186.aspx</link>
            <description>&lt;p&gt;In a recent project I’ve had to implement a feature I think is a good example of a separation of concerns at a small scale.&lt;/p&gt;  &lt;p&gt;This feature involves giving the user a choice from a set of items. The available items from which the user may select an option is determined by a couple of factors. First there is a pool from which they may draw items. Users will preferentially get only those items directly associated with themselves if any are available. If no items are associated with the user then they get those associated with their user class. Finally, if there are no items associated with either the user or the user’s class they get a set of unassociated items as their pool.&lt;/p&gt;  &lt;p&gt;Once we have the pool we also do filtering on a set of dynamic criteria, such as the current order value. This filtered set them becomes the list of options the end user is presented with.&lt;/p&gt;  &lt;p&gt;For various implementation reasons determining the pool items can be expensive but the pool items appropriate to a user change relatively infrequently. They are therefore perfect candidates for caching. The filtering of the pool is based on dynamic criteria that may change frequently and are therefore not amenable to caching.&lt;/p&gt;  &lt;p&gt;What this leads to is that we have three separate concerns:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Retrievable of the pool items&lt;/li&gt;    &lt;li&gt;Caching of the pool items&lt;/li&gt;    &lt;li&gt;Filtering of the pool items&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It would be possible to implement all of these concerns within a single service. That would isolate the client of the service from all of these concerns. It would pass the service the current relevant state (such as order value) and receive in response a list of items to display. That the service may use caching to reduce the impact of potentially expensive operations is irrelevant to the client, as are the exact rules used for filtering. The client is therefore simpler than if it new of these concerns.&lt;/p&gt;  &lt;p&gt;Although the client may be simpler the service itself is not. It contains three concerns and must deal with all their interactions and implementation concerns. This does not conform to the Single Responsibility Principal.&lt;/p&gt;  &lt;p&gt;We can simplify this by making each concern its own distinct service. At the lowest level we have a service that performs only the retrieval of the pool items. This service becomes the only point where we need to consider the current user, as its results will always be restricted to those available to that user.&lt;/p&gt;  &lt;p&gt;On top of the retrieval service we can place a caching service. When items are requested from this service it will preferentially return the cached set if they exist. Otherwise it will utilise the retrieval service, saving the results object in the cache as well as returning them it its own caller.&lt;/p&gt;  &lt;p&gt;Finally we have a filter service. It gets an item pool from the caching service and applies filtering criteria, returning the results to its own caller. It would therefore have the same signature as the original merged service.&lt;/p&gt;  &lt;p&gt;This would seem to have tripled the complexity. Instead of a single service we now have three. However each individual service is significantly simpler than the original implementation. Each has a smaller area of responsibility and interacts with fewer external dependencies. Therefore what we have is lower overall complexity where we trade some increase in complexity between elements for lower complexity inside the elements. As we can use autowiring Dependency Injection containers to do all the hookup for us this is in most cases a significant gain in maintainability.&lt;/p&gt;  &lt;p&gt;We additionally get reuse potential that would not otherwise exist. In this case we may be able to genericise the caching service to be applicable in many scenarios where caching is required. This would allow us to apply caching using interception to a variety of actions with only some configuration of the DI container.&lt;/p&gt;  &lt;p&gt;Another area where benefits can be found is in future extension. Once these services were implemented a further requirement changed how the pool was generated. A new class of item was introduced that need to be handled separately. The same filtering sequence against user and user class were to be used, but we needed to do this separately for items of the class and items not of the class. The two resulting lists would then be merged.&lt;/p&gt;  &lt;p&gt;As the interface of the retrieval service did not change it was possible to constrain the changes purely to this service. This localisation of changes simplified testing significantly.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/186.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/1_K0JiU7heQ" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/08/15/186.aspx</guid>
            <pubDate>Sat, 15 Aug 2009 07:35:42 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/186.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/08/15/186.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/186.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/08/15/186.aspx</feedburner:origLink></item>
        <item>
            <title>Customisation at compile time</title>
            <category>Application Architecture</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/D6yidtT2eIk/185.aspx</link>
            <description>&lt;p&gt;On my current project we’re building multiple websites that share the majority of their logic but each have requirements relevant only to their target audience. This means we need a way to configure the common elements to behave appropriately to each scenario. This post discusses the mechanism I’ve chosen to do this.&lt;/p&gt;  &lt;p&gt;In building this mechanism I had a number of goals:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Minimal Duplication&lt;/strong&gt; It was not acceptable to simply duplicate whatever logic needed to vary. Whatever mechanism I chose has to keep as much logic common as possible to constraint development cost and ensure consistency of operation in the common elements. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Low Coupling&lt;/strong&gt; Most of the elements involved have no need to know about the specific sites and their requirements. These elements do need to know configuration values for their elements but this does not imply that they need to know how decisions for each site are made. This would tie each element to the current sites and require extensive modification if the feature distribution changes or new sites are added. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Simplicity&lt;/strong&gt; As this mechanism will be used throughout the system it needs to be simple to adopt so as not to negatively impact the maintainability of the system. Additionally it should not make the system difficult to deploy or run. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Robustness&lt;/strong&gt; The system should be reliable and not prone to the introduction of errors. In particular it should not increase the risk of errors at deployment. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Testable&lt;/strong&gt; As a strong proponent of TDD I need a solution that will support testing the elements behaviour in each scenario. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Low Effort&lt;/strong&gt; I’m looking for a mechanism that doesn’t take significant amounts of time to use, either during development or at deployment. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I also have an advantage in that I can make the decisions at compile time. In this scenario I don’t need the ability to dynamically change these behaviours. This means &lt;a href="http://abstractcode.com/abstractblog/archive/2009/04/26/158.aspx"&gt;I don’t actually need to configure things&lt;/a&gt; in the sense of having an XML configuration file or similar. Configuration is needed but this may occur at compile time.&lt;/p&gt;  &lt;p&gt;I’ve previously discussed &lt;a href="http://abstractcode.com/abstractblog/archive/2009/07/18/178.aspx"&gt;making configuration using the standard .NET infrastructure injectable&lt;/a&gt;. This does some of what I need, but it still requires XML configuration files which introduce significant complexity and risk to deployments. However I can get where I need to go by keeping the interfaces and changing the implementation.&lt;/p&gt;  &lt;p&gt;In my previous usage the code needing configuration has no dependency on the .NET configuration infrastructure. It happens to be provided an implementation that uses it, but this is not necessary. Hence I define interfaces that provide the necessary configuration options. For each site I provide implementations of the interface that supply configuration values relevant to that site. The dependency injection configuration for each site is configured to supply the implementations relevant to that site, and I have a complete solution that meets all my goals.&lt;/p&gt;  &lt;p&gt;We start with a configuration interface that may look something like:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:50ed64bf-1c3b-422f-9806-1577dd340f4f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color: #000000;"&gt; ISiteConfiguration
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeConfigurationValue { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeOtherConfigurationValue { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;We can start with a single interface and potentially break it apart as the number of properties grows. &lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8f2f773e-0a69-46f0-a16e-5a9338a446da" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeSiteConfiguration : ISiteConfiguration
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeConfigurationValue
    {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeOtherConfigurationValue
    {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
    }
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The implementation above provides fixed values for each of these configuration properties. There is nothing here to get wrong an deployment time, I can’t accidentally misconfigure a feature with an inappropriate configuration setting. I’m looking here to provide a different configuration between sites and other sites get supplied a different implementation of &lt;em&gt;ISiteConfiguration&lt;/em&gt;. This means that I can have some confidence that the set of configuration values will always be consistent.&lt;/p&gt;

&lt;p&gt;It is not mandatory to provide fixed values. The properties may apply logic to determine the configuration value, with the different implementations allowing this logic to be varied between uses (or to support fixed values in some places and calculated in others).&lt;/p&gt;

&lt;p&gt;In the site dependency injection configuration I then specify the appropriate implementation for that site. If I’m using a container that supports configuration in code I can have things such that the implementation to use is baked into the site and there is no configuration to go wrong between deployments. For instance with &lt;a href="http://structuremap.sourceforge.net/"&gt;StructureMap&lt;/a&gt; I could do something like:&lt;/p&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1517ee30-cacd-4399-92b0-465cbdb80038" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;ObjectFactory.Initialize(initialise &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    {
        initialise.ForRequestedType&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;ISiteConfiguration&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;().TheDefaultIsConcreteType&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;SomeSiteConfiguration&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
    });
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;So how does this measure up to my critieria:&lt;/p&gt;

&lt;p /&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Minimal Duplication&lt;/strong&gt; All the instances of a configuration decision are collected into a single location. With some refactoring duplication can essentially be eliminated. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Low Coupling&lt;/strong&gt; There is an abstraction between the use of the configuration and the making of the configuration decisions. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Simplicity&lt;/strong&gt; Using the configuration is as simple as injecting the configuration interface which is trivial with most containers. As demonstrated above there is nothing to configure at deployment which is about as simple as you can get. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Robustness&lt;/strong&gt; The solution ensures configuration decisions are provided, otherwise the system will fail quickly in development. At deployment there is nothing to go wrong as the system is already configured. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Testable&lt;/strong&gt; The configuration interfaces are easily mocked for testing purposes. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Low Effort&lt;/strong&gt; This utilises my existing DI setup and involves relatively trivial interfaces and implementations. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’m therefore pretty happy with this system. It can be seen that it is basically the Strategy pattern as applied to configuration, with the extra goodness that it comes pre-configured at deployment.&lt;/p&gt;

&lt;p&gt;I’ve discussed this in the context of multiple websites as that is the current scenario I’m dealing with, but this approach may be applied whenever multiple executables share common logic requiring configuration that may be defined at development. If you introduced some configuration for selecting configuration implementations only you could also adapt this to providing fixed sets of configuration for the same executable in multiple environments. This would maintain the majority of the benefits (consistent configuration sets, elimination of the majority of configuration values) for a relatively low cost in deployment effort and risk.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/185.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/D6yidtT2eIk" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/08/15/185.aspx</guid>
            <pubDate>Sat, 15 Aug 2009 06:22:33 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/185.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/08/15/185.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/185.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/08/15/185.aspx</feedburner:origLink></item>
        <item>
            <title>Lee Campbell talks interfaces and Unity</title>
            <category>Application Architecture</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/79kksOy2SwY/184.aspx</link>
            <description>&lt;p&gt;Further to the discussion that &lt;a href="http://rhysc.blogspot.com/2009/08/explicit-interfaces.html"&gt;Rhys Campbell&lt;/a&gt; and &lt;a href="http://abstractcode.com/abstractblog/archive/2009/07/09/173.aspx"&gt;myself&lt;/a&gt; have been having regarding interfaces, Lee Campbell has stepped in with &lt;a href="http://leecampbell.blogspot.com/2009/08/how-not-to-write-interface-in-c-30.html"&gt;a discussion of interfaces&lt;/a&gt; primarily related to &lt;a href="http://leecampbell.blogspot.com/2009/08/how-not-to-write-interface-in-c-30.html"&gt;Unity&lt;/a&gt;. Well worth a read, and I like to think somewhat of a justification to go download &lt;a href="http://structuremap.sourceforge.net/"&gt;StructureMap&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/184.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/79kksOy2SwY" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/08/15/184.aspx</guid>
            <pubDate>Sat, 15 Aug 2009 05:22:24 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/184.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/08/15/184.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/184.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/08/15/184.aspx</feedburner:origLink></item>
        <item>
            <title>On the granularity of services</title>
            <category>Application Architecture</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/93hVXbzunWs/183.aspx</link>
            <description>&lt;p&gt;When implementing services there are a number of practices I like to adopt. I have already discussed the &lt;a href="http://abstractcode.com/abstractblog/archive/2009/07/09/173.aspx"&gt;use of interfaces&lt;/a&gt; to define the service contract. This post is to discuss the granularity of the contracts in a system.&lt;/p&gt;  &lt;p&gt;In all but the most trivial of systems multiple service actions will be required. In providing these actions we can choose a position on a continuum. At one end we have a single contract that has all the actions as methods. At the other end we have a separate contract for each action. In between we have contracts that provide subsets of the actions. We must therefore make a decision as to where on this continuum we wish our system to be.&lt;/p&gt;  &lt;p&gt;The single contract that provides all actions is unwieldy and results in code that is difficult to understand and modify. I’d love to claim that this is a deliberate exaggeration that no one would ever do in practice but 9000+ line counter-examples I’ve encountered would make that a lie. Nevertheless this end of the continuum involves a complete lack of separation of concerns and demonstrates terrible maintainability.&lt;/p&gt;  &lt;p&gt;The question therefore is whether the other extreme is similarly bad and that an intermediate position with multiple contracts each containing multiple actions is the reasonable solution. I’ve certainly encountered the argument that having a single action per service interface will result in an unmanageable number of interfaces and classes. This argument holds that complexity of a large number of service interfaces is very high and hence we must consolidate them in order to make the complexity manageable.&lt;/p&gt;  &lt;p&gt;This position reminds me of political compromise where there is often a view that the centerist position between two extremes is preferable. I wont comment on the relative merits of this view in politics but I will assert that I do not believe it to be applicable here.&lt;/p&gt;  &lt;p&gt;I do not agree that many interfaces and classes is in this case more complex overall. Each class that implements a service interface will be less complex as it is now responsible only for a single action. Although there are more classes it is my contention that this implementation is overall simpler as all the elements of any service class are directly associated with the single action of the relevant contract. As the number of actions on the interface increase the number of concerns in the class not relevant to a selected action will also increase. This makes maintaining the service more difficult as the number of concerns increases the cognitive overhead of reasoning about the implementation.&lt;/p&gt;  &lt;p&gt;I also do not agree that fewer service interfaces is inherently simpler. Fine grained service contracts that are properly named can increase readability through additional information carried in the name. A service that requires an &lt;em&gt;IShoppingCartService&lt;/em&gt; may be doing anything to the shopping cart. A service that requires an &lt;em&gt;IClearShoppingCartService&lt;/em&gt; has a clearly indicated requirement to be able to clear the shopping cart contents. An additional benefit is that it can be determined from the number of dependencies that a service is dependent on too many external actions and that refactoring may be applicable.&lt;/p&gt;  &lt;p&gt;It may be suggested that dealing with a large number of services is difficult, error prone or inefficient. Again I do not agree. A decent Dependency Injection framework with automatic registration and wiring will handle the vast majority of the instantiation with some simple conventions (&lt;a href="http://structuremap.sourceforge.net/"&gt;StructureMap&lt;/a&gt;, my current DI container of choice, has some very nice options for this). In general avoiding considerations such as circular dependencies can be managed by avoiding circular references between namespaces and between service types within namespaces.&lt;/p&gt;  &lt;p&gt;Many service interfaces will mean many more service class instances to be instantiated. It is highly unlikely that this will be an efficiency issue. Stateless instances are cheap to instantiate and unlikely to be a significant resource problem in your application. It is much more likely that resource utilisation in the business logic will be orders of magnitude higher and a much greater concern in considering efficiency and performance.&lt;/p&gt;  &lt;p&gt;I therefore find that in the general case it is preferable to have a single action per service interface as this is the least complex and most maintainable option. However there are notable exceptions. In particular this is not (at least currently) my preferred option when dealing with communications using WCF.&lt;/p&gt;  &lt;p&gt;When defining a service contract in WCF there are a number of additional concerns that do not apply to general services, such as the configuration of endpoint addresses and communication channel properties, as well as the metadata to be defined on the service contract and implementation. This additional overhead means that there is additional complexity as the number of service contracts increases. As such a single action per contract is generally not the least complex option.&lt;/p&gt;  &lt;p&gt;Even in this exception I would not start reducing the granularity of business logic. My WCF services are generally facades that invoke business services rather than contain business logic themselves, and these business services would continue to have a single action per service contract.&lt;/p&gt;  &lt;p&gt;It should also be noted that having a single action on the service interface does not imply a single method on the implementing service class. The service class may have whatever methods make sense to effectively implement its function. Only one of these methods will be exposed (via the contract) to other types.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://rhysc.blogspot.com/"&gt;Rhys Campbell&lt;/a&gt; has recently discussed, both in response to my post on interfaces and &lt;a href="http://rhysc.blogspot.com/2009/08/explicit-interfaces.html"&gt;on his own blog&lt;/a&gt;, explicitly implementing interfaces so that you will have compile time errors if a method is removed from an interface but is maintained on a class that implements the interface. At least for services I don’t favour this approach as for a service interface with a single method if that method is removed then the interface itself is unnecessary. Hence I don’t feel explicit implementation is actually beneficial here. I can see value for other classes of interface which have multiple methods, but I’m currently unconvinced that the additional complexity of working with explicitly implemented methods is justified by what I see as relatively small benefits (compiler detection of obsolete methods, not removal of code bloat). It will be interesting to see if Rhys has further thoughts on this topic.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/183.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/93hVXbzunWs" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/08/12/183.aspx</guid>
            <pubDate>Tue, 11 Aug 2009 16:52:30 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/183.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/08/12/183.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/183.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/08/12/183.aspx</feedburner:origLink></item>
        <item>
            <title>Simple organisational improvements: Remember the alphabet</title>
            <category>Development Practices</category>
            <link>http://feedproxy.google.com/~r/Abstractcode/~3/CueyUdsIR7U/182.aspx</link>
            <description>&lt;p&gt;Software contains many examples where a number of named items must be listed in a context where the order is not important. This is particularly prevalent in configuration files but is also found in code. One example I’m currently looking at is a set of WCF service host classes that need to be run in a service. The order they start is irrelevant as long as they are all started. As there are a number of types it’d be nice if I didn’t have to scan the list every time I wanted to see if something was included.&lt;/p&gt;  &lt;p&gt;This is particularly a problem in large configuration files. Using search is an option but this is not always sufficient and fails for printed material. It is for this reason that I alphabetise these items unless ordering is significant. This simplifies the task of working with the items as it becomes predictable where they are located.&lt;/p&gt;  &lt;p&gt;This is useful for configuration files and list items. I’d not recommend it for use in organising methods and other type members. These should be organised by their function, alphabetising would generally decrease the readability of the code.&lt;/p&gt;&lt;img src="http://abstractcode.com/abstractblog/aggbug/182.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/Abstractcode/~4/CueyUdsIR7U" height="1" width="1"/&gt;</description>
            <dc:creator>Colin David Scott</dc:creator>
            <guid isPermaLink="false">http://abstractcode.com/abstractblog/archive/2009/07/22/182.aspx</guid>
            <pubDate>Wed, 22 Jul 2009 06:37:52 GMT</pubDate>
            <wfw:comment>http://abstractcode.com/abstractblog/comments/182.aspx</wfw:comment>
            <comments>http://abstractcode.com/abstractblog/archive/2009/07/22/182.aspx#feedback</comments>
            <wfw:commentRss>http://abstractcode.com/abstractblog/comments/commentRss/182.aspx</wfw:commentRss>
        <feedburner:origLink>http://abstractcode.com/abstractblog/archive/2009/07/22/182.aspx</feedburner:origLink></item>
    </channel>
</rss>
