<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Dude, where's my Kaizen?]]></title>
  <link href="http://www.bjoernrochel.de/atom.xml" rel="self"/>
  <link href="http://www.bjoernrochel.de/"/>
  <updated>2012-04-08T21:32:58+02:00</updated>
  <id>http://www.bjoernrochel.de/</id>
  <author>
    <name><![CDATA[Björn Rochel]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Machine.Fakes is in new hands]]></title>
    <link href="http://www.bjoernrochel.de/2012/04/08/machine-fakes-is-in-new-hands/"/>
    <updated>2012-04-08T21:36:00+02:00</updated>
    <id>http://www.bjoernrochel.de/2012/04/08/machine-fakes-is-in-new-hands</id>
    <content type="html"><![CDATA[<p>I&#8217;m very glad to announce that <a href="http://www.github.com/machine/machine.fakes">Machine.Fakes</a> has found
a new home. <a href="http://github.com/simonhohenadl">Simon Hohenadl</a> and <a href="https://github.com/christiandeger">Christian Deger</a> of
<a href="http://www.autoscout24.de/">Autoscout 24</a> have taken over as active maintainers.</p>

<p>The last <a href="http://nuget.org/packages/Machine.Fakes">nuget release</a> was already done without any involvement from myself.
I&#8217;m doing my best to help them getting up to speed with the internals, but I&#8217;ve high confidence
that they won&#8217;t need much help to really grok it.</p>

<p>Thank you all for your support!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Machine.Fakes needs your help]]></title>
    <link href="http://www.bjoernrochel.de/2012/03/07/machine-fakes-needs-your-help/"/>
    <updated>2012-03-07T15:26:00+01:00</updated>
    <id>http://www.bjoernrochel.de/2012/03/07/machine-fakes-needs-your-help</id>
    <content type="html"><![CDATA[<p>If you ask me about my personal highlights in 2011 (technology wise), I would probably name
<a href="http://www.github.com/bjro/machine.fakes">Machine.Fakes</a> as one of those. Writing and using it
has been such a tremendous amount of fun and was the source of getting to know lots of interesting people.</p>

<p>As technology is the battlefield of opinionated minds, not every feedback I received was pleasant (especially from the Belgian &#8220;what&#8217;s wrong with good old unit test&#8221; brigade),
but overall I&#8217;m very pleased with the perception and the feedback for the framework.</p>

<p>As you may have noticed, I recently made a big technology switch. Since the start of the year I spend
most of the time working with Ruby on Rails on OSX. Originally I thought Mono would give me a great opportunity to continue
using Machine.Fakes, but as it turns out I currently just don&#8217;t have the time to do that. Maintaining a tool/framework
without actually using it on a daily basis feels just wrong. In retrospective I think I must have anticipated something
like this last year, when <a href="http://www.github.com/agross">Alexander Gross</a> and I moved Machine.Fakes into the <a href="https://github.com/machine">Machine</a>
organization on Github.</p>

<p>The worst future for Machine.Fakes I can imagine, is that it becomes just another abandoned .NET open source framework, like all the dead ones
on Codeplex. So this is my call for help:</p>

<p>If you&#8217;re using Machine.Fakes and are willing to take over as the active maintainer, please drop me a line. I will assist you as good as I can in order to make
this happen!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[At the crossroads]]></title>
    <link href="http://www.bjoernrochel.de/2011/11/15/at-the-crossroads/"/>
    <updated>2011-11-15T09:59:00+01:00</updated>
    <id>http://www.bjoernrochel.de/2011/11/15/at-the-crossroads</id>
    <content type="html"><![CDATA[<p>512 days or 17 months or 1.5 years. That&#8217;s the time between todays post and my last one.
Not as long as the Duke Nukem Forever development time, but it still strikes me how much
time has gone by since I&#8217;ve written something down here.</p>

<p>Today is probably going to be a very personal post and for that matter also non-technical one.</p>

<!--more-->


<h2>So, what happened in those 512 days?</h2>

<p>First of all, 512 days ago I deliberately stopped blogging. I had lost all fun in writing.
It felt more than a burden than anything useful to me. Well I guess that had something to do
with a slowly and painful kind of burnout I experienced during my time at a large German consultancy.
It took me a while to realize what&#8217;s going on with me, but in the end, I think, I made the right
decision leaving the contracting, ups sorry, I mean consulting business.</p>

<p>I&#8217;ve spend a lot of my free time in the last 2 years doing some form of community
work. Not via blogging, but rather by</p>

<ul>
<li>Building <a href="http://www.github.com/bjro/machine.fakes/">Machine.Fakes</a>,</li>
<li>speaking at several conferences, user groups and open spaces,</li>
<li>facilitating the F# bookclub in Munich 2010 where we worked through <a href="http://www.manning.com/petricek/">Real-World
Functional Programming</a>,</li>
<li>and helping <a href="http://www.der-albert.com/">Albert Weinert</a> to get the <a href="http://onlineusergroup.de">.NET Online User Group</a> off the ground.</li>
</ul>


<p>Besides I helped sucesfully delivering 3 large projects in the time. Two of them exceeded the customers expectations.
One didn&#8217;t. Simply put, I will never do any form of TFS customization again, never ever.</p>

<p>In mid 2010, out of curiosity, I bought my first Apple computer, a MacBook Pro. I started
to play around with it and actually liked it a lot. Guess what, I&#8217;m an IPhone and IPod owner, too.
I&#8217;m not religious about Apple products, though. They work for me and excactly deliver what
I expect and need from those devices.</p>

<p>Well, and after a while of using OSX, I began revisiting Ruby (after my initial steps in 2008) and
picked up where I left the last time. I learned the basics of using <a href="http://www.vim.org">VIM</a>,
<a href="http://www.git-scm.com">Git</a> and started to play around with <a href="http://www.rubyonrails.org">Ruby on Rails</a> and
with Rails the whole topic of web development (after having left the topic in 2003).</p>

<h2>Big changes are coming for me in 2012</h2>

<p>As you might have noticed, I&#8217;ve revived my blog. The design and the technology chosen might give you
an idea where this is all heading.</p>

<p>Next year I&#8217;m going to work in a new team, with non .NET technology, with a unix based OS as my primary development environment.
I&#8217;m really exited to join <a href="http://www.xing.de">XING</a> as a developer working on their <a href="http://www.rubyonrails.org">Ruby on Rails</a> based
platform.</p>

<p>Besides I&#8217;m planning to learn a new (non .NET) programming language next year, that currently feels completely alien to me, namely <a href="http://www.clojure.org">Clojure</a>.
If everything goes as planned we&#8217;re going to re-use the bookclub format we used in 2010 for the F# bookclub in Munich, though
in a somewhat virtualized form, and try to get into the <a href="http://www.joyofclojure.com">Joy of Clojure</a>.</p>

<p>Last but not least, I&#8217;m not nuts and discard everything I learned during the last years.</p>

<p>I&#8217;m probably going to play with <a href="http://monodevelop.com/">MonoDevelop</a> and try to learn how to build <a href="http://xamarin.com/monotouch">iOS</a>, <a href="http://android.xamarin.com/">Android</a> and <a href="http://www.mono-project.com/MonoMac">Cocoa</a> apps using <a href="http://www.mono-project.com/">Mono</a>.
At least that&#8217;s the plan.</p>

<h2>On leaving .NET</h2>

<p>What does leaving acutally mean? Let&#8217;s break this down a bit:</p>

<ul>
<li><p><strong>Am I going to stop playing the active role for Machine.Fakes?</strong>
Undecided, but probable since I won&#8217;t be using it on a day to day basis next year.
I&#8217;ve ideas for re-using it under Mono, but I haven&#8217;t actually tried so far. We&#8217;ll see &#8230;</p></li>
<li><p><strong>Do I stop being in touch with parts of the .NET community?</strong>
Don&#8217;t be silly</p></li>
<li><p><strong>Do I stop visiting .NET Open Spaces or UserGroups</strong> Well, that is at least not my plan.</p></li>
</ul>


<h2>What, no leaving .NET rant?</h2>

<p>In the last 4 years of active community involvement I had the honour and pleasure to get
to know a great bunch of .NET developers. Talented and gifted people, from all over Germany
(and even outside Germany), each of them awesome in his own way.</p>

<p>Just to name a few <a href="http://www.twitter.com/agross">@agross</a>, <a href="http://www.twitter.com/sforkmann">@sforkmann</a>, <a href="http://www.twitter.com/ilkerde">@ilkerde</a>, <a href="http://www.twitter.com/stefanlieser">@StefanLieser</a>,
<a href="http://www.twitter.com/mikebild">@mikebild</a>, <a href="http://www.twitter.com/sshishkin">@sshishkin</a>, <a href="http://www.twitter.com/lanwin">@lanwin</a>, <a href="http://www.twitter.com/cdeger">@cdeger</a>, <a href="http://www.twitter.com/oakinger">@oakinger</a>, <a href="http://www.twitter.com/jpboodhoo">@jpboodhoo</a>,
<a href="http://www.twitter.com/kostjaklein">@kostjaklein</a>, <a href="http://www.twitter.com/torstenweber">@torstenweber</a>, <a href="http://www.twitter.com/gregyoung">@gregyoung</a> and a lot more.  I could probably make the list a lot longer, but those are the people that managed to
leave a lasting impact on me. Thank you all!</p>

<p>The German .NET community has changed tremendously (for the good) in the time I&#8217;ve been
working with .NET, which is in great parts a result of the work of those people.
Moving away from focus solely on tools crafted by you know who, to embrace and teach practices/principles of
of professional software development, like SOLID, TDD/BDD/ATDD, Continuous Integration, Continuous Delivery, et al.</p>

<p>Microsoft (at least in my opinion) has also changed a lot (well at least in some areas, thank you ASP.NET team!).
They&#8217;re hosting code at publicly available locations, they&#8217;re accepting patches from the outside and they actually listen
carefully to community feedback. They still got a lot to improve, but hey, I wouldn&#8217;t believed you if you&#8217;d told me 4 years ago how parts of MS engage with
the community today.</p>

<h3>Why a technology change?</h3>

<p>Change often implies frustration or dissatisfaction with something. I want this
to be as clear as possible: <strong>Those are not my primary reasons</strong>.</p>

<p>I&#8217;m going to tell you a little secret right now, well sort of. Some of my older colleagues
know this, some still don&#8217;t:</p>

<p><strong>I never owned a computer before my 21st birthday. I didn&#8217;t even write a program till I was 22.</strong></p>

<p>Let that sink in for a moment.  Nevertheless, I somehow managed to leave every company I&#8217;ve worked for
in the the last 10 years with the impression that I was one of the top software
developers they ever worked with.</p>

<p>I often wondered how I managed to pull that off, but I guess it boils down to this: I really, really, really badly want to be
good at what I&#8217;m doing (whatever that is) and am willing to work as hard as necessary in order to reach that goal. I&#8217;ve had
my ups and downs with this strategy, but in the end it made me how I am and helped me to get where I am today.</p>

<p>I honestly believe that you can achieve much more than you might think (though some people prefer to tell you the opposite and are very resistant with that).</p>

<blockquote><p>If you remain in your comfort zone you will not go any further</p><footer><strong>Catherine Pulsifer</strong> <cite></cite></footer></blockquote>


<p><strong>If</strong> you&#8217;re passionate about what you do, <strong>if</strong> you put yourself in the
mood that you basically know nothing and that you can learn (in one way or another)
from nearly anybody around you, <strong>if</strong> you accept that you will fail in one
way or another and failing is ok if and only if you learn from failure,
and last but not least <strong>if</strong> you accept that for the most of us being good at something
actually is the result of continuous learning, hard work, deliberate practice and experience (as
opposed to talent only), <strong> than you define what&#8217;s possible for you and not somebody else.</strong>
At least that&#8217;s my way of thinking &#8230;</p>

<p>I&#8217;d like to think about myself as a developer, not as a C# developer, not as a OO
developer, not as an ASP.NET developer, nor Javascript developer for that matters.
Developer, just developer. Building awesome products in teams is my passion, but not building
awesome products with technlogy X,Y or Z. Languages, technlogies are <strong>part of my toolset, not something I&#8217;m
married to</strong> till death tears us apart.</p>

<h3>Is that all?</h3>

<p>It&#8217;s no big secret that being an engaged person that likes to think outside the
box and not limits his views to the Microsoft way of building software, can
sometimes have a hard time in the Microsoft ecosystem (especially if you like to
work with teams and don&#8217;t want to go down the independent consultant road).</p>

<p>It&#8217;s utterly frustrating to hear from Microsoft evangelists that you&#8217;re not part
of the target group for their product (!!! Visual Studio !!!) if you ask for
more extensibility and less re-implementations of existing open source solutions
(No shit, that really happened to me 1 month ago and I still can&#8217;t believe they
actually said that).</p>

<p>I like the .NET framework and I especially like C#, but I&#8217;m more and
more frustrated with their sales driven approach to software development.  The frustrating part is that in
the end I can understand why they bundle/entangle their products from a sales perspective.
It&#8217;s probably my fault for not realizing earlier that this somewhat clashes
with the idea of continuous improvement.</p>

<p>I sometimes tend to think that a greater part of us (of the .NET community)
would live a lot happier professional live outside the MS ecosystem and those
of us that do manage to pull that off inside that MS ecosystem have build
themselves <strong>a beautiful oasis in a vast and wide desert</strong>.</p>

<p>My current  project team is an example of the latter, using a mix of techniques from
Lean, ATDD , BDD, with feature branching and extensive code review sessions.
And all that is possible without the TFS and without 80% of the features of Visual
Studio. Who would have thought that &#8230;</p>

<p>However this team is breaking apart at the end of the year and I&#8217;m in the
position to start something new next year.  Trying to get a new .NET team
to the same state (or above) was an option, but I decided against that. Instead I
decided to venture into new territory, to extend my toolkit with
a new language and a new platform.</p>

<h3>Why Ruby?</h3>

<blockquote><p>I skate to where the puck is going to be, not where it has been</p><footer><strong>Wayne Gretzky</strong> <cite></cite></footer></blockquote>


<p>I really, really like the quote, I really do, but do I think Ruby is the next
big thing?</p>

<p><strong>Honestly, I don&#8217;t</strong></p>

<p>I don&#8217;t say this in any negative way.  At the end of the end of the day
Ruby is just a tool (though awesome tool) that can be used,
misused and abused for getting things done.</p>

<p>What I&#8217;m more interested in is the Ruby community itself or even more
precise the state of engineering practices in the community. From the outside
they look far ahead than the common denominator in the Microsoft world.  Lots and lots of
.NET OSS frameworks and tools have been influenced by the Ruby community
in one way or another.</p>

<blockquote><p>The Moslems have a proverb,  to understand a man, you&#8217;ve got to walk a mile in his shoes, whether they fit or not.</p><footer><strong>Raymond Wiley Miller</strong> <cite>1964</cite></footer></blockquote>


<p>The Ruby community seems to be a great starting point for my venture.
Lots of innovation going on, lots of passion involved, very OSS centered
and goal orientated.  However, I will never know for sure if those expectations
hold up to the reality unless I work amongst them for a while.</p>

<p>So as you might have guessed, the focus of future posts will probably a lot less
.NET centric &#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[prio conference 2010]]></title>
    <link href="http://www.bjoernrochel.de/2010/06/21/prio-conference-2010/"/>
    <updated>2010-06-21T08:26:00+02:00</updated>
    <id>http://www.bjoernrochel.de/2010/06/21/prio-conference-2010</id>
    <content type="html"><![CDATA[<p>In case you haven’t heard it yet, I’m going to give 2 talks at this years <a href="http://www.prioconference.de/" title="prio. conference">prio conference</a>.</p>

<p><img src="http://www.bjoernrochel.de/images/posts/prioSpreaker-Banner.gif" title="Prio conference 2010" alt="prio conference logo" /></p>

<p>I’m very exited to be part of <a href="http://www.prioconference.de/Speaker/Speaker-prio.conference-2010" title="prio 2010 speakers">this years speaker line up</a> (though the quality of the line up is a bit intimidating ;-)).
Both talks will be on the <a href="http://www.prioconference.de/Programm/Programm-prio.conference-20.-Oktober-2010/prio.conference-Track-32">20th of October in Track 4</a></p>

<ul>
<li>10.05 – 11.05h, Introduction to Rhino.ServiceBus</li>
<li>15.20h – 16.20h, (ServiceBus) Patters for always responsive clients</li>
</ul>


<p>Looking forward to see you there &#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[.NET Open Space Süd Retrospective]]></title>
    <link href="http://www.bjoernrochel.de/2010/06/21/net-open-space-sd-retrospective/"/>
    <updated>2010-06-21T07:46:59+02:00</updated>
    <id>http://www.bjoernrochel.de/2010/06/21/net-open-space-sd-retrospective</id>
    <content type="html"><![CDATA[<p>I haven&#8217;t blogged in a while, mostly because time has become such a limited resource in the last weeks or to be honest actually months.
Blogging, my current project, workshops, the F# Bookclub, preparation of the next conference appearances and of course xUnit.BDDExtensions all want a piece of that cake.
Finding the right balance between those tasks is often not as easy as I would like it to be.
More often as I like skipping blogging seems to be the easiest way to regain some time.
I’m really sorry for that, but you know I try to do my best.
On the other hand this is a new blog post, so things can’t be that bad, can’t they?
Although I haven’t slept much the last 3 days, something inside me urges to write this post.</p>

<!--more-->


<p>Most of the time of the past weekend I spend in Karlsruhe at the .NET Open Space Süd. To sum it up in 5 words: <strong>The event was a blast</strong>.
I had an unbelievable amount of fun coding, chatting, learning with some of smartest people in the German .NET Community.</p>

<p>This was the 4th Open Space event I attended during the last 2 years and I think this one was the best so far. Why?</p>

<ol>
<li>The content of the sessions I attended / participated in was really high quality. I learned something new in every fracking session. Every session was dense,
focused and we had at least one participant who actually had extensive field experience in the related topic area. No CQRS session disaster this time ;-)</li>
<li>Overly generic topics ala “I want to talk about TDD” or “Let’s talk about (D)DDD” were the minority.</li>
</ol>


<p>Besides that, one personal note: It was a blast to meet some of the guys I got to know throughout the last 2 years again and see the personal progression each of them has made.
Guys, your awesome. Keep up your pace!</p>

<p>So, just to give you an idea about the sessions I attended here’s a quick sum-up of my weekend @NOS_SUED.</p>

<h2>WTF is a Monad? (C# Edition)</h2>

<p>The first day started with one of the hardest topics you might imagine, Monads.
@sforkmann did a great job explaining the various basic monadic types and their implementation in C#.
Helped me a lot to form a better picture of this abstract concept in my mind. Was nice to see the Maybe Monad implemented using query comprehension syntax
(though we found out that the related msdn sample doesn’t compile aka sucks). Sadly we didn’t have time enough to take a look at Monads in F#. Homework, I guess.</p>

<p>Funny side note: The word “Monad” was present throughout the complete weekend.
I guess the revelation that most of us have been using a Monad for quite a while now (in LINQ), though not knowing it, shocked quite a bunch of participants.</p>

<h2>Git tips &amp; tricks </h2>

<p>Next up was @agross with Git. I never forgot that Alex was the person that told me about this “Git thing” two years ago, long before the topic suddenly
started to get the attentions of the .NET OS community. Main focus of this session was the various approaches around conflict resolution and a detailed
look at the difference between merging and rebasing.</p>

<p>Especially interesting was to hear a bit more about this experience with Git in the context of a popular .NET OS project (Alex is one of the core maintainers of MSpec).</p>

<h2>Convention over Configuration</h2>

<p>Halfway through Saturday I talked a bit about my experience with “Convention over Configuration”.
My current project uses conventions a lot (but only for binding, thx @ilkerde for the clarification) and I’m really happy with the outcome so far.
On retrospective I think @ilkerde, @agross and myself did a really good job in categorizing the various ways CoC can be applied from sourcecode,
to builds to deployment.</p>

<h2>BDD vs. ATDD</h2>

<p>The biggest session I participated in was the (dunno what was the exact name of the session)
“Behavior Driven Development vs. classic Acceptance Test Driven Development” session. Was cool to have @agross (MSpec maintainer),
@ssishkin ( my personal Fitnesse guru), @sforkmann (creator of NaturalSpec), @DerAlbert and several others who’ve been doing BDD,
something BDD-like or ATTD in a room and hearing their various war stories and views on the topic.</p>

<h2>Introduction to Reactive Extensions</h2>

<p>If you start a day with Monads to be consequent you need to end it with Monads, too. @sshishkin and @sforkmann gave an interesting talk about the ideas behind IObserver,
IObservable and IQbservable. BTW, how cool is this IQbservable idea?</p>

<h2>Specification By Example Do’s &amp; Dont’ s</h2>

<p>In this session we took a look at the various proven practices in BDD specification design and the other side of the coin,
the different flavors of specification smells. We discussed the ObjectMother pattern, TestData Builder Pattern,
modularization strategies for specs and especially the idea of having the specifications side-by-side with the actual production
code in a single assembly. Very good to see other proponents of this idea.</p>

<p>I was literally blown away by finally seeing the grouping functionality for Visual Studio,
I always wanted to have, <a href="http://mokosh.co.uk/wp-content/uploads/2010/04/image23.png">alive</a>. <a href="http://mokosh.co.uk/vscommands/">VsCommands</a>, I’m going to install you today!</p>

<h2>Behavior Driven Development BDD Framework - Shootout (MSpec vs. NaturalSpec vs. xUnit.BDDExtensions)</h2>

<p>The (un)conference ended for me with a side-by-side comparison of different BDD frameworks.
Several guys asked for this. I guess it makes sense when you’ve got the authors of 3 different frameworks for a particular topic together.
Although I didn’t like the idea at first sight (I don’t like framework wars that much, besides in the end the idea of BDD matters, and not the tools) the session turned
out to be a win for all of us. We demo-ed the typical bank transfer sample in each of the frameworks (MSpec, xUnit.BDDExtensions, NaturalSpec).
Afterwards every framework owner demo-ed additional features more or less unique to the particular framework. I guess, each of us got ideas for “new” features ;-)</p>

<h2>Closing thoughts</h2>

<p>I really enjoyed being in Karsruhe. Big thx to the organization team for making this event possible. I hope to see some of you again at the Open Space in Leipzig later this year or same place next year.</p>

<p>Special thx to @agross, @sforkmann, @ilkerde, @roeb and @sshiskin for the intense and inside-full discussions. You rock guys!!!</p>

<p>One last advice: Don’t forget <strong>Jean Clojure’s basal monad</strong></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Kata StringCalculator in F#]]></title>
    <link href="http://www.bjoernrochel.de/2010/05/12/kata-stringcalculator-in-f/"/>
    <updated>2010-05-12T19:36:38+02:00</updated>
    <id>http://www.bjoernrochel.de/2010/05/12/kata-stringcalculator-in-f</id>
    <content type="html"><![CDATA[<p>Yesterday&#8217;s F# bookclub meeting in Munich was awesome as usual. It’s very interesting to see our overall understanding of functional programming progressing.
Slowly, but steady. Main topics we discussed on the last meeting were Currying and Tail Recursion. Finally &#8220;got that&#8221; (at least I think so ;-))</p>

<p>Two meetings ago we decided to do some coding on every meeting. The previous meeting we solved Kata FizzBuzz and on yesterday’s meeting we tried to dance with
<a href="http://osherove.com/tdd-kata-1/">Roy Osheroves StringCalculator</a>. We didn&#8217;t make it completely to the end, but I think we solved most of the Kata.</p>

<!--more-->


<p>You can find yesterdays code at the end of this post. I&#8217;m sure in parts it smells a bit imperative and it definitely uses too much Regex - KungFu,
but overall I&#8217;m pleased with the result. We&#8217;ve tried to incorporate the feedback we got on the Kata FizzBuzz code. I would love to get feedback on this one as well.
What could be done better, cleaner or simply differently?</p>

<figure class='code'><figcaption><span>Kata StringCalculator in F#</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">open</span> <span class="n">System</span>
</span><span class='line'><span class="n">open</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">RegularExpressions</span>
</span><span class='line'><span class="n">open</span> <span class="n">Xunit</span>
</span><span class='line'>
</span><span class='line'><span class="n">let</span> <span class="n">shouldBeEqualTo</span> <span class="n">a</span> <span class="n">b</span> <span class="p">=</span> <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">let</span> <span class="n">parse</span> <span class="k">value</span> <span class="p">=</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">String</span><span class="p">.</span><span class="n">IsNullOrEmpty</span><span class="p">(</span><span class="k">value</span><span class="p">)</span> <span class="n">then</span> <span class="m">0</span> <span class="k">else</span>
</span><span class='line'>    <span class="n">match</span> <span class="n">Int32</span><span class="p">.</span><span class="n">TryParse</span> <span class="k">value</span> <span class="n">with</span>
</span><span class='line'>    <span class="p">|</span> <span class="p">(</span><span class="k">false</span><span class="p">,</span><span class="n">_</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">failwithf</span> <span class="s">&quot;Did not parse value %s&quot;</span> <span class="k">value</span>
</span><span class='line'>    <span class="p">|</span> <span class="p">(</span><span class="k">true</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="n">when</span> <span class="n">n</span> <span class="p">&lt;</span> <span class="m">0</span> <span class="p">-&gt;</span> <span class="n">failwithf</span> <span class="s">&quot;Negatives not allowed %s&quot;</span> <span class="k">value</span>
</span><span class='line'>    <span class="p">|</span> <span class="p">(</span><span class="k">true</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="n">when</span> <span class="n">n</span> <span class="p">&gt;=</span> <span class="m">1000</span> <span class="p">-&gt;</span><span class="m">0</span>
</span><span class='line'>    <span class="p">|</span> <span class="p">(</span><span class="k">true</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">n</span>
</span><span class='line'>
</span><span class='line'><span class="n">let</span> <span class="nf">splitIntoDelimitersAndRest</span> <span class="p">(</span><span class="n">calculationString</span><span class="p">:</span><span class="n">String</span><span class="p">)</span> <span class="p">=</span>
</span><span class='line'>    <span class="n">let</span> <span class="n">defaultDelmiters</span> <span class="p">=</span> <span class="p">[</span><span class="s">&quot;,&quot;</span><span class="p">;</span><span class="s">&quot;\n&quot;</span><span class="p">]</span>
</span><span class='line'>    <span class="n">let</span> <span class="n">regex</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Regex</span><span class="p">(</span><span class="s">&quot;^//(?&lt;defaultDelimiter&gt;.*?)\\n(?&lt;rest&gt;.*)$&quot;</span><span class="p">,</span> <span class="n">RegexOptions</span><span class="p">.</span><span class="n">Singleline</span><span class="p">)</span>
</span><span class='line'>    <span class="n">match</span> <span class="n">regex</span><span class="p">.</span><span class="n">Match</span> <span class="n">calculationString</span> <span class="n">with</span>
</span><span class='line'>    <span class="p">|</span> <span class="n">m</span> <span class="n">when</span> <span class="n">m</span><span class="p">.</span><span class="n">Success</span> <span class="p">-&gt;</span>
</span><span class='line'>        <span class="n">let</span> <span class="n">delimiters</span> <span class="p">=</span> <span class="n">List</span><span class="p">.</span><span class="n">Cons</span><span class="p">(</span><span class="n">m</span><span class="p">.</span><span class="n">Groups</span><span class="p">.[</span><span class="s">&quot;defaultDelimiter&quot;</span><span class="p">].</span><span class="n">Value</span><span class="p">,</span> <span class="n">defaultDelmiters</span><span class="p">)</span>
</span><span class='line'>        <span class="p">(</span><span class="n">delimiters</span><span class="p">,</span> <span class="n">m</span><span class="p">.</span><span class="n">Groups</span><span class="p">.[</span><span class="s">&quot;rest&quot;</span><span class="p">].</span><span class="n">Value</span><span class="p">)</span>
</span><span class='line'>    <span class="p">|</span> <span class="n">_</span> <span class="p">-&gt;</span> <span class="p">(</span><span class="n">defaultDelmiters</span><span class="p">,</span> <span class="n">calculationString</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">let</span> <span class="nf">add</span> <span class="p">(</span><span class="n">calculationString</span><span class="p">:</span><span class="n">String</span><span class="p">)</span> <span class="p">=</span>
</span><span class='line'>   <span class="n">let</span> <span class="n">splitResult</span> <span class="p">=</span> <span class="n">splitIntoDelimitersAndRest</span> <span class="n">calculationString</span>
</span><span class='line'>   <span class="n">let</span> <span class="n">delimiters</span> <span class="p">=</span> <span class="n">fst</span> <span class="n">splitResult</span> <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">toArray</span>
</span><span class='line'>   <span class="n">let</span> <span class="n">rest</span> <span class="p">=</span> <span class="n">snd</span> <span class="n">splitResult</span>
</span><span class='line'>   <span class="n">rest</span><span class="p">.</span><span class="n">Split</span><span class="p">(</span><span class="n">delimiters</span><span class="p">,</span> <span class="n">StringSplitOptions</span><span class="p">.</span><span class="n">RemoveEmptyEntries</span><span class="p">)</span>
</span><span class='line'>   <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">ofSeq</span>
</span><span class='line'>   <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="n">parse</span>
</span><span class='line'>   <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">sum</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">an</span> <span class="n">empty</span> <span class="kt">string</span> <span class="k">is</span> <span class="n">supplied</span> <span class="n">it</span> <span class="n">should</span> <span class="k">return</span> <span class="m">0</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'>    <span class="n">String</span><span class="p">.</span><span class="n">Empty</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="p">(</span><span class="n">shouldBeEqualTo</span> <span class="m">0</span><span class="p">)</span>
</span><span class='line'><span class="na"> </span>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">a</span> <span class="n">single</span> <span class="n">digit</span> <span class="k">is</span> <span class="n">supplied</span> <span class="n">it</span> <span class="n">should</span> <span class="k">return</span> <span class="n">the</span> <span class="n">digits</span> <span class="k">value</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;1&quot;; &quot;2&quot;; &quot;3&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">1</span><span class="p">;</span><span class="m">2</span><span class="p">;</span><span class="m">3</span><span class="p">]</span>
</span><span class='line'><span class="na"> </span>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">separated</span> <span class="n">by</span> <span class="n">a</span> <span class="n">comma</span> <span class="n">it</span> <span class="n">should</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">some</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;1,2&quot;; &quot;3,4&quot;; &quot;4,5&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">3</span><span class="p">;</span><span class="m">7</span><span class="p">;</span><span class="m">9</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">more</span> <span class="n">than</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">separated</span> <span class="n">by</span> <span class="n">a</span> <span class="n">comma</span> <span class="n">it</span> <span class="n">should</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">sum</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;1,2,4,5&quot;; &quot;3,4,5&quot;; &quot;4,5,6,7,8&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">12</span><span class="p">;</span><span class="m">12</span><span class="p">;</span><span class="m">30</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">more</span> <span class="n">than</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">separated</span> <span class="n">by</span> <span class="k">new</span> <span class="n">line</span> <span class="n">character</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">to</span> <span class="n">sum</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;1\n2\n4\n5&quot;; &quot;3\n4\n5&quot;; &quot;4\n5\n6\n7\n8&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">12</span><span class="p">;</span><span class="m">12</span><span class="p">;</span><span class="m">30</span><span class="p">]</span>
</span><span class='line'><span class="na"> </span>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">more</span> <span class="n">than</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">separated</span> <span class="n">by</span> <span class="k">new</span> <span class="n">line</span> <span class="n">character</span> <span class="n">or</span> <span class="n">comma</span> <span class="n">it</span> <span class="n">should</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">some</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;1,2\n4,5&quot;; &quot;3,4\n5&quot;; &quot;4,5\n6\n7\n8&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">12</span><span class="p">;</span><span class="m">12</span><span class="p">;</span><span class="m">30</span><span class="p">]</span>
</span><span class='line'><span class="na"> </span>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">more</span> <span class="n">than</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">separated</span> <span class="n">by</span> <span class="n">a</span> <span class="n">user</span> <span class="n">supplied</span> <span class="k">default</span> <span class="n">delimiter</span> <span class="n">it</span> <span class="n">should</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">sum</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;//*\n1*2*4*5&quot;; &quot;//$\n3$4$5&quot;; &quot;//%\n4%5%6%7%8&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">12</span><span class="p">;</span><span class="m">12</span><span class="p">;</span><span class="m">30</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">more</span> <span class="n">than</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">separated</span> <span class="n">by</span> <span class="n">a</span> <span class="n">user</span> <span class="n">supplied</span> <span class="k">default</span> <span class="n">delimiter</span> <span class="n">or</span> <span class="n">one</span> <span class="n">of</span> <span class="n">the</span> <span class="n">standard</span> <span class="n">delimiters</span> <span class="n">it</span> <span class="n">should</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">sum</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;//*\n1*2*4*5&quot;; &quot;//$\n3$4$5&quot;; &quot;//%\n4\n5%6%7%8&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">12</span><span class="p">;</span><span class="m">12</span><span class="p">;</span><span class="m">30</span><span class="p">]</span>
</span><span class='line'><span class="na"> </span>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="n">digits</span> <span class="n">greater</span> <span class="n">than</span> <span class="m">1000</span> <span class="n">are</span> <span class="n">supplied</span> <span class="n">it</span> <span class="n">should</span> <span class="n">ignore</span> <span class="n">them</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;//*\n1*2000*4*5&quot;; &quot;//$\n3$4$1000&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">10</span><span class="p">;</span><span class="m">7</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="err">``</span><span class="n">When</span> <span class="k">using</span> <span class="nn">more</span> <span class="n">than</span> <span class="n">two</span> <span class="n">digits</span> <span class="n">with</span> <span class="n">with</span> <span class="n">a</span> <span class="n">custom</span> <span class="n">separator</span> <span class="n">of</span> <span class="n">multiple</span> <span class="n">characters</span> <span class="n">ít</span> <span class="n">should</span> <span class="n">be</span> <span class="n">able</span> <span class="n">to</span> <span class="n">sum</span> <span class="n">them</span> <span class="n">up</span><span class="err">``</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [&quot;//asdf\n1asdf4asdf5&quot;; &quot;//as\n3as4as&quot;]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="k">add</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">shouldBeEqualTo</span> <span class="p">[</span><span class="m">10</span><span class="p">;</span><span class="m">7</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Kata FizzBuzz in F#]]></title>
    <link href="http://www.bjoernrochel.de/2010/04/29/kata-fizzbuzz-in-f/"/>
    <updated>2010-04-29T19:00:00+02:00</updated>
    <id>http://www.bjoernrochel.de/2010/04/29/kata-fizzbuzz-in-f</id>
    <content type="html"><![CDATA[<p>Last F# book club meeting in Munich was awesome (as usual). 2 weeks ago we decided to do a Code Kata on each subsequent meeting. This week was our first, with Kata FizzBuzz.</p>

<!--more-->


<p>This is what we came up with. (BTW: Partial function application and pipelining rocks !!!)</p>

<figure class='code'><figcaption><span>Kata FizzBuzz in F#</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">open</span> <span class="n">Xunit</span>
</span><span class='line'>
</span><span class='line'><span class="n">let</span> <span class="n">fizzBuzz</span> <span class="n">number</span> <span class="p">=</span>
</span><span class='line'>     <span class="n">match</span> <span class="n">number</span> <span class="n">with</span>
</span><span class='line'>     <span class="p">|</span> <span class="n">n</span> <span class="n">when</span> <span class="n">n</span><span class="p">%</span><span class="m">15</span><span class="p">=</span><span class="m">0</span> <span class="p">-&gt;</span> <span class="s">&quot;FizzBuzz&quot;</span>
</span><span class='line'>     <span class="p">|</span> <span class="n">n</span> <span class="n">when</span> <span class="n">n</span><span class="p">%</span><span class="m">3</span><span class="p">=</span><span class="m">0</span> <span class="p">-&gt;</span> <span class="s">&quot;Fizz&quot;</span>
</span><span class='line'>     <span class="p">|</span> <span class="n">n</span> <span class="n">when</span> <span class="n">n</span><span class="p">%</span><span class="m">5</span><span class="p">=</span><span class="m">0</span> <span class="p">-&gt;</span> <span class="s">&quot;Buzz&quot;</span>
</span><span class='line'>     <span class="p">|</span> <span class="n">_</span> <span class="p">-&gt;</span> <span class="n">number</span><span class="p">.</span><span class="n">ToString</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="n">let</span> <span class="n">areEqual</span> <span class="n">expected</span> <span class="n">actual</span> <span class="p">=</span>
</span><span class='line'>     <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">expected</span><span class="p">,</span> <span class="n">actual</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="nf">Should_return_the_digit_for_numbers_which_are_not_dividable_by_3_or_5</span><span class="p">()</span>  <span class="p">=</span>
</span><span class='line'><span class="na">    [1;2;11;13;16]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="n">fizzBuzz</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter2</span> <span class="n">areEqual</span> <span class="p">[</span><span class="s">&quot;1&quot;</span><span class="p">;</span><span class="s">&quot;2&quot;</span><span class="p">;</span><span class="s">&quot;11&quot;</span><span class="p">;</span><span class="s">&quot;13&quot;</span><span class="p">;</span><span class="s">&quot;16&quot;</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="nf">Should_return_Fizz_for_digits_dividable_by_3</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [3;6;9;12]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="n">fizzBuzz</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter</span> <span class="p">(</span><span class="n">areEqual</span> <span class="s">&quot;Fizz&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="nf">Should_return_Buzz_for_digits_dividable_by_5</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [5;10;20;25]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="n">fizzBuzz</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter</span> <span class="p">(</span><span class="n">areEqual</span> <span class="s">&quot;Buzz&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="nf">Should_return_FizzBuzz_for_digits_dividable_by_3_and_5</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'><span class="na">    [15;30;45;60]</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">map</span> <span class="n">fizzBuzz</span>
</span><span class='line'>    <span class="p">|&gt;</span> <span class="n">List</span><span class="p">.</span><span class="n">iter</span> <span class="p">(</span><span class="n">areEqual</span> <span class="s">&quot;FizzBuzz&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>If anyone of you hardcore functional guys out there notices something utterly wrong or something that could radically simplified, please let me know.
We’re eager to learn more.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Testing F# code with xUnit.net (on .NET 4.0)]]></title>
    <link href="http://www.bjoernrochel.de/2010/04/19/testing-f-code-with-xunit-net-on-net-4-0/"/>
    <updated>2010-04-19T08:48:46+02:00</updated>
    <id>http://www.bjoernrochel.de/2010/04/19/testing-f-code-with-xunit-net-on-net-4-0</id>
    <content type="html"><![CDATA[<p>A lot of my free time currently goes into learning F#.
While I had a great time playing around with the F# REPL FSI, I came to the conclusion that using FSI is not my
preferred way of a) learning the F# language and b) to develop code.
Writing unit tests simply for the purpose of learning and understanding of a language/component/system (aka &quot;Learning tests&quot;) seems to be a better fit,
at least for me. So, I sat down in order to see how I can use my beloved xUnit.net for this.
As it turns out it&#8217;s not that difficult, but it&#8217;s got some hurdles.</p>

<!--more-->


<h2>Possible runtime differences</h2>

<p>xUnit.net 1.5 is compiled against the .Net Framework 3.5. If you&#8217;re using F# in combination with the VS2010 RC or
RTM (like I do) you&#8217;ve got at least to options to make them work together.</p>

<ul>
<li>Use multi-targeting and configure the F# projects to compile for the .NET 3.5 runtime  (<code>Properties/Application/Target Framework</code>).</li>
<li>Update the app.config files of xunit.console.exe and xunit.gui.exe with a startup section and specify the .NET framework 4.0 version as supported.</li>
</ul>


<figure class='code'><figcaption><span>Update the app config</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'> <span class="nt">&lt;startup&gt;</span>
</span><span class='line'>  <span class="nt">&lt;supportedRuntime</span> <span class="na">version=</span><span class="s">&quot;v4.0.30128&quot;</span> <span class="na">safemode=</span><span class="s">&quot;true&quot;</span><span class="nt">/&gt;</span> <span class="c">&lt;!-- VS2010 RC --&gt;</span>
</span><span class='line'>  <span class="nt">&lt;supportedRuntime</span> <span class="na">version=</span><span class="s">&quot;v4.0.30319&quot;</span> <span class="na">safemode=</span><span class="s">&quot;true&quot;</span><span class="nt">/&gt;</span> <span class="c">&lt;!-- VS2010 RTM --&gt;</span>
</span><span class='line'> <span class="nt">&lt;/startup&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Pay attention to your parentheses</h2>

<p>My choice was to update the xUnit.net configurations. After the update of the configuration files my assembly was loaded,
however the test runner failed to detect my unit tests. As it turns out the open parentheses after a test function play an important role.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="na"> [&lt;Fact&gt;]</span>
</span><span class='line'> <span class="n">let</span> <span class="n">After_converting_a_valid_data_row_the_title_should_have_been_extracted</span> <span class="p">=</span> <span class="c1">//This compiles, but the test doesn&#39;t show up in the test runner.</span>
</span><span class='line'>    <span class="n">let</span> <span class="n">row</span> <span class="p">=</span> <span class="n">convertDataRow</span> <span class="s">&quot;Test, 1234&quot;</span>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">fst</span><span class="p">(</span><span class="n">row</span><span class="p">),</span> <span class="s">&quot;Test&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="na"> [&lt;Fact&gt;]</span>
</span><span class='line'> <span class="n">let</span> <span class="nf">After_converting_a_valid_data_row_the_title_should_have_been_extracted</span><span class="p">()</span> <span class="p">=</span> <span class="c1">//This will work fine</span>
</span><span class='line'>    <span class="n">let</span> <span class="n">row</span> <span class="p">=</span> <span class="n">convertDataRow</span> <span class="s">&quot;Test, 1234&quot;</span>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">fst</span><span class="p">(</span><span class="n">row</span><span class="p">),</span> <span class="s">&quot;Test&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>My first reaction was: WTF? But after reading some more chapters of &quot;Real world functional programming&quot; and a discussion at our
local F# book club the behavior makes sense to me. My current understanding is that omitting the parentheses results in a different method signature.
You can easily spot this in FSI:</p>

<ul>
<li>The first one is <code>val After_converting_a_valid_data_row_the_title_should_have_been_extracted : unit</code></li>
<li>The second one results in a <code>val After_converting_a_valid_data_row_the_title_should_have_been_extracted : unit -&gt; unit</code></li>
</ul>


<p>What you can see here is that the first function signature doesn&#8217;t have a parameter while the second has a parameter of the type <code>unit</code>.
One interesting difference between F# and C# is that the F# equivalent to C#&#8217;s <code>void</code> is an actual type called <code>unit</code>.
The fun part is that <code>()</code> is it&#8217;s only value. Parentheses play a completely different role here ;-)</p>

<p>The xUnit test runner looks for methods with one unit parameter and a return type of unit.
That&#8217;s why you need the parentheses.</p>

<h2>Testing exceptions with xUnit.Net</h2>

<p>One little subtlety I came across when testing exceptions is that you have to explicitly ignore the return value
when you&#8217;re using Assert.Throws and pass in a method which doesn&#8217;t return unit. Feels a bit strange at first, but explainable.
Again a signature mismatch. <code>Assert.Throws</code> expects a method with a <code>unit -&gt; unit</code> signature.
You have to do this in order to please the compiler. (If there&#8217;s a better way for this, please let me know)</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="na">[&lt;Fact&gt;]</span>
</span><span class='line'><span class="n">let</span> <span class="nf">Trying_to_convert_an_invalid_format_throws</span><span class="p">()</span> <span class="p">=</span>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">Throws</span><span class="p">(</span><span class="n">fun</span> <span class="p">()</span> <span class="p">-&gt;</span> <span class="n">convertDataRow</span> <span class="s">&quot;FuBar&quot;</span> <span class="p">|&gt;</span> <span class="n">ignore</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>The ignore function simply throws away any value it receives, returns a unit and makes the F# compiler happy.</p>

<h2>Conclusion</h2>

<p>I hope you saw in this post that testing F# with xUnit.net is actually pretty easy.
It&#8217;s also a wonderful case for language interop on top of the CLR. Go see it for yourself :-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Cutting the fluff from Service registration with StructureMap - revisited]]></title>
    <link href="http://www.bjoernrochel.de/2010/03/15/cutting-the-fluff-from-service-registration-with-structuremap-revisited/"/>
    <updated>2010-03-15T21:28:45+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/03/15/cutting-the-fluff-from-service-registration-with-structuremap-revisited</id>
    <content type="html"><![CDATA[<p>This is just a quick update of an <a href="http://www.bjoernrochel.de/2009/07/24/cutting-the-fluff-from-service-registration-or-how-to-do-funky-stuff-with-coc-castledynamicproxy-structuremap/">older post of mine</a>.
Since StructureMap&#8217;s convention API has <a href="http://www.bjoernrochel.de/2010/01/05/changes-in-structuremap-254/">changed quite a bit</a>,
here is the updated version of the code used in the post using the new APIs introduced in StructureMap 2.5.4.</p>

<!--more-->


<p>The new code is actually easier. It should look something like this &#8230; .</p>

<figure class='code'><figcaption><span>Singleton registration convention</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'>    <span class="k">public</span> <span class="k">class</span> <span class="nc">ServicesAreSingletonsAndProxies</span> <span class="p">:</span> <span class="n">IRegistrationConvention</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="cp">#region IRegistrationConvention Members</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">public</span> <span class="k">void</span> <span class="nf">Process</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">,</span> <span class="n">Registry</span> <span class="n">registry</span><span class="p">)</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="k">if</span> <span class="p">(!</span><span class="n">type</span><span class="p">.</span><span class="n">IsConcrete</span><span class="p">()</span> <span class="p">||</span> <span class="p">!</span><span class="n">IsService</span><span class="p">(</span><span class="n">type</span><span class="p">)</span> <span class="p">||</span> <span class="p">!</span><span class="n">Constructor</span><span class="p">.</span><span class="n">HasConstructors</span><span class="p">(</span><span class="n">type</span><span class="p">))</span>
</span><span class='line'>            <span class="p">{</span>
</span><span class='line'>                <span class="k">return</span><span class="p">;</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">Type</span> <span class="n">pluginType</span> <span class="p">=</span> <span class="n">FindPluginType</span><span class="p">(</span><span class="n">type</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">if</span> <span class="p">(</span><span class="n">pluginType</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class='line'>            <span class="p">{</span>
</span><span class='line'>                <span class="k">return</span><span class="p">;</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">registry</span>
</span><span class='line'>                <span class="p">.</span><span class="n">For</span><span class="p">(</span><span class="n">pluginType</span><span class="p">)</span>
</span><span class='line'>                <span class="p">.</span><span class="n">Singleton</span><span class="p">()</span>
</span><span class='line'>                <span class="p">.</span><span class="n">Use</span><span class="p">(</span><span class="k">new</span> <span class="n">ConfiguredInstance</span><span class="p">(</span><span class="n">type</span><span class="p">)</span>
</span><span class='line'>                <span class="p">{</span>
</span><span class='line'>                  <span class="n">Interceptor</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DynamicProxyInterceptor</span><span class="p">(</span><span class="n">pluginType</span><span class="p">)</span>
</span><span class='line'>                <span class="p">});</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="cp">#endregion</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">private</span> <span class="k">static</span> <span class="kt">bool</span> <span class="nf">IsService</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">)</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">type</span><span class="p">.</span><span class="n">Name</span><span class="p">.</span><span class="n">EndsWith</span><span class="p">(</span><span class="s">&quot;Service&quot;</span><span class="p">);</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">private</span> <span class="k">static</span> <span class="n">Type</span> <span class="nf">FindPluginType</span><span class="p">(</span><span class="n">Type</span> <span class="n">concreteType</span><span class="p">)</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="kt">string</span> <span class="n">interfaceName</span> <span class="p">=</span> <span class="s">&quot;I&quot;</span> <span class="p">+</span> <span class="n">concreteType</span><span class="p">.</span><span class="n">Name</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">concreteType</span>
</span><span class='line'>                <span class="p">.</span><span class="n">GetInterfaces</span><span class="p">()</span>
</span><span class='line'>                <span class="p">.</span><span class="n">Where</span><span class="p">(</span><span class="n">t</span> <span class="p">=&gt;</span> <span class="kt">string</span><span class="p">.</span><span class="n">Equals</span><span class="p">(</span><span class="n">t</span><span class="p">.</span><span class="n">Name</span><span class="p">,</span> <span class="n">interfaceName</span><span class="p">,</span> <span class="n">StringComparison</span><span class="p">.</span><span class="n">Ordinal</span><span class="p">))</span>
</span><span class='line'>                <span class="p">.</span><span class="n">FirstOrDefault</span><span class="p">();</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Plain Old CLR / C# Object]]></title>
    <link href="http://www.bjoernrochel.de/2010/02/25/plain-old-clr-c-object/"/>
    <updated>2010-02-25T20:51:37+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/02/25/plain-old-clr-c-object</id>
    <content type="html"><![CDATA[<p>Crap, time can go by so fast. On Monday a <a href="http://twitter.com/ralfw/status/9446904971">tweet</a> by <strong>Ralf Westphal</strong> caught
my attention and I felt the need to comment. It started as a series of Twitter replies, but to be honest Twitter isn’t suited or made for those kind of discussions.
So I started to write this post in order to explain why I disagree with Ralf (or at least don’t get the intended message of his tweet).
Yeah a short look into the calendar indicates that I’m a little late, but I thought better late than ditch the post and forget about it.</p>

<h2>What got me baffled</h2>

<p>In his tweet he basically states (my translation from German to English) that</p>

<blockquote><p>If a domain model consists only of POCOs it should be called data model</p><footer><strong>Ralf Westphal</strong> <cite><a href='http://twitter.com/ralfw/status/9446904971'>twitter.com/ralfw/status/&hellip;</a></cite></footer></blockquote>


<p>My first thought was a) does he mean anemic domain models and b) what has POCO to do with that? As I found out he didn’t mean
<a href="http://twitter.com/ralfw/status/9493173442">anemic domain models</a>. So let’s take a look at the POCO aspect.</p>

<!--more-->


<h2>POCO / POJO / PONO / POwhatever</h2>

<p>The term exists in several variations and different programming languages. For the sake of simplicity I’m going to use POCO for the rest of the post since
I’m a .NET guy, but same applies of course to all other versions.
The English <a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object">Wikipedia site</a> defines the term “Plain Old CLR Object” as the
following:</p>

<blockquote><p>The term is used to contrast a simple object with one that is designed to be used with a complicated, special object frameworks such as an ORM component. <br/>Another way to put it is that POCO&#8217;s are objects unencumbered with inheritance or attributes needed for specific frameworks …</p><footer><strong>Wikipedia</strong> <cite><a href='http://en.wikipedia.org/wiki/Plain_Old_CLR_Object'>en.wikipedia.org/wiki/&hellip;</a></cite></footer></blockquote>


<p>To me personally, POCO is just a simple, but very important principle or guideline. POCO for me means,
that you should strive to limit the contact area of your own code and the code of third party frameworks as much as possible.
This includes staying away from third-party frameworks with heavy attribute usage and / or inheritance requirements.
Why should you do this? 2 reasons seem to be important to me:</p>

<ul>
<li><strong>Orthogonality</strong>. Two parts of a system, like features, components, classes, whatever are called orthogonal when changes in one don’t affect the other.
Following a POCO approach in a solution can greatly support orthogonality in my personal experience. It helps you to design and build solutions that are easy to change and very adaptable to new requirements or frameworks
(Ever tried to migrate a Microsoft CAB based solution?). Which leads to the second IMHO very important aspect:</li>
<li><strong>Reversibility</strong>. In the end of the day we’re all human. Sometimes we design the wrong way, sometimes the framework doesn’t work as expected,
sometimes a particular framework isn’t exactly the right one any more when requirements change drastically.
All those things happen. All those things can can come up in any project. POCO can help a lot in those situations,
because it limits the impact of external frameworks or components to your code.</li>
</ul>


<p>POCO mostly comes up in the context of an ORM solution. However, the concept of POCO is not directly bound to persistence or even domain models.
Which leads me back to the entry of the post and Ralfs tweet. What is the main distinction between a domain model and something we might call a data model?
In my opinion this is BEHAVIOR. The term POCO itself has nothing to do with behavior itself (at least from my perspective).
Totally different aspects IMHO. So why should a model consisting of POCOs be called data model?</p>

<p>Am I fighting on lost ground here, missing something or confusing something?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Diving into the StoryTeller trunk, Part 11.3: Commands strike back]]></title>
    <link href="http://www.bjoernrochel.de/2010/02/15/diving-into-the-storyteller-trunk-part-11-3-commands-strike-back/"/>
    <updated>2010-02-15T20:21:20+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/02/15/diving-into-the-storyteller-trunk-part-11-3-commands-strike-back</id>
    <content type="html"><![CDATA[<p>One of the things that can hit you really hard when writing blog posts about open source software (like StoryTeller is),
is the fact that your posts tend to get very fast outdated, especially when you don&#8217;t pay that much attention to the detail (like I did, sigh).
If you&#8217;re not aware of what I&#8217;m talking about, it&#8217;s StoryTellers command story. I&#8217;m not sure when it changed but it definitely has changed and I
needed to update my last post <a href="http://www.bjoernrochel.de/2010/01/09/diving-into-the-storyteller-trunk-part-11-2-more-on-commands/">11.2</a> quite a bit in order
to reflect the changes. Today I would like to conclude my trip through StoryTellers UI infrastructure with a look at how Commands are integrated
into the Screen Activation Lifecycle.</p>

<!--more-->


<p>Some of my older posts on the topic showed that the component responsible for Screen activation and deactivation in StoryTeller is the <code>ScreenConductor</code>.
However, when the <code>ScreenConductor</code> activates or deactivates a Screen, it delegates a major part of work to the so called <code>IShellService</code>.
The only implementer of this interface, the <code>ShellService</code>, is just a little facade around three things.</p>

<ol>
<li>The <code>ICommandbar</code>, which is the main toolbar of StoryTeller,</li>
<li>the <code>IOptionsMenu</code>, which is a kind of Shortcut menu for StoryTellers Commands and</li>
<li>the <code>IScreenObjectRegistry</code>, which acts as a store  / front-end for the current Command registration.</li>
</ol>


<figure class='code'><figcaption><span>The ShellService</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">ShellService</span> <span class="p">:</span> <span class="n">IShellService</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">private</span> <span class="k">readonly</span> <span class="n">ICommandBar</span> <span class="n">_Commands</span><span class="p">;</span>
</span><span class='line'>    <span class="k">private</span> <span class="k">readonly</span> <span class="n">IOptionsMenu</span> <span class="n">_options</span><span class="p">;</span>
</span><span class='line'>    <span class="k">private</span> <span class="k">readonly</span> <span class="n">IScreenObjectRegistry</span> <span class="n">_registry</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="nf">ShellService</span><span class="p">(</span>
</span><span class='line'>          <span class="n">IScreenObjectRegistry</span> <span class="n">registry</span><span class="p">,</span>
</span><span class='line'>          <span class="n">ICommandBar</span> <span class="n">Commands</span><span class="p">,</span>
</span><span class='line'>          <span class="n">IOptionsMenu</span> <span class="n">options</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">_registry</span> <span class="p">=</span> <span class="n">registry</span><span class="p">;</span>
</span><span class='line'>        <span class="n">_Commands</span> <span class="p">=</span> <span class="n">Commands</span><span class="p">;</span>
</span><span class='line'>        <span class="n">_options</span> <span class="p">=</span> <span class="n">options</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="cp">#region IShellService Members</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">void</span> <span class="nf">ActivateScreen</span><span class="p">(</span><span class="n">IScreen</span> <span class="n">screen</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">_registry</span><span class="p">.</span><span class="n">ClearTransient</span><span class="p">();</span>
</span><span class='line'>        <span class="n">screen</span><span class="p">.</span><span class="n">Activate</span><span class="p">(</span><span class="n">_registry</span><span class="p">);</span>
</span><span class='line'>        <span class="n">refill</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">void</span> <span class="nf">ClearTransient</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">_registry</span><span class="p">.</span><span class="n">ClearTransient</span><span class="p">();</span>
</span><span class='line'>        <span class="n">refill</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">void</span> <span class="nf">Start</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">refill</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="cp">#endregion</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">private</span> <span class="k">void</span> <span class="nf">refill</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">_Commands</span><span class="p">.</span><span class="n">Refill</span><span class="p">(</span><span class="n">_registry</span><span class="p">.</span><span class="n">Actions</span><span class="p">);</span>
</span><span class='line'>        <span class="n">_options</span><span class="p">.</span><span class="n">Refill</span><span class="p">(</span><span class="n">_registry</span><span class="p">.</span><span class="n">Actions</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can see some interesting aspects in the short code above.</p>

<ol>
<li>The word transient appears several times. StoryTeller differentiates between two types of Commands:
Permanent Commands and transient Commands. Permanent Commands are displayed, well permanently, while transient Commands are
what I depicted as contextual Commands. They are Commands which should be only visible in a particular context.</li>
<li>Contextualization of Commands is handled on a per Screen basis in StoryTeller. Every time a Screen gets activated or
deactivated the <code>ICommandBar</code> and the <code>IOptionsMenu</code> get reset and completely rebuild. With this you can have a very different Command UI
depending on which Screen is activated.</li>
<li>The actual Command configuration in the Screen Activation Lifecycle is completely delegated to the active Screen. In his <code>Activate()</code> method he
receives a reference to the <code>IScreenObjectRegistry</code> which can be used in order to start the Command configuration via a small fluent API.</li>
</ol>


<figure class='code'><figcaption><span>IScreenObjectRegistry </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">IScreenObjectRegistry</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="c1">//Gets a collection of all currently known command configurations  </span>
</span><span class='line'>    <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">ScreenAction</span><span class="p">&gt;</span> <span class="n">Actions</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//Removes all transient command configurations from the registry</span>
</span><span class='line'>    <span class="k">void</span> <span class="nf">ClearTransient</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//DSL starting point for the configuration of transient Commands</span>
</span><span class='line'>    <span class="n">IActionExpression</span> <span class="nf">Action</span><span class="p">(</span><span class="kt">string</span> <span class="n">name</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//DSL starting point for the configuration of permanent Commands</span>
</span><span class='line'>    <span class="n">IActionExpression</span> <span class="nf">PermanentAction</span><span class="p">(</span><span class="kt">string</span> <span class="n">name</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The following code snippet shows an example of how this API could be leveraged inside a Screen.</p>

<figure class='code'><figcaption><span>Inside a screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'>    <span class="k">public</span> <span class="k">void</span> <span class="nf">Activate</span><span class="p">(</span><span class="n">IScreenObjectRegistry</span> <span class="n">screenObjects</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">screenObjects</span>
</span><span class='line'>            <span class="p">.</span><span class="n">Action</span><span class="p">(</span><span class="s">&quot;Save&quot;</span><span class="p">)</span>
</span><span class='line'>            <span class="p">.</span><span class="n">Bind</span><span class="p">(</span><span class="n">ModifierKeys</span><span class="p">.</span><span class="n">Control</span><span class="p">,</span> <span class="n">Key</span><span class="p">.</span><span class="n">S</span><span class="p">)</span>
</span><span class='line'>             <span class="p">.</span><span class="n">To</span><span class="p">(</span><span class="n">_save</span><span class="p">);</span> <span class="c1">//This can be either Systen.Action or an System.Windows.Input.ICommand</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">screenObjects</span>
</span><span class='line'>            <span class="p">.</span><span class="n">Action</span><span class="p">(</span><span class="s">&quot;Cancel&quot;</span><span class="p">)</span>
</span><span class='line'>            <span class="p">.</span><span class="n">Bind</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">Escape</span><span class="p">)</span>
</span><span class='line'>            <span class="p">.</span><span class="n">To</span><span class="p">(</span><span class="n">_cancel</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Gabriel Schenker has <a href="http://www.lostechies.com/blogs/gabrielschenker/archive/2010/01/08/fluent-silverlight-table-of-content.aspx">written an excellent series on how to write such a fluent API</a>.
Although it&#8217;s targeting Silverlight, most of the involved problems are explained in detail there, so forgive me if I don&#8217;t dive into the actual DSL implementation.</p>

<h2>Some final thoughts</h2>

<p>Making the Screen responsible for setting up his Commands makes a lot of sense to me, since the Screen is the unit which gets plugged into the UI infrastructure
and it also very likely plays the role of the Command receiver in terms of the classic GoF pattern description.
This doesn&#8217;t necessary mean that Screens are the only place for Command configuration.
The initialization of modules in a Composite application is also a very likely place for registration of permanent Commands.</p>

<p>I consider having a fluent API for configuring the Commands also a plus, because it IMHO makes the actual Command configuration a lot easier and accessible.
I&#8217;ve used the same setup (fluent API + delegation to screen) on my last 3 projects and it always worked for me like a charm.</p>

<p>Like I mentioned in the previous post, what I don&#8217;t like that much is the idea of mixing in visual aspects (Icon, Size, Location) into the Command configuration,
mostly because I&#8217;ve been burned by this in the past when facing complex menus, like the ribbon.
I think it&#8217;s a good idea to externalize the visual aspect via XML, at least for all the static stuff.</p>

<h2>This is it</h2>

<p>This was the last post about StoryTeller (at least for a while). It has been an interesting voyage which taught me a lot about UI infrastructure design,
StructureMap usage and Convention over Configuration. Although it was primarily my learning excercise I hope you took something interesting with you
from this blog series, too.</p>

<p>I&#8217;m going to continue my research on UI architecture with another deep dive into <a href="http://devlicio.us/blogs/rob_eisenberg/default.aspx">Rob Eisenbergs</a> <a href="http://www.codeplex.com/caliburn">Caliburn</a> soon.
If your interested I would be very happy to have you with me on that trip &#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to: Integrate a Topshelf based service with VS Setup projects]]></title>
    <link href="http://www.bjoernrochel.de/2010/01/09/how-to-integrate-a-topshelf-based-service-with-vs-setup-projects/"/>
    <updated>2010-01-09T20:40:28+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/01/09/how-to-integrate-a-topshelf-based-service-with-vs-setup-projects</id>
    <content type="html"><![CDATA[<p>We’ve recently started to migrate all of our Windows Services from a classic ServiceBase based approach to the hosting framework Topshelf.</p>

<p>Previously we used the standard ServiceInstaller / ServiceProcessInstaller tandem to integrate our services with MSI deployment.
This does not work with Topshelf (since Topshelf does the service installation itself via the Registry).
However it’s pretty easy to write a custom installer for that. You can do something like this:</p>

<figure class='code'><figcaption><span>An installer for Topshelf</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">TopshelfInstaller</span> <span class="p">:</span> <span class="n">Installer</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">private</span> <span class="k">const</span> <span class="kt">string</span> <span class="n">AssemblyIdentifier</span> <span class="p">=</span> <span class="s">&quot;TopshelfAssembly&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="k">private</span> <span class="k">const</span> <span class="kt">string</span> <span class="n">InstallUtilAssemblyParameter</span> <span class="p">=</span> <span class="s">&quot;assemblypath&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Install</span><span class="p">(</span><span class="n">IDictionary</span> <span class="n">stateSaver</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">topshelfAssembly</span> <span class="p">=</span> <span class="n">Context</span><span class="p">.</span><span class="n">Parameters</span><span class="p">[</span><span class="n">InstallUtilAssemblyParameter</span><span class="p">];</span>
</span><span class='line'>        <span class="n">stateSaver</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">AssemblyIdentifier</span><span class="p">,</span> <span class="n">topshelfAssembly</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">RunHidden</span><span class="p">(</span><span class="n">topshelfAssembly</span><span class="p">,</span> <span class="s">&quot;/install&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">base</span><span class="p">.</span><span class="n">Install</span><span class="p">(</span><span class="n">stateSaver</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Uninstall</span><span class="p">(</span><span class="n">IDictionary</span> <span class="n">savedState</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">topshelfAssembly</span> <span class="p">=</span> <span class="n">savedState</span><span class="p">[</span><span class="n">AssemblyIdentifier</span><span class="p">].</span><span class="n">ToString</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">RunHidden</span><span class="p">(</span><span class="n">topshelfAssembly</span><span class="p">,</span> <span class="s">&quot;/uninstall&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">base</span><span class="p">.</span><span class="n">Uninstall</span><span class="p">(</span><span class="n">savedState</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">private</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">RunHidden</span><span class="p">(</span><span class="kt">string</span> <span class="n">primaryOutputAssembly</span><span class="p">,</span> <span class="kt">string</span> <span class="n">arguments</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">startInfo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProcessStartInfo</span><span class="p">(</span><span class="n">primaryOutputAssembly</span><span class="p">)</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">WindowStyle</span> <span class="p">=</span> <span class="n">ProcessWindowStyle</span><span class="p">.</span><span class="n">Hidden</span><span class="p">,</span>
</span><span class='line'>            <span class="n">Arguments</span> <span class="p">=</span> <span class="n">arguments</span>
</span><span class='line'>        <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">using</span> <span class="p">(</span><span class="n">var</span> <span class="n">process</span> <span class="p">=</span> <span class="n">Process</span><span class="p">.</span><span class="n">Start</span><span class="p">(</span><span class="n">startInfo</span><span class="p">))</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">process</span><span class="p">.</span><span class="n">WaitForExit</span><span class="p">();</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The interesting part is this line:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">var</span> <span class="n">topshelfAssembly</span> <span class="p">=</span> <span class="n">Context</span><span class="p">.</span><span class="n">Parameters</span><span class="p">[</span><span class="n">InstallUtilAssemblyParameter</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>Took me some time to find this. During installation the Parameter Dictionary attached to the Context
contains the full target filename of the assembly being installed (key is <code>assemblypath</code>).
With this path you can directly launch the <code>/install</code> or <code>/uninstall</code> command for the Topshelf based exe.</p>

<p>HTH</p>

<p>P.S.: <a href="http://devcity.net/Articles/339/3/article.aspx">This</a> resource pointed me in the right direction.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Diving into the StoryTeller trunk, Part 11.2: More on Commands]]></title>
    <link href="http://www.bjoernrochel.de/2010/01/09/diving-into-the-storyteller-trunk-part-11-2-more-on-commands/"/>
    <updated>2010-01-09T20:38:35+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/01/09/diving-into-the-storyteller-trunk-part-11-2-more-on-commands</id>
    <content type="html"><![CDATA[<p>Let’s take a look at some of the questions I left unanswered in the last post.</p>

<h2>Is the basic GoF Command pattern sufficient for a modern composite application?</h2>

<p>The basic GoF Command pattern has no notion of visual state of a <code>Command</code>, such as <code>(Is)Enabled</code> or <code>(Is)Visible</code>.
Its original purpose was to encapsulate an action, so that it can be passed around and executed at some later point of time. Not more, not less.</p>

<figure class='code'><figcaption><span>The basic Command interface</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">ICommand</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">Execute</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Obviously real world desktop apps need something a bit more sophisticated. I’ve seen several infrastructures in my (not so old)
career so far (home grown as well as OS alternatives), which extended this basic idea with at least one of those properties mentioned above.
Take for instance the P&amp;P Composite UI Application Block (now better known as part of the Smart Client Software Factory). CAB implements a
delegate based variation on the Command pattern. The delegate represents the action which is passed around. However, this delegate is managed by
the Command class which has a notion of Status.</p>

<figure class='code'><figcaption><span>CommandStatus in CAB</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">enum</span> <span class="n">CommandStatus</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">Enabled</span><span class="p">,</span> <span class="c1">//visible and enabled</span>
</span><span class='line'>  <span class="n">Disabled</span><span class="p">,</span> <span class="c1">//visible and disabled </span>
</span><span class='line'>  <span class="n">Unavailable</span> <span class="c1">//invisible </span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The WPF Command infrastructure version of the Command interface is more like the original pattern and adds the <code>Enabled</code> Property and an <code>EnabledChangedEvent</code> to the interface definition.</p>

<figure class='code'><figcaption><span>WPFs ICommand interface</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">ICommand</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">Execute</span><span class="p">();</span>
</span><span class='line'>  <span class="kt">bool</span> <span class="n">Enabled</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">event</span> <span class="n">EventHandler</span> <span class="n">EnabledChanged</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>To be honest, the <code>Command</code> interface <strong>never looked like the original GoF definition in
ANY APPLICATION or project I’ve worked on so far</strong>. It always had a slight modification in one or another way.</p>

<h2>StoryTeller’s Command interface</h2>

<p>StoryTeller is a WPF based application, so naturally it gets the WPF Command infrastructure out of the box. However it composes the
WPF <code>ICommand</code> into a StoryTeller specific structure, the <code>IScreenAction</code>.</p>

<figure class='code'><figcaption><span>The ScreenAction abstraction</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">IScreenAction</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="kt">bool</span> <span class="n">IsPermanent</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="n">InputBinding</span> <span class="n">Binding</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="n">Icon</span> <span class="n">Icon</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="n">ICommand</span> <span class="n">Command</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="kt">bool</span> <span class="n">ShortcutOnly</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">BuildButton</span><span class="p">(</span><span class="n">ICommandBar</span> <span class="n">bar</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>IScreenAction</code> extends the capabilities of the original GoF-Pattern with a lot of metadata, mostly for visual aspects (Icon, Description).
If you’re wondering why he included visual aspects: That basically tries to solve a reoccurring problem in composite apps: In
composite applications modules are not known at compile time to the infrastructure. Neither are all their capabilities and how they might be
displayed in the infrastructure shell. Because of that, the infrastructure needs a dynamic, deferred way for doing the shells visual
configuration at application startup. One way to implement this is to delegate the responsibility for setting this up to the modules itself.
This can be done during the module load time or every time a screen is displayed. This fits very well with the idea of the Open Closed
Principle, since adding new modules/screens doesn’t require any reconfiguration/recompilation of other modules or the infrastructure.
This is more or less the approach that StoryTeller takes.</p>

<h2>Some personal thoughts on IScreenAction</h2>

<p>I’ve worked on three applications in the past which followed down the same road. One thing I noticed
throughout those three applications is that this approach isn’t really well suited when you’ve got strict and/or complex requirements about how
the UI of an application should look. Let me clarify a bit what I mean:</p>

<ul>
<li><strong>The Ordering Problem</strong>. Even if you organize tools representing commands in a simple toolbar (as StoryTeller does) you can very
easily get into situations where the product owner wants to have the tools in a very specific order which is different to module load
order, some internal event order, whatever. I’ve encountered this several times now. First time we solved this by introducing a global
constant class containing tool names. Very, very bad idea, do not repeat this. This introduces a kind of hidden temporal coupling,
because now modules must be loaded in a particular order (so that a tool already exists to which we can refer by name). StoryTellers
take on this is a bit better (but IMHO not much). The Icon class has an Integer based Order property. All tools get sorted based on this
property in StoryTellers CommandBar when it’s reloaded. This is less coupled, because it eliminates the temporal aspect of the coupling,
but still has coupling.</li>
<li><strong>API bloat with visual aspects</strong>. One area where I really started to find this approach annoying is when you stop having simple
toolbars and start to use more complex menu types like for instance the Ribbon. Taking the ribbon as an example: Now don’t have simply
an ordering problem, but at minimum an icon problem (Normal icon vs.  Quick Access Toolbar), a size problem (Displayed large or small) and
a positioning problem (/Tab/Group/ElementGroup vs.  /ApplicationMenu/Left). We added all those stuff to our Command
registration and guess what, we weren’t happy with that. We created a monster API actually doing very little.</li>
</ul>


<p>So what do I (currently) prefer? Our current project (also using the Ribbon) completely strips the visual aspect of the Command. Our Command
API looks very much like the WPF one, with the only addition of an Id property. The whole visual aspect is configured using an XML file which
is loaded at application startup.</p>

<figure class='code'><figcaption><span>Using xml to wire up the ribbon</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="p">&lt;</span><span class="n">Ribbon</span><span class="p">&gt;</span>
</span><span class='line'>   <span class="p">&lt;</span><span class="n">Tab</span><span class="p">&gt;</span>
</span><span class='line'>      <span class="p">&lt;</span><span class="n">Group</span><span class="p">&gt;</span>
</span><span class='line'>         <span class="p">&lt;</span><span class="n">Button</span> <span class="n">imageId</span><span class="p">=</span><span class="err">”</span><span class="n">CancelIcon</span><span class="err">“</span> <span class="n">commandId</span><span class="p">=</span><span class="err">”</span><span class="n">CommandXYZ</span><span class="err">”</span> <span class="p">/&gt;</span>
</span><span class='line'>      <span class="p">&lt;/</span><span class="n">Group</span><span class="p">&gt;</span>
</span><span class='line'>   <span class="p">&lt;/</span><span class="n">Tab</span><span class="p">&gt;</span>
</span><span class='line'><span class="p">&lt;/</span><span class="n">Ribbon</span><span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>I think you get the point. It works very well for our scope. (Slight warning though: This solution might not be the best in
case you need to represent menu state based on dynamically loaded data).</p>

<p>See you next time for: Commands strike back ;-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[StructureMap: Registry usage]]></title>
    <link href="http://www.bjoernrochel.de/2010/01/06/structuremap-registry-usage/"/>
    <updated>2010-01-06T00:37:22+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/01/06/structuremap-registry-usage</id>
    <content type="html"><![CDATA[<p>I recently read something like this on Twitter:</p>

<blockquote><p>It feels wrong to have registration and scanning in the Registry class</p></blockquote>


<p><strong>I absolutely second that</strong>.
One of the decisions in my current project (a composite smart client) was to
separate these two things, in order to give clear guidance on &#8220;which to use when&#8221;.
Here is our setup:</p>

<figure class='code'><figcaption><span>Bootstrapping our container</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">ObjectFactory</span><span class="p">.</span><span class="n">Initialize</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">x</span><span class="p">.</span><span class="n">AddRegistry</span><span class="p">(</span><span class="k">new</span> <span class="n">InfrastructureRegistry</span><span class="p">());</span>
</span><span class='line'>  <span class="n">x</span><span class="p">.</span><span class="n">Scan</span><span class="p">(</span><span class="n">scanner</span> <span class="p">=&gt;</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>          <span class="n">scanner</span><span class="p">.</span><span class="n">AssembliesFromPath</span><span class="p">(</span><span class="s">&quot;Modules&quot;</span><span class="p">);</span>
</span><span class='line'>          <span class="n">scanner</span><span class="p">.</span><span class="n">Convention</span><span class="p">&lt;</span><span class="n">ProjectConventions</span><span class="p">&gt;();</span>
</span><span class='line'>          <span class="n">scanner</span><span class="p">.</span><span class="n">LookForRegistries</span><span class="p">();</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<ul>
<li>We packaged our conventions for the project into a single composite convention. This includes the easier mappings ala <code>IFoo</code> &#8211; <code>Foo</code>, as
well as more complex conventions for services which are automatically instrumented on creation via <code>Castle.DynamicProxy</code>.</li>
<li><code>Registry</code> classes are used for all the stuff we&#8217;re not able to configure via conventions.<br/>
  This includes mappings from interfaces to types with completely different names, adding externally created stuff to the container or configuring complex constructions like a composite.</li>
<li>There is exactly 1 <code>Registry</code> per module in our application (+ 1 for the infrastructure).</li>
<li><code>Registry</code> classes are dynamically found during the scan process by looking into each assembly configured in the Scanner.</li>
</ul>


<p>Any opinions? If you&#8217;re using Registries, how do you use them?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Changes in StructureMap 2.5.4]]></title>
    <link href="http://www.bjoernrochel.de/2010/01/05/changes-in-structuremap-254/"/>
    <updated>2010-01-05T23:45:14+01:00</updated>
    <id>http://www.bjoernrochel.de/2010/01/05/changes-in-structuremap-254</id>
    <content type="html"><![CDATA[<p>Interesting what some people do during the Christmas holidays. In the case of Jeremy D. Miller this was releasing a new version of
StructureMap and working heavily on the Fubu MVC codebase. Today I had the pleasure to migrate my current projects codebase (which previously
used Unity and Unity.Interception) to the newest StructureMap version 2.5.4 and Castle.DynamicProxy2.</p>

<p>It was an interesting experience mostly because I tried to re-use some code pieces for setting up conventions
from an earlier project based on StructureMap 2.5.3. Quite a few things have been changed in the API. Needless to say, all for good. I just
wanted to give a quick overview of the things I noticed.</p>

<h2>ITypeScanner was replaced by IRegistrationConvention</h2>

<p>If you wanted to write custom conventions in StructureMap 2.5.3 you needed to implement the <code>ITypeScanner</code> interface
(An example is described <a href="http://www.bjoernrochel.de/2009/07/24/cutting-the-fluff-from-service-registration-or-how-to-do-funky-stuff-with-coc-castledynamicproxy-structuremap/">here</a>).</p>

<figure class='code'><figcaption><span>ITypeScanner was replaced by IRegistrationConvention</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="c1">//StructureMap 2.5.3 </span>
</span><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">ITypeScanner</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">Process</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">,</span> <span class="n">PluginGraph</span> <span class="n">graph</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>  
</span><span class='line'><span class="c1">//StructureMap 2.5.4 </span>
</span><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">IRegistrationConvention</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">Process</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">,</span> <span class="n">Registry</span> <span class="n">registry</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, it&#8217;s not only a renaming. The signature of the <code>Process</code>-method has changed, too. It now uses the good old <code>Registry</code> class for doing the
registration. Very consistent, good choice. Modifying the <code>PluginGraph</code> always left me with the feeling that I was digging too deep into the StuctureMap internals.</p>

<h2>TypeRules base class has been replaced with Extension methods</h2>

<p>In the past you could re-use some utility methods for reflection (like checking whether a type is concrete, etc.) by
inheriting from the <code>TypeRules</code> class. This class has been completely removed, but the functionality is still available via Extension methods.</p>

<h2>Lot&#8217;s and lot&#8217;s of renaming </h2>

<p>A lot of renaming has been done. The most interesting stuff happened in the <code>Registry</code> class.</p>

<figure class='code'><figcaption><span>Renamings in the fluent api</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="c1">//StructureMap 2.5.3</span>
</span><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">MyRegistry</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="nf">MyRegistry</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">ForRequestedType</span><span class="p">&lt;</span><span class="n">IFoo</span><span class="p">&gt;().</span><span class="n">TheDefaultIsConcreteType</span><span class="p">&lt;</span><span class="n">Foo</span><span class="p">&gt;();</span>
</span><span class='line'>        <span class="n">ForRequestedType</span><span class="p">&lt;</span><span class="n">IBar</span><span class="p">&gt;().</span><span class="n">TheDefaultIsConcreteType</span><span class="p">&lt;</span><span class="n">Bar</span><span class="p">&gt;().</span><span class="n">AsSingleton</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">ForRequestedType</span><span class="p">&lt;</span><span class="n">IFoo</span><span class="p">&gt;().</span><span class="n">TheDefault</span><span class="p">.</span><span class="n">IsThis</span><span class="p">(</span><span class="k">new</span> <span class="n">Foo</span><span class="p">)</span>
</span><span class='line'>        <span class="n">ForRequestedType</span><span class="p">&lt;</span><span class="n">IBar</span><span class="p">&gt;().</span><span class="n">TheDefault</span><span class="p">.</span><span class="n">Is</span><span class="p">.</span><span class="n">ConstructedBy</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">Bar</span><span class="p">());</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//StructureMap 2.5.4</span>
</span><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">MyRegistry</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="nf">MyRegistry</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="n">For</span><span class="p">&lt;</span><span class="n">IFoo</span><span class="p">&gt;().</span><span class="n">Use</span><span class="p">&lt;</span><span class="n">Foo</span><span class="p">&gt;();</span>
</span><span class='line'>        <span class="n">ForSingleton</span><span class="p">&lt;</span><span class="n">IBar</span><span class="p">&gt;().</span><span class="n">Use</span><span class="p">&lt;</span><span class="n">Bar</span><span class="p">&gt;();</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">For</span><span class="p">&lt;</span><span class="n">IFoo</span><span class="p">&gt;().</span><span class="n">Use</span><span class="p">(</span><span class="k">new</span> <span class="n">Foo</span><span class="p">)</span>
</span><span class='line'>        <span class="n">For</span><span class="p">&lt;</span><span class="n">IBar</span><span class="p">&gt;().</span><span class="n">Use</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">Bar</span><span class="p">());</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The new version is a lot less wordier and a lot more consistent in naming. It also feels very familiar, although
I&#8217;m unable to determine why.</p>

<h2>Some new slick features I noticed</h2>

<p>From playing around with the StructureMap trunk version during the &#8220;Diving into the StoryTeller Series&#8221; I already stumbled over this killer
feature.</p>

<figure class='code'><figcaption><span>Convention based registration</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">_container</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Container</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="n">x</span><span class="p">.</span><span class="n">Scan</span><span class="p">(</span><span class="n">s</span> <span class="p">=&gt;</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>   <span class="n">s</span><span class="p">.</span><span class="n">AssemblyContainingType</span><span class="p">&lt;</span><span class="n">ISomeService</span><span class="p">&gt;();</span>
</span><span class='line'>   <span class="n">s</span><span class="p">.</span><span class="n">ConnectImplementationsToTypesClosing</span><span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="n">IHandler</span><span class="p">&lt;&gt;));</span>
</span><span class='line'><span class="p">}));</span>
</span></code></pre></td></tr></table></div></figure>


<p>Jimmy Bogard has already written an <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/12/17/advanced-structuremap-connecting-implementations-to-open-generic-types.aspx">excellent article</a>
about that particular feature. So instead let us take a look at this new ability.</p>

<figure class='code'><figcaption><span>Accessing the container model</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">ObjectFactory</span><span class="p">.</span><span class="n">Model</span><span class="p">.</span><span class="n">GetAllPossible</span><span class="p">&lt;</span><span class="n">IInitializable</span><span class="p">&gt;();</span>
</span></code></pre></td></tr></table></div></figure>


<p>This resolves all instances from the configuration model which implement the <code>IInitializable</code> interface.</p>

<h2>Stuff you don&#8217;t see</h2>

<p>If you&#8217;ve followed Jeremy D. Miller closely on Twitter you may have heard that the biggest
change in the new release has happened under the hood of StructureMap.  In previous versions StructureMap used Reflection.Emit to emit an
assembly for the construction of the types at runtime. This has been completely rewritten using <code>ExpressionTrees</code>. I&#8217;m quite curious how he
implemented that.</p>

<p>Feels like yet another source to learn has emerged ;-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Diving into the StoryTeller trunk, Part 11.1: Commands]]></title>
    <link href="http://www.bjoernrochel.de/2009/12/23/diving-into-the-storyteller-trunk-part-111-commands/"/>
    <updated>2009-12-23T20:16:13+01:00</updated>
    <id>http://www.bjoernrochel.de/2009/12/23/diving-into-the-storyteller-trunk-part-111-commands</id>
    <content type="html"><![CDATA[<p>Welcome back to the <strong>Diving into the StoryTeller trunk</strong> series. The main
topic for the last couple of posts about StoryTeller is its Command handling or to be a bit more specific the reoccurring problem of how
Screen related Commands are managed in the app infrastructure. Couple of posts? Yeah, right. Today&#8217;s post is going to be a bit shorter than the
usual posts in the series. When I started to write this post I quickly realized that this topic contains more aspects to talk about than I had
originally anticipated. Besides that Christmas is near, I&#8217;m running out of time for this year and just wanted to get at least some bits of the
content out there before going on vacation. Today&#8217;s post is going to be more general one on the topic.</p>

<h2>The Command Pattern </h2>

<p>A lot of the content in this series dealt exclusively with how the StoryTeller UI layer manages Screens in its content area,
but as you know most of the time an application consists of more parts than just the plain content area. A typical desktop application is probably going to have some sort
of Mainmenu, a Statusbar and of course Contextmenus. Items displayed in those areas very often represent actions an application can perform.
They act as a trigger of those actions. A typical way to implement this is using the <a href="http://en.wikipedia.org/wiki/Command_pattern">GoF-Command Pattern</a>, which separates
the invoker of an action (for instance a button) from the receiver instance which executes the action by introducing the Command abstraction. A Command encapsulates the knowledge needed for an
invokation of the receiver instance, so that it can be executed at a later time.</p>

<h2>Commands are potentially contextual </h2>

<p>An application can have lots and lots of Commands. While some of these Commands are available all of the time, some of them can only be executed in a particular context.
A typical example for Commands of the first category might be the &#8220;Exit application&#8221; Command. A typical example for the latter category might be the &#8220;Undo&#8221; Command or &#8220;Redo&#8221; Command in all
kinds of text editors, which can only be executed when the currently viewed document has some changes. It&#8217;s not uncommon to have lots of contextual Commands in an application that are only related to a very
specific Screen or Screen state. When we think in terms of usability, the least a user should be able to take granted from an app is that the
app appropriately shows which actions can be performed at a particular point of time. This can be achieved by enabling / disabling related
items depending on the availability of the Command (be it manually or through databinding). Sometimes though,it might be a better approach to
have an even more contextualized UI that only shows the commands related to the current context. So for instance if no code editor view is shown
in Visual Studio than the &#8220;Undo&#8221; and &#8220;Redo&#8221; Commands should also not be visible (NOTE: VS doesn&#8217;t actually behave that way). I think this idea
of contextualized UIs becomes more and more popular. Office 2007/2010 was build with this idea in mind.</p>

<h2>Commands in a Composite UI</h2>

<p>Composite UIs which follow the Open / Closed Principle add another problem to the mix. When new modules or new screens are loaded into a
composite app, they shouldn&#8217;t need to modify the app infrastructure in order to add their related commands. The app infrastructure needs to
provide a way to plug those commands in without modification. This also includes their visual representation (Text, Tooltip, Icon, etc.).</p>

<h2>Questions we should take a closer look at</h2>

<ul>
<li>Do we need to differentiate between types of Commands?</li>
<li>Who is responsible for adding / registering commands?</li>
<li>How is the visual representation of commands configured?</li>
<li>Who is responsible for deciding whether Commands are available?</li>
<li>Is there build-in .NET Framework support for this?</li>
<li><strong>Of course: How does StoryTeller implement all this?</strong></li>
</ul>


<p>I&#8217;m afraid that&#8217;s all for today. I wish you all merry Christmas and a
happy new year. See you in 2010</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[I'm doing the T-thing now, too]]></title>
    <link href="http://www.bjoernrochel.de/2009/12/10/im-doing-the-t-thing-now-too/"/>
    <updated>2009-12-10T19:33:32+01:00</updated>
    <id>http://www.bjoernrochel.de/2009/12/10/im-doing-the-t-thing-now-too</id>
    <content type="html"><![CDATA[<p>Although I&#8217;m still not 100% sure that I &#8220;get&#8221; what the Twitter fuzz is all about, I decided to give it a try and use it for a while, in order
to form my opinion about it. If you&#8217;re interested to stay in touch via Twitter, here&#8217;s my Url:</p>

<p><a href="http://www.twitter.com/BjoernRochel">http://www.twitter.com/BjoernRochel</a></p>

<p>Cheerz Björn</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What's next?]]></title>
    <link href="http://www.bjoernrochel.de/2009/11/30/whats-next/"/>
    <updated>2009-11-30T10:56:11+01:00</updated>
    <id>http://www.bjoernrochel.de/2009/11/30/whats-next</id>
    <content type="html"><![CDATA[<p>With the &#8220;Diving into the StoryTeller trunk series&#8221; coming to an end in the near future, I&#8217;m currently looking into several ideas for future
posts. Here&#8217;s what I&#8217;m currently thinking about:</p>

<ol>
<li><p><strong>xUnit.BDDExtensions feature walkthrough and documentation</strong> Let&#8217;s be honest it&#8217;s currently not documented at all. I failed big time at
documenting its usage scenarios and behavior so far. However, since more and more projects at my current client are starting to use it I&#8217;m
definitely going to fix this &#8230;</p></li>
<li><p><strong>Look into various options to implement the Active Object Pattern</strong> I&#8217;ve been reading several papers
about infrastructures for command execution lately. It&#8217;s an interesting topic for distributed scenarios (for instance used in a CQSR design),
but also for local scenarios when you start to think about multi core / concurrent programming. As modern applications are becoming more and
more connected on the one hand and need to use local resources more efficiently on the other (because processors won&#8217;t get a lot faster in
the near future) I think there&#8217;s a huge need for having a simple consistent design for both. Things I&#8217;d like to look into are the
Concurrency Coordination Runtime, the .NET 4.0 Task Parallel Library and Application Spaces, as well as more specific topics like integrating
such an infrastructure with an MVVM design.</p></li>
<li><p><strong>Dive into the Caliburn trunk</strong> Last but not least I would love to spend some time with Caliburn
since WPF is more and more on my radar and Caliburn seems to fully embrace and extend a lot of the original WPF design ideas in a composite
context. I think I would do something similar to the StoryTeller series.</p></li>
</ol>


<p>So, what do you think? Any preferences? Any priorities?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Diving into the StoryTeller trunk, Part 10: Coordination Baby!]]></title>
    <link href="http://www.bjoernrochel.de/2009/11/30/diving-into-the-storyteller-trunk-part-10-coordination-baby/"/>
    <updated>2009-11-30T10:13:22+01:00</updated>
    <id>http://www.bjoernrochel.de/2009/11/30/diving-into-the-storyteller-trunk-part-10-coordination-baby</id>
    <content type="html"><![CDATA[<p>So far in this series I&#8217;ve talked extensively about what I consider the
most of the important parts in the StoryTeller UI design. This includes
<a href="http://www.bjoernrochel.de/2009/08/14/diving-into-the-storyteller-trunk-part-7-screens/">Screens</a>,
<a href="http://www.bjoernrochel.de/2009/08/21/diving-into-the-storyteller-trunk-part-8-the-screencollection/">the ScreenCollection</a>,
<a href="http://www.bjoernrochel.de/2009/09/07/diving-into-the-storyteller-trunk-part-9-screensubject-screenfactory/">ScreenSubject</a>,
<a href="http://www.bjoernrochel.de/2009/09/07/diving-into-the-storyteller-trunk-part-9-screensubject-screenfactory/">ScreenFactory</a>,
<a href="http://www.bjoernrochel.de/2009/07/20/diving-into-the-storyteller-trunk-part-5-the-eventaggregator/">EventAggregation</a>
and the application of <a href="http://www.bjoernrochel.de/2009/07/13/diving-into-the-storyteller-trunk-part1-convention-based-registration/">Convention of Configuration</a>
in general. You could say that we mostly talked about ingredients. Today
I would like to take some additional time in order to show how this is
all assembled into an actual API. Those of you who&#8217;ve already spend
some time with the StoryTeller codebase too or have watched one of the
many screencasts about the <a href="http://www.jeremydmiller.com/ppatterns/Default.aspx?Page=ScreenActivationLifecycle&amp;AspxAutoDetectCookieSupport=1">Screen Activation Lifecycle</a>
probably know what I&#8217;m going to show today. Today is all about the
<code>ScreenConductor</code>.</p>

<h2>ScreenConductor vs. Application Controller</h2>

<p>Before we dive into the actual code, let&#8217;s take a short break and talk a
little bit about a more high level view on the <code>ScreenConductor</code> and the
role the conductor is fulfilling in an application. Martin Fowler
identified the pattern <code>ApplicationController</code> some years ago as</p>

<blockquote><p>A centralized point for handling screen navigation and the flow of an<br/>application.</p><footer><strong>Martin Fowler</strong> <cite><a href='http://martinfowler.com/eaaCatalog/applicationController.html'>martinfowler.com/eaaCatalog/&hellip;</a></cite></footer></blockquote>


<p>It&#8217;s kind of a coordination structure which manages / coordinates the
lifecylcle of child screens in an application as well as the lifecycle
of the application shell itself (think about controlled shutdown for
instance). You can think of the <code>ApplicationController</code> as a <em>Facade</em> client
code can call in order to create/activate/deactivate screens in an
application. This <em>Facade</em> defines a nice separation between application
code on the one side and the UI infrastructure on the other side. It
shields away implementation details of how the application is actually
displaying screens (tab style, web style, etc) from the client and also
provides a nice point for handling scenarios like dirty checks on
screens. As an application grows over time, it&#8217;s a good idea to break
down the <code>ApplicationController</code> into several collaborating classes and
decouple its implementation details from another in order to make the
system more manageable and maintainable. Adding a new screen to the
application for instance should not require a change in one of the UI
infrastructure classes. When breaking down the <code>ApplicationController</code>
into several collaborating classes a good lead is to use the <em>Single
Responsibility Principle</em> in order to identify responsibilities which can
be extracted.</p>

<p>That&#8217;s exactly what Jeremy D. Miller did in his <a href="http://www.jeremydmiller.com/ppatterns/Default.aspx?Page=ScreenActivationLifecycle&amp;AspxAutoDetectCookieSupport=1">Screen Activation Lifecycle</a>.
He broke down the ApplicationController pattern into several smaller
responsibilities, the most important beeing</p>

<ul>
<li>the <code>ScreenCollection</code> (which keeps track of all existing screens and the one beeing the active screen)</li>
<li>the <code>ScreenSubject</code> (which is used to separate identification and creation of a screen from the screen itself)</li>
<li><code>Screens</code> (which provide the content beeing displayed and hooks for instance for the dirty check when the application closes)</li>
<li>and the <code>ScreenConductor</code>.</li>
</ul>


<p>Jeremy describes the <code>ScreenConductor</code> and its responsibilities as the
following:</p>

<p><em>&#8220;Controls the activation and deactivation lifecycle of the
screens within the application. Depending on the application, the
conductor may be synchronizing the menu state of the shell, attaching
views in the main panel or otherwise, and calling hook methods on the
Presenter&#8217;s to bootstrap the screen. It may also be just as important to
deactivate a screen when it&#8217;s made the inactive tab to stop timers. My
first exposure to a ScreenConductor was an insurance application that
was built with web style navigation. Anytime the user moved away from a
screen we needed to check for &#8220;dirty&#8221; screens to give the user a chance
to deal with unsaved work. On the other hand, we also had to check the
entry into a requested screen to see if we could really open the screen
based on pessimistic locking or permission rules. We pulled our a Layer
SuperType for our Presenters for methods like CanLeave() and CanEnter().
The ScreenConductor would use these methods and others to manage screen
navigation.&#8221;</em></p>

<p>To me the ScreenConductor is more or less an OCP-fied
subset of the original <code>ApplicationController</code> pattern, focussing on the
coordination and facade ideas of the original pattern.</p>

<h2>A short episode in the Screen Activation Lifecycle </h2>

<p>Let&#8217;s give our discussion a bit more detail. So far it was rather abstract. Before looking into the
actual code I would like to walk you through a typical usecase which
depicts how those components actually work together. The example Jeremy
mostly uses for this is &#8220;Opening a source code file in Visual Studio&#8221; .
I&#8217;m lazy, so I&#8217;m going to reuse this one.</p>

<p><em>&#8220;Consider you double-click a file in the Solution Explorer of VS. When you do this for the first
time a new tab displaying the contents of the file will be opened. Doing
the same thing a second time will not open a new tab, but rather focus
the existing tab displaying the contents of the file.&#8221;</em></p>

<p>It&#8217;s actually easy to translate this story into a more abstract version using the
responsibilities described in this post so far.</p>

<p><em>&#8220;Consider you want to open a <code>Screen</code> via a <code>ScreenSubject</code>. When the <code>ScreenSubject</code>
detects that no related <code>Screen</code> is being displayed to the user, a new
<code>Screen</code> will be created by the subject and then added to the
<code>ScreenCollection</code>. Doing the same thing a second time will not open
up a <code>Screen</code>, but rather activate the existing <code>SCREEN</code>, because
the <code>ScreenSubject</code> detected that the <code>Screen</code> is already open.&#8221;</em></p>

<p>The interesting sidenode in this design is that from a client code
perspective there is no difference between opening up a screen and
activating a screen. Really nice &#8230;</p>

<h2>Usecase: Opening a Screen </h2>

<p>Now that you&#8217;re familiar with the first scenario, time to show some code.  I&#8217;ve shown parts of this before in the previous post (Sorry for the
duplication), but added some bits in order to illustrate this example.
From the outside world a call to the <code>ScreenConductor</code> for this usecase
might look like this.</p>

<figure class='code'><figcaption><span>Opening a Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">var</span> <span class="n">subject</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CSharpFileSubject</span><span class="p">(</span><span class="s">@&quot;C:\\end\\of\\the\\world.cs&quot;</span><span class="p">);</span>
</span><span class='line'><span class="n">screenConductor</span><span class="p">.</span><span class="n">OpenScreen</span><span class="p">(</span><span class="n">subject</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that all data needed for creating the actual Screen will be passed in with the
concrete <code>ScreenSubject</code> implementation.</p>

<figure class='code'><figcaption><span>A ScreenSubject</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">CSharpFileSubject</span> <span class="p">:</span> <span class="n">IScreenSubject</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">private</span> <span class="n">_fileName</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="nf">CSharpFileSubject</span><span class="p">(</span><span class="kt">string</span> <span class="n">fileName</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">_fileName</span> <span class="p">=</span> <span class="n">fileName</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="kt">bool</span> <span class="nf">Matches</span><span class="p">(</span><span class="n">IScreen</span> <span class="n">screen</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">screen</span> <span class="k">is</span> <span class="n">SourceCodeScreen</span> <span class="p">&amp;&amp;</span>
</span><span class='line'>      <span class="kt">string</span><span class="p">.</span><span class="n">Equals</span><span class="p">((</span><span class="n">SourceCodeScreen</span><span class="p">)</span><span class="n">screen</span><span class="p">.</span><span class="n">FileName</span><span class="p">,</span> <span class="n">_fileName</span><span class="p">));</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="n">IScreen</span> <span class="nf">CreateScreen</span><span class="p">(</span><span class="n">IScreenFactory</span> <span class="n">screenFactory</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">var</span> <span class="n">screen</span> <span class="p">=</span> <span class="n">screenFactory</span><span class="p">.</span><span class="n">Build</span><span class="p">&lt;</span><span class="n">SourceCodeScreen</span><span class="p">&gt;();</span>
</span><span class='line'>    <span class="n">screen</span><span class="p">.</span><span class="n">File</span> <span class="p">=</span> <span class="n">_fileName</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">screen</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is not quite the code I would write for a real
system, but I think you get the point. Two methods need to be
implemented for the <code>IScreenSubject</code> interface. This is <code>bool
Matches(IScreen)</code> which identifies a related screen and <code>IScreen
CreateScreen(IScreenFactory)</code> which is used to create the screens. I
really like this kind of API design since it gives you all sorts of
extension points without the need to open up the actual infrastructure.
Want to show a <code>WaitCursor</code> while you create the <code>Screen</code>? Go ahead. Want to
do some loading before the screen is opened? Here&#8217;s the place to do it .
. . Anyway, the <code>ScreenConductors</code> side of things looks like this.</p>

<figure class='code'><figcaption><span>Opening a Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">virtual</span> <span class="k">void</span> <span class="nf">OpenScreen</span><span class="p">(</span><span class="n">IScreenSubject</span> <span class="n">subject</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">subject</span><span class="p">.</span><span class="n">Matches</span><span class="p">(</span><span class="n">_screens</span><span class="p">.</span><span class="n">Active</span><span class="p">))</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">IScreen</span> <span class="n">screen</span> <span class="p">=</span> <span class="n">findScreenMatchingSubject</span><span class="p">(</span><span class="n">subject</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">screen</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">screen</span> <span class="p">=</span> <span class="n">createNewActiveScreen</span><span class="p">(</span><span class="n">subject</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">activate</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">_screens</span><span class="p">.</span><span class="n">Show</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Pretty slick, isn&#8217;t it? The whole code is really dense. Most of the methods involved are not more
than 5 lines long. Finding the related screen for instance is just a
matter of a LINQ-Query on the ScreenCollection using the <code>Matches</code> method
as its predicate.</p>

<figure class='code'><figcaption><span>Finding a matching Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="n">IScreen</span> <span class="nf">findScreenMatchingSubject</span><span class="p">(</span><span class="n">IScreenSubject</span> <span class="n">subject</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">_screens</span><span class="p">.</span><span class="n">AllScreens</span><span class="p">.</span><span class="n">FirstOrDefault</span><span class="p">(</span><span class="n">subject</span><span class="p">.</span><span class="n">Matches</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Creation of the target screen on the other hand is just a matter of
handing the <code>ScreenFactory</code> (which is actually just a facade to the
IoC-Container of choice) to the subject, activating the created Screen
and adding it to the <code>ScreenCollection</code>.</p>

<figure class='code'><figcaption><span>Creating a new Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="n">IScreen</span> <span class="nf">createNewActiveScreen</span><span class="p">(</span><span class="n">IScreenSubject</span> <span class="n">subject</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">IScreen</span> <span class="n">screen</span> <span class="p">=</span> <span class="n">subject</span><span class="p">.</span><span class="n">CreateScreen</span><span class="p">(</span><span class="n">_factory</span><span class="p">);</span>
</span><span class='line'>  <span class="n">activate</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'>  <span class="n">_screens</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">screen</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The last missing piece here is the actual activation of the Screen.</p>

<figure class='code'><figcaption><span>Activating a Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="k">void</span> <span class="nf">activate</span><span class="p">(</span><span class="n">IScreen</span> <span class="n">screen</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">_shellService</span><span class="p">.</span><span class="n">ActivateScreen</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is delegated to the so called <code>IShellService</code>. You might ask yourself why this
particular dependency exists (at least I did). The main purpose of this
service is mostly the topic of registering Commands and filling up
option panes related to the current Screen. This will be a post on its
own, so don&#8217;t be mad at me, when I don&#8217;t cover it today. Instead I would
like to take a look at another common use case:</p>

<h2>Usecase: Closing a Screen </h2>

<p>Now that we&#8217;ve seen how a Screen gets opened, let&#8217;s take a look
at the other side of the coin, at how it&#8217;s closed.</p>

<figure class='code'><figcaption><span>Closing a Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">virtual</span> <span class="k">void</span> <span class="nf">Close</span><span class="p">(</span><span class="n">IScreen</span> <span class="n">screen</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">removeScreen</span><span class="p">(</span><span class="n">screen</span><span class="p">))</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">activateCurrentScreen</span><span class="p">();</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Most of the handling is in the removeScreen-method (btw, where does this
convention of having all private methods beeing camel-cased come from?
Is this some Java-exposure leaking through? ;-))</p>

<figure class='code'><figcaption><span>Removing a Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="kt">bool</span> <span class="nf">removeScreen</span><span class="p">(</span><span class="n">IScreen</span> <span class="n">screen</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(!</span><span class="n">screen</span><span class="p">.</span><span class="n">CanClose</span><span class="p">())</span> <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">_events</span><span class="p">.</span><span class="n">RemoveListener</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'>  <span class="n">_screens</span><span class="p">.</span><span class="n">Remove</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'>  <span class="n">_shellService</span><span class="p">.</span><span class="n">ClearTransient</span><span class="p">();</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">true</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>It delegates the decision whether a screen can be closed
to the screen and in case it can be closed, it removes the screen from
the <code>EventAggregator</code> (<code>_events</code>), from the <code>ScreenCollection</code> (<code>_screens</code>) and
clears its Command-registration (<code>_shellService.ClearTransient()</code>),
before it activates the next screen becoming visible (in case there is one).</p>

<figure class='code'><figcaption><span>Activating the current Screen</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="k">void</span> <span class="nf">activateCurrentScreen</span><span class="p">()</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">IScreen</span> <span class="n">screen</span> <span class="p">=</span> <span class="n">_screens</span><span class="p">.</span><span class="n">Active</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">screen</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">activate</span><span class="p">(</span><span class="n">screen</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Usecase: App shutdown coordination </h2>

<p>There is another common usecase implemented by the
<code>ScreenConductor</code> I would like to show you. This is how the whole app
shutdown is coordinated. Prerequesites for this: Instances interested in
beeing notified when the user tries to shut the application down, need
to a) implement the <code>IClosable</code> interface and b) be registered at the
<code>EventAggregator</code>. The latter is done automatically for screens by
the <code>ScreenConductor</code>.</p>

<figure class='code'><figcaption><span>The IClosable interface</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">interface</span> <span class="n">ICloseable</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">AddCanCloseMessages</span><span class="p">(</span><span class="n">CloseToken</span> <span class="n">token</span><span class="p">);</span>
</span><span class='line'>  <span class="k">void</span> <span class="nf">PerformShutdown</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>IClosable</code> interface just consists of two methods. <code>void AddCanCloseMessage(CloseToken)</code> is called in order
to get feedback from listeners whether the application is allowed to be
shutdown. You can think of <code>CloseToken</code> as a more or less extended version
of <code>CancelEventArgs</code>.</p>

<figure class='code'><figcaption><span>The CloseToken class</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">CloseToken</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">private</span> <span class="k">readonly</span> <span class="n">List</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;</span> <span class="n">_messages</span> <span class="p">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;();</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="kt">string</span><span class="p">[]</span> <span class="n">Messages</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_messages</span><span class="p">.</span><span class="n">ToArray</span><span class="p">();</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="k">void</span> <span class="nf">AddMessage</span><span class="p">(</span><span class="kt">string</span> <span class="n">message</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">_messages</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The following code piece is hooked into the Closing - event of
StoryTellers main window (the shell). It heavily leverages the delegate
based eventing of StoryTellers <code>EventBroker</code> in order to interact with all
interested listeners.</p>

<figure class='code'><figcaption><span>Shutdown as implemented in StoryTeller</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="kt">bool</span> <span class="nf">CanClose</span><span class="p">()</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">var</span> <span class="n">token</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CloseToken</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">_events</span><span class="p">.</span><span class="n">SendMessage</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="n">x</span><span class="p">.</span><span class="n">AddCanCloseMessages</span><span class="p">(</span><span class="n">token</span><span class="p">));</span>
</span><span class='line'>  <span class="kt">bool</span> <span class="n">returnValue</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">token</span><span class="p">.</span><span class="n">Messages</span><span class="p">.</span><span class="n">Length</span> <span class="p">&gt;</span> <span class="m">0</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="kt">string</span> <span class="n">userMessage</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Join</span><span class="p">(</span><span class="s">&quot;\\n&quot;</span><span class="p">,</span> <span class="n">token</span><span class="p">.</span><span class="n">Messages</span><span class="p">);</span>
</span><span class='line'>    <span class="n">returnValue</span> <span class="p">=</span> <span class="n">_messageBox</span><span class="p">.</span><span class="n">AskUser</span><span class="p">(</span><span class="n">CAN_CLOSE_TITLE</span><span class="p">,</span> <span class="n">userMessage</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">returnValue</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="n">_events</span><span class="p">.</span><span class="n">SendMessage</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="n">x</span><span class="p">.</span><span class="n">PerformShutdown</span><span class="p">());</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">return</span> <span class="n">returnValue</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>One remark to the <code>CanClose()</code> code: <code>_messageBox</code> is just a small wrapper abstraction
in order to make user interaction via message prompts testable. There&#8217;s
nothing really fancy behind that. I really like the way the app shutdown
is implemented. In fact we&#8217;ve added something very similar in my current
project. However, I&#8217;m not so sure when it comes to the question whether
this particular code piece should be part of the <code>ScreenConductor</code>. You
could argue that this method has only very limited cohesion with the
rest of the <code>ScreenConductors</code> methods. In fact most of the stuff the
<code>ScreenConductor</code> interacts with (<code>ScreenCollection</code>, <code>Screens</code>,
<code>ScreenFactory</code>, <code>ScreenSubject</code>) isn&#8217;t touched in this method. Besides
that, it&#8217;s code that client application code normally IMHO doesn&#8217;t need
to or even should call. In our current app we&#8217;ve extracted this
responsibillity into a separate class called the
<code>ApplicationShutdownCoordinator</code> because of that.</p>

<h2>Some more impressions on StoryTellers ScreenConductor </h2>

<p>The exposed API of the <code>ScreenConductor</code> is pretty small, actually only 6 &#8220;core&#8221; methods and some overloads.
Most of the API really shines. However, besides the already mentioned
<code>CanClose()</code> functionality, there is another functionality which in my
opinion should not be in the ScreenConductor. Can you spot it?</p>

<p><img src="http://www.bjoernrochel.de/images/posts/screenconductorinterface1.jpg" alt="image" /></p>

<p><code>LoadHierarchy(Func&lt;Hierarchy&gt;))</code> looks a bit misplaced to me because it
seems to work on a different abstraction level than the rest of the
methods. It looks very application specific, while the rest of the
StoryTeller APIs look very general purpose (independent from the fact
whether Jeremy actually wanted to achieve this or this being just the
result of applying good design practices). Same applies to one of the
messages / events the <code>ScreenConductor</code> is registered for at the
<code>EventBroker</code>, namely <code>DeleteTestMessage</code>. I don&#8217;t think it should be
directly handled in the <code>ScreenConductor</code>.</p>

<p><img src="http://www.bjoernrochel.de/images/posts/screenconductorhandlers.jpg" alt="image" /></p>

<p>A static code analysis might indicate that the <code>ScreenConductor</code> has too
many dependencies. In fact there&#8217;re 7 direct dependencies injected into
the constructor,</p>

<ul>
<li><code>IEventAggregator</code></li>
<li><code>IScreenCollection</code></li>
<li><code>IScreenFactory</code></li>
<li><code>IApplicationShell</code></li>
<li><code>IShellService</code></li>
<li><code>IScreenObjectLocator</code></li>
<li><code>IMessageCreator</code></li>
</ul>


<p>6 transient dependencies (method parameters or local scope),</p>

<ul>
<li><code>IScreen</code></li>
<li><code>IScreenSubject</code></li>
<li><code>CloseToken</code></li>
<li><code>UserScreenActivation</code></li>
<li><code>OpenItemMessage</code></li>
<li><code>DeleteTestMessage</code></li>
</ul>


<p>and 3 Message interests</p>

<ul>
<li><code>UserscreenActivation</code></li>
<li><code>OpenItemMessage</code></li>
<li><code>DeleteTestMessage</code></li>
</ul>


<p>in the <code>ScreenConductor</code> class. Sounds pretty heavy and like a refactoring
candidate at first. However, as I mentioned earlier, the <code>ScreenConductor</code>
is mostly a <em>Facade</em> with some additional coordination logic in it. When I
say &#8220;some additional coordination logic&#8221; I mean this literally.
<strong>ScreenConductor has just round about 250 LOC</strong>.</p>

<p>I&#8217;m totally ok with this. It certainly has some potential for optimization of the
dependencies (<code>IScreenObjectLocator</code> seems to be at least partially
obsolete, <code>IApplicationShell</code> and <code>IShellService</code> could be merged I guess),
but even without that I consider it a really good example of strong
extensible design for composite desktop apps. For me personally the
<code>ScreenConductor</code> fills a really important gap in the p&amp;p composite app
guidance (be it CAB / SCSF or PRISM). I always had the feeling that I&#8217;m
missing something there, but was unable to point out exactly what I&#8217;ve
been missing. This feeling mostly came up when I added some screen
activation or screen creation logic in places that didn&#8217;t felt right.
Now I know why. Interestingly others <a href="http://neverindoubtnet.blogspot.com/2009/05/birth-and-death-of-m-v-vm-triads.html">observed the need for something similar as well</a>.
Having a <code>ScreenConductor</code> in your application makes IMHO the whole UI
infrastructure a lot more approchable and easier to understand.</p>

<h2>Some final thoughts </h2>

<p>I left the rest of the <code>ScreenConductors</code> code out of
this post intentionally, because if there&#8217;s one thing I&#8217;d like you to
take from this post or even the complete series is that StoryTeller is a
really good learning resource. I really value, what I&#8217;ve learned from
the Dovetail guys while inspecting the code. I wish I had done this
earlier. Good design matters. If you&#8217;re looking for a place to learn
more about it, the <a href="http://storyteller.tigris.org/source/browse/storyteller/">StoryTeller codebase</a>
might just be the place for you. See you next time, when we take some
time to dig into the command structure of StoryTeller &#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Notifications the WPF way (I guess), Part III]]></title>
    <link href="http://www.bjoernrochel.de/2009/11/02/notifications-the-wpf-way-i-guess-part-iii/"/>
    <updated>2009-11-02T19:59:00+01:00</updated>
    <id>http://www.bjoernrochel.de/2009/11/02/notifications-the-wpf-way-i-guess-part-iii</id>
    <content type="html"><![CDATA[<p>Today I would like to conclude my little series about the Notification
Pattern with (I guess at least for some of you) the most interesting
part:</p>

<h2>Today is all about displaying Notifications in the UI</h2>

<p>This post will guide you through all the steps I took in order to achieve the
affect I demonstrated in the <a href="http://www.bjoernrochel.de/2009/10/20/notifications-the-wpf-way-i-guess-part-i/">introduction post</a>.
Last time I showed how I&#8217;m transfering <code>Notifications</code> from the <code>ViewModel</code>
into the logical WPF tree. If you&#8217;ve not read the previous posts, please
give them some minutes, because I&#8217;m not going to repeat a lot of them
today. You can find them
<a href="http://www.bjoernrochel.de/2009/08/28/implementing-the-notification-pattern-using-dataannotation-validators/">here</a>,
<a href="http://www.bjoernrochel.de/2009/10/20/notifications-the-wpf-way-i-guess-part-i/">here</a>
and
<a href="http://www.bjoernrochel.de/2009/10/27/notifications-the-wpf-way-i-guess-part-ii/">here</a>.
As always, a quick reminder:</p>

<blockquote><p>What I&#8217;m showing is in this series is <strong>how I&#8217;ve implemented the Notification Pattern</strong>. I&#8217;m not claiming that
it&#8217;s the only or the best way to do so. However, it&#8217;s the one that works
very good for me.</p></blockquote>

<h3>How to get the red border effect</h3>

<p>The red border has to be displayed when Notifications exists for a control. Technically
this means that the attached property <code>Notifications</code> (which is defined
on the <code>ValidationBehavior</code> class I showed in the last post) is set to a
non empty <code>NotificationCollection</code>. We can react to this by defining a
<code>DataTrigger</code> for this. In my own words I would describe a <code>DataTrigger</code> as</p>

<blockquote><p>An in XAML defined event handler with a related criteria. When the
criteria is matched the DataTrigger gets executed. When it isn&#8217;t matched
any more, the DataTrigger reverts the state of the element on which it&#8217;s
defined to the state before it was executed.</p></blockquote>

<p>Sounds usable for our purpose. Think about it, we only want to show the red border, when the
attached property is set to a non empty collection. If the property is
reset the border needs to disappear. The only difficulty with
<code>DataTriggers</code> we need to solve on our way is how to configure that
exactly in XAML. <code>DataTriggers</code> can be easily set on primitives (such as
string, bool, etc.) or null, but there isn&#8217;t an out of the box way for
setting our criteria in XAML. However you can use a custom Converter for
converting our value to a primitive &#8220;switch&#8221;.</p>

<figure class='code'><figcaption><span>An IValueConverter for our DataTrigger</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="na">[ValueConversion(typeof(NotificationCollection), typeof(bool))]</span>
</span><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">ContainsNotificationConverter</span> <span class="p">:</span> <span class="n">IValueConverter</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="kt">object</span> <span class="nf">Convert</span><span class="p">(</span><span class="kt">object</span> <span class="k">value</span><span class="p">,</span> <span class="n">Type</span> <span class="n">targetType</span><span class="p">,</span> <span class="kt">object</span> <span class="n">parameter</span><span class="p">,</span> <span class="n">CultureInfo</span> <span class="n">culture</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">(</span><span class="k">value</span> <span class="p">!=</span> <span class="k">null</span> <span class="p">&amp;&amp;</span> <span class="k">value</span> <span class="k">is</span> <span class="n">NotificationCollection</span> <span class="p">&amp;&amp;</span> <span class="p">((</span><span class="n">NotificationCollection</span><span class="p">)</span><span class="k">value</span><span class="p">).</span><span class="n">Any</span><span class="p">());</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="kt">object</span> <span class="nf">ConvertBack</span><span class="p">(</span><span class="kt">object</span> <span class="k">value</span><span class="p">,</span> <span class="n">Type</span> <span class="n">targetType</span><span class="p">,</span> <span class="kt">object</span> <span class="n">parameter</span><span class="p">,</span> <span class="n">CultureInfo</span> <span class="n">culture</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="k">throw</span> <span class="k">new</span> <span class="nf">NotSupportedException</span><span class="p">();</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Using this converter we can define our DataTrigger in the Style for Controls like this:</p>

<figure class='code'><figcaption><span>Wiring it up via XAML DataTriggers</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;Fx:ContainsNotificationConverter</span> <span class="na">x:Key=</span><span class="s">&quot;notificationConverter&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;Style</span> <span class="na">TargetType=</span><span class="s">&quot;Control&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>     <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;Fx:ValidationBehavior.IsEnabled&quot;</span> <span class="na">Value=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>     <span class="nt">&lt;Style.Triggers&gt;</span>
</span><span class='line'>         <span class="nt">&lt;DataTrigger</span>
</span><span class='line'>             <span class="na">Binding=</span><span class="s">&quot;{Binding Path=(Fx:ValidationBehavior.Notifications),</span>
</span><span class='line'><span class="s">             Converter={StaticResource notificationConverter},</span>
</span><span class='line'><span class="s">             RelativeSource={x:Static RelativeSource.Self}}&quot;</span> <span class="na">Value=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>             <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;BorderBrush&quot;</span> <span class="na">Value=</span><span class="s">&quot;Red&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>             <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;BorderThickness&quot;</span> <span class="na">Value=</span><span class="s">&quot;2&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>         <span class="nt">&lt;/DataTrigger&gt;</span>
</span><span class='line'>     <span class="nt">&lt;/Style.Triggers&gt;</span>
</span><span class='line'> <span class="nt">&lt;/Style&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>How to display Notifications in a Tooltip</h2>

<p>Solving the Tooltip requirement was a bit more tricky (at least for me).
It took me quite some time to figure out how to do this in WPF. The
solution I&#8217;m going to show uses only XAML based code.</p>

<h3>1. Integrating the Tooltip into the DataTrigger</h3>

<p>We simply use our <code>DataTrigger</code> to automatically set the tooltip of a
<code>Control</code> in case <code>Notifications</code> exist.</p>

<figure class='code'><figcaption><span>Adding the tooltip </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;ToolTip</span> <span class="na">x:Key=</span><span class="s">&quot;ValidationErrorTooltip&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;Style</span> <span class="na">TargetType=</span><span class="s">&quot;Control&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>     <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;Fx:ValidationBehavior.IsEnabled&quot;</span> <span class="na">Value=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>     <span class="nt">&lt;Style.Triggers&gt;</span>
</span><span class='line'>         <span class="nt">&lt;DataTrigger</span>
</span><span class='line'>             <span class="na">Binding=</span><span class="s">&quot;{Binding Path=(Fx:ValidationBehavior.Notifications),</span>
</span><span class='line'><span class="s">             Converter={StaticResource notificationConverter},</span>
</span><span class='line'><span class="s">             RelativeSource={x:Static RelativeSource.Self}}&quot;</span> <span class="na">Value=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>             <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;BorderBrush&quot;</span> <span class="na">Value=</span><span class="s">&quot;Red&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>             <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;BorderThickness&quot;</span> <span class="na">Value=</span><span class="s">&quot;2&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>             <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;ToolTip&quot;</span> <span class="na">Value=</span><span class="s">&quot;{StaticResource ValidationErrorTooltip}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>         <span class="nt">&lt;/DataTrigger&gt;</span>
</span><span class='line'>     <span class="nt">&lt;/Style.Triggers&gt;</span>
</span><span class='line'> <span class="nt">&lt;/Style&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now the UI looks really crappy. The tooltip is not recognizable as one.</p>

<p><img src="http://www.bjoernrochel.de/images/posts/signup_emptytooltip1.jpg" alt="Horrible Tooltip" /></p>

<h3>2. Using a ControlTemplate to style the Tooltip</h3>

<p>In order to shape the appearance of the tooltip we can use XAML Styles again.
You can change the whole visual appearance of a Control using Styles and
<code>ControlTemplates</code>. The template I defined consists mostly of a <code>DockPanel</code>
containing a <code>Label</code> (which provides the tooltips caption) and a <code>TextBlock</code>
(which will later contain the Notification messages).</p>

<figure class='code'><figcaption><span>Adding ControlTemplates to the mix  </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;Style</span> <span class="na">x:Key=</span><span class="s">&quot;ErrorTooltipSyle&quot;</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>    <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;Template&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;Setter.Value&gt;</span>
</span><span class='line'>            <span class="nt">&lt;ControlTemplate</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                <span class="nt">&lt;Border</span> <span class="na">BorderBrush=</span><span class="s">&quot;Black&quot;</span> <span class="na">BorderThickness=</span><span class="s">&quot;1&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                    <span class="nt">&lt;DockPanel&gt;</span>
</span><span class='line'>                        <span class="nt">&lt;Label</span> <span class="na">DockPanel.Dock=</span><span class="s">&quot;Top&quot;</span>
</span><span class='line'>                          <span class="na">FontWeight=</span><span class="s">&quot;Bold&quot;</span>
</span><span class='line'>                          <span class="na">Background=</span><span class="s">&quot;Red&quot;</span>
</span><span class='line'>                          <span class="na">Foreground=</span><span class="s">&quot;White&quot;</span>
</span><span class='line'>                          <span class="na">Content=</span><span class="s">&quot;Validation Error&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>                        <span class="nt">&lt;TextBlock</span>
</span><span class='line'>                          <span class="na">Padding=</span><span class="s">&quot;10&quot;</span>
</span><span class='line'>                          <span class="na">Background=</span><span class="s">&quot;White&quot;</span>
</span><span class='line'>                          <span class="na">Foreground=</span><span class="s">&quot;Black&quot;</span>
</span><span class='line'>                          <span class="na">TextWrapping=</span><span class="s">&quot;WrapWithOverflow&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>                    <span class="nt">&lt;/DockPanel&gt;</span>
</span><span class='line'>                <span class="nt">&lt;/Border&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/ControlTemplate&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/Setter.Value&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/Setter&gt;</span>
</span><span class='line'><span class="nt">&lt;/Style&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="c">&lt;!-- Notice the defined Style --&gt;</span>
</span><span class='line'><span class="nt">&lt;ToolTip</span> <span class="na">x:Key=</span><span class="s">&quot;ValidationErrorTooltip&quot;</span> <span class="na">Style=</span><span class="s">&quot;{StaticResource ErrorTooltipSyle}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;Style</span> <span class="na">TargetType=</span><span class="s">&quot;Control&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>    <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;Fx:ValidationBehavior.IsEnabled&quot;</span> <span class="na">Value=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;Style.Triggers&gt;</span>
</span><span class='line'>        <span class="nt">&lt;DataTrigger</span>
</span><span class='line'>            <span class="na">Binding=</span><span class="s">&quot;{Binding Path=(Fx:ValidationBehavior.Notifications),</span>
</span><span class='line'><span class="s">            Converter={StaticResource notificationConverter},</span>
</span><span class='line'><span class="s">            RelativeSource={x:Static RelativeSource.Self}}&quot;</span> <span class="na">Value=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>            <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;BorderBrush&quot;</span> <span class="na">Value=</span><span class="s">&quot;Red&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>            <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;BorderThickness&quot;</span> <span class="na">Value=</span><span class="s">&quot;2&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>            <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;ToolTip&quot;</span> <span class="na">Value=</span><span class="s">&quot;{StaticResource ValidationErrorTooltip}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/DataTrigger&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/Style.Triggers&gt;</span>
</span><span class='line'><span class="nt">&lt;/Style&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Our UI now looks like this.</p>

<p><img src="http://www.bjoernrochel.de/images/posts/signup_tooltipwithcontroltemplate1.jpg" alt="Tooltip with ItemSource" /></p>

<p>Not as crappy as before but we&#8217;re not finished yet, because we&#8217;re not
displaying <code>Notifications</code> yet.</p>

<h3>3. Define Databinding on the Tooltip</h3>

<p>Defining the Databinding was probably the hardest step I had to take.
The problem is that you need to get the <code>NotificationCollection</code> from the
<code>Control</code> the tooltip is displayed on. Being not a WPF pro figuring out
how to do this took quite some time. Anyway, in order to get the
<code>Notification</code> there isn&#8217;t much you need to do. The trick is to bind
against the property <code>PlacementTarget</code> on the <code>ToolTip</code> class itself. This
property holds the reference to the <code>Control</code> on which the <code>Tooltip</code> instance is
displayed. All we need to do is to add a <code>Binding</code> to the <code>Style</code> definition
that sets the <code>DataContext</code> of the <code>ToolTip</code> to the related control.</p>

<figure class='code'><figcaption><span>Binding the PlacementTarget to display the tooltip</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;Style</span> <span class="na">x:Key=</span><span class="s">&quot;ErrorTooltipSyle&quot;</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>    ...
</span><span class='line'>    <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;DataContext&quot;</span> <span class="na">Value=</span><span class="s">&quot;{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>    ...
</span><span class='line'><span class="nt">&lt;/Style&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The next step we need to take is setting up the Binding
for our attached property <code>Notifications</code>. Because a
<code>NotificationCollection</code> can contain more than one <code>Notification</code> I used the
<code>ItemsControl</code> for displaying them in the content area of the tooltip.</p>

<figure class='code'><figcaption><span>Displaying the Notifications </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;Style</span> <span class="na">x:Key=</span><span class="s">&quot;ErrorTooltipSyle&quot;</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>  <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;DataContext&quot;</span> <span class="na">Value=</span><span class="s">&quot;{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;Template&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;Setter.Value&gt;</span>
</span><span class='line'>            <span class="nt">&lt;ControlTemplate</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                <span class="nt">&lt;Border</span> <span class="na">BorderBrush=</span><span class="s">&quot;Black&quot;</span> <span class="na">BorderThickness=</span><span class="s">&quot;1&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                    <span class="nt">&lt;DockPanel&gt;</span>
</span><span class='line'>                        <span class="nt">&lt;Label</span> <span class="na">DockPanel.Dock=</span><span class="s">&quot;Top&quot;</span>
</span><span class='line'>                          <span class="na">FontWeight=</span><span class="s">&quot;Bold&quot;</span>
</span><span class='line'>                          <span class="na">Background=</span><span class="s">&quot;Red&quot;</span>
</span><span class='line'>                          <span class="na">Foreground=</span><span class="s">&quot;White&quot;</span>
</span><span class='line'>                          <span class="na">Content=</span><span class="s">&quot;Validation Error&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>                        <span class="nt">&lt;TextBlock</span>
</span><span class='line'>                          <span class="na">Padding=</span><span class="s">&quot;10&quot;</span>
</span><span class='line'>                          <span class="na">Background=</span><span class="s">&quot;White&quot;</span>
</span><span class='line'>                          <span class="na">Foreground=</span><span class="s">&quot;Black&quot;</span>
</span><span class='line'>                          <span class="na">TextWrapping=</span><span class="s">&quot;WrapWithOverflow&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>
</span><span class='line'>                            <span class="c">&lt;!-- This control displays our Notifications --&gt;</span>
</span><span class='line'>                            <span class="nt">&lt;ItemsControl</span> <span class="na">x:Name=</span><span class="s">&quot;notifications&quot;</span>
</span><span class='line'>                                <span class="na">HorizontalAlignment=</span><span class="s">&quot;Stretch&quot;</span>
</span><span class='line'>                                <span class="na">Margin=</span><span class="s">&quot;10&quot;</span>
</span><span class='line'>                                <span class="na">VerticalAlignment=</span><span class="s">&quot;Center&quot;</span>
</span><span class='line'>                                <span class="na">ItemsSource=</span><span class="s">&quot;{Binding Path=(Fx:ValidationBehavior.Notifications), Mode=OneWay}&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>
</span><span class='line'>                         <span class="nt">&lt;/TextBlock&gt;</span>
</span><span class='line'>                    <span class="nt">&lt;/DockPanel&gt;</span>
</span><span class='line'>                <span class="nt">&lt;/Border&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/ControlTemplate&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/Setter.Value&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/Setter&gt;</span>
</span><span class='line'><span class="nt">&lt;/Style&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Voila, our <code>Notifications</code> are finally displayed in the UI.</p>

<p><img src="http://www.bjoernrochel.de/images/posts/signup_tooltipwithcontroltemplate2.jpg" alt="Tooltip with ControlTemplate" /></p>

<h3>4. Styling the Notifications</h3>

<p>In my initial post I showed a hyphen in front of each Notification. This
is fairly easy to add, too. All you have to do is to define a
<code>DataTemplate</code> for the <code>Notification</code> class and set it as the <code>ItemTemplate</code>
of the <code>ItemsControl</code> we&#8217;re using.</p>

<figure class='code'><figcaption><span>Styling the notifications</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="c">&lt;!-- Very simple data template --&gt;</span>
</span><span class='line'><span class="nt">&lt;DataTemplate</span> <span class="na">x:Key=</span><span class="s">&quot;NotificationTemplate&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>    <span class="nt">&lt;StackPanel</span> <span class="na">Orientation=</span><span class="s">&quot;Horizontal&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;TextBlock&gt;</span>-<span class="nt">&lt;/TextBlock&gt;</span>
</span><span class='line'>        <span class="nt">&lt;TextBlock</span> <span class="na">x:Name=</span><span class="s">&quot;notification&quot;</span> <span class="na">Text=</span><span class="s">&quot;{Binding}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/StackPanel&gt;</span>
</span><span class='line'><span class="nt">&lt;/DataTemplate&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;Style</span> <span class="na">x:Key=</span><span class="s">&quot;ErrorTooltipSyle&quot;</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>  <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;DataContext&quot;</span> <span class="na">Value=</span><span class="s">&quot;{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;Setter</span> <span class="na">Property=</span><span class="s">&quot;Template&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;Setter.Value&gt;</span>
</span><span class='line'>            <span class="nt">&lt;ControlTemplate</span> <span class="na">TargetType=</span><span class="s">&quot;ToolTip&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                <span class="nt">&lt;Border</span> <span class="na">BorderBrush=</span><span class="s">&quot;Black&quot;</span> <span class="na">BorderThickness=</span><span class="s">&quot;1&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                    <span class="nt">&lt;DockPanel&gt;</span>
</span><span class='line'>                        <span class="nt">&lt;Label</span> <span class="na">DockPanel.Dock=</span><span class="s">&quot;Top&quot;</span>
</span><span class='line'>                          <span class="na">FontWeight=</span><span class="s">&quot;Bold&quot;</span>
</span><span class='line'>                          <span class="na">Background=</span><span class="s">&quot;Red&quot;</span>
</span><span class='line'>                          <span class="na">Foreground=</span><span class="s">&quot;White&quot;</span>
</span><span class='line'>                          <span class="na">Content=</span><span class="s">&quot;Validation Error&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>                        <span class="nt">&lt;TextBlock</span>
</span><span class='line'>                          <span class="na">Padding=</span><span class="s">&quot;10&quot;</span>
</span><span class='line'>                          <span class="na">Background=</span><span class="s">&quot;White&quot;</span>
</span><span class='line'>                          <span class="na">Foreground=</span><span class="s">&quot;Black&quot;</span>
</span><span class='line'>                          <span class="na">TextWrapping=</span><span class="s">&quot;WrapWithOverflow&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                          <span class="nt">&lt;ItemsControl</span> <span class="na">x:Name=</span><span class="s">&quot;notifications&quot;</span>
</span><span class='line'>                                <span class="na">HorizontalAlignment=</span><span class="s">&quot;Stretch&quot;</span>
</span><span class='line'>                                <span class="na">Margin=</span><span class="s">&quot;10&quot;</span>
</span><span class='line'>                                <span class="na">VerticalAlignment=</span><span class="s">&quot;Center&quot;</span>
</span><span class='line'>                                <span class="na">ItemsSource=</span><span class="s">&quot;{Binding Path=(Fx:ValidationBehavior.Notifications), Mode=OneWay}&quot;</span>
</span><span class='line'>
</span><span class='line'>                                <span class="err">&lt;!--</span> <span class="err">This</span> <span class="err">uses</span> <span class="err">configures</span> <span class="err">our</span> <span class="err">template</span> <span class="err">for</span> <span class="err">Notifications</span> <span class="err">--</span><span class="nt">&gt;</span>
</span><span class='line'>                                ItemTemplate=&quot;{StaticResource NotificationTemplate}&quot; /&gt;
</span><span class='line'>
</span><span class='line'>              <span class="nt">&lt;/TextBlock&gt;</span>
</span><span class='line'>                    <span class="nt">&lt;/DockPanel&gt;</span>
</span><span class='line'>                <span class="nt">&lt;/Border&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/ControlTemplate&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/Setter.Value&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/Setter&gt;</span>
</span><span class='line'><span class="nt">&lt;/Style&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>DataTemplates</code> are also the weapon of choice if you want to
display errors in a different fashion than warnings (for instance by
displaying a different icon). This is where we arrived:</p>

<p><img src="http://www.bjoernrochel.de/images/posts/signup_tooltipfinishedjpg.jpg" alt="Tooltip finished" /></p>

<h2>Closing thoughts</h2>

<p>What I like about the current solution is that it demonstrates in a nice
way how different the programming model of WPF actually is compared to
WinForms. The only imperative code we have is the code that transfers
<code>Notifications</code> from our <code>ViewModel</code> into the logical tree. That&#8217;s it. The
rest, the complete visual appeal of the Notifications, is a separated
concern. Those two things can be changed independently from each other,
even by different roles in a development team (Designer/Developer).
Besides that I like the way the composition based WPF model helps my
application code to stay focused and clean with only minimum
implementation constraints on the ViewModel itself (the
INotificationSource interface). There&#8217;re certainly things to improve
both in XAML and in the glue code but I consider it a good start to
build on &#8230;</p>
]]></content>
  </entry>
  
</feed>

