<?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; Patterns</title>
	<atom:link href="http://blog.danmerino.com/category/patterns/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>Value Objects in F#</title>
		<link>http://blog.danmerino.com/value-objects-in-f/</link>
		<comments>http://blog.danmerino.com/value-objects-in-f/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 07:26:10 +0000</pubDate>
		<dc:creator>danmerino</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[parallelization]]></category>
		<category><![CDATA[Value Objects]]></category>

		<guid isPermaLink="false">http://blog.codingadventure.com/?p=202</guid>
		<description><![CDATA[Tweet Value Objects are objects that can be shared across different parts of a program. This can have great benefits in performance. Because  the objects are shared it is very important for value objects to be immutable. If value objects are not immutable &#8230; <a href="http://blog.danmerino.com/value-objects-in-f/">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%2Fvalue-objects-in-f%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/value-objects-in-f/"></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/value-objects-in-f/"  data-text="Value Objects in F#" 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/value-objects-in-f/" 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/value-objects-in-f/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Value Objects are objects that can be shared across different parts of a program. This can have great benefits in performance. Because  the objects are shared it is very important for value objects to be immutable. If value objects are not immutable then any part of the program can change the values and all the other parts can do calculation with erroneous data.</p>
<p>One of my goals was to make immutable objects in F# to take advantage of parallelization and automatic compiler optimizations:</p>
<pre class="brush: csharp; title: ; notranslate">

type City(Name:string, X: float, Y:float) =

member t.Name = Name

member t.X = X

member t.Y = Y

type NeighborCities(city1:string, city2:string) =

member x.fromCity = city1

member x.toCity = city2
</pre>
<p>Objects created in this manner in F# are immutable. Even when they are accessed in other projects outside their definition, their members are read-only. This was specially helpful on the distributed traveling salesman problem I built last year.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danmerino.com/value-objects-in-f/feed/</wfw:commentRss>
		<slash:comments>1</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>

