<?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:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Coding Efficiency</title>
	
	<link>http://www.codingefficiency.com</link>
	<description>All about coding more efficient.</description>
	<lastBuildDate>Fri, 25 Dec 2009 19:18:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CodingEfficiency" /><feedburner:info uri="codingefficiency" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Ruby on Rails as a .Net developer</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/73py0TZc2yg/</link>
		<comments>http://www.codingefficiency.com/2009/12/25/ruby-on-rails-as-a-net-developer/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 19:14:07 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/2009/12/25/ruby-on-rails-as-a-net-developer/</guid>
		<description><![CDATA[Over the last few months I made an interesting transition, I switched from pure C# / .Net development using Windows to Ruby / Ruby on Rails using Linux.
Before I begin I have to say something about the context in which I used RoR. A new customer came to us and wanted a web based app [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few months I made an interesting transition, I switched from pure C# / .Net development using Windows to Ruby / Ruby on Rails using Linux.</p>
<p>Before I begin I have to say something about the context in which I used RoR. A new customer came to us and wanted a web based app for internal usage, it would be used by about 10 people. It should be accessible via Internet as the employees of said customer would work from home. I had a direct contact to the customer and deployed 3 to 4 times a week. Therefore, using RoR, even with next to no prior experience, wasn’t really a risk as the customer could always see my progress and had a more and more usable application from day 1.</p>
<p>The first problem was the IDE, I really love Visual Studio, at least with ReSharper and I think it was a logical choice to use JetBrains Rubymine. I don’t regret it, instead I’d advice every .Net developer that wants to try RoR to use Rubymine. </p>
<p>The second problem was the OS switch from Windows to Linux, I used Ubuntu 9.04 and later 9.10. I started with a VM and later switched to dual boot, please don’t make my mistake: NEVER install Linux before Windows. Windows 7 kills Ubuntus boot loader and as someone with next to no Linux experience it’s some trouble.</p>
<p>Linux is a pretty nice OS though, the GUIs (Gnome, KDE etc.) are pretty bad compared to Windows, so don’t expect too much, they are usable but don’t stand a chance against Windows 7. As I was used to do everything with a nice blinky blinky GUI, I tried to avoid the console, a terrible mistake. Linux’ console interface is powerful, easy to learn and (in general) way faster than doing it with the GUI. Before anyone asks, I don’t want to code without a full fledged IDE, so VI, VIM Nano etc. weren’t an option.</p>
<p>Back to topic or better let’s start with the topic <img src='http://www.codingefficiency.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>First, it’s important to realize that Ruby and Ruby on Rails are two distinct things, Ruby is a language and Ruby on Rails is framework written in Ruby. I didn’t read a book to learn either, instead I just started hacking away. Whenever I found a problem I asked mr. Google.</p>
<p>What I learned in the very beginning, if you don’t find a solution on google’s first page, you are trying to do it wrong. Rails is an opinionated framework, if you try to do something in a different way you are pretty much lost, but this is not a downside, it’s actually very good as you can say that there *is* THE way.</p>
<p>Asp.Net MVC is the complete opposite, you have plenty of choices like what data access to use etc. You won’t find a single solution for any given problem, instead you’ll get people that tell you NHibernate is the best, others say that pure Ado.Net is the best etc. But this too is not a downside, it’s just the opposite approach. RoRs pros in this area are ASPs contras and vice versa.</p>
<p>Rails is completely model driven and the model is database driven, Active Record is a core part of RoR. For guys that are new to the MVC pattern or have a pretty good understanding of how it should work but haven’t actually used it, Rails is really great as you’ll learn what MVC really means. ASP.Net, again, gives you the choice to do MVC or crappy VC with all stuff inside the controller.</p>
<p>The Rails ecosystem is really great, for almost any infrastructure concern there is a plugin waiting to be used. Be it background tasks, authentication you name it. Asp.Net has the .Net framework that might lack some comfort but is way more powerful.</p>
<p>I like and love C# but Ruby is so much more expressive and easier to learn or write. Even subtle things like writing <em>do_stuff if condition_is_met?</em>&#160; instead of <em>if (condition_is_met){ do_stuff(); }</em> make a difference in terms of expressiveness.</p>
<p>The really disturbing thing are the background tasks. For active record and the configurations to work properly, it’s required to load the entire rails stack every time you want to execute a task. If those tasks are done every hour, it’s not a problem but if you want to do it every minute, it’s a significant overhead. It takes about 10 seconds for the rails stack to load, that’s 10 seconds of 100% CPU usage every minute for about 3 to 5 seconds of work. To get rid of this, I had to take a pretty hacky way, instead of running such a background task, I simply issue a web request to a specific url.</p>
<p>One thing you’ll notice is that Active Record is both, RoRs strength and weakness. For all but the simplest queries the performance is not acceptable once your db reaches a specific size and you have to fall back to raw sql. And if I may give you an advice, if possible use a copy of the production database for performance optimizing, nothing is more accurate and useful. Please note that I really love optimizing queries and that I have a pretty low tolerance before I start to work on a query (again). </p>
<p>Deployment with RoR used to be pretty hard, but that’s no longer the case. Just google Phusion Passenger and Capistrano. A single command (cap deploy) is enough and you’re done. It’ll take care of almost anything out of the box and is easily extendable.</p>
<p>Early next year, I’m going to work on a personal project with ASP.Net MVC (and Visual Studio &lt;3) as I’d like to see how it feels after working with RoR.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/_r9os3_uC6deAk9rMOOTNb-e4ic/0/da"><img src="http://feedads.g.doubleclick.net/~a/_r9os3_uC6deAk9rMOOTNb-e4ic/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_r9os3_uC6deAk9rMOOTNb-e4ic/1/da"><img src="http://feedads.g.doubleclick.net/~a/_r9os3_uC6deAk9rMOOTNb-e4ic/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=73py0TZc2yg:PpC-iScTzFg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=73py0TZc2yg:PpC-iScTzFg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=73py0TZc2yg:PpC-iScTzFg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=73py0TZc2yg:PpC-iScTzFg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=73py0TZc2yg:PpC-iScTzFg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=73py0TZc2yg:PpC-iScTzFg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=73py0TZc2yg:PpC-iScTzFg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/73py0TZc2yg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/12/25/ruby-on-rails-as-a-net-developer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/12/25/ruby-on-rails-as-a-net-developer/</feedburner:origLink></item>
		<item>
		<title>Rails 2.3.4 – Ubuntu 9.10 – Enterprise Ruby 1.8.7</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/wraWGwQ7dZw/</link>
		<comments>http://www.codingefficiency.com/2009/11/23/rails-2-3-4-ubuntu-9-10-enterprise-ruby-1-8-7/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 14:59:29 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/2009/11/23/rails-2-3-4-ubuntu-9-10-enterprise-ruby-1-8-7/</guid>
		<description><![CDATA[
If you can live without the edge Ruby / Rails stuff, I’d recommend you to use Enterprise Ruby. Less memory consumption. Easy to install. Stable.
To install it, just get the newest tarball (at the time of writing this, its this). Unpack it and run the installer. The installer will check if you have all the [...]]]></description>
			<content:encoded><![CDATA[</p>
<p>If you can live without the edge Ruby / Rails stuff, I’d recommend you to use <a href="http://www.rubyenterpriseedition.com">Enterprise Ruby</a>. Less memory consumption. Easy to install. Stable.</p>
<p>To install it, just get the newest tarball (at the time of writing this, its <a href="http://rubyforge.org/frs/download.php/66162/ruby-enterprise-1.8.7-2009.10.tar.gz">this</a>). Unpack it and run the installer. The installer will check if you have all the required libraries, if you don’t, just install them, it shoudln’t be more than three.</p>
<p>Usually, the defaults are just fine, if you leave them, it will install ruby in /opt/ruby-enterprise-xxx. When your done, you should symlink the following:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:b5a3db4f-6272-4f3c-8244-2fd9370d4e7a" class="wlWriterEditableSmartContent">
<pre name="code" class="xml:nogutter">sudo cd /opt/ruby-enterprise-1.8.7-2009.10/bin
sudo ln ./ruby /usr/local/bin/ruby
sudo ln ./rails /usr/local/bin/rails
sudo ln ./rake /usr/local/bin/rake
sudo ln ./gem /usr/local/bin/gem
</pre>
</div>
<p>If you want <a href="http://www.modrails.com/">passenger</a>, you could run the passenger nginx/apache installer, a really nice way to deploy rails apps.</p>
<p>You’re done. Just one thing to remember, if you install gems that create an executeable like capistrano, javan-whenever, cucumber etc, you should symlink them as well, otherwise you’ll have to call them from /opt/ruby-enterprise-xxx/bin</p>
<p>One more nice thing, if you installed postgresql and libpq-dev it will automatically install the pg-gem. This works with mysql and sqlite as well.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/xzrV6j_pIE1p95hp6N05F95INXo/0/da"><img src="http://feedads.g.doubleclick.net/~a/xzrV6j_pIE1p95hp6N05F95INXo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/xzrV6j_pIE1p95hp6N05F95INXo/1/da"><img src="http://feedads.g.doubleclick.net/~a/xzrV6j_pIE1p95hp6N05F95INXo/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=wraWGwQ7dZw:U0Sx_fWCdRk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=wraWGwQ7dZw:U0Sx_fWCdRk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=wraWGwQ7dZw:U0Sx_fWCdRk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=wraWGwQ7dZw:U0Sx_fWCdRk:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=wraWGwQ7dZw:U0Sx_fWCdRk:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=wraWGwQ7dZw:U0Sx_fWCdRk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=wraWGwQ7dZw:U0Sx_fWCdRk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/wraWGwQ7dZw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/11/23/rails-2-3-4-ubuntu-9-10-enterprise-ruby-1-8-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/11/23/rails-2-3-4-ubuntu-9-10-enterprise-ruby-1-8-7/</feedburner:origLink></item>
		<item>
		<title>Singleton Pattern – There Can Only Be One</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/B38wFvqq6ZE/</link>
		<comments>http://www.codingefficiency.com/2009/08/20/singleton-pattern-there-can-only-be-one/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 12:27:36 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=405</guid>
		<description><![CDATA[A short description of the singleton pattern with an example in C#.]]></description>
			<content:encoded><![CDATA[<p>The singleton pattern belongs to the creational patterns and is widely used in one form or another.</p>
<h3>Use Cases</h3>
<p>If you need to access an object throughout the application, like settings, the singleton pattern could be considered.</p>
<p>If you have objects that are very expensive to create but are state less (it doesn’t matter if you use instance a, instance b or instance c), the singleton pattern can improve the performance.</p>
<h3>How does it work</h3>
<p>The singleton pattern limits the number of instances of a given type, usually, the number is limited to just one.</p>
<p>The classical singleton pattern forbids direct instantiation of the object. Instead, you have to call a static method to get the current instance.</p>
<h3>Example (in C#)</h3>
<pre name="code" class="c-sharp:nocontrols">
public sealed class Settings
{
    private static readonly Settings _instance = new Settings();
    static Settings() { }
    private Settings() { }

    public static Settings Instance
    {
        get { return _instance; }
    }
}
</pre>
<h3>Another Method</h3>
<p>Many IoC-container have some kind of lifecycle and most of them have a lifecycle called singleton or similar. </p>
<p>Whenever the container is asked for a type with it’s lifecycle set to singleton, the container will try to find an existing instance of the requested type. </p>
<p>If no such instance exists, the container will create one and save it for all further requests. </p>
<p>If the type exists, the container will simply return the existing instance.</p>
<h3>Drawbacks</h3>
<p>Without an IoC container, you rely on the implementation of the singleton in regards to unit testing.</p>
<p>A singleton is basically nothing more than a global variable. Therefore, all negative aspects of global variables apply to a singleton.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/Tzml4M0eoTQ-w1jeEHI4e2pG2PA/0/da"><img src="http://feedads.g.doubleclick.net/~a/Tzml4M0eoTQ-w1jeEHI4e2pG2PA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Tzml4M0eoTQ-w1jeEHI4e2pG2PA/1/da"><img src="http://feedads.g.doubleclick.net/~a/Tzml4M0eoTQ-w1jeEHI4e2pG2PA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=B38wFvqq6ZE:gd3y9fwOFAY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=B38wFvqq6ZE:gd3y9fwOFAY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=B38wFvqq6ZE:gd3y9fwOFAY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=B38wFvqq6ZE:gd3y9fwOFAY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=B38wFvqq6ZE:gd3y9fwOFAY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=B38wFvqq6ZE:gd3y9fwOFAY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=B38wFvqq6ZE:gd3y9fwOFAY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/B38wFvqq6ZE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/08/20/singleton-pattern-there-can-only-be-one/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/08/20/singleton-pattern-there-can-only-be-one/</feedburner:origLink></item>
		<item>
		<title>Facade Pattern – If You Have Something To Hide</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/ehOPUB28TL0/</link>
		<comments>http://www.codingefficiency.com/2009/08/19/facade-pattern-if-you-have-something-to-hide/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 18:44:39 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=398</guid>
		<description><![CDATA[A short description of the facade pattern with an example in C#.]]></description>
			<content:encoded><![CDATA[<p>The facade pattern belongs to the structural design patterns and is used rather often.</p>
<h3>Use cases</h3>
<p>If you have a bunch of low level services and want to consume them together in the UI, the facade pattern can help with that.</p>
<p>If you have to talk to an ugly legacy application, the facade pattern can be used to built an anti corruption layer.</p>
<p>If the underlying services take many arguments but your applications only uses very few of them and uses default values for the rest, the facade pattern can be used to hide those useless arguments. Doing so can lead to simpler and more readable calling code.</p>
<h3>How does it work?</h3>
<p>The mechanic behind the facade pattern is very easy to understand. </p>
<p>Imagine you want to display customers and their orders, the data access might be splitted up into a customerDAO and a orderDAO. </p>
<p>Without the facade pattern, you’d have to call the customerDAO for all customers and afterwards the orderDAO to receive all orders for each customer.</p>
<p>The facade pattern would simply hide those two calls and make them internally, exposing just a single method GetCustomersWithOrders(). </p>
<p><img src="http://yuml.me/diagram/scruffy/class/[CustomerDAO], [OrderDAO], [Facade], [Facade]uses-.-&gt;[CustomerDAO], [Facade]uses-.-&gt;[OrderDAO], [Client]uses-.-&gt;[Facade]." alt="facadepattern uml" /></p>
<h3>Example (in C#)</h3>
<p>First, the very basic DAOs:</p>
<pre name="code" class="c-sharp:nocontrols">
public class CustomerDAO
{
    public Customer[] GetAllCustomers()
    {
        // Db Access...
    }
}

public class OrderDAO
{
    public Order[] GetOrdersWithCustomerIn(
        params Guid[] customerIds)
    {
        // Db Access...
    }
}
</pre>
<p>The UI that consumes those two DAOs:</p>
<pre name="code" class="c-sharp:nocontrols">
public class ClientWithoutFacade
{
    public void DisplayCustomersWithOrders()
    {
        var customers = new CustomerDAO()
            .GetAllCustomers();

        var orders = new OrderDAO()
            .GetOrdersWithCustomers(
                customers.Select(x => x.Id).ToArray()
             );

        var custWithOrders = new List&lt;CustomerWithOrders>();

        foreach (var customer in customers)
        {
            var cwo = new CustomerWithOrders(customer);
            cwo.AddOrders(orders
                .Where(x => x.CustomerId == cwo.Id));
            custWithOrders.Add(cwo);
        }

        // Fancy UI binding....
    }
}
</pre>
<p>Now, the facade and the new UI client</p>
<pre name="code" class="c-sharp:nocontrols">
public class Facade
{
    public CustomerWithOrder[] GetCustomersWithOrders()
    {
        var customers = new CustomerDAO()
            .GetAllCustomers();

        var orders = new OrderDAO()
            .GetOrdersWithCustomers(
                customers.Select(x => x.Id).ToArray()
             );

        var custWithOrders = new List&lt;CustomerWithOrders>();

        foreach (var customer in customers)
        {
            var cwo = new CustomerWithOrders(customer);
            cwo.AddOrders(orders
                .Where(x => x.CustomerId == cwo.Id));
            custWithOrders.Add(cwo);
        }

        return custWithOrders.ToArray();
    }
}

public class ClientWithFacade
{
    public void DisplayCustomersWithOrders()
    {
        var customers = new Facade().GetCustomersWithOrders();
        // Fancy UI binding....
    }
}
</pre>

<p><a href="http://feedads.g.doubleclick.net/~a/cb_UCA5IRnVe8QwBINKfBiacnEg/0/da"><img src="http://feedads.g.doubleclick.net/~a/cb_UCA5IRnVe8QwBINKfBiacnEg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/cb_UCA5IRnVe8QwBINKfBiacnEg/1/da"><img src="http://feedads.g.doubleclick.net/~a/cb_UCA5IRnVe8QwBINKfBiacnEg/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=ehOPUB28TL0:p5G_jauenKs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=ehOPUB28TL0:p5G_jauenKs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=ehOPUB28TL0:p5G_jauenKs:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=ehOPUB28TL0:p5G_jauenKs:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=ehOPUB28TL0:p5G_jauenKs:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=ehOPUB28TL0:p5G_jauenKs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=ehOPUB28TL0:p5G_jauenKs:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/ehOPUB28TL0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/08/19/facade-pattern-if-you-have-something-to-hide/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/08/19/facade-pattern-if-you-have-something-to-hide/</feedburner:origLink></item>
		<item>
		<title>Unit Testing and NHibernate – Quick Start</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/vRJnIYAlepI/</link>
		<comments>http://www.codingefficiency.com/2009/08/12/unit-testing-and-nhibernate-quick-start/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 18:13:46 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=381</guid>
		<description><![CDATA[A quick start solution for nhibernate and inmemory unit tests.]]></description>
			<content:encoded><![CDATA[<p> I made a very basic solution for those who have trouble to get NHibernate up and running with unit tests.</p>
<ul>
<li>Configuration is done via xml.</li>
<li>LinFu is used as proxy factory.</li>
<li>MbUnit / Galio is used for testing, dlls are included.</li>
<li>NHibernate 2.1 and all the required files are included.</li>
<li>SQLite is included for both, 32bit 64bit.</li>
</ul>
<p>For those who have a 64bit os, as long as you use the ReSharpers test runner, everything should work fine. If you want to use another test runner, Icarus for example, you&#8217;d have to reference System.Data.SQLite.DLL from lib/sqlite64.</p>
<p>If there is anything wrong, please leave a comment. </p>
<p>The archive is available <a href='http://www.codingefficiency.com/wp-content/uploads/2009/08/NHibernateBasics.zip'>here</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/A5C-BVRBW_Nlc78cb4W4_zLv4MM/0/da"><img src="http://feedads.g.doubleclick.net/~a/A5C-BVRBW_Nlc78cb4W4_zLv4MM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/A5C-BVRBW_Nlc78cb4W4_zLv4MM/1/da"><img src="http://feedads.g.doubleclick.net/~a/A5C-BVRBW_Nlc78cb4W4_zLv4MM/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=vRJnIYAlepI:nDW7X_dw9kU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=vRJnIYAlepI:nDW7X_dw9kU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=vRJnIYAlepI:nDW7X_dw9kU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=vRJnIYAlepI:nDW7X_dw9kU:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=vRJnIYAlepI:nDW7X_dw9kU:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=vRJnIYAlepI:nDW7X_dw9kU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=vRJnIYAlepI:nDW7X_dw9kU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/vRJnIYAlepI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/08/12/unit-testing-and-nhibernate-quick-start/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/08/12/unit-testing-and-nhibernate-quick-start/</feedburner:origLink></item>
		<item>
		<title>Useful Extension Methods</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/hLd1GzSt7X0/</link>
		<comments>http://www.codingefficiency.com/2009/08/10/useful-extension-methods/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 17:27:56 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=364</guid>
		<description><![CDATA[Some useful extension methods for Action, Action&#60;T> Func&#60;TResult>, IDataRecord and DataRow]]></description>
			<content:encoded><![CDATA[<p>Since C# 3.0 it’s possible to write extension methods, these are very useful if you want to extend existing classes but don’t have access to them or if you simply want to add some convenience methods. The latter is what I did here.</p>
<p>If you want to know more about extension methods, you should take a look at <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">this</a></p>
<h3>Action Extensions</h3>
<p>I really like the generic Action, Action&lt;T&gt; but i don’t to check for null every single time I want to fire them:</p>
<pre name="code" class="c-sharp:nocontrols">
public static class ActionExtensions
{
    public static void Fire(this Action action)
    {
        if (action != null)
        {
            action();
        }
    }

    public static void Fire&lt;T>(this Action&lt;T> action, T arg)
    {
        if (action != null)
        {
            action(arg);
        }
    }

    public static void Fire&lt;T, T2>(this Action&lt;T, T2> action,
        T arg1, T2 arg2)
    {
        if (action != null)
        {
            action(arg1, arg2);
        }
    }
}
</pre>
<p>The same can be done with Func&lt;T, TResult&gt;:</p>
<pre name="code" class="c-sharp:nocontrols">
public static TResult Fire&lt;TResult>(this Func&lt;TResult> func)
{
    return func != null ? func() : default(TResult);
}

public static TResult Fire&lt;TResult, T>(this Func&lt;T, TResult> func, T arg)
{
    return func != null ? func(arg) : default(TResult);
}
</pre>
<h3>DataRow/ IDataRecord Extensions</h3>
<p>If you have to work with IDataReaders or DataRows, chances are high that some of your code looks like this:</p>
<pre name="code" class="c-sharp:nocontrols">
public void DoSomething(DataTable table)
{
    foreach (DataRow row in table.Rows)
    {
        var customer = new Customer
        {
            Id = (Guid)row["Id"],
            FirstName = row["FirstName"] == DBNull.Value
                                      ? string.Empty
                                      : (string)row["FirstName"],
            CompanyName = row["CompanyName"] == DBNull.Value
                                      ? null
                                      : (string)row["CompanyName"]
        };
    }
}

public void DoOtherThings(Customer customer, DataRow row)
{
    row["Id"] = customer.Id;
    row["FirstName"] = customer.FirstName == null
                                    ? string.Empty
                                    : customer.FirstName;
    row["CompanyName"] = customer.CompanyName == null
                        || customer.CompanyName == string.Empty
                                    ? DBNull.Value
                                    : customer.CompanyName;
}
</pre>
<p>These extension methods will reduce the code duplication and make it a little more readable:</p>
<pre name="code" class="c-sharp:nocontrols">
public static class DataRecordExtensions
{
    public static T GetValue&lt;T>(this IDataRecord dataRecord, string column)
    {
        return GetValue&lt;T>(dataRecord, column, true);
    }

    public static T GetValue&lt;T>(this IDataRecord dataRecord, string column,
                                              bool nullString)
    {
        var value = dataRecord[column];

        if (value == DBNull.Value)
        {
            if (typeof(T) == typeof(string))
            {
                if (!nullString)
                {
                    return (T) (object) string.Empty;
                }
            }

            return default(T);
        }

        return (T)value;
    }

    public static T GetValue&lt;T>(this DataRow row, string column)
    {
        return GetValue&lt;T>(row, column, true);
    }

    public static T GetValue&lt;T>(this DataRow row, string column,
                                              bool nullString)
    {
        var value = row[column];

        if (value == DBNull.Value)
        {
            if (typeof(T) == typeof(string))
            {
                if (!nullString)
                {
                    return (T)(object)string.Empty;
                }
            }

            return default(T);
        }

        return (T)value;
    }

    public static void SetValue&lt;T>(this DataRow row, T value,
                                                 string column)
    {
        SetValue(row, value, column, false);
    }

    public static void SetValue&lt;T>(this DataRow row, T value,
       string column, bool defaultDbNull)
    {
        object val = value;

        if (defaultDbNull)
        {
            if (ReferenceEquals(value, default(T)))
            {
                val = DBNull.Value;
            }
            else if (typeof(T) == typeof(string))
            {
                if ((string)(object)value == string.Empty)
                {
                    val = DBNull.Value;
                }
            }
        }
        else if(val == null)
        {
            if (typeof(T) == typeof(string))
            {
                val = string.Empty;
            }
            else
            {
                val = DBNull.Value;
            }
        }

        row[column] = val;
    }
}
</pre>
<p>The code above could now be written as:</p>
<pre name="code" class="c-sharp:nocontrols">
public void DoSomeThing(DataTable table)
{
    foreach (DataRow row in table.Rows)
    {
        var customer = new Customer
        {
            Id = row.GetValue&lt;Guid>("Id"),
            FirstName = row.GetValue&lt;string>("FirstName"),
            CompanyName = row.GetValue&lt;string>("CompanyName", true)
        };
    }
}

public void DoOtherThings(Customer customer, DataRow row)
{
    row.SetValue(customer.Id, "Id");
    row.SetValue(customer.FirstName, "FirstName");
    row.SetValue(customer.CompanyName, "CompanyName", true);
}
</pre>

<p><a href="http://feedads.g.doubleclick.net/~a/nsKVfXi9TcPbtMlCaODV0as76uo/0/da"><img src="http://feedads.g.doubleclick.net/~a/nsKVfXi9TcPbtMlCaODV0as76uo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/nsKVfXi9TcPbtMlCaODV0as76uo/1/da"><img src="http://feedads.g.doubleclick.net/~a/nsKVfXi9TcPbtMlCaODV0as76uo/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=hLd1GzSt7X0:7YZi61GEvFk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=hLd1GzSt7X0:7YZi61GEvFk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=hLd1GzSt7X0:7YZi61GEvFk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=hLd1GzSt7X0:7YZi61GEvFk:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=hLd1GzSt7X0:7YZi61GEvFk:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=hLd1GzSt7X0:7YZi61GEvFk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=hLd1GzSt7X0:7YZi61GEvFk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/hLd1GzSt7X0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/08/10/useful-extension-methods/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/08/10/useful-extension-methods/</feedburner:origLink></item>
		<item>
		<title>Is applying good practices always good?</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/G6koaqDtHhM/</link>
		<comments>http://www.codingefficiency.com/2009/08/05/is-applying-good-practices-always-good/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 20:48:47 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=360</guid>
		<description><![CDATA[Today a new trainee was assigned to me. He just completed his in-firm training in another company and applied for a job.
In order to find out what he knows, I thought it would be a good idea to go through the code of some tools.
We began with a tool that prints invoices. It has two [...]]]></description>
			<content:encoded><![CDATA[<p>Today a new trainee was assigned to me. He just completed his in-firm training in another company and applied for a job.</p>
<p>In order to find out what he knows, I thought it would be a good idea to go through the code of some tools.</p>
<p>We began with a tool that prints invoices. It has two Forms and no architecture at all. Although the code was a mess, he could easily tell how it worked.</p>
<p>After that, we looked at a tool that queried some data, applied some validation to it and if necessary corrected it. This tool used the command pattern for validation / correction and loaded those commands via reflection (about 50 in total). It was way harder for him to tell what was going on. He asked why anyone would do things this complicated and suggested to use a switch block instead. </p>
<p>The third tool was a little more complicated, the UI used the passive view pattern, persistence was dealt with by NHibernate, StructureMap wired it all up and a test suite covered most parts. I wrote it some time ago but could still easily navigate through it as it was well structured but he was totally overwhelmed and unable to say anything other than “it is too complicated”. </p>
<p>Using these good practices has advantages but these advantages are not free. </p>
<p>There are way more developers like my trainee that never heard about good practices and the like than those who heard about these practices let alone applying them. These developers usually just want to get the job done and mostly they do so, on time.</p>
<p>If you work in a very small IT department that only deals with in house stuff, should you use patterns, unit testing and other good practices or should you just do it “the good old way”?</p>
<p>If you do it “the good old way” maintenance costs increase, time to bug fix / feature are less predictable yet everyone should be able work with it.</p>
<p>If you use good practices, maintaining your applications either can’t be done by every “developer from the street” or requires high upfront learning effort. On the other side, after this initial learning phase is over, the maintenance costs should decrease and time to bug fix / feature should be more predictable.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/ffgeQG3HT9FOQ9yLYodAqy9MRXk/0/da"><img src="http://feedads.g.doubleclick.net/~a/ffgeQG3HT9FOQ9yLYodAqy9MRXk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ffgeQG3HT9FOQ9yLYodAqy9MRXk/1/da"><img src="http://feedads.g.doubleclick.net/~a/ffgeQG3HT9FOQ9yLYodAqy9MRXk/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=G6koaqDtHhM:zOuPRc1yWnQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=G6koaqDtHhM:zOuPRc1yWnQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=G6koaqDtHhM:zOuPRc1yWnQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=G6koaqDtHhM:zOuPRc1yWnQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=G6koaqDtHhM:zOuPRc1yWnQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=G6koaqDtHhM:zOuPRc1yWnQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=G6koaqDtHhM:zOuPRc1yWnQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/G6koaqDtHhM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/08/05/is-applying-good-practices-always-good/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/08/05/is-applying-good-practices-always-good/</feedburner:origLink></item>
		<item>
		<title>Database Myths: Stored Procedures vs. Ad Hoc Queries</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/eyXkM9Ag_4k/</link>
		<comments>http://www.codingefficiency.com/2009/08/03/database-myths-stored-procedures-vs-ad-hoc-queries/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 14:24:52 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Myths]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=356</guid>
		<description><![CDATA[A post about common myths sorrounding stored procedures and ad hoc queries in MS SQL Server 2005/8]]></description>
			<content:encoded><![CDATA[<p>Note: I’m talking about MS SQL Server 2005/8, it may or may not apply to other dbs.</p>
<h3>Security:</h3>
<blockquote><p>You can set rights on the procedure level.</p>
</blockquote>
<p>Yes, but you can also set the rights on view / table level.</p>
<blockquote><p>Stored procedures are always immune to sql injection.&#160; <br />Ad hoc queries are always vulnerable to sql injection.</p>
</blockquote>
<p>Wrong.</p>
<p>Stored procedures that take strings as parameters and execute them as sql are open to sql injection.</p>
<p>Parameterized ad hoc queries prevent sql injection.</p>
<h3>Performance / Execution Plan:</h3>
<blockquote><p>The execution plans for stored procedures are cached whereas ad hoc queries generate new plans each time they are executed.</p>
</blockquote>
<p>Wrong. </p>
<p>If there is an execution plan that fits the query, it will&#160; be used. The db doesn’t care if the query was an ad hoc query or a stored procedure.</p>
<p>Stored procedures are not pre compiled, they are compiled at runtime, just like any other query.</p>
<p>There is one thing to note though, if you change a parameter (not the value but for example nvarchar(9) instead of nvarchar(22)) a new plan will be generated.</p>
<p>Source: <a href="http://technet.microsoft.com/en-us/library/ms190201%28SQL.90%29.aspx">MSDN1</a>, <a href="http://technet.microsoft.com/en-us/library/ms181055%28SQL.90%29.aspx">MSDN2</a>, <a href="http://technet.microsoft.com/en-us/library/ms175580%28SQL.90%29.aspx">MSDN3</a>, <a href="http://testdrivendevelopment.wordpress.com/2009/03/10/nhibernate-queries-sql-server-execution-plans/">Blog</a></p>
<h3>Centralization:</h3>
<blockquote><p>With stored procedures you “have it all in one place”</p>
</blockquote>
<p>Wrong.</p>
<p>You don’t have it one place, it’s in your application as calling code and in your db as stored procedure. If the procedure changes and requires different parameters the calling code has to change too.</p>
<blockquote><p>You can reuse stored procedures and only have to optimize them once</p>
</blockquote>
<p>While that is true, you have to see the other side too. If multiple applications use a stored procedure you can’t easily change the procedure. You’d have to make sure that you didn’t break any of the other applications.</p>
<h3>Optional Parameters:</h3>
<p>If you use stored procedures for updates, you have to choose between efficient queries and maintenance.</p>
<p>If you want to go with efficient queries, you’d have to create stored procedures for every use case that changes something. If you have a customer table, the procedures might be ChangeCustomerAddress, ChangeCustomerBillingAddress, ChangeCustomerName etc.</p>
<p>If you favor easier maintenance, you’d just have one big update procedure that takes all values at once.</p>
<p>With Ad Hoc Queries you can have both. It’s easier to maintain and more efficient if you have a single method that builds a query that excludes all unnecessary columns.</p>
<h3>Changes:</h3>
<blockquote><p>You can change a stored procedure without redeploying the application</p>
</blockquote>
<p>That is partially true.</p>
<p>If you really have to change a query, you’ll likely have to change the application too.</p>
<p>Tuning is another story, you can tune a stored procedure without touching the application. On the other hand, you could also tune the query in isolation and change the query when the application has to be recompiled for other reasons.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/iMwWPHS88vmtdxWXBDcDiWuu7Ww/0/da"><img src="http://feedads.g.doubleclick.net/~a/iMwWPHS88vmtdxWXBDcDiWuu7Ww/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/iMwWPHS88vmtdxWXBDcDiWuu7Ww/1/da"><img src="http://feedads.g.doubleclick.net/~a/iMwWPHS88vmtdxWXBDcDiWuu7Ww/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=eyXkM9Ag_4k:OT7gMUuqCWY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=eyXkM9Ag_4k:OT7gMUuqCWY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=eyXkM9Ag_4k:OT7gMUuqCWY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=eyXkM9Ag_4k:OT7gMUuqCWY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=eyXkM9Ag_4k:OT7gMUuqCWY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=eyXkM9Ag_4k:OT7gMUuqCWY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=eyXkM9Ag_4k:OT7gMUuqCWY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/eyXkM9Ag_4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/08/03/database-myths-stored-procedures-vs-ad-hoc-queries/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/08/03/database-myths-stored-procedures-vs-ad-hoc-queries/</feedburner:origLink></item>
		<item>
		<title>Maintainable but not Learnable?</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/UXaVoxGU0Ek/</link>
		<comments>http://www.codingefficiency.com/2009/07/30/maintainable-but-not-learnable/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 20:31:22 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[Reply]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=351</guid>
		<description><![CDATA[Although learning a code base is required to effectively maintain it, learning and maintaining it are two different things that should be looked at independently. The learning cost is a one time cost, fixing a bug or adding a feature isn’t. ]]></description>
			<content:encoded><![CDATA[<p>This is in reply to Ayendes <a href="http://ayende.com/Blog/archive/2009/07/29/what-is-maintainable.aspx">post</a>.</p>
<p>For those who haven’t followed the discussion, it all began with the release of <a href="http://nhforge.org/">NHibernate</a> 2.1. </p>
<p>Shortly thereafter, Patrick Smacchia made this <a href="http://codebetter.com/blogs/patricksmacchia/archive/2009/07/21/nhibernate-2-1-changes-overview.aspx">post</a> where he analyzed what had changed in NHibernates code base and measured its quality with <a href="http://www.ncover.com/">NCover</a>. He came to the conclusion that it must be a daily pain to maintain the code.</p>
<p>In <a href="http://ayende.com/Blog/archive/2009/07/21/answering-to-nhibernate-codebase-quality-criticism.aspx">reply</a> to that, Ayende said that it is structured, although not by name spaces and the like (Patrick’s analyses made that assumption) but different. To underline that NHibernate is indeed maintainable, he pointed out that several new committers joined last year, that there were many new features while at the same time fewer issues.</p>
<p><a href="http://weblogs.asp.net/fbouma/">Frans Bouma</a> made a comment where he said that even a mess looks like something acceptable to those who work with it. This lead to Ayendes post to which I respond.</p>
<p>I have to say, I agree with Ayende. Rating how maintainable a code base is can only be done by those who actually maintain it.</p>
<p>If you have to deal with an application written in a language you don’t know, would you say it’s unmaintainable just because you&#8217;re not familiar with the language? Code bases are to some extent like that as many, especially larger ones, have their very own conventions and those conventions aren’t limited to naming conventions and the like but can span the structural or architectural level too.</p>
<p>The VB6 application I talked about in my <a href="http://www.codingefficiency.com/2009/07/11/refactoring-series-part-1/">refactoring series</a> was very easy to learn as the only used pattern was copy and paste. You’d simply go in, copy what you need and paste it into its new place, everyone could do it, everyone did it. However, the result was an unmaintainable mess. </p>
<p>Therefore I’d say that although learning a code base is required to effectively maintain it, learning and maintaining it are two different metrics that should be looked at independently. The learning cost is a one time cost, fixing a bug or adding a feature isn’t.</p>
<p>Yet another story is ongoing learning, this has directly to do with maintainability as no one can remember every line of an entire code base that has a sufficient size. </p>
<p>If a code base is consistent with its conventions, no matter how strange they may first seem to outsiders, there shouldn’t be a problem as this ongoing learning cost will stay at a low level. </p>
<p>Please note that I’m not saying “that’s a convention” excuses everything. There has to be a structure and concepts have to be followed but this structure or these concepts don’t necessarily have to jump the outsider right in the eye as it is the case in the classical three tier architecture for example.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/1k1VhHHR72f4sxmT_rf4ONGYL3s/0/da"><img src="http://feedads.g.doubleclick.net/~a/1k1VhHHR72f4sxmT_rf4ONGYL3s/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1k1VhHHR72f4sxmT_rf4ONGYL3s/1/da"><img src="http://feedads.g.doubleclick.net/~a/1k1VhHHR72f4sxmT_rf4ONGYL3s/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=UXaVoxGU0Ek:CUGvdC3dn20:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=UXaVoxGU0Ek:CUGvdC3dn20:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=UXaVoxGU0Ek:CUGvdC3dn20:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=UXaVoxGU0Ek:CUGvdC3dn20:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=UXaVoxGU0Ek:CUGvdC3dn20:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=UXaVoxGU0Ek:CUGvdC3dn20:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=UXaVoxGU0Ek:CUGvdC3dn20:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/UXaVoxGU0Ek" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/07/30/maintainable-but-not-learnable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/07/30/maintainable-but-not-learnable/</feedburner:origLink></item>
		<item>
		<title>Dependency Injection with StructureMap</title>
		<link>http://feedproxy.google.com/~r/CodingEfficiency/~3/PbRn1DJOeAg/</link>
		<comments>http://www.codingefficiency.com/2009/07/29/dependency-injection-with-structuremap/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 18:55:15 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.codingefficiency.com/?p=344</guid>
		<description><![CDATA[A little introduction into Dependency Injection and StructureMap]]></description>
			<content:encoded><![CDATA[<p>A very good explanation of what dependency injection is can be found <a href="http://www.martinfowler.com/articles/injection.html">here</a>. </p>
<p>Very short: Dependency injection allows you to decouple your code and to make it more unit test friendly.</p>
<p>Another commonly used phrase for dependency injection (DI) is inversion of control (IoC).</p>
<h3></h3>
<h3>What is a DI / IoC container?</h3>
<p>A DI or IoC container is basically just a type map that returns a specific implementation for a requested type. To do so, it has to be told in some way what it should return if it is asked for a given type.</p>
<p>Most, if not all, DI Containers automatically resolve dependencies. You&#8217;ll find an example below.</p>
<p>The benefits are less code duplication, you specify a concrete implementation at most once and easier unit testing because you can test each component in isolation.</p>
<p>The only downside is the little upfront effort that is required to configure the container.</p>
<h3>What is StructureMap?</h3>
<p>StructureMap is a popular DI Container with a very easy to use api for both, normal use and configuration.</p>
<p>The mainly used method of StructureMap is in the static ObjectFactory, the methods signature is T GetInstance&lt;T&gt;(). This method is enough for most use cases.</p>
<p>StructureMaps configuration capabilities are also very interesting as they are powerful while easy to use.</p>
<p>You can get it and further information <a href="http://structuremap.sourceforge.net/">here</a>.</p>
<h3>A very basic example:</h3>
<p>We have these interfaces: </p>
<pre name="code" class="c-sharp:nocontrols">
public interface IA
{
    IB B { get;}
}
public interface IB
{
    IC C { get; }
}
public interface IC { }
</pre>
<p>With these implementations:</p>
<pre name="code" class="c-sharp:nocontrols">
public class A : IA
{
    private readonly IB _b;

    public A(IB b)
    {
        _b = b;
    }

    public IB B
    {
        get { return _b; }
    }
}

public class B : IB
{
    private readonly IC _c;

    public B(IC c)
    {
        _c = c;
    }

    public IC C
    {
        get { return _c; }
    }
}

public class C : IC { }
</pre>
<p>Such a construct has a long and ugly instantiation, new A(new B(new C()))). Unit testing the class that makes this call is limited as it directly depends on A, B and C.</p>
<p>The other option would be to create B in A’s constructor and C in B’s but doing so would harm unit testing even more as testing A without B and B without C is impossible.</p>
<p>Before I show you how to configure StructureMap, you should know that StructureMap uses so called Registries for in-code configuration and that you’d normally configure it as early as possible. </p>
<p>In WinForms “as early as possible” might be before calling Application.Run. If you name the registry ClientRegistry, it could look like this:</p>
<pre name="code" class="c-sharp:nocontrols">
public static void Main(params string[] args)
{
    ObjectFactory.Initialize(x => x.AddRegistry(new ClientRegistry());
    Application.Run(ObjectFactory.GetInstance&lt;Form1>());
}
</pre>
<p>And here are two ClientRegistry implementations that would do the job for the above case:</p>
<pre name="code" class="c-sharp:nocontrols">
public class ClientRegistry : Registry
{
    public ClientRegistry()
    {
        ForRequestedType&lt;IA>()
            .TheDefaultIsConcreteType&lt;A>();

        ForRequestedType&lt;IB>()
            .TheDefaultIsConcreteType&lt;B>();

        ForRequestedType&lt;IC>()
            .TheDefaultIsConcreteType&lt;C>();
    }
}

public ClientRegistry()
{
    Scan(x =>
             {
                 x.TheCallingAssembly();
                 x.WithDefaultConventions();
             });
}
</pre>
<p>Both would map IA to A, IB to B and IC to C, in implementation 1 it&#8217;s obvious but it looks like code duplication. In implementation 2 its not obvious at all, basically all it does is looking through all types in the calling assembly and to map those types that conform to the default conventions. The default convention is mapping interfaces to classes with the same name just without the leading I.</p>
<p>All you have to do now is call ObjectFactory.GetInstance&lt;IA&gt;(), in this case you&#8217;ll get an instance of A. </p>

<p><a href="http://feedads.g.doubleclick.net/~a/5RsPMeVb8pk3fuqz9ayC32YB8zo/0/da"><img src="http://feedads.g.doubleclick.net/~a/5RsPMeVb8pk3fuqz9ayC32YB8zo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/5RsPMeVb8pk3fuqz9ayC32YB8zo/1/da"><img src="http://feedads.g.doubleclick.net/~a/5RsPMeVb8pk3fuqz9ayC32YB8zo/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=PbRn1DJOeAg:KLqhu7RscBE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=PbRn1DJOeAg:KLqhu7RscBE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=PbRn1DJOeAg:KLqhu7RscBE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=PbRn1DJOeAg:KLqhu7RscBE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=PbRn1DJOeAg:KLqhu7RscBE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/CodingEfficiency?a=PbRn1DJOeAg:KLqhu7RscBE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/CodingEfficiency?i=PbRn1DJOeAg:KLqhu7RscBE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/CodingEfficiency/~4/PbRn1DJOeAg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingefficiency.com/2009/07/29/dependency-injection-with-structuremap/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.codingefficiency.com/2009/07/29/dependency-injection-with-structuremap/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.790 seconds -->
