<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Dan Merino&#039;s Blog &#187; Best practice</title>
	<atom:link href="http://blog.danmerino.com/category/best-practice/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.danmerino.com</link>
	<description>Computer Science, Entrepreneurship and Productivity</description>
	<lastBuildDate>Wed, 21 Dec 2011 06:34:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Testing Code Contracts .NET 4.0</title>
		<link>http://blog.danmerino.com/testing-code-contracts-net-4-0/</link>
		<comments>http://blog.danmerino.com/testing-code-contracts-net-4-0/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 08:26:02 +0000</pubDate>
		<dc:creator>danmerino</dc:creator>
				<category><![CDATA[Best practice]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code contracts]]></category>
		<category><![CDATA[postconditions]]></category>
		<category><![CDATA[preconditions]]></category>
		<category><![CDATA[test project]]></category>

		<guid isPermaLink="false">http://newdevcentral.com/?p=435</guid>
		<description><![CDATA[Tweet Why use Code Contracts? By providing pre-compiled code contract interfaces other developers can adhere to signatures and also expected behavior. This is specially important due to the Liskov substitution principle. Dino Esposito wrote a great article on the topic &#8230; <a href="http://blog.danmerino.com/testing-code-contracts-net-4-0/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danmerino.com%2Ftesting-code-contracts-net-4-0%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://blog.danmerino.com/testing-code-contracts-net-4-0/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://blog.danmerino.com/testing-code-contracts-net-4-0/"  data-text="Testing Code Contracts .NET 4.0" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://blog.danmerino.com/testing-code-contracts-net-4-0/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://blog.danmerino.com/testing-code-contracts-net-4-0/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><h2>Why use Code Contracts?</h2>
<p>By providing pre-compiled <a href="http://research.microsoft.com/en-us/projects/contracts/">code contract</a> interfaces other developers can adhere to signatures and also expected behavior. This is specially important due to the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov substitution principle</a>.</p>
<p>Dino Esposito wrote a great article on the topic called <a href="http://dotnetslackers.com/articles/net/code-contracts-preview-interfaces.aspx"> Code Contracts Preview: Interfaces</a>.</p>
<h2>Testing</h2>
<p>To test that all the right contracts are in place a test project can be created.</p>
<p>Testing preconditions is possible by catching the exceptions created by those preconditions.</p>
<pre class="brush: csharp; title: ; notranslate">

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ModelNegativeBlance()
        {
            Account acc = new Account()
            {
                AccountName = &quot;NewAccount&quot;,
                //0 or positive is expected
                Balance = -99,
                CreationDate = DateTime.Now

            };
        }
</pre>
<p>Testing postconditions is a bit tricky because the exceptions raised by postconditions are not meant to be caught. Therefore plain strings have to be used.</p>
<pre class="brush: csharp; title: ; notranslate">

    public static class TestHelpers
    {
        public static string ContractExceptionName = &quot;System.Diagnostics.Contracts.__ContractsRuntime+ContractException&quot;;

    }
</pre>
<pre class="brush: csharp; title: ; notranslate">
    public class RepositoryTests
    {
        public RepositoryTests()
        {

        }

        [TestMethod]
        public void InsertModelWithNoParitionKeyDueToBadRepository()
        {

            var repo = SetBadRepo();
            Account acc = new Account()
            {
                AccountName = &quot;NewAccount&quot;,
                Balance = 0,
                CreationDate = DateTime.Now

            };

            try
            {
                repo.InsertAccount(acc);
            }
            catch (Exception ex)
            {
                Assert.AreEqual(TestHelpers.ContractExceptionName, ex.GetType().FullName);
            }
        }
    }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.danmerino.com/testing-code-contracts-net-4-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t copy-and-paste code. Don&#8217;t use debug driven development!</title>
		<link>http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/</link>
		<comments>http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 06:42:11 +0000</pubDate>
		<dc:creator>danmerino</dc:creator>
				<category><![CDATA[Best practice]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[copy and paste]]></category>
		<category><![CDATA[debug driven development]]></category>

		<guid isPermaLink="false">http://newdevcentral.com/?p=371</guid>
		<description><![CDATA[Tweet I see a common copy-and-paste trend specially with junior developers. Many of them just copy-and-paste random internet code into their projects. After a while I ask them about their implementation&#8230; very few understand what they copy-and-paste and the implications &#8230; <a href="http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danmerino.com%2Fdont-copy-and-paste-code-dont-use-debug-driven-development%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/"  data-text="Don&#8217;t copy-and-paste code. Don&#8217;t use debug driven development!" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>I see a common copy-and-paste trend specially with junior developers. Many of them just copy-and-paste random internet code into their projects. After a while I ask them about their implementation&#8230; very few understand what they copy-and-paste and the implications of that implementation!</p>
<p>I inquired further and it was clear that many used debug driven development to get the copy-and-paste code to work. Debug driven development is basically 90% random changes and 10% thinking. This is a popular trend within .NET developers with visual studio because the IDE is amazing&#8230;very little planning is required to get things to work.</p>
<p>After a few weeks of seeing this trend I did the following:</p>
<p>If they asked me a question that I believe is due to copy-and-paste or debug driven development I reply with the following questions</p>
<ol>
<li>Did you google the problem? (some  of them didn&#8217;t google the problem!)</li>
<li>Did you copy and paste code? Is that code giving you the error? If you did copy and paste, <em>what does the code do</em>?</li>
<li><em>What are 2 different approaches to this implementation and why did you choose this implementation</em>?</li>
</ol>
<p><strong>After answering the questions above, they were able to solve the problems themselves. That makes them happy and it makes me happy!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danmerino.com/dont-copy-and-paste-code-dont-use-debug-driven-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dynamic Azure Web Services URLs</title>
		<link>http://blog.danmerino.com/dynamic-azure-web-services-urls/</link>
		<comments>http://blog.danmerino.com/dynamic-azure-web-services-urls/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 18:04:41 +0000</pubDate>
		<dc:creator>danmerino</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Best practice]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.codingadventure.com/?p=180</guid>
		<description><![CDATA[Tweet I am currently building a lot of web services for Azure. Since URLs change a lot in development and deployment environments I needed to be able to have those services URLs updated dynamically. The easiest way I found is using handlers and: &#8230; <a href="http://blog.danmerino.com/dynamic-azure-web-services-urls/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danmerino.com%2Fdynamic-azure-web-services-urls%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://blog.danmerino.com/dynamic-azure-web-services-urls/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://blog.danmerino.com/dynamic-azure-web-services-urls/"  data-text="Dynamic Azure Web Services URLs" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://blog.danmerino.com/dynamic-azure-web-services-urls/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://blog.danmerino.com/dynamic-azure-web-services-urls/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>I am currently building a lot of web services for <a href="www.microsoft.com/windowsazure/">Azure</a>. Since URLs change a lot in development and deployment environments I needed to be able to have those services URLs updated dynamically. The easiest way I found is using <strong>handlers </strong>and:</p>
<pre class="brush: csharp; title: ; notranslate">

HttpContext.Current.Request.Headers[&quot;Host&quot;];
</pre>
<p>This way none of the URLs get hard-coded and deployment is extremely easy. This also brings a lot of neat options for <em><a href="http://en.wikipedia.org/wiki/Intercloud">Intercloud </a></em>communication, but that is for another post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danmerino.com/dynamic-azure-web-services-urls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The build process</title>
		<link>http://blog.danmerino.com/the-build-process/</link>
		<comments>http://blog.danmerino.com/the-build-process/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 16:53:13 +0000</pubDate>
		<dc:creator>danmerino</dc:creator>
				<category><![CDATA[Best practice]]></category>
		<category><![CDATA[Build]]></category>

		<guid isPermaLink="false">http://blog.codingadventure.com/?p=144</guid>
		<description><![CDATA[Tweet Ensure Binary Integrity of your build: The build either works or it does not work. This way it is easier to rule out errors in different layers of the project like database or logic Builds are obtained from files &#8230; <a href="http://blog.danmerino.com/the-build-process/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danmerino.com%2Fthe-build-process%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://blog.danmerino.com/the-build-process/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://blog.danmerino.com/the-build-process/"  data-text="The build process" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://blog.danmerino.com/the-build-process/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://blog.danmerino.com/the-build-process/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><ul>
<li>Ensure Binary Integrity of your build:
<ul>
<li>The build either works or it does not work. This way it is easier to rule out errors in different layers of the project like database or logic</li>
</ul>
</li>
</ul>
<ul>
<li>Builds are obtained from files under source control:
<ul>
<li>This means that if a problem happens the error can be linked to a specific commit or tag under version control</li>
</ul>
</li>
</ul>
<ul>
<li>Build can happen remotely (production) or locally:
<ul>
<li>This allows for the same local configuration to be tested on different environments remotely (production)</li>
<li>It can also allow specific special configuration (ex: a tag under version control) to be propagated to production</li>
<li>Basically helps prevent configuration errors by removing as much as possible the human aspect</li>
</ul>
</li>
</ul>
<ul>
<li>All builds should contain a database script (under version control) that creates/restores the database  from scratch
<ul>
<li>This way each database change is linked to a version under version control and errors can be linked to each build</li>
<li>Also this makes finding bugs a lot easier as they are easier to reproduce</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.danmerino.com/the-build-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unitiy IOC</title>
		<link>http://blog.danmerino.com/unitiy-ioc/</link>
		<comments>http://blog.danmerino.com/unitiy-ioc/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 16:37:21 +0000</pubDate>
		<dc:creator>danmerino</dc:creator>
				<category><![CDATA[Best practice]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Inversion of Control]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://blog.codingadventure.com/?p=138</guid>
		<description><![CDATA[Tweet Basically, with Unity, I ended up writing something like this (simplified version) : Unity turned out to work very well. I was able to save a lot of lines of code and it is greatly improving the testability of &#8230; <a href="http://blog.danmerino.com/unitiy-ioc/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danmerino.com%2Funitiy-ioc%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://blog.danmerino.com/unitiy-ioc/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://blog.danmerino.com/unitiy-ioc/"  data-text="Unitiy IOC" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://blog.danmerino.com/unitiy-ioc/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://blog.danmerino.com/unitiy-ioc/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Basically, with <a title="Unitiy" href="http://www.codeplex.com/unity/">Unity</a>, I ended up writing something like this (simplified version) :</p>
<pre class="brush: csharp; title: ; notranslate">IUnityContainer container = new UnityContainer();

container.RegisterType&lt;IRiskRepository, RiskRepository&gt;();

string conn = ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString;

container.Configure&lt;InjectedMembers&gt;().ConfigureInjectionFor&lt;RiskRepository&gt;(new InjectionConstructor(conn));

UnityControllerFactory factory = new UnityControllerFactory(container);

ControllerBuilder.Current.SetControllerFactory(factory);
</pre>
<p><strong><em>Unity turned out to work very well. I was able to save a lot of lines of code and it is greatly improving the testability of the dynamic modules I am using on the mock project architecture. Also, it was easy to set up. Its a good alternative to the other more popular IOC.<br />
</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danmerino.com/unitiy-ioc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

