<?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>Tim Van Wassenhove &#187; Patterns</title>
	<atom:link href="http://www.timvw.be/category/information-technology/csharp/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Presenting ValueType&lt;T&gt;</title>
		<link>http://www.timvw.be/presenting-valuetypet/</link>
		<comments>http://www.timvw.be/presenting-valuetypet/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 18:27:34 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1650</guid>
		<description><![CDATA[Here is a base class for some code that i have written once too many in my life: (In case you&#8217;re an early adaptor (.Net 4.0) you may want to use System.Tuple&#60;T1&#62; as base class) public class ValueType&#60;T&#62; : IComparable, IComparable&#60;ValueType&#60;T&#62;&#62;, IEquatable&#60;ValueType&#60;T&#62;&#62; where T : IComparable&#60;T&#62; { protected T Value { get; private set; }public [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a base class for some code that i have written once too many in my life: (In case you&#8217;re an early adaptor (.Net 4.0) you may want to use System.Tuple&lt;T1&gt; as base class)</p>

<pre class="brush: csharp;">public class ValueType&lt;T&gt; : IComparable, IComparable&lt;ValueType&lt;T&gt;&gt;, IEquatable&lt;ValueType&lt;T&gt;&gt;
 where T : IComparable&lt;T&gt;
{
 protected T Value { get; private set; }public ValueType(T value)
 {
  Value = value;
 }

 public override int GetHashCode()
 {
  return Value.GetHashCode();
 }

 public override string ToString()
 {
  return Value.ToString();
 }

 public override bool Equals(object obj)
 {
  return Equals(obj as ValueType&lt;T&gt;);
 }

 public bool Equals(ValueType&lt;T&gt; other)
 {
  return Compare(this, other) == 0;
 }

 public int CompareTo(object obj)
 {
  return CompareTo(this, obj as ValueType&lt;T&gt;);
 }

 public int CompareTo(ValueType&lt;T&gt; other)
 {
  return Compare(this, other);
 }

 static int Compare(ValueType&lt;T&gt; instance1, ValueType&lt;T&gt; instance2)
 {
  if (ReferenceEquals(instance1, instance2)) return 0;
  if (ReferenceEquals(instance1, null)) return -1;
  if (ReferenceEquals(instance2, null)) return 1;

  if (ReferenceEquals(instance1.Value, instance2.Value)) return 0;
  if (ReferenceEquals(instance1.Value, null)) return -1;
  if (ReferenceEquals(instance2.Value, null)) return 1;

  return instance1.Value.CompareTo(instance2.Value);
 }

 public static bool operator ==(ValueType&lt;T&gt; instance1, ValueType&lt;T&gt; instance2)
 {
  return Compare(instance1, instance2) == 0;
 }

 public static bool operator !=(ValueType&lt;T&gt; instance1, ValueType&lt;T&gt; instance2)
 {
  return !(instance1 == instance2);
 }

 public static bool operator &lt;(ValueType&lt;T&gt; instance1, ValueType&lt;T&gt; instance2)
 {
   return Compare(instance1, instance2) &lt; 0;
 }

 public static bool operator &gt;(ValueType&lt;T&gt; instance1, ValueType&lt;T&gt; instance2)
 {
   return Compare(instance1, instance2) &gt; 0;
 }
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/presenting-valuetypet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Do we need an EventAggregator when we have an IOC container?</title>
		<link>http://www.timvw.be/do-we-need-an-eventaggregator-when-we-have-an-ioc-container/</link>
		<comments>http://www.timvw.be/do-we-need-an-eventaggregator-when-we-have-an-ioc-container/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 11:55:34 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1642</guid>
		<description><![CDATA[An Event Aggregator is an example of a Publish/Subscribe channel. A while ago i started wondering if we still need an Event Aggregator in our compisite applications if we have an IOC container that takes cares of dependency wiring. An IOC container can easily inject the Event/MessageHandler(s) in the Event/MessagePublisher(s)&#8230; I&#8217;m still not sure about [...]]]></description>
			<content:encoded><![CDATA[<p>An <a href="http://msdn.microsoft.com/en-us/library/cc707867.aspx">Event Aggregator</a> is an example of a <a href="http://www.eaipatterns.com/PublishSubscribeChannel.html">Publish/Subscribe channel</a>. A while ago i started wondering if we still need an Event Aggregator in our compisite applications if we have an IOC container that takes cares of dependency wiring. An IOC container can easily inject the Event/MessageHandler(s) in the Event/MessagePublisher(s)&#8230;  I&#8217;m still not sure about the answer (Yes/No).</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/do-we-need-an-eventaggregator-when-we-have-an-ioc-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating series of elements</title>
		<link>http://www.timvw.be/creating-series-of-elements/</link>
		<comments>http://www.timvw.be/creating-series-of-elements/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 20:02:23 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1579</guid>
		<description><![CDATA[Lately i have done quite a bit of charting. Very often the X-axis is populated with a series of numbers or dates. This can be as simple as: (My very little DSL in Jeremy D. Miller Style) [Test] public void ShouldBeAbleToGetSeriesOfNumbers() { // Arrange var series = 3.To(5); // Act var elements = series.Elements; // [...]]]></description>
			<content:encoded><![CDATA[<p>Lately i have done quite a bit of charting. Very often the X-axis is populated with a series of numbers or dates. This can be as simple as: (My very little DSL in <a href="http://codebetter.com/blogs/jeremy.miller/archive/2010/01/06/writing-internal-dsl-s-in-msdn.aspx">Jeremy D. Miller Style</a>)</p>

<pre class="brush: csharp;">[Test] public void ShouldBeAbleToGetSeriesOfNumbers()
{
 // Arrange
 var series = 3.To(5);

 // Act
 var elements = series.Elements;

 // Assert
 var expected = new[] { 3, 4, 5 };
 CollectionAssert.AreEqual(expected, elements);
}

[Test] public void ShouldBeAbleToGetSeriesOfDays()
{
 // Arrange
 var now = DateTime.Now.Date;
 var twoDaysLater = now.AddDays(2);
 var series = now.To(twoDaysLater);

 // Act
 var elements = series.Elements;

 // Assert
 var expectedDays = new[] { now, now.AddDays(1), now.AddDays(2) };
 CollectionAssert.AreEqual(expectedDays, elements);
}</pre>

<p>And here is the code that makes these tests pass:</p>

<pre class="brush: csharp;">public static class Series
{
 public static Series&lt;int&gt; To(this int from, int to)
 {
  return Create(from, to);
 }

 public static Series&lt;DateTime&gt; To(this DateTime from, DateTime to)
 {
  return Create(from, to);
 }

 public static Series&lt;DateTime&gt; Create(DateTime from, DateTime to)
 {
  return new Series&lt;DateTime&gt;(from.Date, to.Date, d =&gt; d.AddDays(1));
 }

 public static Series&lt;DateTime&gt; Create(DateTime from, int numberOfDays)
 {
  return Create(from.Date, from.Date.AddDays(numberOfDays));
 }

 public static Series&lt;int&gt; Create(int from, int to)
 {
  return new Series&lt;int&gt;(from, to, n =&gt; n + 1);
 }

 public static Series&lt;int&gt; Create(int from, int to, int stepSize)
 {
  return new Series&lt;int&gt;(from, to, n =&gt; n + stepSize);
 }
}</pre>

<br/>
<pre class="brush: csharp;">public class Series&lt;T&gt; : IEnumerable&lt;T&gt; where T : IComparable
{
 public T From { get; private set; }
 public T To { get; private set; }
 public Func&lt;T, T&gt; Step { get; private set; }

 public Series(T from, T to, Func&lt;T, T&gt; step)
 {
  From = from;
  To = to;
  Step = step;
 }

 public IEnumerable&lt;T&gt; Elements
 {
  get
  {
   var current = From;
   while (current.CompareTo(To) &lt;= 0)
   {
    yield return current;
    current = Step(current);
   }
  }
 }

 public IEnumerator&lt;T&gt; GetEnumerator()
 {
  return Elements.GetEnumerator();
 }

 IEnumerator IEnumerable.GetEnumerator()
 {
  return GetEnumerator();
 }
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/creating-series-of-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In case you really have to Append one array to another</title>
		<link>http://www.timvw.be/in-case-you-really-have-to-append-one-array-to-another/</link>
		<comments>http://www.timvw.be/in-case-you-really-have-to-append-one-array-to-another/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 07:46:32 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1544</guid>
		<description><![CDATA[Here is another problem i&#8217;ve seen people solve once too many: Append one array to another. STOP. Revisit the problem. Can&#8217;t you simply use List&#60;T&#62; and move on to solving actual business problems? In case you really can&#8217;t get rid of the arrays read the following: Given() { source = new[] { SourceElement }; destination [...]]]></description>
			<content:encoded><![CDATA[<p>Here is another problem i&#8217;ve seen people solve once too many: Append one array to another. STOP. Revisit the problem. Can&#8217;t you simply use List&lt;T&gt; and move on to solving actual business problems? In case you really can&#8217;t get rid of the arrays read the following:</p>

<pre class="brush: csharp;">Given()
{
 source = new[] { SourceElement };
 destination = new[] { DestinationElement };
}</pre>

<br/>

<pre class="brush: csharp;">When()
{
 source.AppendTo(ref destination);
}</pre>

<br/>

<pre class="brush: csharp;">ThenTheDestinationShouldStillHaveTheDestinationElement()
{
 Assert.AreEqual(DestinationElement, destination[0]);
}</pre>

<br/>

<pre class="brush: csharp;">ThenTheDestinationShouldHaveBeenExtendedWithTheSourceElement()
{
 Assert.AreEqual(SourceElement, destination[1]);
}</pre>

<p>And here is the code which satisfies the requirements:</p>

<pre class="brush: csharp;">public static class Extensions
{
 public static void AppendTo&lt;T&gt;(this T[] sourceArray, ref T[] destinationArray)
 {
  var sourceLength = sourceArray.Length;
  var destinationLength = destinationArray.Length;
  var extendedLength = destinationLength + sourceLength;
  Array.Resize(ref destinationArray, extendedLength);
  Array.Copy(sourceArray, 0, destinationArray, destinationLength, sourceLength);
 }
}</pre>

<p>Perhaps it&#8217;s time to start (or does it exist already, cause i can&#8217;t find it) an open-source project with extension methods.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/in-case-you-really-have-to-append-one-array-to-another/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>About databinding and composite views</title>
		<link>http://www.timvw.be/about-databinding-and-composite-views/</link>
		<comments>http://www.timvw.be/about-databinding-and-composite-views/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 15:44:43 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1417</guid>
		<description><![CDATA[A couple of days ago i had a databound ItemsControl (collection of Model.Cell) which instantiated sub views (with their own viewmodel). &#60;Grid.Resources&#62; &#60;DataTemplate x:Key=&#34;CellTemplate&#34;&#62; &#60;views:CellView /&#62; &#60;/DataTemplate&#62; &#60;/Grid.Resources&#62; &#60;ItemsControl ItemTemplate=&#34;{StaticResource CellTemplate}&#34; ItemsSource=&#34;{Binding Cells}&#34; /&#62; &#60;/Grid&#62; Because each CellViewModel needs to know which cell they manage i used the following dirty hack: public CellView() { Loaded [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago i had a databound ItemsControl (collection of Model.Cell) which instantiated sub views (with their own viewmodel).</p>

<pre class="brush: xml;">
 &lt;Grid.Resources&gt;
  &lt;DataTemplate x:Key=&quot;CellTemplate&quot;&gt;
   &lt;views:CellView /&gt;
  &lt;/DataTemplate&gt;
 &lt;/Grid.Resources&gt;
 &lt;ItemsControl 
   ItemTemplate=&quot;{StaticResource CellTemplate}&quot; 
   ItemsSource=&quot;{Binding Cells}&quot; /&gt;
&lt;/Grid&gt;</pre> 

<p>Because each CellViewModel needs to know which cell they manage i used the following dirty hack:</p>

<pre class="brush: csharp;">public CellView() 
{
 Loaded += CellView_Loaded; 
}

void CellView_Loaded(object sender, RoutedEventArgs e) 
{
 DataContext = new CellViewModel(DataContext);
}</pre>

<p>Later on that day i realised there was a much cleaner solution: Let the BoardViewModel expose a collection of ViewModels.CellViewModel instead of Model.Cell. What a relief that i don&#8217;t have to use the Loaded event hack <img src='http://www.timvw.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/about-databinding-and-composite-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring M-V-VM</title>
		<link>http://www.timvw.be/exploring-m-v-vm/</link>
		<comments>http://www.timvw.be/exploring-m-v-vm/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 15:10:10 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1403</guid>
		<description><![CDATA[A couple of years ago a collegue recommended Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET and i noticed that my code started to gravitate towards an Model-View-ViewModel architecture. Due to shortcomings and painful experiences i gave up on databinding and began to use Passieve View instead. Passive View doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago a collegue recommended <a href="http://www.amazon.com/Data-Binding-Windows-Forms-2-0/dp/032126892X">Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET</a> and i noticed that my code started to gravitate towards an <a href="http://en.wikipedia.org/wiki/Model_View_ViewModel">Model-View-ViewModel</a> architecture. Due to shortcomings and painful experiences i gave up on databinding and began to use <a href="http://martinfowler.com/eaaDev/PassiveScreen.html">Passieve View</a> instead.</p>

<p>Passive View doesn&#8217;t work (well) with smart views so i decided to give M-V-VM another because i really wanted to leverage WPF&#8217;s rich support for databinding.</p>

<p>The key difference between M-V-VM and Passive View is, imho, the fact that the ViewModel is unaware of the View unlike Passive View where the Presenter knows about the (simple) View.</p>

<p>When we test a Presenter i notice that we end up writing interaction based tests (assertions on a mocked view) and when we test a ViewModel we end up writing state-based tests instead.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/exploring-m-v-vm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extension methods to improve readability</title>
		<link>http://www.timvw.be/extension-methods-to-improve-readability/</link>
		<comments>http://www.timvw.be/extension-methods-to-improve-readability/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 06:59:16 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1234</guid>
		<description><![CDATA[A common reason to take advantage of extension methods is to enhance readability (think fluent interfaces). My team uses the specification pattern regularly and in case a requirement says something like &#8220;if the player has reached level 10 a message should be displayed&#8221; they would implement it as: if (new HasReachedLevel(10).IsSatisfiedBy(player)) { view.DisplayMessage(&#34;Congratulations! You have [...]]]></description>
			<content:encoded><![CDATA[<p>A common reason to take advantage of extension methods is to enhance readability (think fluent interfaces). My team uses the <a href="http://en.wikipedia.org/wiki/Specification_pattern">specification pattern</a> regularly and in case a requirement says something like &#8220;if the player has reached level 10 a message should be displayed&#8221; they would implement it as:</p>

<pre class="brush: csharp;">if (new HasReachedLevel(10).IsSatisfiedBy(player))
{
 view.DisplayMessage(&quot;Congratulations! You have reached level 10.&quot;);
}</pre>

<p>Pretty good but did you notice that they changed the order of player and level in their (code) story? With the aid of an extension method we can express this requirement as:</p>

<pre class="brush: csharp;">if (player.Satisfies(new HasReachedLevel(10)))
{
 view.DisplayMessage(&quot;Congratulations! You have reached level 10.&quot;);
}</pre>

<p>Here is the extension method that allows you to express the requirement in this way:</p>

<pre class="brush: csharp;">public static bool Satisfies&lt;T&gt;(this T candidate, ISpecification&lt;T&gt; specification)
{
 return specification.IsSatisfiedBy(candidate);
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/extension-methods-to-improve-readability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Experimenting with ControlStateMachine and Fluent interfaces</title>
		<link>http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/</link>
		<comments>http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 18:52:25 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1157</guid>
		<description><![CDATA[A long time ago i read Build your own CAB series and recently i noticed that there is a wiki: Presentation Patterns Wiki! and it inspired me to experiment with state machines. Here are a couple of examples: controlStateMachine = new ControlStateMachine&#60;States&#62;(this); controlStateMachine.AfterEachStateChange() .Do(MakeRelevantButtonsVisible); controlStateMachine.WhenStateChangesTo(States.RetrievingSubscriptionPeriod) .TheOnlyVisibleControlsAre(flowLayoutPanel1, datePicker1); controlStateMachine.WhenStateChangesTo(States.RetrievingCustomerInformation) .MakeVisible(customerInput1) .Do(() =&#62; customerInput1.Dock = DockStyle.Fill); controlStateMachine.WhenStateChangesTo(States.Ready) [...]]]></description>
			<content:encoded><![CDATA[<p>A long time ago i read <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/07/25/the-build-your-own-cab-series-table-of-contents.aspx">Build your own CAB series</a> and recently i noticed that there is a wiki: <a href="http://www.jeremydmiller.com/ppatterns/Default.aspx?Page=MainPage&#038;AspxAutoDetectCookieSupport=1">Presentation Patterns Wiki!</a> and it inspired me to experiment with state machines. Here are a couple of examples:</p>

<pre class="brush: csharp;">controlStateMachine = new ControlStateMachine&lt;States&gt;(this);

controlStateMachine.AfterEachStateChange()
 .Do(MakeRelevantButtonsVisible);

controlStateMachine.WhenStateChangesTo(States.RetrievingSubscriptionPeriod)
 .TheOnlyVisibleControlsAre(flowLayoutPanel1, datePicker1);

controlStateMachine.WhenStateChangesTo(States.RetrievingCustomerInformation)
 .MakeVisible(customerInput1)
 .Do(() =&gt; customerInput1.Dock = DockStyle.Fill);

controlStateMachine.WhenStateChangesTo(States.Ready)
 .MakeInvisible(customerInput1);
</pre>

<p>And here is another example:</p>

<pre class="brush: csharp;">wizardStateMachine = new WizardStateMachine&lt;States&gt;(controlStateMachine);

wizardStateMachine.InState(States.RetrievingSubscriptionPeriod)
 .OnCommand(WizardCommands.Next)
  .TransitionTo(States.RetrievingCustomerInformation);

wizardStateMachine.InState(States.RetrievingCustomerInformation)
 .OnCommand(WizardCommands.Back)
  .TransitionTo(States.RetrievingSubscriptionPeriod)
 .OnCommand(WizardCommands.Finish)
  .TransitionTo(States.Ready);

wizardStateMachine.InState(States.Ready)
 .OnCommand(WizardCommands.New)
  .Do(() =&gt; MessageBox.Show(&quot;Currently not supported&quot;));
</pre>

<p>Stay tuned for future posts where i describe the problem space that have lead to this API.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Strict mocks lead to overspecification</title>
		<link>http://www.timvw.be/strict-mocks-lead-to-overspecification/</link>
		<comments>http://www.timvw.be/strict-mocks-lead-to-overspecification/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 15:49:02 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=927</guid>
		<description><![CDATA[Here is an example that demonstrates how strick mocks lead to overspecification. Imagine that we are creating a simple screen in a Passive View architecture. The first feature that we implement is displaying the message &#8220;edit&#8221; when the user clicks the edit button: [Fact] public void ShouldDisplayEditClickMessage() { // Establish context MockRepository mockRepository = new [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an example that demonstrates how strick mocks lead to overspecification. Imagine that we are creating a simple screen in a <a href="http://martinfowler.com/eaaDev/PassiveScreen.html">Passive View</a> architecture. The first feature that we implement is displaying the message &#8220;edit&#8221; when the user clicks the edit button:</p>

<pre class="brush: csharp;">[Fact] public void ShouldDisplayEditClickMessage()
{
 // Establish context
 MockRepository mockRepository = new MockRepository();
 IView view = mockRepository.StrictMock&lt;IView&gt;();
 Expect.Call(delegate { view.EditClick += null; }).IgnoreArguments();
 mockRepository.Replay(view);

 // Create sut
 Presenter sut = new Presenter(view);

 // Setup expectations
 mockRepository.BackToRecord(view, BackToRecordOptions.Expectations);
 Expect.Call(delegate { view.DisplayClickMessage(&quot;edit&quot;); });
 mockRepository.ReplayAll();

 // Exercise
 RhinoMocksExtensions
  .GetEventRaiser(view, delegate(IView v) { v.EditClick += null; })
  .Raise(view, EventArgs.Empty);

 // Verify
 mockRepository.VerifyAll();
}</pre>

<br/>

<img src="http://www.timvw.be/wp-content/images/overspecification-01.PNG" alt="screenshot of test runner with all tests passing" />

<p>And now we add the feature that displays the message &#8220;save&#8221; whenever the user clicks on the save button:</p>

<pre class="brush: csharp;">[Fact] public void ShouldDisplaySaveClickMessage()
{
 // Establish context
 MockRepository mockRepository = new MockRepository();
 IView view = mockRepository.StrictMock&lt;IView&gt;();
 Expect.Call(delegate { view.EditClick += null; }).IgnoreArguments();
 Expect.Call(delegate { view.SaveClick += null; }).IgnoreArguments();
 mockRepository.Replay(view);

 // Create sut
 Presenter sut = new Presenter(view);

 // Setup expectations
 mockRepository.BackToRecord(view, BackToRecordOptions.Expectations);
 Expect.Call(delegate { view.DisplayClickMessage(&quot;save&quot;); });
 mockRepository.ReplayAll();

 // Exercise
 RhinoMocksExtensions
  .GetEventRaiser(view, delegate(IView v) { v.SaveClick += null; })
  .Raise(view, EventArgs.Empty);

  // Verify
  mockRepository.VerifyAll();
 }
}</pre>

<br/>

<img src="http://www.timvw.be/wp-content/images/overspecification-02.PNG" alt="screenshot of test runner with a failing test." />

<p>Although we implemented the feature correctly, and left the code of the first feature untouched, we notice that our ShouldDisplayEditClickMessage test fails because it is not expecting a subscription to the SaveClick event. Imho, this way of testing is a testing <b>anti-pattern</b>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/strict-mocks-lead-to-overspecification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Refactoring EffectivityManager</title>
		<link>http://www.timvw.be/refactoring-effectivitymanager/</link>
		<comments>http://www.timvw.be/refactoring-effectivitymanager/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 15:33:22 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=588</guid>
		<description><![CDATA[A while ago i presented the EffectivityManager. Having used this class for a while i have decided to rename it to Temporal&#60;T&#62;. The implementation of IList&#60;T&#62; is not required anymore because a user is typically only interested in a specific effectivity, not the evolution of the effectivities. /// &#60;summary&#62; /// Represents an element that changes [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago i presented the <a href="http://www.timvw.be/presenting-a-generic-effectivitymanager/">EffectivityManager</a>. Having used this class for a while i have decided to rename it to Temporal&lt;T&gt;. The implementation of IList&lt;T&gt; is not required anymore because a user is typically only interested in a specific effectivity, not the evolution of the effectivities.</p>

<pre class="brush: csharp;">/// &lt;summary&gt;
/// Represents an element that changes over time. 
/// It consists out of effectivities of that element that are effective in different periods.
/// &lt;/summary&gt;
public interface ITemporal&lt;T&gt;
{
 /// &lt;summary&gt;
 /// Modifies the element from the given date.
 /// &lt;/summary&gt;
 /// &lt;param name=&quot;element&quot;&gt;&lt;/param&gt;
 /// &lt;param name=&quot;from&quot;&gt;&lt;/param&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 void Modify(T element, DateTime from);

 /// &lt;summary&gt;
 /// Gets the effectivity that is valid on the given date.
 /// &lt;/summary&gt;
 /// &lt;param name=&quot;validityDate&quot;&gt;The validity date.&lt;/param&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 IEffectivity&lt;T&gt; GetSnapshot(DateTime validityDate);

 /// &lt;summary&gt;
 /// Tries to get the effectivity that is valid on the given date.
 /// &lt;/summary&gt;
 /// &lt;param name=&quot;validityDate&quot;&gt;&lt;/param&gt;
 /// &lt;param name=&quot;effectivity&quot;&gt;&lt;/param&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 bool TryGetSnapshot(DateTime validityDate, out IEffectivity&lt;T&gt; effectivity);
}</pre>

<p>In the implementation i have added a constructor that accepts a <a href="http://www.timvw.be/presenting-a-generic-discreterange/">DiscreteValuesGenerator&lt;DateTime&gt;</a> which makes it possible to create Periods with a resolution of a day instead of seconds.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/refactoring-effectivitymanager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
