<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>Eleutian SpeakENG Development Blog - development</title>
    <link>http://blog.eleutian.com/</link>
    <description />
    <language>en-us</language>
    <copyright>Eleutian Technology</copyright>
    <lastBuildDate>Fri, 09 May 2008 05:56:48 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>aaron@eleutian.com</managingEditor>
    <webMaster>aaron@eleutian.com</webMaster>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=b9a23177-84da-4932-894c-877f59e8ca0f</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,b9a23177-84da-4932-894c-877f59e8ca0f.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,b9a23177-84da-4932-894c-877f59e8ca0f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=b9a23177-84da-4932-894c-877f59e8ca0f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Just wanted to quickly note that I tracked down the performance issue in Rhino.Mocks
and <a href="http://rhino-tools.svn.sourceforge.net/viewvc/rhino-tools?view=rev&amp;revision=1382">patched
it</a>. I also updated the <a href="http://blog.eleutian.com/2008/05/08/MockFrameworkBenchmarks.aspx">original
post</a> with the new numbers. Enjoy!
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=b9a23177-84da-4932-894c-877f59e8ca0f" />
      </body>
      <title>Rhino.Mocks Performance Issue Fixed</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,b9a23177-84da-4932-894c-877f59e8ca0f.aspx</guid>
      <link>http://blog.eleutian.com/2008/05/09/RhinoMocksPerformanceIssueFixed.aspx</link>
      <pubDate>Fri, 09 May 2008 05:56:48 GMT</pubDate>
      <description>&lt;p&gt;
Just wanted to quickly note that I tracked down the performance issue in Rhino.Mocks
and &lt;a href="http://rhino-tools.svn.sourceforge.net/viewvc/rhino-tools?view=rev&amp;amp;revision=1382"&gt;patched
it&lt;/a&gt;. I also updated the &lt;a href="http://blog.eleutian.com/2008/05/08/MockFrameworkBenchmarks.aspx"&gt;original
post&lt;/a&gt; with the new numbers. Enjoy!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=b9a23177-84da-4932-894c-877f59e8ca0f" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,b9a23177-84da-4932-894c-877f59e8ca0f.aspx</comments>
      <category>development</category>
      <category>frameworks</category>
      <category>mocking</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=96b7cc98-7d7d-4aae-8734-42347fcf8464</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,96b7cc98-7d7d-4aae-8734-42347fcf8464.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,96b7cc98-7d7d-4aae-8734-42347fcf8464.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=96b7cc98-7d7d-4aae-8734-42347fcf8464</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <strong>UPDATE: I tracked down the issue
and committed a patch to Rhino.Mocks. Rhino.Mocks is now much more competetive performance
wise, our CI build time nearly halved, and about 4 minutes out of 7 of our test time
has disappeared. New numbers below.</strong>
        <p>
I've complained before that <a href="http://blog.eleutian.com/2007/12/15/MockingIsSlow.aspx">Mocking
is Slow</a> but I never really dove further into it. Today I decided to actually compare
Rhino.Mocks to other mock frameworks on a pure performance basis to see if it was
a global problem. I timed 2000 unit tests across 100 classes with 20 tests each. The
results were a bit surprising:
</p>
        <table>
          <tbody>
            <tr>
              <th>
Framework</th>
              <th>
                <a href="http://testdriven.net/">TD.NET</a> Time</th>
              <th>
nunit-console Time</th>
            </tr>
            <tr>
              <td>
                <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a> old trunk</td>
              <td>
57.36s</td>
              <td>
28.82s</td>
            </tr>
            <tr>
              <td>
                <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a> new trunk</td>
              <td>
22.94s</td>
              <td>
7.59s</td>
            </tr>
            <tr>
              <td>
                <a href="http://code.google.com/p/moq/">Moq</a> trunk</td>
              <td>
18.30s</td>
              <td>
5.91s</td>
            </tr>
            <tr>
              <td>
                <a href="http://www.typemock.com/">TypeMock</a> 4.2.3 Reflective Mocks</td>
              <td>
15.36s</td>
              <td>
9.35s</td>
            </tr>
            <tr>
              <td>
                <a href="http://www.typemock.com/">TypeMock</a> 4.2.3 Natural Mocks</td>
              <td>
16.92s</td>
              <td>
9.56s</td>
            </tr>
          </tbody>
        </table>
        <p>
That's right, according to these tests, <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a> is
at least 3 times slower than the other frameworks when under heavy load in TD.NET
and five times slower in the console according to these tests. It's also interesting
to note that <a href="http://www.typemock.com/">TypeMock</a> is faster than <a href="http://code.google.com/p/moq/">Moq</a> in
TD.NET, but slower in the console runner.
</p>
        <p>
While running the Rhino.Mocks tests it is very clear that there is a degrading performance
issue. All the other frameworks executed tests with a near constant speed per test,
but Rhino.Mocks slowed down noticeably about half way through.
</p>
        <p>
Please feel free to try it yourself, grab the project <a href="http://blog.eleutian.com/download/BenchMock.zip">here</a>.
You should be able to just run the 4 strategy .bat files (run-rhino, run-moq, run-tmock-reflective,
run-tmock-natural). Let me know if you find anything interesting.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=96b7cc98-7d7d-4aae-8734-42347fcf8464" />
      </body>
      <title>Mock Framework Benchmarks</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,96b7cc98-7d7d-4aae-8734-42347fcf8464.aspx</guid>
      <link>http://blog.eleutian.com/2008/05/08/MockFrameworkBenchmarks.aspx</link>
      <pubDate>Thu, 08 May 2008 15:52:30 GMT</pubDate>
      <description>&lt;strong&gt;UPDATE: I tracked down the issue and committed a patch to Rhino.Mocks. Rhino.Mocks
is now much more competetive performance wise, our CI build time nearly halved, and
about 4 minutes out of 7 of our test time has disappeared. New numbers below.&lt;/strong&gt; 
&lt;p&gt;
I've complained before that &lt;a href="http://blog.eleutian.com/2007/12/15/MockingIsSlow.aspx"&gt;Mocking
is Slow&lt;/a&gt; but I never really dove further into it. Today I decided to actually compare
Rhino.Mocks to other mock frameworks on a pure performance basis to see if it was
a global problem. I timed 2000 unit tests across 100 classes with 20 tests each. The
results were a bit surprising:
&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;
Framework&lt;/th&gt;
&lt;th&gt;
&lt;a href="http://testdriven.net/"&gt;TD.NET&lt;/a&gt; Time&lt;/th&gt;
&lt;th&gt;
nunit-console Time&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino.Mocks&lt;/a&gt; old trunk&lt;/td&gt;
&lt;td&gt;
57.36s&lt;/td&gt;
&lt;td&gt;
28.82s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino.Mocks&lt;/a&gt; new trunk&lt;/td&gt;
&lt;td&gt;
22.94s&lt;/td&gt;
&lt;td&gt;
7.59s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; trunk&lt;/td&gt;
&lt;td&gt;
18.30s&lt;/td&gt;
&lt;td&gt;
5.91s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.typemock.com/"&gt;TypeMock&lt;/a&gt; 4.2.3 Reflective Mocks&lt;/td&gt;
&lt;td&gt;
15.36s&lt;/td&gt;
&lt;td&gt;
9.35s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.typemock.com/"&gt;TypeMock&lt;/a&gt; 4.2.3 Natural Mocks&lt;/td&gt;
&lt;td&gt;
16.92s&lt;/td&gt;
&lt;td&gt;
9.56s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
That's right, according to these tests, &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino.Mocks&lt;/a&gt; is
at least 3 times slower than the other frameworks when under heavy load in TD.NET
and five times slower in the console according to these tests. It's also interesting
to note that &lt;a href="http://www.typemock.com/"&gt;TypeMock&lt;/a&gt; is faster than &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; in
TD.NET, but slower in the console runner.
&lt;/p&gt;
&lt;p&gt;
While running the Rhino.Mocks tests it is very clear that there is a degrading performance
issue. All the other frameworks executed tests with a near constant speed per test,
but Rhino.Mocks slowed down noticeably about half way through.
&lt;/p&gt;
&lt;p&gt;
Please feel free to try it yourself, grab the project &lt;a href="http://blog.eleutian.com/download/BenchMock.zip"&gt;here&lt;/a&gt;.
You should be able to just run the 4 strategy .bat files (run-rhino, run-moq, run-tmock-reflective,
run-tmock-natural). Let me know if you find anything interesting.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=96b7cc98-7d7d-4aae-8734-42347fcf8464" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,96b7cc98-7d7d-4aae-8734-42347fcf8464.aspx</comments>
      <category>development</category>
      <category>frameworks</category>
      <category>mocking</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=2e4f933f-e00c-445e-ab64-17bf9a64d96f</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,2e4f933f-e00c-445e-ab64-17bf9a64d96f.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,2e4f933f-e00c-445e-ab64-17bf9a64d96f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2e4f933f-e00c-445e-ab64-17bf9a64d96f</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As some of you who follow <a href="http://twitter.com/aaronjensen">me on twitter</a> know,
I've been working on Yet Another Context/Specification Framework as an experiment.
Yeah, I know we already have <a href="http://nspec.tigris.org/">NSpec</a> and <a href="http://nbehave.org/">NBehave</a>,
and they're great and all, but MSpec takes things on from a slightly different angle,
and it's just an experiment (for now). Here's a sample Description:
</p>
        <pre name="code">[Description]
public class Transferring_between_from_account_and_to_account
{
  static Account fromAccount;
  static Account toAccount;

  Context before_each =()=&gt;
  {
    fromAccount = new Account {Balance = 1m};
    toAccount = new Account {Balance = 1m};
  };
  
  When the_transfer_is_made =()=&gt;
  {
    fromAccount.Transfer(1m, toAccount);
  };
   
  It should_debit_the_from_account_by_the_amount_transferred =()=&gt;
  {
    fromAccount.Balance.ShouldEqual(0m);
  };

  It should_credit_the_to_account_by_the_amount_transferred =()=&gt;
  {
    toAccount.Balance.ShouldEqual(2m);
  };
}
</pre>
        <p>
And a TestDriven.NET run:
</p>
        <pre>------ Test started: Assembly: Machine.Specifications.Example.dll ------

Transferring between from account and to account
  When the transfer is made
    * It should debit the from account by the amount transferred
    * It should credit the to account by the amount transferred


2 passed, 0 failed, 0 skipped, took 0.79 seconds.
</pre>
        <br />
        <h3>Err, What?
</h3>
        <p>
Different eh? The idea was heavily inspired by <a href="http://twitter.com/sbellware">Scott
Bellware</a>'s SpecUnit.Net framework he showed at the ALT.NET conference. It also
took heavy cues from <a href="http://rspec.info/">RSpec</a> and my insanity. I realize
that the the code doesn't look much like C# code and I'm OK with that. Many have and
will ask why I don't just use Boo or RSpec w/ IronRuby eventually or even one of the
existing Context/Spec/BDD frameworks. Those are good questions, but my main motivations
are tooling and syntax. I enjoy the tooling I get in C# and I personally like the
syntax in this library considering the limitations imposed by C#.
</p>
        <h3>How's it work?
</h3>
        <p>
The simplest way to describe it is to compare it to a normal *Unit style testing framework: 
</p>
        <ul>
          <li>
Description = TestContext 
</li>
          <li>
Context before_each = SetUp 
</li>
          <li>
Context before_all = SetUpFixture 
</li>
          <li>
Context after_each = TearDown 
</li>
          <li>
Context after_all = TearDownFixture 
</li>
          <li>
When = Also SetUp, but happens after Context before_each 
</li>
          <li>
It = Test 
</li>
        </ul>
        <p>
Rather than methods and attributes, MSpec uses named delegates and anonymous functions.
The only reason for this is readability. You'll also notice that the fields used in
the context are static. This is necessary so that the anonymous functions in the field
initializers can access them. Probably the first thing you noticed is the =()=&gt;
construct. I won't mention the names that this was given on twitter, but I think it's
an acceptable thing to have to deal with in exchange for the cleanliness of the rest
of the syntax.
</p>
        <h3>Ok, you're crazy, but how do I try it?
</h3>
        <p>
First, this is a very rough cut. Everything is subject to change as we experiment
with the language. That said, here's how you play with it:
</p>
        <ol>
          <li>
Grab the drop <a href="http://blog.eleutian.com/download/mspec-0.1.zip">here</a>. 
</li>
          <li>
Extract it somewhere. Put it somewhere semi-permanent because the <a href="http://testdriven.net/">TestDriven.NET</a> runner
will need a static location for the MSpec TDNet Runner. 
</li>
          <li>
If you want TestDriven.NET support, run <strong>InstallTDNetRunner.bat</strong></li>
          <li>
Check out the example in <strong>Machine.Specifications.Example</strong>. Note that
you can run with TD.NET. 
</li>
          <li>
Create a project of your own. Just add <strong>Machine.Specifications.dll</strong> and
get started. 
</li>
          <li>
Send me feedback! Leave comments, email me, <a href="http://twitter.com/aaronjensen">tweet
me</a>, whatever. 
</li>
        </ol>
        <p>
Also, this is part of <a href="http://www.assembla.com/wiki/show/machine">Machine</a>,
so feel free to take a <a href="http://svn2.assembla.com/svn/machine/trunk/">look
at the code</a> and/or submit patches. There's also a <a href="http://www.gallio.org/">Gallio</a> adapter
in there, but I didn't include it in the release as it's not quite polished enough
yet. If you're interested in it, talk to me. Special thanks to <a href="http://twitter.com/sbellware">Scott
Bellware</a>, <a href="http://blog.bits-in-motion.com/">Jeff Brown</a> and <a href="http://weblogs.asp.net/nunitaddin/default.aspx">Jamie
Cansdale</a> for their help and support.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2e4f933f-e00c-445e-ab64-17bf9a64d96f" />
      </body>
      <title>Introducing Machine.Specifications (or MSpec for short)</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,2e4f933f-e00c-445e-ab64-17bf9a64d96f.aspx</guid>
      <link>http://blog.eleutian.com/2008/05/08/IntroducingMachineSpecificationsOrMSpecForShort.aspx</link>
      <pubDate>Thu, 08 May 2008 07:11:27 GMT</pubDate>
      <description>&lt;p&gt;
As some of you who follow &lt;a href="http://twitter.com/aaronjensen"&gt;me on twitter&lt;/a&gt; know,
I've been working on Yet Another Context/Specification Framework as an experiment.
Yeah, I know we already have &lt;a href="http://nspec.tigris.org/"&gt;NSpec&lt;/a&gt; and &lt;a href="http://nbehave.org/"&gt;NBehave&lt;/a&gt;,
and they're great and all, but MSpec takes things on from a slightly different angle,
and it's just an experiment (for now). Here's a sample Description:
&lt;/p&gt;
&lt;pre name="code"&gt;[Description]
public class Transferring_between_from_account_and_to_account
{
  static Account fromAccount;
  static Account toAccount;

  Context before_each =()=&amp;gt;
  {
    fromAccount = new Account {Balance = 1m};
    toAccount = new Account {Balance = 1m};
  };
  
  When the_transfer_is_made =()=&amp;gt;
  {
    fromAccount.Transfer(1m, toAccount);
  };
   
  It should_debit_the_from_account_by_the_amount_transferred =()=&amp;gt;
  {
    fromAccount.Balance.ShouldEqual(0m);
  };

  It should_credit_the_to_account_by_the_amount_transferred =()=&amp;gt;
  {
    toAccount.Balance.ShouldEqual(2m);
  };
}
&lt;/pre&gt;
&lt;p&gt;
And a TestDriven.NET run:
&lt;/p&gt;
&lt;pre&gt;------ Test started: Assembly: Machine.Specifications.Example.dll ------

Transferring between from account and to account
  When the transfer is made
    * It should debit the from account by the amount transferred
    * It should credit the to account by the amount transferred


2 passed, 0 failed, 0 skipped, took 0.79 seconds.
&lt;/pre&gt;
&lt;br&gt;
&lt;h3&gt;Err, What?
&lt;/h3&gt;
&lt;p&gt;
Different eh? The idea was heavily inspired by &lt;a href="http://twitter.com/sbellware"&gt;Scott
Bellware&lt;/a&gt;'s SpecUnit.Net framework he showed at the ALT.NET conference. It also
took heavy cues from &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt; and my insanity. I realize
that the the code doesn't look much like C# code and I'm OK with that. Many have and
will ask why I don't just use Boo or RSpec w/ IronRuby eventually or even one of the
existing Context/Spec/BDD frameworks. Those are good questions, but my main motivations
are tooling and syntax. I enjoy the tooling I get in C# and I personally like the
syntax in this library considering the limitations imposed by C#.
&lt;/p&gt;
&lt;h3&gt;How's it work?
&lt;/h3&gt;
&lt;p&gt;
The simplest way to describe it is to compare it to a normal *Unit style testing framework: 
&lt;ul&gt;
&lt;li&gt;
Description = TestContext 
&lt;li&gt;
Context before_each = SetUp 
&lt;li&gt;
Context before_all = SetUpFixture 
&lt;li&gt;
Context after_each = TearDown 
&lt;li&gt;
Context after_all = TearDownFixture 
&lt;li&gt;
When = Also SetUp, but happens after Context before_each 
&lt;li&gt;
It = Test 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Rather than methods and attributes, MSpec uses named delegates and anonymous functions.
The only reason for this is readability. You'll also notice that the fields used in
the context are static. This is necessary so that the anonymous functions in the field
initializers can access them. Probably the first thing you noticed is the =()=&amp;gt;
construct. I won't mention the names that this was given on twitter, but I think it's
an acceptable thing to have to deal with in exchange for the cleanliness of the rest
of the syntax.
&lt;/p&gt;
&lt;h3&gt;Ok, you're crazy, but how do I try it?
&lt;/h3&gt;
&lt;p&gt;
First, this is a very rough cut. Everything is subject to change as we experiment
with the language. That said, here's how you play with it:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Grab the drop &lt;a href="http://blog.eleutian.com/download/mspec-0.1.zip"&gt;here&lt;/a&gt;. 
&lt;li&gt;
Extract it somewhere. Put it somewhere semi-permanent because the &lt;a href="http://testdriven.net/"&gt;TestDriven.NET&lt;/a&gt; runner
will need a static location for the MSpec TDNet Runner. 
&lt;li&gt;
If you want TestDriven.NET support, run &lt;strong&gt;InstallTDNetRunner.bat&lt;/strong&gt; 
&lt;li&gt;
Check out the example in &lt;strong&gt;Machine.Specifications.Example&lt;/strong&gt;. Note that
you can run with TD.NET. 
&lt;li&gt;
Create a project of your own. Just add &lt;strong&gt;Machine.Specifications.dll&lt;/strong&gt; and
get started. 
&lt;li&gt;
Send me feedback! Leave comments, email me, &lt;a href="http://twitter.com/aaronjensen"&gt;tweet
me&lt;/a&gt;, whatever. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Also, this is part of &lt;a href="http://www.assembla.com/wiki/show/machine"&gt;Machine&lt;/a&gt;,
so feel free to take a &lt;a href="http://svn2.assembla.com/svn/machine/trunk/"&gt;look
at the code&lt;/a&gt; and/or submit patches. There's also a &lt;a href="http://www.gallio.org/"&gt;Gallio&lt;/a&gt; adapter
in there, but I didn't include it in the release as it's not quite polished enough
yet. If you're interested in it, talk to me. Special thanks to &lt;a href="http://twitter.com/sbellware"&gt;Scott
Bellware&lt;/a&gt;, &lt;a href="http://blog.bits-in-motion.com/"&gt;Jeff Brown&lt;/a&gt; and &lt;a href="http://weblogs.asp.net/nunitaddin/default.aspx"&gt;Jamie
Cansdale&lt;/a&gt; for their help and support.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2e4f933f-e00c-445e-ab64-17bf9a64d96f" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,2e4f933f-e00c-445e-ab64-17bf9a64d96f.aspx</comments>
      <category>development</category>
      <category>frameworks</category>
      <category>open source</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=40a71cf3-47c7-49ac-bb9c-ab9074adf0d1</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,40a71cf3-47c7-49ac-bb9c-ab9074adf0d1.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,40a71cf3-47c7-49ac-bb9c-ab9074adf0d1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=40a71cf3-47c7-49ac-bb9c-ab9074adf0d1</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Currently in Rhino.Mocks, making mocks fire events and ensuring that an event on your
SUT was fired are both awkward and verbose at best. Here is an example of both things
at once:
</p>
        <pre class="c-sharp" name="code">[Test]
public void ViewFiresBeginDrag_Always_FiresChangedEvent()
{
  IEventRaiser raiser;
  bool eventFired = false;
  using (Record)
  {
    The&lt;ICardView&gt;().BeginDrag += null;
    raiser = LastCall.IgnoreArguments().GetEventRaiser();

    Target.Changed += (x,y) =&gt; eventFired = true;
  }

  using (Playback)
  {
    raiser.Raise(The&lt;ICardView&gt;(), EventArgs.Empty);
    Assert.IsTrue(eventFired);
  }
}</pre>
        <p>
Nice eh? First, is there a better way to do either of these things that I'm missing?
Please tell me if so. Next, if not, what can we do to clean this up?
</p>
        <p>
Well, Ayende and I <a href="http://www.ayende.com/Blog/archive/2007/06/09/Rhino-Mocks-Events-Twisting-the-Syntax.aspx">discussed
this</a> in the past and Ayende spiked it and <a href="http://www.ayende.com/Blog/archive/2007/06/23/Natural-Event-Syntax-for-Rhino-Mocks.aspx">asked
for feedback</a>. The feedback was mixed and for one reason or another it never made
it into Rhino.Mocks that I know of.
</p>
        <p>
Well, today while I was working on <a href="http://svn2.assembla.com/svn/machine/trunk/Machine.Testing/">Machine.Testing</a> and
another side project, I decided to give something else a shot. What I ended up with
is this:
</p>
        <pre class="c-sharp" name="code">[Test]
public void ViewFiresBeginDrag_Always_FiresChangedEvent()
{
  using (Record)
  {
    PrimeEventFiringOn&lt;ICardView&gt;(x =&gt; x.BeginDrag += null);
    Target.Changed += NewEventFireExpectation&lt;EventHandler&gt;();
  }

  using (Playback)
  {
    FireEventOn&lt;ICardView&gt;(EventArgs.Empty);
  }
}</pre>
        <p>
Better, but I still don't think it's perfect. Also, it probably requires some explanation,
so let's pick it apart piece by piece:
</p>
        <pre class="c-sharp" name="code">    PrimeEventFiringOn&lt;ICardView&gt;(x =&gt; x.BeginDrag += null);</pre>
        <p>
=&gt; +=??? Ugly huh? I really wish we could just refer to an event somehow without
having to do +=/-=. At least I'm <a href="http://twitter.com/jbogard/statuses/788133552">not
using vb though</a>... Alas, we cannot access them easily so we're stuck hacking away
like this. So this particular method will basically get the mock or stub ICardView.BeginDrag
ready to be fired. This needs to be done during the record phase it seems. You can
only prime one event at a time per mock, so if you need to do more than one you can
revert to the normal Rhino.Mocks syntax.
</p>
        <pre class="c-sharp" name="code">    Target.Changed += NewEventFireExpectation&lt;EventHandler&gt;();</pre>
        <p>
This was a fun method. This method actually creates a new DynamicMethod in the signature
required by the event and creates a new delegate. The method tracks whether or not
it was fired, and at the end of the Playback phase in my fixture it will assert that
all of the events were actually fired.
</p>
        <pre class="c-sharp" name="code">    FireEventOn&lt;ICardView&gt;(EventArgs.Empty);</pre>
        <p>
This actually fires the event we set up in the Prime call. It can only be called after
you've primed an event.
</p>
        <p>
So, the whole thing is kind of "magic", but it's less code if you can accept the magic.
I think we can make things even better though, but it'd require changes to Rhino.Mocks
and it's time for bed so maybe Ayende can swing by the Eleutian office while in Seattle
and we can work on it. Here's what I'm thinking:
</p>
        <pre class="c-sharp" name="code">[Test]
public void ViewFiresBeginDrag_Always_FiresChangedEvent()
{
  using (Record)
  {
    Target.Changed += Mocks.CreateEventHandler&lt;EventHandler&gt;();
  }

  using (Playback)
  {
    EventRaiser.Raise(() =&gt; The<icardview>
().BeginDrag += null, The&lt;ICardView&gt;(), EventArgs.Empty); } }
</icardview></pre>
        <p>
There's probably more you can do with the CreateEventHandler syntax like add more
specific expectations, assert its not fired, assert that it's fired X times, etc.
The EventRaiser syntax is ugly, but it doesn't involve strings or the fire from the
right hand side like the syntax I mentioned at the beginning of the post.
</p>
        <p>
You can get the source <a href="http://svn2.assembla.com/svn/machine/trunk/Machine.Testing/">here</a>,
but I warn you it's first draft and there are hacky bits.
</p>
        <p>
Oh, and here's some example tests using the TestsFor fixture:
</p>
        <pre class="c-sharp" name="code">[TestFixture]
public class CardPresenterTests : TestsFor&lt;CardPresenter&gt;
{
  private Card _card;

  public override void SetupContainer()
  {
    Override&lt;ICardView&gt;(With.Stub);
  }

  public override void BeforeEachTest()
  {
    _card = new Card(0);
  }

  [Test]
  public void OnBeginDrag_Always_SetsIsInFluxToTrue()
  {
    using (Record)
    {
      PrimeEventFiringOn&lt;ICardView&gt;(x =&gt; x.BeginDrag += null);
    }

    using (Playback)
    {
      FireEventOn<icardview>
(EventArgs.Empty); Assert.That(The&lt;ICardView&gt;().IsInFlux, Is.True); } } [Test]
public void OnBeginDrag_Always_FiresChangedEvent() { using (Record) { PrimeEventFiringOn&lt;ICardView&gt;(x
=&gt; x.BeginDrag += null); Target.Changed += NewEventFireExpectation&lt;EventHandler&gt;();
} using (Playback) { FireEventOn&lt;ICardView&gt;(EventArgs.Empty); } } }
</icardview></pre>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=40a71cf3-47c7-49ac-bb9c-ab9074adf0d1" />
      </body>
      <title>Mocking Events</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,40a71cf3-47c7-49ac-bb9c-ab9074adf0d1.aspx</guid>
      <link>http://blog.eleutian.com/2008/04/13/MockingEvents.aspx</link>
      <pubDate>Sun, 13 Apr 2008 08:30:10 GMT</pubDate>
      <description>&lt;p&gt;
Currently in Rhino.Mocks, making mocks fire events and ensuring that an event on your
SUT was fired are both awkward and verbose at best. Here is an example of both things
at once:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[Test]
public void ViewFiresBeginDrag_Always_FiresChangedEvent()
{
  IEventRaiser raiser;
  bool eventFired = false;
  using (Record)
  {
    The&amp;lt;ICardView&amp;gt;().BeginDrag += null;
    raiser = LastCall.IgnoreArguments().GetEventRaiser();

    Target.Changed += (x,y) =&amp;gt; eventFired = true;
  }

  using (Playback)
  {
    raiser.Raise(The&amp;lt;ICardView&amp;gt;(), EventArgs.Empty);
    Assert.IsTrue(eventFired);
  }
}&lt;/pre&gt;
&lt;p&gt;
Nice eh? First, is there a better way to do either of these things that I'm missing?
Please tell me if so. Next, if not, what can we do to clean this up?
&lt;/p&gt;
&lt;p&gt;
Well, Ayende and I &lt;a href="http://www.ayende.com/Blog/archive/2007/06/09/Rhino-Mocks-Events-Twisting-the-Syntax.aspx"&gt;discussed
this&lt;/a&gt; in the past and Ayende spiked it and &lt;a href="http://www.ayende.com/Blog/archive/2007/06/23/Natural-Event-Syntax-for-Rhino-Mocks.aspx"&gt;asked
for feedback&lt;/a&gt;. The feedback was mixed and for one reason or another it never made
it into Rhino.Mocks that I know of.
&lt;/p&gt;
&lt;p&gt;
Well, today while I was working on &lt;a href="http://svn2.assembla.com/svn/machine/trunk/Machine.Testing/"&gt;Machine.Testing&lt;/a&gt; and
another side project, I decided to give something else a shot. What I ended up with
is this:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[Test]
public void ViewFiresBeginDrag_Always_FiresChangedEvent()
{
  using (Record)
  {
    PrimeEventFiringOn&amp;lt;ICardView&amp;gt;(x =&amp;gt; x.BeginDrag += null);
    Target.Changed += NewEventFireExpectation&amp;lt;EventHandler&amp;gt;();
  }

  using (Playback)
  {
    FireEventOn&amp;lt;ICardView&amp;gt;(EventArgs.Empty);
  }
}&lt;/pre&gt;
&lt;p&gt;
Better, but I still don't think it's perfect. Also, it probably requires some explanation,
so let's pick it apart piece by piece:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;    PrimeEventFiringOn&amp;lt;ICardView&amp;gt;(x =&amp;gt; x.BeginDrag += null);&lt;/pre&gt;
&lt;p&gt;
=&amp;gt; +=??? Ugly huh? I really wish we could just refer to an event somehow without
having to do +=/-=. At least I'm &lt;a href="http://twitter.com/jbogard/statuses/788133552"&gt;not
using vb though&lt;/a&gt;... Alas, we cannot access them easily so we're stuck hacking away
like this. So this particular method will basically get the mock or stub ICardView.BeginDrag
ready to be fired. This needs to be done during the record phase it seems. You can
only prime one event at a time per mock, so if you need to do more than one you can
revert to the normal Rhino.Mocks syntax.
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;    Target.Changed += NewEventFireExpectation&amp;lt;EventHandler&amp;gt;();&lt;/pre&gt;
&lt;p&gt;
This was a fun method. This method actually creates a new DynamicMethod in the signature
required by the event and creates a new delegate. The method tracks whether or not
it was fired, and at the end of the Playback phase in my fixture it will assert that
all of the events were actually fired.
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;    FireEventOn&amp;lt;ICardView&amp;gt;(EventArgs.Empty);&lt;/pre&gt;
&lt;p&gt;
This actually fires the event we set up in the Prime call. It can only be called after
you've primed an event.
&lt;/p&gt;
&lt;p&gt;
So, the whole thing is kind of "magic", but it's less code if you can accept the magic.
I think we can make things even better though, but it'd require changes to Rhino.Mocks
and it's time for bed so maybe Ayende can swing by the Eleutian office while in Seattle
and we can work on it. Here's what I'm thinking:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[Test]
public void ViewFiresBeginDrag_Always_FiresChangedEvent()
{
  using (Record)
  {
    Target.Changed += Mocks.CreateEventHandler&amp;lt;EventHandler&amp;gt;();
  }

  using (Playback)
  {
    EventRaiser.Raise(() =&amp;gt; The&lt;icardview&gt;
().BeginDrag += null, The&amp;lt;ICardView&amp;gt;(), EventArgs.Empty); } }
&lt;/pre&gt;
&lt;p&gt;
There's probably more you can do with the CreateEventHandler syntax like add more
specific expectations, assert its not fired, assert that it's fired X times, etc.
The EventRaiser syntax is ugly, but it doesn't involve strings or the fire from the
right hand side like the syntax I mentioned at the beginning of the post.
&lt;/p&gt;
&lt;p&gt;
You can get the source &lt;a href="http://svn2.assembla.com/svn/machine/trunk/Machine.Testing/"&gt;here&lt;/a&gt;,
but I warn you it's first draft and there are hacky bits.
&lt;/p&gt;
&lt;p&gt;
Oh, and here's some example tests using the TestsFor fixture:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[TestFixture]
public class CardPresenterTests : TestsFor&amp;lt;CardPresenter&amp;gt;
{
  private Card _card;

  public override void SetupContainer()
  {
    Override&amp;lt;ICardView&amp;gt;(With.Stub);
  }

  public override void BeforeEachTest()
  {
    _card = new Card(0);
  }

  [Test]
  public void OnBeginDrag_Always_SetsIsInFluxToTrue()
  {
    using (Record)
    {
      PrimeEventFiringOn&amp;lt;ICardView&amp;gt;(x =&amp;gt; x.BeginDrag += null);
    }

    using (Playback)
    {
      FireEventOn&lt;icardview&gt;
(EventArgs.Empty); Assert.That(The&amp;lt;ICardView&amp;gt;().IsInFlux, Is.True); } } [Test]
public void OnBeginDrag_Always_FiresChangedEvent() { using (Record) { PrimeEventFiringOn&amp;lt;ICardView&amp;gt;(x
=&amp;gt; x.BeginDrag += null); Target.Changed += NewEventFireExpectation&amp;lt;EventHandler&amp;gt;();
} using (Playback) { FireEventOn&amp;lt;ICardView&amp;gt;(EventArgs.Empty); } } }
&lt;/pre&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=40a71cf3-47c7-49ac-bb9c-ab9074adf0d1" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,40a71cf3-47c7-49ac-bb9c-ab9074adf0d1.aspx</comments>
      <category>development</category>
      <category>frameworks</category>
      <category>mocking</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=36e031ad-2d2a-4d3a-b509-51bd7a507d1c</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,36e031ad-2d2a-4d3a-b509-51bd7a507d1c.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,36e031ad-2d2a-4d3a-b509-51bd7a507d1c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=36e031ad-2d2a-4d3a-b509-51bd7a507d1c</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.clariusconsulting.net/kzu">Daniel Cazzulino</a>, author of Moq
posted a good comment on my last post where I suggested looking into a Mockito like
syntax for .NET Mock Frameworks.
</p>
        <p>
          <em>On the surface, Mockito's approach seems good. But if you do the "true" comparison,
you'll see that stub(...) is exactly the same as mock.Expect(...) in Moq.</em>
        </p>
        <p>
          <em>Then, when you do verify(...), you have to basically repeat the exact same expression
you put in stub(...). This might work if you only have a couple calls to verify, but
for anything else, it will be a lot of repeated code, I'm afraid. </em>
        </p>
        <p>
I thought this too. See my comment <a href="http://monkeyisland.pl/2008/02/24/can-i-test-what-i-want-please/#comments">here</a> from
a month ago. <a href="http://monkeyisland.pl/">Szczepan</a> made a good point and
I've thought about it more since then.
</p>
        <p>
When combined with my position on loose vs strict mocks (almost always use loose),
I'd say that *most* of the time you are either stubbing or verifying. Meaning, if
you're verifying you don't need to stub unless of course that method returns something
that is critical to the flow of your test, in which case you don't really need to
verify, because the flow would have verified. That's a mouthful, but does that make
sense?
</p>
        <p>
I haven't used mockito, and I know there are times I use Expect.Call with return values
that matter (which would essentially require you to duplicate stub &amp; verify),
but maybe that's a smell? Maybe if you think you need that you can do state based
testing or change your API?
</p>
        <p>
Here's an example Test using Rhino.Mocks:
</p>
        <pre class="c-sharp" name="code">[Test]
public void SomeMethod_Always_CallsSendMail()
{
  IMailSender sender = mocks.DynamicMock<imailsender>
(); UnderTest underTest = new UnderTest(sender); using (mocks.Record()) { Expect.Call(sender.SendMail()).Returns(true);
} underTest.SomeMethod(); mocks.Verify(sender); }
</imailsender></pre>
        <p>
And some code this is testing (obviously not test driven, but you get the idea):
</p>
        <pre class="c-sharp" name="code">public void SomeMethod()
{
  if (!_sender.SendMail())
  {
    throw new Exception("OH NOS");
  }
}</pre>
        <p>
Notice that here we would need to stub and verify separately with Mockito like syntax.
This would look something like this:
</p>
        <pre class="c-sharp" name="code">[Test]
public void SomeMethod_Always_CallsSendMail()
{
  IMailSender sender = mocks.DynamicMock<imailsender>
(); UnderTest underTest = new UnderTest(sender); Stub.That(() =&gt; sender.SendMail()).Returns(true);
underTest.SomeMethod(); Verify.That(() =&gt; sender.SendMail()).WasCalled(); }
</imailsender></pre>
        <p>
This may violate DRY, but what if you designed your API differently? Maybe SendMail
should throw an exception on failure instead of returning a boolean? This would make
the return value unnecessary and remove the need for the Stub call. Clearly you can't
always do this, especially with unwrapped legacy or API code, but it's something to
think about.
</p>
        <p>
Also, I think you shouldn't be verifying more than one method generally to go along
with the one assert/test rule, so a single repeat would not be that horrendous. Heck,
you could even do:
</p>
        <pre class="c-sharp" name="code">[Test]
public void SomeMethod_Always_CallsSendMail()
{
  IMailSender sender = mocks.DynamicMock<imailsender>
(); UnderTest underTest = new UnderTest(sender); Stub.That(var sendMail = () =&gt;
sender.SendMail()).Returns(true); underTest.SomeMethod(); Verify.That(sendMail).WasCalled();
}
</imailsender></pre>
        <p>
I think the syntax would lead to better, more concise tests. But maybe it would just
be too annoying? I wouldn't know until I tried it for a while I guess.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=36e031ad-2d2a-4d3a-b509-51bd7a507d1c" />
      </body>
      <title>Separate Stub and Verify != Duplicate code necessarily</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,36e031ad-2d2a-4d3a-b509-51bd7a507d1c.aspx</guid>
      <link>http://blog.eleutian.com/2008/04/03/SeparateStubAndVerifyDuplicateCodeNecessarily.aspx</link>
      <pubDate>Thu, 03 Apr 2008 16:44:29 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.clariusconsulting.net/kzu"&gt;Daniel Cazzulino&lt;/a&gt;, author of Moq
posted a good comment on my last post where I suggested looking into a Mockito like
syntax for .NET Mock Frameworks.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;On the surface, Mockito's approach seems good. But if you do the "true" comparison,
you'll see that stub(...) is exactly the same as mock.Expect(...) in Moq.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Then, when you do verify(...), you have to basically repeat the exact same expression
you put in stub(...). This might work if you only have a couple calls to verify, but
for anything else, it will be a lot of repeated code, I'm afraid. &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I thought this too. See my comment &lt;a href="http://monkeyisland.pl/2008/02/24/can-i-test-what-i-want-please/#comments"&gt;here&lt;/a&gt; from
a month ago. &lt;a href="http://monkeyisland.pl/"&gt;Szczepan&lt;/a&gt; made a good point and
I've thought about it more since then.
&lt;/p&gt;
&lt;p&gt;
When combined with my position on loose vs strict mocks (almost always use loose),
I'd say that *most* of the time you are either stubbing or verifying. Meaning, if
you're verifying you don't need to stub unless of course that method returns something
that is critical to the flow of your test, in which case you don't really need to
verify, because the flow would have verified. That's a mouthful, but does that make
sense?
&lt;/p&gt;
&lt;p&gt;
I haven't used mockito, and I know there are times I use Expect.Call with return values
that matter (which would essentially require you to duplicate stub &amp;amp; verify),
but maybe that's a smell? Maybe if you think you need that you can do state based
testing or change your API?
&lt;/p&gt;
&lt;p&gt;
Here's an example Test using Rhino.Mocks:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[Test]
public void SomeMethod_Always_CallsSendMail()
{
  IMailSender sender = mocks.DynamicMock&lt;imailsender&gt;
(); UnderTest underTest = new UnderTest(sender); using (mocks.Record()) { Expect.Call(sender.SendMail()).Returns(true);
} underTest.SomeMethod(); mocks.Verify(sender); }
&lt;/pre&gt;
&lt;p&gt;
And some code this is testing (obviously not test driven, but you get the idea):
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;public void SomeMethod()
{
  if (!_sender.SendMail())
  {
    throw new Exception("OH NOS");
  }
}&lt;/pre&gt;
&lt;p&gt;
Notice that here we would need to stub and verify separately with Mockito like syntax.
This would look something like this:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[Test]
public void SomeMethod_Always_CallsSendMail()
{
  IMailSender sender = mocks.DynamicMock&lt;imailsender&gt;
(); UnderTest underTest = new UnderTest(sender); Stub.That(() =&amp;gt; sender.SendMail()).Returns(true);
underTest.SomeMethod(); Verify.That(() =&amp;gt; sender.SendMail()).WasCalled(); }
&lt;/pre&gt;
&lt;p&gt;
This may violate DRY, but what if you designed your API differently? Maybe SendMail
should throw an exception on failure instead of returning a boolean? This would make
the return value unnecessary and remove the need for the Stub call. Clearly you can't
always do this, especially with unwrapped legacy or API code, but it's something to
think about.
&lt;/p&gt;
&lt;p&gt;
Also, I think you shouldn't be verifying more than one method generally to go along
with the one assert/test rule, so a single repeat would not be that horrendous. Heck,
you could even do:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;[Test]
public void SomeMethod_Always_CallsSendMail()
{
  IMailSender sender = mocks.DynamicMock&lt;imailsender&gt;
(); UnderTest underTest = new UnderTest(sender); Stub.That(var sendMail = () =&amp;gt;
sender.SendMail()).Returns(true); underTest.SomeMethod(); Verify.That(sendMail).WasCalled();
}
&lt;/pre&gt;
&lt;p&gt;
I think the syntax would lead to better, more concise tests. But maybe it would just
be too annoying? I wouldn't know until I tried it for a while I guess.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=36e031ad-2d2a-4d3a-b509-51bd7a507d1c" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,36e031ad-2d2a-4d3a-b509-51bd7a507d1c.aspx</comments>
      <category>development</category>
      <category>frameworks</category>
      <category>mocking</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <em>(Note: I'm going to speak about .NET mock projects here for the most part, but
most of them have Java quasi-equivalents.) </em>
        </p>
        <p>
The original mocking frameworks like <a href="http://nmock.org/">NMock</a> required
you to setup expectations by passing strings for method names. This was fragile and
made refactoring more difficult. 
</p>
        <p>
A few mock frameworks now allow you to define expectations and mock results in a strongly
typed manner. <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a> and <a href="http://www.typemock.com/">TypeMock</a> use
a record/replay method to setup expectations. The record replay method is mostly necessary
because the same calls are made on the same objects under two different scenarios.
This leads to a few issues. 
</p>
        <p>
The first issue is confusion and barrier to entry. Many people have complained that
the Record/Replay method is not straight forward and the whole paradigm is confusing.
There are also complains about the naming, are you really recording and then replaying?
It's just kind of a strange thing. Of course most of us learn to live with it, understand
it, and accept it for what it is. Recently though, a few mock frameworks have popped
up that do away with this model.
</p>
        <p>
In the .NET world we have <a href="http://code.google.com/p/moq/">Moq</a>. Moq gets
rid of the need for record/replay because recordings have a very different syntax.
They use lambdas instead of actual calls to the mock object. This allows the framework
to know when you are recording an expectation and when you are fulfilling an expectation.
It adds a bit of noise in the form of "() =&gt;" but all in all it's not bad. Of course
this requires C# 3.0, but it's good to keep looking ahead. 
</p>
        <p>
In the Java world we have <a href="http://code.google.com/p/mockito/">Mockito</a>.
Mockito also does away with the record/replay model but it does it in a different
way. At first I wasn't a fan, but thinking about it more, I like it. Mockito has two
main apis, stub and verify. Stub is equivalent to SetupResult.For, and verify is equivalent
to Expect.Call with a verify. The interesting bit is that the stubbing happens before
the the class under test is invoked and the verifying (which includes describing the
method to be verified) happens after the class under test is invoked. This is best
shown with an example stolen from the Mockito site:
</p>
        <pre class="java" name="code">  
  //stubbing using built-in anyInt() argument matcher
  stub(mockedList.get(anyInt())).toReturn("element");
  
  //stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher):
  stub(mockedList.contains(argThat(isValid()))).toReturn("element");
  
  //following prints "element"
  System.out.println(mockedList.get(999));
  
  //you can also verify using argument matcher
  verify(mockedList).get(anyInt());</pre>
        <p>
Obviously it would take a bit of imagination to arrive at a .NET equivalent, but you
get the idea. I like this because the normal test structure is Setup Stuff-&gt;Do
Stuff to Test-&gt;Verify Stuff did what it should have. The normal record/replay model
requires you to set up verifications before you actually Do Stuff (though you call
VerifyAll afterwards). This is a bit less natural. I feel syntax like this (yeah,
I like the new NUnit syntax) would be more intention revealing:
</p>
        <pre class="java" name="code">Assert.That(() =&gt; someMock.Foo(), Was.Called);</pre>
        <p>
Or:
</p>
        <pre class="java" name="code">Verify.That(() =&gt; someMock.Foo()).WasCalled();</pre>
        <p>
Then you would stub like this:
</p>
        <pre class="java" name="code">Stub.That(() =&gt; someMock.Bar()).Returns(3);</pre>
        <p>
          <strong>Note: No idea if this is feasible or makes sense or not, my lambda experience
is limited to light reading, but you get the idea. I'm sure the syntax could also
be prettier.</strong>
        </p>
        <p>
Rhino.Mocks is my current mock framework of choice. I'm used to it, I've lightly contributed
to it, and I've been working with it for a while now. Despite that, I do think that
there is definitely more to explore in the mocking arena especially with C# 3.0.
</p>
        <p>
There are lots of other fun things to talk about too... like <a href="http://www.typemock.com/">TypeMock</a>'s
magic, but that's another day still...
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24" />
      </body>
      <title>The Past, Present and Future (?) of Mocking</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24.aspx</guid>
      <link>http://blog.eleutian.com/2008/04/03/ThePastPresentAndFutureOfMocking.aspx</link>
      <pubDate>Thu, 03 Apr 2008 04:13:18 GMT</pubDate>
      <description>&lt;p&gt;
&lt;em&gt;(Note: I'm going to speak about .NET mock projects here for the most part, but
most of them have Java quasi-equivalents.) &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
The original mocking frameworks like &lt;a href="http://nmock.org/"&gt;NMock&lt;/a&gt; required
you to setup expectations by passing strings for method names. This was fragile and
made refactoring more difficult. 
&lt;/p&gt;
&lt;p&gt;
A few mock frameworks now allow you to define expectations and mock results in a strongly
typed manner. &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino Mocks&lt;/a&gt; and &lt;a href="http://www.typemock.com/"&gt;TypeMock&lt;/a&gt; use
a record/replay method to setup expectations. The record replay method is mostly necessary
because the same calls are made on the same objects under two different scenarios.
This leads to a few issues. 
&lt;/p&gt;
&lt;p&gt;
The first issue is confusion and barrier to entry. Many people have complained that
the Record/Replay method is not straight forward and the whole paradigm is confusing.
There are also complains about the naming, are you really recording and then replaying?
It's just kind of a strange thing. Of course most of us learn to live with it, understand
it, and accept it for what it is. Recently though, a few mock frameworks have popped
up that do away with this model.
&lt;/p&gt;
&lt;p&gt;
In the .NET world we have &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt;. Moq gets
rid of the need for record/replay because recordings have a very different syntax.
They use lambdas instead of actual calls to the mock object. This allows the framework
to know when you are recording an expectation and when you are fulfilling an expectation.
It adds a bit of noise in the form of "() =&amp;gt;" but all in all it's not bad. Of course
this requires C# 3.0, but it's good to keep looking ahead. 
&lt;/p&gt;
&lt;p&gt;
In the Java world we have &lt;a href="http://code.google.com/p/mockito/"&gt;Mockito&lt;/a&gt;.
Mockito also does away with the record/replay model but it does it in a different
way. At first I wasn't a fan, but thinking about it more, I like it. Mockito has two
main apis, stub and verify. Stub is equivalent to SetupResult.For, and verify is equivalent
to Expect.Call with a verify. The interesting bit is that the stubbing happens before
the the class under test is invoked and the verifying (which includes describing the
method to be verified) happens after the class under test is invoked. This is best
shown with an example stolen from the Mockito site:
&lt;/p&gt;
&lt;pre class="java" name="code"&gt;  
  //stubbing using built-in anyInt() argument matcher
  stub(mockedList.get(anyInt())).toReturn("element");
  
  //stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher):
  stub(mockedList.contains(argThat(isValid()))).toReturn("element");
  
  //following prints "element"
  System.out.println(mockedList.get(999));
  
  //you can also verify using argument matcher
  verify(mockedList).get(anyInt());&lt;/pre&gt;
&lt;p&gt;
Obviously it would take a bit of imagination to arrive at a .NET equivalent, but you
get the idea. I like this because the normal test structure is Setup Stuff-&amp;gt;Do
Stuff to Test-&amp;gt;Verify Stuff did what it should have. The normal record/replay model
requires you to set up verifications before you actually Do Stuff (though you call
VerifyAll afterwards). This is a bit less natural. I feel syntax like this (yeah,
I like the new NUnit syntax) would be more intention revealing:
&lt;/p&gt;
&lt;pre class="java" name="code"&gt;Assert.That(() =&amp;gt; someMock.Foo(), Was.Called);&lt;/pre&gt;
&lt;p&gt;
Or:
&lt;/p&gt;
&lt;pre class="java" name="code"&gt;Verify.That(() =&amp;gt; someMock.Foo()).WasCalled();&lt;/pre&gt;
&lt;p&gt;
Then you would stub like this:
&lt;/p&gt;
&lt;pre class="java" name="code"&gt;Stub.That(() =&amp;gt; someMock.Bar()).Returns(3);&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;Note: No idea if this is feasible or makes sense or not, my lambda experience
is limited to light reading, but you get the idea. I'm sure the syntax could also
be prettier.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Rhino.Mocks is my current mock framework of choice. I'm used to it, I've lightly contributed
to it, and I've been working with it for a while now. Despite that, I do think that
there is definitely more to explore in the mocking arena especially with C# 3.0.
&lt;/p&gt;
&lt;p&gt;
There are lots of other fun things to talk about too... like &lt;a href="http://www.typemock.com/"&gt;TypeMock&lt;/a&gt;'s
magic, but that's another day still...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,48e6b33d-7fa0-4a4d-a441-4b3d5e21bd24.aspx</comments>
      <category>development</category>
      <category>frameworks</category>
      <category>mocking</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=3d1771d8-9a20-4080-a03e-e8525c00e1d1</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,3d1771d8-9a20-4080-a03e-e8525c00e1d1.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,3d1771d8-9a20-4080-a03e-e8525c00e1d1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=3d1771d8-9a20-4080-a03e-e8525c00e1d1</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://schambers.lostechies.com/">Sean Chambers</a> asked how we work with
a published branch so I figured I'd post on the topic since it's a somewhat interesting
one. It's not trivial and it took us a few tries to get it where it is now, and it's
still not quite right.
</p>
        <p>
The first step to using a published branch is to create the branch. You can do that
like this:
</p>
        <pre>svn cp https://yoursvnserver/svn/project/trunk \
https://yoursvnserver/svn/project/branches/published \
-m "Branching published"</pre>
        <p>
After that, we actually check out the whole branch. It's very useful to have both
the pubilshed branch and the trunk on one machine. Note that this isn't necessarily
trivial, and its feasibility entirely depends on your build. Having a location agnostic
build is a very important thing, and this is one of the reasons.
</p>
        <p>
Now we have both trunk and published branches. We <strong>almost</strong> always do
all of our work in trunk and then merge over to published. Jacob actually wrote a
PowerShell script to make the merges easier (from merge.ps1):
</p>
        <pre>param([int]$rev, [string]$reverse)

if (!$rev)
{
  echo "Merge.ps1 REV [REVERSE]"
  return;
}
if (!$reverse)
{
  $branch = "Published"
  $url = "https://yoursvnserver/svn/project/trunk"
}
else
{
  $branch = "Trunk"
  $url = "https://yoursvnserver/svn/project/published"
}

$previous = $rev - 1
$range = $previous.ToString() + ":" + $rev

pushd $branch
svn merge -r $range $url
popd</pre>
        <p>
The reason for doing most of the work in trunk is that often times any issues we run
into on the published site will still be issues in the trunk. It makes sense to apply
the work there first and then merge it over. The only time we patch published is when
we need to apply a hack to make something work so that we can fix it the right way
on the trunk later, or maybe that feature is completely different on the trunk and
the fix would not apply. Of course this is dangerous and you have to be sure to remember
to fix the underlying issue in the trunk before publishing from trunk again. Bug trackers
help with that.
</p>
        <p>
That brings us to publishing from trunk again. Merging everything from trunk into
the published branch is a giant pain, and just won't work if you've applied many hacks
to the published branch. I strongly advise against this. Instead, just start over:
</p>
        <pre>svn rm https://yoursvnserver/svn/project/branches/published \
-m "Deleteing published branch"
svn cp https://yoursvnserver/svn/project/trunk \
https://yoursvnserver/svn/project/branches/published \
-m "Branching published"</pre>
        <p>
By nuking it and recopying it you can just svn up on your published branch and you'll
have everything from trunk. For whatever reason, not removing it before copying it
again caused us issues. I'd recommend this two phase approach.
</p>
        <p>
Other things we learned in the course of this are the pros and cons of shared "stuff".
We have at least 10 gigs of course content and a few other resources that don't need
to be in the separate branches like our main trunk and published. We pulled those
into their own repositories and keep them in shared directories so both installs can
reference it. We also have a set of common build scripts between the two. This is
both a good and a bad thing. It's good because it removes some duplication and it
allows us to use a separate repo for these scripts (which is handy for some TC build
configurations that only need the scripts, though I guess we could just checkout the
scripts directory from trunk...) but it's bad because sometimes things will get out
of sync. We'll make changes to the shared scripts, and fix the trunk, but it doesn't
make sense or it's prohibitive to fix the published branch. You can see in the screen
shot I posted our published branch is currently failing. This is likely why. I'd probably
recommend keeping all build scripts branched so that you don't run into these sort
of issues.
</p>
        <p>
The next thing to worry about is the database. I mentioned that we use multiple databases
on the build server. What I didn't mention is that we also use multiple databases
on our dev machines. We usually have three. One for trunk, one for trunk tests and
one for published. The trunk tests db is imported "light" so our tests run
in 6 minutes instead of 14. The trunk db is a nearly full copy of the production db
so we have data when we poke around the site on our dev machines. We have a ConnectionStrings.config
that is generated from the database info you pass the build script. You can do something
like: msbuild Default.msbuild /p:DatabaseName=published and it'd build with the appropriate
connection string.
</p>
        <p>
For web applications you also have to worry about IIS. We have two web applications
configured in IIS. One for trunk and one for published. This allows us to easily switch
between the two by just changing our port or vdir in our url. They have multiple virtual
directories underneath them that point to our various shared directories.
</p>
        <p>
I think that's most of the tips I can think of right now. Let me know if you have
any questions about anything.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=3d1771d8-9a20-4080-a03e-e8525c00e1d1" />
      </body>
      <title>Working with a published branch</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,3d1771d8-9a20-4080-a03e-e8525c00e1d1.aspx</guid>
      <link>http://blog.eleutian.com/2008/03/08/WorkingWithAPublishedBranch.aspx</link>
      <pubDate>Sat, 08 Mar 2008 00:21:29 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://schambers.lostechies.com/"&gt;Sean Chambers&lt;/a&gt; asked how we work with
a published branch so I figured I'd post on the topic since it's a somewhat interesting
one. It's not trivial and it took us a few tries to get it where it is now, and it's
still not quite right.
&lt;/p&gt;
&lt;p&gt;
The first step to using a published branch is to create the branch. You can do that
like this:
&lt;/p&gt;
&lt;pre&gt;svn cp https://yoursvnserver/svn/project/trunk \
https://yoursvnserver/svn/project/branches/published \
-m &amp;quot;Branching published&amp;quot;&lt;/pre&gt;
&lt;p&gt;
After that, we actually check out the whole branch. It's very useful to have both
the pubilshed branch and the trunk on one machine. Note that this isn't necessarily
trivial, and its feasibility entirely depends on your build. Having a location agnostic
build is a very important thing, and this is one of the reasons.
&lt;/p&gt;
&lt;p&gt;
Now we have both trunk and published branches. We &lt;strong&gt;almost&lt;/strong&gt; always do
all of our work in trunk and then merge over to published. Jacob actually wrote a
PowerShell script to make the merges easier (from merge.ps1):
&lt;/p&gt;
&lt;pre&gt;param([int]$rev, [string]$reverse)

if (!$rev)
{
  echo &amp;quot;Merge.ps1 REV [REVERSE]&amp;quot;
  return;
}
if (!$reverse)
{
  $branch = &amp;quot;Published&amp;quot;
  $url = &amp;quot;https://yoursvnserver/svn/project/trunk&amp;quot;
}
else
{
  $branch = &amp;quot;Trunk&amp;quot;
  $url = &amp;quot;https://yoursvnserver/svn/project/published&amp;quot;
}

$previous = $rev - 1
$range = $previous.ToString() + &amp;quot;:&amp;quot; + $rev

pushd $branch
svn merge -r $range $url
popd&lt;/pre&gt;
&lt;p&gt;
The reason for doing most of the work in trunk is that often times any issues we run
into on the published site will still be issues in the trunk. It makes sense to apply
the work there first and then merge it over. The only time we patch published is when
we need to apply a hack to make something work so that we can fix it the right way
on the trunk later, or maybe that feature is completely different on the trunk and
the fix would not apply. Of course this is dangerous and you have to be sure to remember
to fix the underlying issue in the trunk before publishing from trunk again. Bug trackers
help with that.
&lt;/p&gt;
&lt;p&gt;
That brings us to publishing from trunk again. Merging everything from trunk into
the published branch is a giant pain, and just won't work if you've applied many hacks
to the published branch. I strongly advise against this. Instead, just start over:
&lt;/p&gt;
&lt;pre&gt;svn rm https://yoursvnserver/svn/project/branches/published \
-m &amp;quot;Deleteing published branch&amp;quot;
svn cp https://yoursvnserver/svn/project/trunk \
https://yoursvnserver/svn/project/branches/published \
-m &amp;quot;Branching published&amp;quot;&lt;/pre&gt;
&lt;p&gt;
By nuking it and recopying it you can just svn up on your published branch and you'll
have everything from trunk. For whatever reason, not removing it before copying it
again caused us issues. I'd recommend this two phase approach.
&lt;/p&gt;
&lt;p&gt;
Other things we learned in the course of this are the pros and cons of shared &amp;quot;stuff&amp;quot;.
We have at least 10 gigs of course content and a few other resources that don't need
to be in the separate branches like our main trunk and published. We pulled those
into their own repositories and keep them in shared directories so both installs can
reference it. We also have a set of common build scripts between the two. This is
both a good and a bad thing. It's good because it removes some duplication and it
allows us to use a separate repo for these scripts (which is handy for some TC build
configurations that only need the scripts, though I guess we could just checkout the
scripts directory from trunk...) but it's bad because sometimes things will get out
of sync. We'll make changes to the shared scripts, and fix the trunk, but it doesn't
make sense or it's prohibitive to fix the published branch. You can see in the screen
shot I posted our published branch is currently failing. This is likely why. I'd probably
recommend keeping all build scripts branched so that you don't run into these sort
of issues.
&lt;/p&gt;
&lt;p&gt;
The next thing to worry about is the database. I mentioned that we use multiple databases
on the build server. What I didn't mention is that we also use multiple databases
on our dev machines. We usually have three. One for trunk, one for trunk tests and
one for published. The trunk tests db is imported &amp;quot;light&amp;quot; so our tests run
in 6 minutes instead of 14. The trunk db is a nearly full copy of the production db
so we have data when we poke around the site on our dev machines. We have a ConnectionStrings.config
that is generated from the database info you pass the build script. You can do something
like: msbuild Default.msbuild /p:DatabaseName=published and it'd build with the appropriate
connection string.
&lt;/p&gt;
&lt;p&gt;
For web applications you also have to worry about IIS. We have two web applications
configured in IIS. One for trunk and one for published. This allows us to easily switch
between the two by just changing our port or vdir in our url. They have multiple virtual
directories underneath them that point to our various shared directories.
&lt;/p&gt;
&lt;p&gt;
I think that's most of the tips I can think of right now. Let me know if you have
any questions about anything.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=3d1771d8-9a20-4080-a03e-e8525c00e1d1" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,3d1771d8-9a20-4080-a03e-e8525c00e1d1.aspx</comments>
      <category>continuous integration</category>
      <category>development</category>
      <category>teamcity</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=4e3d3cae-f228-4bab-9a2b-261d6993b1e2</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,4e3d3cae-f228-4bab-9a2b-261d6993b1e2.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,4e3d3cae-f228-4bab-9a2b-261d6993b1e2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=4e3d3cae-f228-4bab-9a2b-261d6993b1e2</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/TeamCityrocks_AB04/projects_2.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="projects" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/TeamCityrocks_AB04/projects_thumb.png" width="155" align="right" border="0" />
          </a>I've <a href="http://blog.eleutian.com/Default.aspx#a59f6c9cd-c0de-4bc4-8235-e5838734b1cb">mentioned
before</a> how much I like <a href="http://www.jetbrains.com/teamcity/">TeamCity</a>,
but I didn't really talk about how we use it. Just recently Jacob completed some work
on multiple build configurations that make our life much easier. I thought I'd go
over them here to give you an idea of how we handle continuous integration here.
</p>
        <h4>Continuous Integration configurations
</h4>
        <ul>
          <li>
            <strong>CI - Trunk</strong> - This is the CI configuration everyone has. This watches
the trunk of our SVN repository and builds whenever it changes it will also run our
database migrations against the CI trunk database. It has all our built assemblies
as artifacts. The artifacts take up a lot of space so we don't keep them around that
long. 
</li>
          <li>
            <strong>CI - Published</strong> - This is just like CI - Trunk, but it watches our
published branch, which is where we put everything that we're about to publish to
the live site. We keep two branches so that we can make quick fixes to the published
site without having to publish new features we're working on. This has its own database
that is migrated. 
</li>
          <li>
            <strong>Nightly - Trunk </strong>- This runs daily rather than watching our source
control. It migrates a database on our production database server that is a copy of
our live database. It also builds and deploys the trunk to a test address on our production
servers. This allows our team in Korea and our stakeholders to see changes every day
in a safe environment. The Nightly is also a big part of our localization story, which
I'll save for another post.</li>
        </ul>
        <h4>Database build configurations
</h4>
        <ul>
          <li>
            <strong>Snapshot </strong>- This and the other db build configurations probably deserve
their own post with more details, but I'll do my best to explain these briefly. The
Snapshot build configuration takes a point in time snapshot of the database and packages
it into a zip file. The zip file becomes a TeamCity artifact that other projects can
depend on. 
</li>
          <li>
            <strong>Nightly/CI Trunk/CI Published Baseline</strong> - 
<p>
These configurations import database snapshots into the database they refer to. The
only time we need to do this is if a bad migration runs, or we want to "refresh" the
data inside the database.
</p><p>
It is important to note that we do not run CI - Trunk on a complete snapshot of the
live database. When we do, it greatly increases the build time because our integration
tests run significantly slower in a real database. Instead, we import a "light" database
which contains all of the tables, but only the data from our static tables. The users,
records, and anything else that grows as we get more and more users are just left
empty. This means that we have zero sample data for these things during our integration
tests, so we rely heavily on <a href="http://blog.eleutian.com/2007/09/29/FluentFixtures.aspx">Fluent
Fixtures</a> to set up sample data. 
</p><p>
The other two databases do run nearly complete copies of the live database (we exclude
log tables basically), so we still get to test our migrations and our site on real
data.
</p></li>
        </ul>
        <h4>Utility build configurations
</h4>
        <ul>
          <li>
            <strong>StatSVN</strong> - Runs <a href="http://www.statsvn.org/">StatSVN</a> on our
codebase. Somewhat useful source statistics. Mostly use it to see growth and churn. 
</li>
          <li>
            <strong>Duplicate Finder </strong>- Haven't really done this much to be honest, but
TeamCity has <a href="http://blogs.jetbrains.com/teamcity/2007/09/14/code-duplicates-search/">build
configurations</a> whose sole purpose is finding duplicate code.</li>
        </ul>
        <p>
I'm completely enamored with this set of configurations. It makes so many things so
painless. We still have room for improvement, managing all of the configuration differences
in the sites is difficult. We also lack a one click live publish ability. We still
follow a manual script for that, which is error prone and dangerous. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=4e3d3cae-f228-4bab-9a2b-261d6993b1e2" />
      </body>
      <title>Our build configurations</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,4e3d3cae-f228-4bab-9a2b-261d6993b1e2.aspx</guid>
      <link>http://blog.eleutian.com/2008/03/07/OurBuildConfigurations.aspx</link>
      <pubDate>Fri, 07 Mar 2008 14:40:24 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/TeamCityrocks_AB04/projects_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="projects" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/TeamCityrocks_AB04/projects_thumb.png" width="155" align="right" border="0"&gt;&lt;/a&gt;I've &lt;a href="http://blog.eleutian.com/Default.aspx#a59f6c9cd-c0de-4bc4-8235-e5838734b1cb"&gt;mentioned
before&lt;/a&gt; how much I like &lt;a href="http://www.jetbrains.com/teamcity/"&gt;TeamCity&lt;/a&gt;,
but I didn't really talk about how we use it. Just recently Jacob completed some work
on multiple build configurations that make our life much easier. I thought I'd go
over them here to give you an idea of how we handle continuous integration here.
&lt;/p&gt;
&lt;h4&gt;Continuous Integration configurations
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CI - Trunk&lt;/strong&gt; - This is the CI configuration everyone has. This watches
the trunk of our SVN repository and builds whenever it changes it will also run our
database migrations against the CI trunk database. It has all our built assemblies
as artifacts. The artifacts take up a lot of space so we don't keep them around that
long. 
&lt;li&gt;
&lt;strong&gt;CI - Published&lt;/strong&gt; - This is just like CI - Trunk, but it watches our
published branch, which is where we put everything that we're about to publish to
the live site. We keep two branches so that we can make quick fixes to the published
site without having to publish new features we're working on. This has its own database
that is migrated. 
&lt;li&gt;
&lt;strong&gt;Nightly - Trunk &lt;/strong&gt;- This runs daily rather than watching our source
control. It migrates a database on our production database server that is a copy of
our live database. It also builds and deploys the trunk to a test address on our production
servers. This allows our team in Korea and our stakeholders to see changes every day
in a safe environment. The Nightly is also a big part of our localization story, which
I'll save for another post.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Database build configurations
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Snapshot &lt;/strong&gt;- This and the other db build configurations probably deserve
their own post with more details, but I'll do my best to explain these briefly. The
Snapshot build configuration takes a point in time snapshot of the database and packages
it into a zip file. The zip file becomes a TeamCity artifact that other projects can
depend on. 
&lt;li&gt;
&lt;strong&gt;Nightly/CI Trunk/CI Published Baseline&lt;/strong&gt; - 
&lt;p&gt;
These configurations import database snapshots into the database they refer to. The
only time we need to do this is if a bad migration runs, or we want to "refresh" the
data inside the database.
&lt;/p&gt;
&lt;p&gt;
It is important to note that we do not run CI - Trunk on a complete snapshot of the
live database. When we do, it greatly increases the build time because our integration
tests run significantly slower in a real database. Instead, we import a "light" database
which contains all of the tables, but only the data from our static tables. The users,
records, and anything else that grows as we get more and more users are just left
empty. This means that we have zero sample data for these things during our integration
tests, so we rely heavily on &lt;a href="http://blog.eleutian.com/2007/09/29/FluentFixtures.aspx"&gt;Fluent
Fixtures&lt;/a&gt; to set up sample data. 
&lt;/p&gt;
&lt;p&gt;
The other two databases do run nearly complete copies of the live database (we exclude
log tables basically), so we still get to test our migrations and our site on real
data.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Utility build configurations
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;StatSVN&lt;/strong&gt; - Runs &lt;a href="http://www.statsvn.org/"&gt;StatSVN&lt;/a&gt; on our
codebase. Somewhat useful source statistics. Mostly use it to see growth and churn. 
&lt;li&gt;
&lt;strong&gt;Duplicate Finder &lt;/strong&gt;- Haven't really done this much to be honest, but
TeamCity has &lt;a href="http://blogs.jetbrains.com/teamcity/2007/09/14/code-duplicates-search/"&gt;build
configurations&lt;/a&gt; whose sole purpose is finding duplicate code.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
I'm completely enamored with this set of configurations. It makes so many things so
painless. We still have room for improvement, managing all of the configuration differences
in the sites is difficult. We also lack a one click live publish ability. We still
follow a manual script for that, which is error prone and dangerous. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=4e3d3cae-f228-4bab-9a2b-261d6993b1e2" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,4e3d3cae-f228-4bab-9a2b-261d6993b1e2.aspx</comments>
      <category>continuous integration</category>
      <category>development</category>
      <category>teamcity</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=bc476d8f-0e69-43af-bc61-4c22c5e8f9eb</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,bc476d8f-0e69-43af-bc61-4c22c5e8f9eb.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,bc476d8f-0e69-43af-bc61-4c22c5e8f9eb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=bc476d8f-0e69-43af-bc61-4c22c5e8f9eb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="210" alt="stack-bar-chart" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/AddingburnupchartstoMingleretroactively_15031/stack-bar-chart_3.png" width="244" align="left" border="0" />
        </p>
        <p>
With <a href="http://studios.thoughtworks.com/mingle-project-intelligence">Mingle</a> 1.1
came several great new features, including date fields. Now you can track the date
that you complete tickets by adding a Date Done field (along with a Complete transition
that sets the Date Done field to today) and locking the Status field to be editable
only by transition. 
</p>
        <p>
This works great for newly completed cards, but what about all the cards you already
have in your system? Sure you could go without burn-up charts and date stats for those
cards, but what's the fun in that?  
</p>
        <br style="clear: both" />
        <p>
          <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="120" alt="taskcomplete" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/AddingburnupchartstoMingleretroactively_15031/taskcomplete_3.jpg" width="604" border="0" />
        </p>
        <p>
Fortunately, Mingle tracks every card change in a history table. Each change in the
history table has a date associated with it, so all we need to do is figure out how
to extract the dates we care about. I must warn you, this is a little complicated,
and I'm sure ThoughtWorks doesn't at all condone you mucking around in your Mingle
database manually, so please please back up your database before attempting this and
of course I'm not responsible for any damage you do. That said, here's how I was able
to retroactively add dates for our two date fields: Date Done, and Date Published.
</p>
        <ol>
          <li>
Back up your Mingle DB. Really. 
</li>
          <li>
You'll need to identify the field you use to track status. We use Status. We have
Status of 'Not Done', 'Done', and 'Published'. 
</li>
          <li>
Then connect to your Mingle database and find the name of your cards table and your
card_versions table. Our project is called "eleutian_speakeng" so our tables are "eleutian_speakeng_cards"
and "eleutian_speakeng_card_versions". 
</li>
          <li>
Add the date fields you want to populate if you don't already have them. We have "Date
Added", "Date Published" and "Date Done". 
</li>
          <li>
Figure out which fields in the two tables you identified map to your status and dates
(this is usually as easy as prepending cp_ and box_caring your names. For us we have
cp_status, cp_date_published, cp_date_done, cp_date_added. 
</li>
          <li>
Edit the following queries to add in your table/field names and values (these are
the exact queries I ran, you'll need to change the red things): <pre>UPDATE <font color="#ff0000">eleutian_speakeng_cards</font> SET <font color="#ff0000">cp_date_added</font> =
created_at UPDATE <font color="#ff0000">eleutian_speakeng_cards</font> card SET card.<font color="#ff0000">cp_date_done</font> =
( SELECT MAX(b.updated_at) FROM <font color="#ff0000">eleutian_speakeng_card_versions</font> a
INNER JOIN <font color="#ff0000">eleutian_speakeng_card_versions</font> b ON a.number
= b.number AND b.version=a.version+1 WHERE card.number = a.number AND (a.<font color="#ff0000">cp_status</font>=<font color="#ff0000">'Not
Done'</font> OR a.<font color="#ff0000">cp_status</font> IS NULL) AND b.<font color="#ff0000">cp_status</font>=<font color="#ff0000">'Done'</font> )
WHERE card.<font color="#ff0000">cp_status</font> = <font color="#ff0000">'Done'</font> UPDATE <font color="#ff0000">eleutian_speakeng_cards</font> card
SET card.<font color="#ff0000">cp_date_published</font> = ( SELECT MAX(b.updated_at)
FROM <font color="#ff0000">eleutian_speakeng_card_versions</font> a INNER JOIN <font color="#ff0000">eleutian_speakeng_card_versions</font> b
ON a.number = b.number AND b.version=a.version+1 WHERE card.number = a.number AND
(a.<font color="#ff0000">cp_status</font> IN (<font color="#ff0000">'Done', 'Not Done'</font>)
OR a.<font color="#ff0000">cp_status</font> IS NULL) AND b.<font color="#ff0000">cp_status</font>=<font color="#ff0000">'Published'</font> )
WHERE card.<font color="#ff0000">cp_status</font> = <font color="#ff0000">'Published'</font></pre></li>
          <li>
Once you've got that taken care of, go ahead and run the queries (you backed up right?)
and you should be good to go.</li>
        </ol>
        <p>
So now that you've got your dates in your database, how do you get the cool burn up
chart? Like this (mold to your project):
</p>
        <pre>{{
   stack-bar-chart
   x-label-start: SELECT MIN('Date Added')
   x-label-end: Today
   x-label-step: 14
   chart-height: 600
   chart-width: 700
   plot-height: 500
   plot-width: 550
   conditions: Type IN (Bug, Story, Task)
   three-d: true
   cumulative: true
   series:
     - label: 'Not Done'
       color: #e6a800
       data: SELECT 'Date Added', COUNT(*)
       type: area
     - label: Done
       color: #1bcc00
       type: area
       data: SELECT 'Date Done', Count(*) WHERE 'Date Done' IS NOT NULL
     - label: Published
       color: #0079bf
       type: area
       data: SELECT 'Date Published', COUNT(*) WHERE 'Date Published' IS NOT NULL
}}
</pre>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=bc476d8f-0e69-43af-bc61-4c22c5e8f9eb" />
      </body>
      <title>Adding burn-up charts to Mingle retroactively</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,bc476d8f-0e69-43af-bc61-4c22c5e8f9eb.aspx</guid>
      <link>http://blog.eleutian.com/2008/01/11/AddingBurnupChartsToMingleRetroactively.aspx</link>
      <pubDate>Fri, 11 Jan 2008 07:48:38 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="210" alt="stack-bar-chart" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/AddingburnupchartstoMingleretroactively_15031/stack-bar-chart_3.png" width="244" align="left" border="0"&gt; 
&lt;/p&gt;
&lt;p&gt;
With &lt;a href="http://studios.thoughtworks.com/mingle-project-intelligence"&gt;Mingle&lt;/a&gt; 1.1
came several great new features, including date fields. Now you can track the date
that you complete tickets by adding a Date Done field (along with a Complete transition
that sets the Date Done field to today) and locking the Status field to be editable
only by transition. 
&lt;/p&gt;
&lt;p&gt;
This works great for newly completed cards, but what about all the cards you already
have in your system? Sure you could go without burn-up charts and date stats for those
cards, but what's the fun in that?&amp;nbsp; 
&lt;/p&gt;
&lt;br style="clear: both"&gt;
&lt;p&gt;
&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="120" alt="taskcomplete" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/AddingburnupchartstoMingleretroactively_15031/taskcomplete_3.jpg" width="604" border="0"&gt;
&lt;/p&gt;
&lt;p&gt;
Fortunately, Mingle tracks every card change in a history table. Each change in the
history table has a date associated with it, so all we need to do is figure out how
to extract the dates we care about. I must warn you, this is a little complicated,
and I'm sure ThoughtWorks doesn't at all condone you mucking around in your Mingle
database manually, so please please back up your database before attempting this and
of course I'm not responsible for any damage you do. That said, here's how I was able
to retroactively add dates for our two date fields: Date Done, and Date Published.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Back up your Mingle DB. Really. 
&lt;li&gt;
You'll need to identify the field you use to track status. We use Status. We have
Status of 'Not Done', 'Done', and 'Published'. 
&lt;li&gt;
Then connect to your Mingle database and find the name of your cards table and your
card_versions table. Our project is called "eleutian_speakeng" so our tables are "eleutian_speakeng_cards"
and "eleutian_speakeng_card_versions". 
&lt;li&gt;
Add the date fields you want to populate if you don't already have them. We have "Date
Added", "Date Published" and "Date Done". 
&lt;li&gt;
Figure out which fields in the two tables you identified map to your status and dates
(this is usually as easy as prepending cp_ and box_caring your names. For us we have
cp_status, cp_date_published, cp_date_done, cp_date_added. 
&lt;li&gt;
Edit the following queries to add in your table/field names and values (these are
the exact queries I ran, you'll need to change the red things): &lt;pre&gt;UPDATE &lt;font color="#ff0000"&gt;eleutian_speakeng_cards&lt;/font&gt; SET &lt;font color="#ff0000"&gt;cp_date_added&lt;/font&gt; =
created_at UPDATE &lt;font color="#ff0000"&gt;eleutian_speakeng_cards&lt;/font&gt; card SET card.&lt;font color="#ff0000"&gt;cp_date_done&lt;/font&gt; =
( SELECT MAX(b.updated_at) FROM &lt;font color="#ff0000"&gt;eleutian_speakeng_card_versions&lt;/font&gt; a
INNER JOIN &lt;font color="#ff0000"&gt;eleutian_speakeng_card_versions&lt;/font&gt; b ON a.number
= b.number AND b.version=a.version+1 WHERE card.number = a.number AND (a.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt;=&lt;font color="#ff0000"&gt;'Not
Done'&lt;/font&gt; OR a.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt; IS NULL) AND b.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt;=&lt;font color="#ff0000"&gt;'Done'&lt;/font&gt; )
WHERE card.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt; = &lt;font color="#ff0000"&gt;'Done'&lt;/font&gt; UPDATE &lt;font color="#ff0000"&gt;eleutian_speakeng_cards&lt;/font&gt; card
SET card.&lt;font color="#ff0000"&gt;cp_date_published&lt;/font&gt; = ( SELECT MAX(b.updated_at)
FROM &lt;font color="#ff0000"&gt;eleutian_speakeng_card_versions&lt;/font&gt; a INNER JOIN &lt;font color="#ff0000"&gt;eleutian_speakeng_card_versions&lt;/font&gt; b
ON a.number = b.number AND b.version=a.version+1 WHERE card.number = a.number AND
(a.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt; IN (&lt;font color="#ff0000"&gt;'Done', 'Not Done'&lt;/font&gt;)
OR a.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt; IS NULL) AND b.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt;=&lt;font color="#ff0000"&gt;'Published'&lt;/font&gt; )
WHERE card.&lt;font color="#ff0000"&gt;cp_status&lt;/font&gt; = &lt;font color="#ff0000"&gt;'Published'&lt;/font&gt; &lt;/pre&gt;
&lt;li&gt;
Once you've got that taken care of, go ahead and run the queries (you backed up right?)
and you should be good to go.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
So now that you've got your dates in your database, how do you get the cool burn up
chart? Like this (mold to your project):
&lt;/p&gt;
&lt;pre&gt;{{
   stack-bar-chart
   x-label-start: SELECT MIN('Date Added')
   x-label-end: Today
   x-label-step: 14
   chart-height: 600
   chart-width: 700
   plot-height: 500
   plot-width: 550
   conditions: Type IN (Bug, Story, Task)
   three-d: true
   cumulative: true
   series:
     - label: 'Not Done'
       color: #e6a800
       data: SELECT 'Date Added', COUNT(*)
       type: area
     - label: Done
       color: #1bcc00
       type: area
       data: SELECT 'Date Done', Count(*) WHERE 'Date Done' IS NOT NULL
     - label: Published
       color: #0079bf
       type: area
       data: SELECT 'Date Published', COUNT(*) WHERE 'Date Published' IS NOT NULL
}}
&lt;/pre&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=bc476d8f-0e69-43af-bc61-4c22c5e8f9eb" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,bc476d8f-0e69-43af-bc61-4c22c5e8f9eb.aspx</comments>
      <category>agile</category>
      <category>development</category>
      <category>mingle</category>
      <category>tips</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=59f6c9cd-c0de-4bc4-8235-e5838734b1cb</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,59f6c9cd-c0de-4bc4-8235-e5838734b1cb.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,59f6c9cd-c0de-4bc4-8235-e5838734b1cb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=59f6c9cd-c0de-4bc4-8235-e5838734b1cb</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of the first things we did when we got a dedicated development server was set
up a <a href="http://en.wikipedia.org/wiki/Continuous_Integration">continuous integration</a> server.
The natural choice at the time was <a href="http://confluence.public.thoughtworks.org/display/CCNET/Welcome+to+CruiseControl.NET">CruiseControl.NET</a>,
so Jacob set off to get CC.NET installed and configured. I can't speak to that experience
first hand, but I know it wasn't a fun one. Lots of XML hell and other non-fun issues.
The end result wasn't too bad--it'd email us when builds failed with a log, and we
could review logs on our CC.NET site. There was really nothing WOW about it--it got
the job done. It was frustrating at times because the logs it spat out were difficult
to visually parse; it was basically just a verbose msbuild log with a failure mixed
in somewhere... and that was if we were lucky enough to get a text log and not have
to filter through the build xml log. I'm sure there are some things we could have
done to filter that log down, apply xslt to our build.xml to make it more readable,
or configure things to be nicer, but I'm also sure it would have involved hard work
and more XML hell.
</p>
        <p>
Enter the free Professional Edition of <a href="http://www.jetbrains.com/teamcity/">TeamCity
3.0</a>. We'd tried out TC briefly in the past and it seriously lacked in the .NET
support arena. Now they tout support for msbuild (2.0 and 3.5), sln files from 2003-2008,
and even a nifty .NET duplicate code finder. They also have some pretty slick support
for NUnit which I'll get to in a moment. 
</p>
        <p>
I decided to give it a shot again, so I set up the TC server on our Ubuntu VM. Why
there? Well that's where most of our server tools are: SVN, <a href="http://studios.thoughtworks.com/mingle-project-intelligence">Mingle</a>, <a href="http://www.fogcreek.com/FogBugz/">FogBugz</a> and
now TC. But wait, aren't we a .NET shop? Yep. The build doesn't happen on our Ubuntu
VM. See, TC is set up to have a server + build agents. A build agent is basically
a machine that will check out code, do the build when the server tells it to, and
report back results and artifacts to the build server. This way you can have multiple
Build Agents--different Operating Systems, system configurations, physical locations,
etc. 
</p>
        <p>
Installing both the Server and the Agent were relatively straight forward. There were
a few annoyances with the Agent's configuration (like I couldn't get it to use a network
drive as a working directory and it took some tweaking to get things to work with
our directory structure,) but after a while I got things up and running.
</p>
        <p>
          <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/buildconfiguration_2.jpg">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="buildconfiguration" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/buildconfiguration_thumb.jpg" width="137" align="right" border="0" />
          </a> Setting
up a build configuration in Team City is an absolute breeze. You basically have a
7 step Q&amp;A about your configuration:
</p>
        <ol>
          <li>
            <strong>General Settings </strong>- Name, Description, a few options and the build
number format (we use our SVN revision #)</li>
          <li>
            <strong>Version Control Settings</strong> - You can set up multiple repos to check
out with a number of tools (SVN, CVS, Perforce, StarTeam, ClearCase). I'm sure it
wouldn't be too difficult to write plugins for Mecurial or Git. You can also tell
it where to check out relative to your working directory and whether or not you want
it to check out to the agent or on the server and then have the agent download it.</li>
          <li>
            <strong>Build Runner</strong> - Mainly .NET and Java stuff. No Rake yet, but it supports
command line so you could just use that. With MSBuild you just specify your MSBuild
file, the target you want to run, and any command line args you need. It's here that
you specify your build artifacts as well. Build artifacts are kept along side your
completed builds and easily accessible from the TC site.</li>
          <li>
            <strong>Build Triggering</strong> - You can trigger based on VCS changes, schedule,
other build configurations completing, or automatic retry.</li>
          <li>
            <strong>Artifact dependencies</strong> - This makes it really easy to say you want
Build A to use Build B's artifacts. For example, an installer configuration that will
build an installer based on the last successful CI build.</li>
          <li>
            <strong>Properties and environment variables </strong>- Self explanatory.</li>
          <li>
            <strong>Agent requirements</strong> - Not all your agents can run all your build configurations
necessarily. This will let you set requirements. You can set these based on environment
variables, machine names, etc.</li>
        </ol>
        <p>
          <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/configs_2.jpg">
            <img style="border-right: 0px; border-top: 0px; margin: 0px 0px 0px 25px; border-left: 0px; border-bottom: 0px" height="187" alt="configs" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/configs_thumb.jpg" width="604" border="0" />
          </a>
        </p>
        <p>
Once you get things set up, you get to see the really cool stuff. TeamCity is just
packed full of nerdgasm features that just work out of the box. No need to scour the
web for plugins, deal with XML, or anything like that. Things just work. For example,
you get full status of the build while it is happening--you can see what MSBuild step
the build is on, you can see the important messages in the Build Log (it filters them
quite intelligently), you can even see what threads/processes are running. If you
use their NUnit runner you can even see how many tests have passed/failed/been ignored. 
</p>
        <p>
          <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/testduration_2.jpg">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="80" alt="testduration" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/testduration_thumb.jpg" width="244" align="right" border="0" />
          </a> That's
not all you can see with their test runner though. You also get a full history of
each test--how long each test took each time it was run, and if one fails you can
see in what build it first failed, if it has been fixed yet, and you can even click
to open it in your IDE if you have the Visual Studio plugin <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/failedtext_2.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="82" alt="failedtext" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/failedtext_thumb.jpg" width="187" align="right" border="0" /></a>installed.
The Tests tab even orders your tests by Duration so you have an idea which tests you
may need to optimize.
</p>
        <p>
          <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/statistics_2.jpg">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="statistics" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/statistics_thumb.jpg" width="211" align="left" border="0" />
          </a>Then
there's the Statistics tab. This tab is a one stop shop for build health. You can
see how long the build takes, how often it succeeds, how many tests you have, how
many fail and even how big the artifacts are. You can see our build #s jump when we
switched from a counter to SVN revision. 
</p>
        <p>
If there aren't enough features for you, don't worry, like with most JetBrains products
you can extend them to your hearts content. That is if you can stomach the lack of
API documentation (surprisingly, normal usage documentation is pretty good). One of
our guys is working on a Campfire Notification plugin at the moment so we can get
better build notifications in our Campfire. That's another post though. 
</p>
        <p>
All in all, we're very happy with TeamCity. It's just as free as CC.NET for a team
of our size, it's much much easier to set up, and it has way more features. How could
you go wrong? I'm sure its only a matter of time before we start to see rake runners,
MBUnit test runners, and many other things to make TC even better. JetBrains has a
winner here.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=59f6c9cd-c0de-4bc4-8235-e5838734b1cb" />
      </body>
      <title>CruiseControl.NET is dead. Long live Team City!</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,59f6c9cd-c0de-4bc4-8235-e5838734b1cb.aspx</guid>
      <link>http://blog.eleutian.com/2007/12/22/CruiseControlNETIsDeadLongLiveTeamCity.aspx</link>
      <pubDate>Sat, 22 Dec 2007 22:13:34 GMT</pubDate>
      <description>&lt;p&gt;
One of the first things we did when we got a dedicated development server was set
up a &lt;a href="http://en.wikipedia.org/wiki/Continuous_Integration"&gt;continuous integration&lt;/a&gt; server.
The natural choice at the time was &lt;a href="http://confluence.public.thoughtworks.org/display/CCNET/Welcome+to+CruiseControl.NET"&gt;CruiseControl.NET&lt;/a&gt;,
so Jacob set off to get CC.NET installed and configured. I can't speak to that experience
first hand, but I know it wasn't a fun one. Lots of XML hell and other non-fun issues.
The end result wasn't too bad--it'd email us when builds failed with a log, and we
could review logs on our CC.NET site. There was really nothing WOW about it--it got
the job done. It was frustrating at times because the logs it spat out were difficult
to visually parse; it was basically just a verbose msbuild log with a failure mixed
in somewhere... and that was if we were lucky enough to get a text log and not have
to filter through the build xml log. I'm sure there are some things we could have
done to filter that log down, apply xslt to our build.xml to make it more readable,
or configure things to be nicer, but I'm also sure it would have involved hard work
and more XML hell.
&lt;/p&gt;
&lt;p&gt;
Enter the free Professional Edition of &lt;a href="http://www.jetbrains.com/teamcity/"&gt;TeamCity
3.0&lt;/a&gt;. We'd tried out TC briefly in the past and it seriously lacked in the .NET
support arena. Now they tout support for msbuild (2.0 and 3.5), sln files from 2003-2008,
and even a nifty .NET duplicate code finder. They also have some pretty slick support
for NUnit which I'll get to in a moment. 
&lt;/p&gt;
&lt;p&gt;
I decided to give it a shot again, so I set up the TC server on our Ubuntu VM. Why
there? Well that's where most of our server tools are: SVN, &lt;a href="http://studios.thoughtworks.com/mingle-project-intelligence"&gt;Mingle&lt;/a&gt;, &lt;a href="http://www.fogcreek.com/FogBugz/"&gt;FogBugz&lt;/a&gt; and
now TC. But wait, aren't we a .NET shop? Yep. The build doesn't happen on our Ubuntu
VM. See, TC is set up to have a server + build agents. A build agent is basically
a machine that will check out code, do the build when the server tells it to, and
report back results and artifacts to the build server. This way you can have multiple
Build Agents--different Operating Systems, system configurations, physical locations,
etc. 
&lt;/p&gt;
&lt;p&gt;
Installing both the Server and the Agent were relatively straight forward. There were
a few annoyances with the Agent's configuration (like I couldn't get it to use a network
drive as a working directory and it took some tweaking to get things to work with
our directory structure,) but after a while I got things up and running.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/buildconfiguration_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="buildconfiguration" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/buildconfiguration_thumb.jpg" width="137" align="right" border="0"&gt;&lt;/a&gt; Setting
up a build configuration in Team City is an absolute breeze. You basically have a
7 step Q&amp;amp;A about your configuration:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;General Settings &lt;/strong&gt;- Name, Description, a few options and the build
number format (we use our SVN revision #)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version Control Settings&lt;/strong&gt; - You can set up multiple repos to check
out with a number of tools (SVN, CVS, Perforce, StarTeam, ClearCase). I'm sure it
wouldn't be too difficult to write plugins for Mecurial or Git. You can also tell
it where to check out relative to your working directory and whether or not you want
it to check out to the agent or on the server and then have the agent download it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build Runner&lt;/strong&gt; - Mainly .NET and Java stuff. No Rake yet, but it supports
command line so you could just use that. With MSBuild you just specify your MSBuild
file, the target you want to run, and any command line args you need. It's here that
you specify your build artifacts as well. Build artifacts are kept along side your
completed builds and easily accessible from the TC site.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build Triggering&lt;/strong&gt; - You can trigger based on VCS changes, schedule,
other build configurations completing, or automatic retry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Artifact dependencies&lt;/strong&gt; - This makes it really easy to say you want
Build A to use Build B's artifacts. For example, an installer configuration that will
build an installer based on the last successful CI build.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Properties and environment variables &lt;/strong&gt;- Self explanatory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agent requirements&lt;/strong&gt; - Not all your agents can run all your build configurations
necessarily. This will let you set requirements. You can set these based on environment
variables, machine names, etc.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/configs_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; margin: 0px 0px 0px 25px; border-left: 0px; border-bottom: 0px" height="187" alt="configs" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/configs_thumb.jpg" width="604" border="0"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Once you get things set up, you get to see the really cool stuff. TeamCity is just
packed full of nerdgasm features that just work out of the box. No need to scour the
web for plugins, deal with XML, or anything like that. Things just work. For example,
you get full status of the build while it is happening--you can see what MSBuild step
the build is on, you can see the important messages in the Build Log (it filters them
quite intelligently), you can even see what threads/processes are running. If you
use their NUnit runner you can even see how many tests have passed/failed/been ignored. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/testduration_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="80" alt="testduration" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/testduration_thumb.jpg" width="244" align="right" border="0"&gt;&lt;/a&gt; That's
not all you can see with their test runner though. You also get a full history of
each test--how long each test took each time it was run, and if one fails you can
see in what build it first failed, if it has been fixed yet, and you can even click
to open it in your IDE if you have the Visual Studio plugin &lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/failedtext_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="82" alt="failedtext" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/failedtext_thumb.jpg" width="187" align="right" border="0"&gt;&lt;/a&gt;installed.
The Tests tab even orders your tests by Duration so you have an idea which tests you
may need to optimize.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/statistics_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="statistics" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/CruiseControl.NETisdead.LongliveTeamCity_C953/statistics_thumb.jpg" width="211" align="left" border="0"&gt;&lt;/a&gt;Then
there's the Statistics tab. This tab is a one stop shop for build health. You can
see how long the build takes, how often it succeeds, how many tests you have, how
many fail and even how big the artifacts are. You can see our build #s jump when we
switched from a counter to SVN revision. 
&lt;/p&gt;
&lt;p&gt;
If there aren't enough features for you, don't worry, like with most JetBrains products
you can extend them to your hearts content. That is if you can stomach the lack of
API documentation (surprisingly, normal usage documentation is pretty good). One of
our guys is working on a Campfire Notification plugin at the moment so we can get
better build notifications in our Campfire. That's another post though. 
&lt;/p&gt;
&lt;p&gt;
All in all, we're very happy with TeamCity. It's just as free as CC.NET for a team
of our size, it's much much easier to set up, and it has way more features. How could
you go wrong? I'm sure its only a matter of time before we start to see rake runners,
MBUnit test runners, and many other things to make TC even better. JetBrains has a
winner here.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=59f6c9cd-c0de-4bc4-8235-e5838734b1cb" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,59f6c9cd-c0de-4bc4-8235-e5838734b1cb.aspx</comments>
      <category>development</category>
      <category>reviews</category>
      <category>continuous integration</category>
      <category>teamcity</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=2f6428cf-3603-4eab-b54b-50564c4344ea</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,2f6428cf-3603-4eab-b54b-50564c4344ea.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,2f6428cf-3603-4eab-b54b-50564c4344ea.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2f6428cf-3603-4eab-b54b-50564c4344ea</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just profiled one of our test assemblies (Unit tests only, nothing hitting the db).
Mocking accounts for 196 seconds out of 357 seconds. The culprit is System.Reflection.Emit.TypeBuilder.CreateType().
This doesn't come as <strong>that</strong> much of a surprise to me, but still...
is there a faster way to do this? Oh and yeah, the results of CreateType are cached
so its only the first time you create a mock of a type that you take this hit, but
still... that can really slow down TDD.
</p>
        <p>
And no, I'm not saying we should stop mocking... just raising awareness.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2f6428cf-3603-4eab-b54b-50564c4344ea" />
      </body>
      <title>Mocking is slow</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,2f6428cf-3603-4eab-b54b-50564c4344ea.aspx</guid>
      <link>http://blog.eleutian.com/2007/12/15/MockingIsSlow.aspx</link>
      <pubDate>Sat, 15 Dec 2007 19:17:03 GMT</pubDate>
      <description>&lt;p&gt;
I just profiled one of our test assemblies (Unit tests only, nothing hitting the db).
Mocking accounts for 196 seconds out of 357 seconds. The culprit is System.Reflection.Emit.TypeBuilder.CreateType().
This doesn't come as &lt;strong&gt;that&lt;/strong&gt; much of a surprise to me, but still...
is there a faster way to do this? Oh and yeah, the results of CreateType are cached
so its only the first time you create a mock of a type that you take this hit, but
still... that can really slow down TDD.
&lt;/p&gt;
&lt;p&gt;
And no, I'm not saying we should stop mocking... just raising awareness.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2f6428cf-3603-4eab-b54b-50564c4344ea" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,2f6428cf-3603-4eab-b54b-50564c4344ea.aspx</comments>
      <category>annoyances</category>
      <category>development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=61bc5554-4bd6-44d0-b3d9-0c806a5377db</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,61bc5554-4bd6-44d0-b3d9-0c806a5377db.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,61bc5554-4bd6-44d0-b3d9-0c806a5377db.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=61bc5554-4bd6-44d0-b3d9-0c806a5377db</wfw:commentRss>
      <slash:comments>14</slash:comments>
      <title>Vim Screencast Tutorial Teaser</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,61bc5554-4bd6-44d0-b3d9-0c806a5377db.aspx</guid>
      <link>http://blog.eleutian.com/2007/12/13/VimScreencastTutorialTeaser.aspx</link>
      <pubDate>Thu, 13 Dec 2007 10:23:32 GMT</pubDate>
      <description>&lt;p&gt;
I'm going to be putting together a series of Vim Screencast Tutorials. My goal is
to raise awareness in the world of just how powerful Vim is and then get everyone
comfortable using it. Here's a short trailer that shows a few of the things that make
me love Vim. This is my first Screencast of this kind and, yea, I know its cheesy. 
&lt;/p&gt;
&lt;script type="text/javascript" src="download/swfobject.js"&gt;&lt;/script&gt;
&lt;div id="flashcontent" align="center"&gt;
&lt;p style="font-weight:bold;font-size:14pt;"&gt;
You need to upgrade your Flash Player to at&lt;br&gt;
least version 8 to view this video content.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.adobe.com/flashplayer/"&gt;Click here to download the latest version
of Flash Player&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;
&lt;script type="text/javascript"&gt;
// &lt;![CDATA[
var so = new SWFObject("download/flvplayer.swf?theFile=/download/vimteaser.flv&amp;defaultImage=download/vimteaser.jpg&amp;theVolume=100&amp;startPlayingOnload=no&amp;popUpHelp=no&amp;bufferSeconds=2&amp;videoSmoothing=yes","flvplayer","640","495","8","#000000");
so.addParam("movie","download/flvplayer.swf?theFile=/download/vimteaser.flv&amp;defaultImage=download/vimteaser.jpg&amp;theVolume=100&amp;startPlayingOnload=no&amp;popUpHelp=no&amp;bufferSeconds=2&amp;videoSmoothing=yes");
so.addParam("bgcolor","#000000");
so.write("flashcontent");
// ]]&gt;
&lt;/script&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/vimteaser.wmv"&gt;Click here to save&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
You can get Vim &lt;a href="http://vim.sourceforge.net"&gt;here&lt;/a&gt;, but I'd ultimately
recommend &lt;a href="http://www.plimus.com/jsp/redirect.jsp?contractId=1723786&amp;referrer=aaronjensen"&gt;ViEmu&lt;/a&gt;,
a Vim emulator plugin for Visual Studio.
&lt;/p&gt;
&lt;a href="http://blog.eleutian.com/viemupromo.html" rel="nofollow" class="feedonly"&gt;&lt;img src="http://blog.eleutian.com/viemu15.jpg" class="feedonly"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=61bc5554-4bd6-44d0-b3d9-0c806a5377db" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,61bc5554-4bd6-44d0-b3d9-0c806a5377db.aspx</comments>
      <category>development</category>
      <category>screencast</category>
      <category>vim</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=9ae72631-5839-4026-ae1c-26277219b004</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9ae72631-5839-4026-ae1c-26277219b004</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I haven't posted in a while and decided to write a post about one of the little side
projects I've worked on recently. I've been lurking around on the ALT.NET mailing
list and one of the discussions I take time to keep up on is on Dependency Injection.
Something I worked on a few weeks ago has some applications in that area and so I
decided to see what others thought.
</p>
        <p>
I was talking with Aaron several months ago about how many problems constructors in
static languages cause. Ruby people are fortunate in that their constructors are much
more flexible than their C# counterparts. Want something to be a singleton? Just return
the same instance from the constructor! Lifecycle management and dependencies can
be the responsibility of the constructor itself, rather than a function of a container.
</p>
        <p>
Over the weekend a few weeks ago I wrote a library and MsBuild task that factors constructor
calls out of code and into a pluggable factory system. You write code like this:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> EmailSender
{ <span class="kwrd">public</span><span class="kwrd">void</span> TellEveryone(<span class="kwrd">string</span> message)
{ TemplateRenderer renderer = <span class="kwrd">new</span> TemplateRenderer(); Console.WriteLine(renderer.Render(message));
} } <span class="kwrd">public</span><span class="kwrd">class</span> EmailSenderFactory
: IObjectFactory&lt;EmailSender&gt; { <span class="kwrd">public</span> EmailSender
Create() { <span class="kwrd">return</span><span class="kwrd">new</span> EmailSender();
} } EmailSender emailer = <span class="kwrd">new</span> EmailSender(); emailer.TellEveryone(<span class="str">"Hello,
World!"</span>);</pre>
        <p>
What the MsBuild task will do is this:
</p>
        <ol>
          <li>
Load the target Assemblies using Mono.Cecil 
</li>
          <li>
Find implementations of IObjectFactory&lt;T&gt; and add them to the FactoryMap. 
</li>
          <li>
Find all instances of new T() for each of those factories and replace them with a
call to the Factories.Create&lt;T&gt;(), ignoring calls inside of factories themselves. 
</li>
          <li>
Weave the FactoryMap (just the Factory Type and its Object Type) into the Assembly. 
</li>
        </ol>
        <p>
So during runtime we can use the FactoryMap that was serialized in step #4 to create
the factories and use them to create instances as they're demanded. It is important
to note that your objects need default constructors, even though they may never be
called after the code is weaved. One can also replace the factory implementations
with IoC backed versions, etc... 
</p>
        <p>
          <strong>Why don't you just create a New.Instance&lt;T&gt; method and use that instead
of new T() and save yourself the pain of weaving and having a task and all that, moron?</strong>
        </p>
        <p>
Good question! I mostly did this as an exercise in code weaving using Cecil. It's
a great library, although testing code using it can be a little tedious. I'm tossing
around some other projects that I'd love to give a shot and this was a good introduction.
You could achieve the same thing using reflection to build your factory map and remember
to use New.Instance&lt;T&gt;() instead of the new operator in your code. Granted,
the manual approach plasters the concern all over the place and weaving keeps the
original code free from that. Which may be an OK trade-off for the simplified process.
</p>
        <p>
Another thing to consider, is that perhaps the weaving approach has its applications
elsewhere? Use it specifically for creating domain classes to transparently provide
domain services? Another idea I've been tossing around is if you're writing code for
platforms where garbage collection isn't as performant (xbox for example) you may
be able to transparently add object pooling and other patterns after the fact and
not clutter up the original code. 
</p>
        <p>
I want to release the code anyway, but I'm tired of uploading zip files. All my future
source releases will probably be via Google Code or some public svn repository. Stay
tuned.... In the meantime, feel free to opine.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=9ae72631-5839-4026-ae1c-26277219b004" />
      </body>
      <title>A Weird Code Weaving Thing I Wrote</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</guid>
      <link>http://blog.eleutian.com/2007/11/18/AWeirdCodeWeavingThingIWrote.aspx</link>
      <pubDate>Sun, 18 Nov 2007 23:49:46 GMT</pubDate>
      <description>&lt;p&gt;
I haven't posted in a while and decided to write a post about one of the little side
projects I've worked on recently. I've been lurking around on the ALT.NET mailing
list and one of the discussions I take time to keep up on is on Dependency Injection.
Something I worked on a few weeks ago has some applications in that area and so I
decided to see what others thought.
&lt;/p&gt;
&lt;p&gt;
I was talking with Aaron several months ago about how many problems constructors in
static languages cause. Ruby people are fortunate in that their constructors are much
more flexible than their C# counterparts. Want something to be a singleton? Just return
the same instance from the constructor! Lifecycle management and dependencies can
be the responsibility of the constructor itself, rather than a function of a container.
&lt;/p&gt;
&lt;p&gt;
Over the weekend a few weeks ago I wrote a library and MsBuild task that factors constructor
calls out of code and into a pluggable factory system. You write code like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EmailSender
{ &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TellEveryone(&lt;span class="kwrd"&gt;string&lt;/span&gt; message)
{ TemplateRenderer renderer = &lt;span class="kwrd"&gt;new&lt;/span&gt; TemplateRenderer(); Console.WriteLine(renderer.Render(message));
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EmailSenderFactory
: IObjectFactory&amp;lt;EmailSender&amp;gt; { &lt;span class="kwrd"&gt;public&lt;/span&gt; EmailSender
Create() { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; EmailSender();
} } EmailSender emailer = &lt;span class="kwrd"&gt;new&lt;/span&gt; EmailSender(); emailer.TellEveryone(&lt;span class="str"&gt;&amp;quot;Hello,
World!&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;
What the MsBuild task will do is this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Load the target Assemblies using Mono.Cecil 
&lt;/li&gt;
&lt;li&gt;
Find implementations of IObjectFactory&amp;lt;T&amp;gt; and add them to the FactoryMap. 
&lt;/li&gt;
&lt;li&gt;
Find all instances of new T() for each of those factories and replace them with a
call to the Factories.Create&amp;lt;T&amp;gt;(), ignoring calls inside of factories themselves. 
&lt;/li&gt;
&lt;li&gt;
Weave the FactoryMap (just the Factory Type and its Object Type) into the Assembly. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
So during runtime we can use the FactoryMap that was serialized in step #4 to create
the factories and use them to create instances as they're demanded. It is important
to note that your objects need default constructors, even though they may never be
called after the code is weaved. One can also replace the factory implementations
with IoC backed versions, etc... 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Why don't you just create a New.Instance&amp;lt;T&amp;gt; method and use that instead
of new T() and save yourself the pain of weaving and having a task and all that, moron?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Good question! I mostly did this as an exercise in code weaving using Cecil. It's
a great library, although testing code using it can be a little tedious. I'm tossing
around some other projects that I'd love to give a shot and this was a good introduction.
You could achieve the same thing using reflection to build your factory map and remember
to use New.Instance&amp;lt;T&amp;gt;() instead of the new operator in your code. Granted,
the manual approach plasters the concern all over the place and weaving keeps the
original code free from that. Which may be an OK trade-off for the simplified process.
&lt;/p&gt;
&lt;p&gt;
Another thing to consider, is that perhaps the weaving approach has its applications
elsewhere? Use it specifically for creating domain classes to transparently provide
domain services? Another idea I've been tossing around is if you're writing code for
platforms where garbage collection isn't as performant (xbox for example) you may
be able to transparently add object pooling and other patterns after the fact and
not clutter up the original code. 
&lt;/p&gt;
&lt;p&gt;
I want to release the code anyway, but I'm tired of uploading zip files. All my future
source releases will probably be via Google Code or some public svn repository. Stay
tuned.... In the meantime, feel free to opine.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=9ae72631-5839-4026-ae1c-26277219b004" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</comments>
      <category>development</category>
      <category>emit</category>
      <category>inversion of control</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=77a7c478-8e27-4eb4-b261-df50bfe7aab4</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,77a7c478-8e27-4eb4-b261-df50bfe7aab4.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,77a7c478-8e27-4eb4-b261-df50bfe7aab4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=77a7c478-8e27-4eb4-b261-df50bfe7aab4</wfw:commentRss>
      <slash:comments>10</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So everyone's been talking about the ALT.NET conference, <a href="http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx">Microsoft's
take on MVC</a>, and such, so I thought I'd briefly chime in with my thoughts. <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MonoRailvs.System.Web.MVC_106/Photo%2018.jpg" atomicselection="true"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="180" alt="Photo 18" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MonoRailvs.System.Web.MVC_106/Photo%2018_thumb.jpg" width="240" align="right" border="0" /></a> First
of all, the ALT.NET conference rocked and I'm very much looking forward to the next
one. Secondly, I must disclose that <a href="http://codebetter.com/blogs/scott.bellware/default.aspx">Scott
Bellware</a> paid me to say that ($1, signed) but I would have said it anyway.
</p>
        <p>
OK, so then there's the whole MVC thing. This too was awesome and I should try and
collect a signed dollar from Scott Guthrie. It's obvious he and the team working on
MVC are doing their research. That said, as many of you know we're a MonoRail shop
here. We've been using it heavily for about a year now and we've got a lot of code
written on it. So what will the impending release of the MS MVC framework mean for
us? What will it mean for MonoRail? 
</p>
        <p>
I can tell you that for us, it will very likely mean a week of exploration and quite possibly
followed by a week or two of porting. That's right, it looked that good. Everything
about it was done right or flexible enough to be made right so there really isn't
any room to complain about it. MonoRail has more features at the moment, but the ones
we use wouldn't be difficult to implement on top of MS MVC.
</p>
        <p>
So what does this mean for MonoRail? I can't <a href="http://www.castleproject.org/">speak
to that</a>, but I will tell you what I would like for it to mean. What I'd like to
see is MonoRail become more like Rails. I want to see something built on top of MS
MVC that even more-so favors Convention over Configuration--including but not limited
to generators and such. I want it to take it to the next level and be exactly what
the community wants for a C# web platform. Dave Laribee <a href="http://codebetter.com/blogs/david_laribee/archive/2007/10/16/system-web-eatitmonorail.aspx">says</a> that
"MonoRail will remain a viable option for smaller or more edgy shops." I think that's
true, but I want to see it built on top of MS MVC. 
</p>
        <p>
Why throw away all of the framework code built into MonoRail? A few reasons:
</p>
        <ol>
          <li>
Castle has already been <a href="http://svn.castleproject.org:8080/svn/castle/trunk/MonoRail2/">experimenting</a> with
throwing most of it away and starting over anyway.</li>
          <li>
MS MVC will be built into the .NET Framework. This means  easier sells to
big shops, and MonoRail would be just a free supplement rather than a replacement/paradigm
shift, making it an easier sell.</li>
          <li>
MS MVC appears to be more modular. There is no massive Controller or ginormous SmartDispatcherController.
You can even start with a one method interface and implement your controller however
you want.</li>
          <li>
Routing.</li>
          <li>
It was built with testability in mind. MonoRail is now mostly testable, but it is
not nearly as clean as it could be... or as MS MVC's testability looks.</li>
          <li>
We can probably all but throw away our <a href="http://blog.eleutian.com/2007/02/21/EleutianToolsCodeGeneratorSource.aspx">codegenerator</a> and
use lambdas. We could reinvent this for MonoRail in .NET 3.5 but then we'd be... reinventing...
it... 
</li>
        </ol>
        <p>
Whatever we decide to do when it comes out, we'll talk about our experiences so you
can judge for yourself after we judge for ourselves how viable it will be to port
from MonoRail to MS MVC.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=77a7c478-8e27-4eb4-b261-df50bfe7aab4" />
      </body>
      <title>MonoRail vs. System.Web.MVC</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,77a7c478-8e27-4eb4-b261-df50bfe7aab4.aspx</guid>
      <link>http://blog.eleutian.com/2007/10/16/MonoRailVsSystemWebMVC.aspx</link>
      <pubDate>Tue, 16 Oct 2007 06:59:24 GMT</pubDate>
      <description>&lt;p&gt;
So everyone's been talking about the ALT.NET conference, &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx"&gt;Microsoft's
take on MVC&lt;/a&gt;, and such, so I thought I'd briefly chime in with my thoughts. &lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MonoRailvs.System.Web.MVC_106/Photo%2018.jpg" atomicselection="true"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="180" alt="Photo 18" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MonoRailvs.System.Web.MVC_106/Photo%2018_thumb.jpg" width="240" align="right" border="0"&gt;&lt;/a&gt; First
of all, the ALT.NET conference rocked and I'm very much looking forward to the next
one. Secondly, I must disclose that &lt;a href="http://codebetter.com/blogs/scott.bellware/default.aspx"&gt;Scott
Bellware&lt;/a&gt;&amp;nbsp;paid me to say that ($1, signed) but I would have said it anyway.
&lt;/p&gt;
&lt;p&gt;
OK, so then there's the whole MVC thing. This too was awesome and I should try and
collect a signed dollar from Scott Guthrie. It's obvious he and the team working on
MVC are doing their research. That said, as many of you know we're a MonoRail shop
here. We've been using it heavily for about a year now and we've got a lot of code
written on it. So what will the impending release of the MS MVC framework mean for
us? What will it mean for MonoRail? 
&lt;/p&gt;
&lt;p&gt;
I can tell you that for us, it will very likely mean a week of exploration and quite&amp;nbsp;possibly
followed by a week or two of porting. That's right, it looked that good. Everything
about it was done right or flexible enough to be made right so there really isn't
any room to complain about it. MonoRail has more features at the moment, but the ones
we use wouldn't be difficult to implement on top of MS MVC.
&lt;/p&gt;
&lt;p&gt;
So what does this mean for MonoRail? I can't &lt;a href="http://www.castleproject.org/"&gt;speak
to that&lt;/a&gt;, but I will tell you what I would like for it to mean. What I'd like to
see is MonoRail become more like Rails. I want to see something built on top of MS
MVC that even more-so favors Convention over Configuration--including but not limited
to generators and such. I want it to take it to the next level and be exactly what
the community wants for a C# web platform. Dave Laribee &lt;a href="http://codebetter.com/blogs/david_laribee/archive/2007/10/16/system-web-eatitmonorail.aspx"&gt;says&lt;/a&gt;&amp;nbsp;that
"MonoRail will remain a viable option for smaller or more edgy shops." I think that's
true, but I want to see it built on top of MS MVC. 
&lt;/p&gt;
&lt;p&gt;
Why throw away all of the framework code built into MonoRail? A few reasons:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Castle has already been &lt;a href="http://svn.castleproject.org:8080/svn/castle/trunk/MonoRail2/"&gt;experimenting&lt;/a&gt; with
throwing most of it&amp;nbsp;away and starting over anyway.&lt;/li&gt;
&lt;li&gt;
MS MVC will be built into the .NET Framework. This means&amp;nbsp;&amp;nbsp;easier sells to
big shops, and MonoRail would be just a free supplement rather than a replacement/paradigm
shift, making it an easier sell.&lt;/li&gt;
&lt;li&gt;
MS MVC appears to be more modular. There is no massive Controller or ginormous SmartDispatcherController.
You can even start with a one method interface and implement your controller however
you want.&lt;/li&gt;
&lt;li&gt;
Routing.&lt;/li&gt;
&lt;li&gt;
It was built with testability in mind. MonoRail is now mostly testable, but it is
not nearly as clean as it could be... or as MS MVC's testability looks.&lt;/li&gt;
&lt;li&gt;
We can probably all but throw away our &lt;a href="http://blog.eleutian.com/2007/02/21/EleutianToolsCodeGeneratorSource.aspx"&gt;codegenerator&lt;/a&gt; and
use lambdas. We could reinvent this for MonoRail in .NET 3.5 but then we'd be... reinventing...
it... 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Whatever we decide to do when it comes out, we'll talk about our experiences so you
can judge for yourself after we judge for ourselves how viable it will be to port
from MonoRail to MS MVC.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=77a7c478-8e27-4eb4-b261-df50bfe7aab4" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,77a7c478-8e27-4eb4-b261-df50bfe7aab4.aspx</comments>
      <category>development</category>
      <category>monorail</category>
      <category>mvc</category>
      <category>microsoft</category>
      <category>altnetconf</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=f507cec2-8153-4b4b-aa14-f04b376299cf</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,f507cec2-8153-4b4b-aa14-f04b376299cf.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,f507cec2-8153-4b4b-aa14-f04b376299cf.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f507cec2-8153-4b4b-aa14-f04b376299cf</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
First some code:
</p>
        <pre class="csharpcode">UserEntity user = New.User(<span class="str">"bob"</span>).In(New.Company(<span class="str">"ACME"</span>)).With(
New.PurchaseOf(<span class="str">"ProductA"</span>) .WithAcademicSlot(1).FilledBy(New.EnrollmentIn(101).ThatIsCanceled)
.WithPlacementSlot(1).FilledBy(New.EnrollmentIn(1))); </pre>
        <p>
This code actually creates and saves (with NHibernate) about 12 or so entities. We
can use code like this to set up one off sample data for tests in a way that's easy
to read, understand and change. There is a decent amount of magic that goes into making
this work and I wanted to talk about how I did it.
</p>
        <p>
First we have a <strong>FixtureContext</strong>, which is just a hub for the DaoFactory,
the current Session, and three helper classes, <strong>New</strong>, <strong>Current</strong> and <strong>Existing</strong>. <strong>New </strong>is
the class responsible for the beginning of the syntax you see above. Each method on
New returns a <strong>Creator</strong>. There's some magic in <strong>Creator</strong>,
so here's the code:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> Creator&lt;T&gt;
: FixtureContextAware <span class="kwrd">where</span> T: <span class="kwrd">class</span>, <span class="kwrd">new</span>()
{ <span class="kwrd">private</span> T _creation; <span class="kwrd">protected</span> T
Creation { get { <span class="kwrd">return</span> _creation; } <span class="kwrd">private</span> set
{ <span class="kwrd">if</span> (Current.Get&lt;T&gt;() != <span class="kwrd">value</span>)
{ Current.Push(<span class="kwrd">value</span>); } _creation = <span class="kwrd">value</span>;
} } <span class="kwrd">public</span> Creator(IFixtureContext context) : <span class="kwrd">base</span>(context)
{ Creation = <span class="kwrd">new</span> T(); } <span class="kwrd">public</span> Creator(IFixtureContext
context, T creation) : <span class="kwrd">base</span>(context) { Creation = creation;
} <span class="kwrd">public</span><span class="kwrd">static</span><span class="kwrd">implicit</span><span class="kwrd">operator</span> T(Creator&lt;T&gt;
creator) { <span class="kwrd">if</span> (creator._creation == <span class="kwrd">null</span>) <span class="kwrd">throw</span><span class="kwrd">new</span> Exception(
String.Format(<span class="str">"Creation of {0} is null, it probably shouldn't be."</span>, <span class="kwrd">typeof</span>(T)));
creator.Current.Pop&lt;T&gt;(); <span class="kwrd">return</span> creator._creation;
} } </pre>
        <p>
The first thing is that <strong>Creator </strong>is a subclass of <strong>FixtureContextAware</strong>,
which is just a helper base class that provides access to <strong>FixtureContext</strong>'s
children. Next there are a few references to <strong>Current</strong> which is simply
a collection of stacks of entities so that <strong>Creators </strong>can refer to
other entities that are being created so they don't have to be passed around. This
is better explained with an example. In the beginning example you see New.EnrollmentIn(101).
An enrollment requires a User to be created, so because there is a User in this creation
context, we can do this:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span> CourseEnrollmentCreator(IFixtureContext
context, <span class="kwrd">short</span> number) : <span class="kwrd">base</span>(context)
{ <strong>Creation.User = Current.User; </strong> Creation.Course = Existing.Course(number);
Creation.StartDate = DateTime.Now; Creation.EndDate = DateTime.Now.AddMonths(1); Session.Save(Creation);
} </pre>
        <p>
The next thing is that the creation itself is stored as <strong>Creation</strong> in
the <strong>Creator</strong>. This can either be new'd up or can be passed in to the
constructor. 
</p>
        <p>
The coolest part (at least in my opinion) is the implicit operator. This allows you
do to do things like: UserEntity user = New.User().Foo(), where each of those methods
returns a UserCreator, but at the end of all of it the <strong>Creator</strong> is
implicitly cast to a UserEntity, the thing actually being created. This also serves
as an excellent time to pop the entity from the <strong>Current</strong> stack. 
</p>
        <p>
Next we have the <strong>Existing</strong> class. This is essentially just a wrapper
for your Daos/Repositories so you can fetch things that are already in your database
(like Existing.Course(number) or Existing.User("bob")).
</p>
        <p>
With this simple framework in place, the next step is to start writing your domain
specific Creators. Here's an example:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> PurchaseCreator
: Creator&lt;PurchasedProductEntity&gt; { <span class="kwrd">protected</span> PurchaseCreator(IFixtureContext
context, PurchasedProductEntity creation) : <span class="kwrd">base</span>(context,
creation) { } <span class="kwrd">public</span> PurchaseCreator(IFixtureContext context, <span class="kwrd">string</span> productName)
: <span class="kwrd">base</span>(context) { Creation.Product = New.Product(productName); <span class="rem">//
Initialize Purchase...</span> } <span class="kwrd">public</span> PurchaseCreatorWithSlot
WithAcademicSlot(<span class="kwrd">int</span> credits) { <span class="rem">// Create
and add slot...</span><span class="kwrd">return</span><span class="kwrd">new</span> PurchaseCreatorWithSlot(Context, <span class="kwrd">this</span>,
slot); } <span class="kwrd">public</span> PurchaseCreatorWithSlot WithPlacementSlot(<span class="kwrd">int</span> credits)
{ <span class="rem">// Create and add slot...</span><span class="kwrd">return</span><span class="kwrd">new</span> PurchaseCreatorWithSlot(Context, <span class="kwrd">this</span>,
slot); } <span class="kwrd">public</span> PurchaseCreator WithSessions(<span class="kwrd">int</span> credits)
{ <span class="rem">// Create and add sessions...</span><span class="kwrd">return</span><span class="kwrd">this</span>;
} } <span class="kwrd">public</span><span class="kwrd">class</span> PurchaseCreatorWithSlot
: PurchaseCreator { <span class="kwrd">private</span><span class="kwrd">readonly</span> PurchasedCourseEnrollmentSlotEntity
_slot; <span class="kwrd">public</span> PurchaseCreatorWithSlot(IFixtureContext context,
PurchasedProductEntity creation, PurchasedCourseEnrollmentSlotEntity slot) : <span class="kwrd">base</span>(context,
creation) { _slot = slot; } <span class="kwrd">public</span> PurchaseCreatorWithSlot
FilledBy(CourseEnrollmentEntity enrollment) { <span class="rem">// Fill slot...</span> }
} </pre>
        <p>
Then we add a PurchaseOf(string productName) method to <strong>New</strong> that will
create and return a new PurchaseCreator. We can also add a PurchaseOf(ProductEntity
product) and pass a New.Product("Product") in instead. You can make it as granular
or magic as you want. Also, Notice that WithAcademicSlot and WithPlacementSlot return
a subclass of PurchaseCreator that adds another method. Using techniques like this
you can make some very verbose and context sensitive fixtures.
</p>
        <p>
We also make our HibernateTests base class FixtureContextAware so that we can
use nice syntax in our tests. 
</p>
        <p>
Here is the <a href="/download/FluentFixtures.zip">source</a> for the basic framework.
Let me know what you think. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=f507cec2-8153-4b4b-aa14-f04b376299cf" />
      </body>
      <title>Fluent Fixtures</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,f507cec2-8153-4b4b-aa14-f04b376299cf.aspx</guid>
      <link>http://blog.eleutian.com/2007/09/29/FluentFixtures.aspx</link>
      <pubDate>Sat, 29 Sep 2007 23:48:54 GMT</pubDate>
      <description>&lt;p&gt;
First some code:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;UserEntity user = New.User(&lt;span class="str"&gt;"bob"&lt;/span&gt;).In(New.Company(&lt;span class="str"&gt;"ACME"&lt;/span&gt;)).With(
New.PurchaseOf(&lt;span class="str"&gt;"ProductA"&lt;/span&gt;) .WithAcademicSlot(1).FilledBy(New.EnrollmentIn(101).ThatIsCanceled)
.WithPlacementSlot(1).FilledBy(New.EnrollmentIn(1))); &lt;/pre&gt;
&lt;p&gt;
This code actually creates and saves (with NHibernate) about 12 or so entities. We
can use code like this to set up one off sample data for tests in a way that's easy
to read, understand and change. There is a decent amount of magic that goes into making
this work and I wanted to talk about how I did it.
&lt;/p&gt;
&lt;p&gt;
First we have a &lt;strong&gt;FixtureContext&lt;/strong&gt;, which is just a hub for the DaoFactory,
the current Session, and three helper classes, &lt;strong&gt;New&lt;/strong&gt;, &lt;strong&gt;Current&lt;/strong&gt; and &lt;strong&gt;Existing&lt;/strong&gt;. &lt;strong&gt;New &lt;/strong&gt;is
the class responsible for the beginning of the syntax you see above. Each method on
New returns a &lt;strong&gt;Creator&lt;/strong&gt;. There's some magic in &lt;strong&gt;Creator&lt;/strong&gt;,
so here's the code:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Creator&amp;lt;T&amp;gt;
: FixtureContextAware &lt;span class="kwrd"&gt;where&lt;/span&gt; T: &lt;span class="kwrd"&gt;class&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt;()
{ &lt;span class="kwrd"&gt;private&lt;/span&gt; T _creation; &lt;span class="kwrd"&gt;protected&lt;/span&gt; T
Creation { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _creation; } &lt;span class="kwrd"&gt;private&lt;/span&gt; set
{ &lt;span class="kwrd"&gt;if&lt;/span&gt; (Current.Get&amp;lt;T&amp;gt;() != &lt;span class="kwrd"&gt;value&lt;/span&gt;)
{ Current.Push(&lt;span class="kwrd"&gt;value&lt;/span&gt;); } _creation = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; Creator(IFixtureContext context) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(context)
{ Creation = &lt;span class="kwrd"&gt;new&lt;/span&gt; T(); } &lt;span class="kwrd"&gt;public&lt;/span&gt; Creator(IFixtureContext
context, T creation) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(context) { Creation = creation;
} &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;implicit&lt;/span&gt; &lt;span class="kwrd"&gt;operator&lt;/span&gt; T(Creator&amp;lt;T&amp;gt;
creator) { &lt;span class="kwrd"&gt;if&lt;/span&gt; (creator._creation == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception(
String.Format(&lt;span class="str"&gt;"Creation of {0} is null, it probably shouldn't be."&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T)));
creator.Current.Pop&amp;lt;T&amp;gt;(); &lt;span class="kwrd"&gt;return&lt;/span&gt; creator._creation;
} } &lt;/pre&gt;
&lt;p&gt;
The first thing is that &lt;strong&gt;Creator &lt;/strong&gt;is a subclass of &lt;strong&gt;FixtureContextAware&lt;/strong&gt;,
which is just a helper base class that provides access to &lt;strong&gt;FixtureContext&lt;/strong&gt;'s
children. Next there are a few references to &lt;strong&gt;Current&lt;/strong&gt; which is simply
a collection of stacks of entities so that &lt;strong&gt;Creators &lt;/strong&gt;can refer to
other entities that are being created so they don't have to be passed around.&amp;nbsp;This
is better explained with an example. In the beginning example you see New.EnrollmentIn(101).
An enrollment requires a User to be created, so because there is a User in this creation
context, we can do this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; CourseEnrollmentCreator(IFixtureContext
context, &lt;span class="kwrd"&gt;short&lt;/span&gt; number) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(context)
{ &lt;strong&gt;Creation.User = Current.User; &lt;/strong&gt; Creation.Course = Existing.Course(number);
Creation.StartDate = DateTime.Now; Creation.EndDate = DateTime.Now.AddMonths(1); Session.Save(Creation);
} &lt;/pre&gt;
&lt;p&gt;
The next thing is that the creation itself is stored as &lt;strong&gt;Creation&lt;/strong&gt; in
the &lt;strong&gt;Creator&lt;/strong&gt;. This can either be new'd up or can be passed in to the
constructor. 
&lt;/p&gt;
&lt;p&gt;
The coolest part (at least in my opinion) is the implicit operator. This allows you
do to do things like: UserEntity user = New.User().Foo(), where each of those methods
returns a UserCreator, but at the end of all of it the &lt;strong&gt;Creator&lt;/strong&gt; is
implicitly cast to a UserEntity, the thing actually being created. This also serves
as an excellent time to pop the entity from the &lt;strong&gt;Current&lt;/strong&gt; stack. 
&lt;/p&gt;
&lt;p&gt;
Next we have the &lt;strong&gt;Existing&lt;/strong&gt; class. This is essentially just a wrapper
for your Daos/Repositories so you can fetch things that are already in your database
(like Existing.Course(number) or Existing.User("bob")).
&lt;/p&gt;
&lt;p&gt;
With this simple framework in place, the next step is to start writing your domain
specific Creators. Here's an example:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; PurchaseCreator
: Creator&amp;lt;PurchasedProductEntity&amp;gt; { &lt;span class="kwrd"&gt;protected&lt;/span&gt; PurchaseCreator(IFixtureContext
context, PurchasedProductEntity creation) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(context,
creation) { } &lt;span class="kwrd"&gt;public&lt;/span&gt; PurchaseCreator(IFixtureContext context, &lt;span class="kwrd"&gt;string&lt;/span&gt; productName)
: &lt;span class="kwrd"&gt;base&lt;/span&gt;(context) { Creation.Product = New.Product(productName); &lt;span class="rem"&gt;//
Initialize Purchase...&lt;/span&gt; } &lt;span class="kwrd"&gt;public&lt;/span&gt; PurchaseCreatorWithSlot
WithAcademicSlot(&lt;span class="kwrd"&gt;int&lt;/span&gt; credits) { &lt;span class="rem"&gt;// Create
and add slot...&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; PurchaseCreatorWithSlot(Context, &lt;span class="kwrd"&gt;this&lt;/span&gt;,
slot); } &lt;span class="kwrd"&gt;public&lt;/span&gt; PurchaseCreatorWithSlot WithPlacementSlot(&lt;span class="kwrd"&gt;int&lt;/span&gt; credits)
{ &lt;span class="rem"&gt;// Create and add slot...&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; PurchaseCreatorWithSlot(Context, &lt;span class="kwrd"&gt;this&lt;/span&gt;,
slot); } &lt;span class="kwrd"&gt;public&lt;/span&gt; PurchaseCreator WithSessions(&lt;span class="kwrd"&gt;int&lt;/span&gt; credits)
{ &lt;span class="rem"&gt;// Create and add sessions...&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; PurchaseCreatorWithSlot
: PurchaseCreator { &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; PurchasedCourseEnrollmentSlotEntity
_slot; &lt;span class="kwrd"&gt;public&lt;/span&gt; PurchaseCreatorWithSlot(IFixtureContext context,
PurchasedProductEntity creation, PurchasedCourseEnrollmentSlotEntity slot) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(context,
creation) { _slot = slot; } &lt;span class="kwrd"&gt;public&lt;/span&gt; PurchaseCreatorWithSlot
FilledBy(CourseEnrollmentEntity enrollment) { &lt;span class="rem"&gt;// Fill slot...&lt;/span&gt; }
} &lt;/pre&gt;
&lt;p&gt;
Then we add a PurchaseOf(string productName) method to &lt;strong&gt;New&lt;/strong&gt; that will
create and return a new PurchaseCreator. We can also add a PurchaseOf(ProductEntity
product) and pass a New.Product("Product") in instead. You can make it as granular
or magic as you want. Also, Notice that WithAcademicSlot and WithPlacementSlot return
a subclass of PurchaseCreator that adds another method. Using techniques like this
you can make some very verbose and context sensitive fixtures.
&lt;/p&gt;
&lt;p&gt;
We also make our HibernateTests base class&amp;nbsp;FixtureContextAware so that we can
use nice syntax in our tests. 
&lt;/p&gt;
&lt;p&gt;
Here is the &lt;a href="/download/FluentFixtures.zip"&gt;source&lt;/a&gt;&amp;nbsp;for the basic framework.
Let me know what you think.&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=f507cec2-8153-4b4b-aa14-f04b376299cf" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,f507cec2-8153-4b4b-aa14-f04b376299cf.aspx</comments>
      <category>development</category>
      <category>ideas</category>
      <category>nhibernate</category>
      <category>source</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=6164281b-37de-46f3-8206-0233e604b6b4</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,6164281b-37de-46f3-8206-0233e604b6b4.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,6164281b-37de-46f3-8206-0233e604b6b4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6164281b-37de-46f3-8206-0233e604b6b4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.jameskovacs.com/">James Kovacs</a> replied to one of my many NHibernate
Optimization Ramblings:
</p>
        <p>
          <em>In the first example, how do you know that most customers are unimportant until
you fetch them from the database? You have an additional problem that generally NHibernate
sessions are short - especially in web apps or web services. So when do you reset
your fetching strategy. What if one portion of your code uses different customer properties
than another? The adaptive fetcher needs to do a lot of analysis of your post-fetch
code paths or monitor the behaviour of the application as it executes. As it stands,
NHibernate has a lot of options besides adaptive queries, which I believe are better
including projections (since you as a programmer know the data you need) and lazy-loading
of properties - both collections and objects. There are probably others. We're talking
about saving milliseconds on DB queries when a round-trip to the DB is at least an
order of magnitude greater. I personally feel that adaptive queries would require
a lot of work for little gain. I call YAGNI.</em>
        </p>
        <p>
This is probably my fault, but his first question tells me he doesn't quite understand
what I'm trying to propose. It doesn't matter if customers are important or unimportant.
Once the both code paths are hit and the strategy fully adapts, it is adapted and
it will fetch a superset of fields that are required for that query in the context
it is called. There's no reason to ever reset that strategy unless the code changes.  
</p>
        <p>
Context is another important aspect of the adaptive queries, and I'm not sure how
I'd implement it. At the moment I'm thinking that something along the line of scopes
(nested or single level) so for each scope/query combination there would be a strategy.
That's the answer to his second issue. The only analysis it needs to do is it needs
to pay attention to what properties are hit on the entities it fetched by proxying
that entity. That's it. No instrumentation, parsing, or any other crazy stuff. 
</p>
        <p>
 
</p>
        <pre class="csharpcode">
          <span class="kwrd">using</span> (Query.Scope(<span class="str">"Print
Customer Stuff"</span>)) { customers = LoadCustomers(); ... }</pre>
        <p>
As for the YAGNI assertion, I understand why you'd call YAGNI on the 2-12ms standard
savings I showed in my tests, but You Already Do Need It at times (we use
projections for just this) it's just that this would be automatic and require less
maintenance and you wouldn't have to choose to do it. It would be free savings
and require less manual optimization. If someone further down your chain decides they
need to log customer.Name, you don't have to climb back up, find the original query
and add it there. With projections at least you'd know you'd need to add it, but you'd
have to change the query and change your DTO (anonymous types will help with this...
I guess). 
</p>
        <p>
My point is, You don't <strong>need</strong> an inversion of control container, it's
just easier. You don't <strong>need</strong> auto mocking containers for tests, it's
just easier and you don't have to change your code when you change your constructor.
You don't even need Mocks, you could write those by hand too. Well, with adaptive
queries you don't have to change your code when you decide to access another field...
or even another collection.... 
</p>
        <p>
You also have to look further than field adapting. There's the potential to adapt collection
initialization as well. No more select N+1's for those devs that don't pay attention,
they'd just go away like magic. And yes, I realize hand optimized queries will generally
prevail, but adaptively optimized queries will be <strong>free</strong>. 
</p>
        <p>
Today, Jacob had another great idea for a use of adaptive queries. We often do this
in our MonoRail actions:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">void</span> ShowCourseEnrollments([EntityParameter]
User user) { <span class="kwrd">foreach</span> (CourseEnrollment ce <span class="kwrd">in</span> user.CourseEnrollments)
{ <span class="rem">// Do something with ce.Course.Number</span> } }</pre>
        <p>
This probably requires a bit of explanation. Basically, when someone goes to the url <em>/Controller/ShowCourseEnrollments.rails?user=1</em>,
MR's databinding (and our EntityParameter binder) will do an NHibernate session.Load&lt;User&gt;(1).
At this time, the db hasn't been hit. As soon as we start enumerating user.CourseEnrollments,
we're select N+1ing. Furthermore, we could potentially be doing another fetch for
ce.Course. The solution to this is to either change your mapping to always fetch these
things (bad idea anyone?) or to do something like this:
</p>
        <pre class="csharpcode">user = UserDao.FetchUserWithEnrollments(user.Id);</pre>
        <p>
Well, what if adaptive queries kicked in at the databind, and instead of adding that
method to your dao, you just got what you needed? Sure, YAGNI, but You Are Gonna Want
It... if I or someone ever implements it.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=6164281b-37de-46f3-8206-0233e604b6b4" />
      </body>
      <title>Adaptive Queries... YAGNI?</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,6164281b-37de-46f3-8206-0233e604b6b4.aspx</guid>
      <link>http://blog.eleutian.com/2007/08/28/AdaptiveQueriesYAGNI.aspx</link>
      <pubDate>Tue, 28 Aug 2007 05:11:52 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.jameskovacs.com/"&gt;James Kovacs&lt;/a&gt; replied to one of my many NHibernate
Optimization Ramblings:
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;In the first example, how do you know that most customers are unimportant until
you fetch them from the database? You have an additional problem that generally NHibernate
sessions are short - especially in web apps or web services. So when do you reset
your fetching strategy. What if one portion of your code uses different customer properties
than another? The adaptive fetcher needs to do a lot of analysis of your post-fetch
code paths or monitor the behaviour of the application as it executes. As it stands,
NHibernate has a lot of options besides adaptive queries, which I believe are better
including projections (since you as a programmer know the data you need) and lazy-loading
of properties - both collections and objects. There are probably others. We're talking
about saving milliseconds on DB queries when a round-trip to the DB is at least an
order of magnitude greater. I personally feel that adaptive queries would require
a lot of work for little gain. I call YAGNI.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
This is probably my fault, but his first question tells me he doesn't quite understand
what I'm trying to propose. It doesn't matter if customers are important or unimportant.
Once the both code paths are hit and the strategy fully adapts, it is adapted and
it will fetch a superset of fields that are required for that query in the context
it is called. There's no reason to ever reset that strategy unless the code changes.&amp;nbsp; 
&lt;p&gt;
Context is another important aspect of the adaptive queries, and I'm not sure how
I'd implement it. At the moment I'm thinking that something along the line of scopes
(nested or single level) so for each scope/query combination there would be a strategy.
That's the answer to his second issue. The only analysis it needs to do is it needs
to pay attention to what properties are hit on the entities it fetched by proxying
that entity. That's it. No instrumentation, parsing, or any other crazy stuff. 
&lt;p&gt;
&amp;nbsp;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (Query.Scope(&lt;span class="str"&gt;"Print
Customer Stuff"&lt;/span&gt;)) { customers = LoadCustomers(); ... }&lt;/pre&gt;
&lt;p&gt;
As for the YAGNI assertion, I understand why you'd call YAGNI on the 2-12ms standard
savings I showed in my tests, but&amp;nbsp;You Already Do Need It at times&amp;nbsp;(we use
projections for just this) it's just that this would be automatic and require less
maintenance and you wouldn't have to choose to do it.&amp;nbsp;It would be free savings
and require less manual optimization. If someone further down your chain decides they
need to log customer.Name, you don't have to climb back up, find the original query
and add it there. With projections at least you'd know you'd need to add it, but you'd
have to change the query and change your DTO (anonymous types will help with this...
I guess). 
&lt;p&gt;
My point is, You don't &lt;strong&gt;need&lt;/strong&gt; an inversion of control container, it's
just easier. You don't &lt;strong&gt;need&lt;/strong&gt; auto mocking containers for tests, it's
just easier and you don't have to change your code when you change your constructor.
You don't even need Mocks, you could write those by hand too. Well, with adaptive
queries you don't have to change your code when you decide to access another field...
or even another collection.... 
&lt;p&gt;
You also have to look further than field adapting. There's the potential to adapt&amp;nbsp;collection
initialization as well. No more select N+1's for those devs that don't pay attention,
they'd just go away like magic. And yes, I realize hand optimized queries will generally
prevail, but adaptively optimized queries will be &lt;strong&gt;free&lt;/strong&gt;. 
&lt;p&gt;
Today, Jacob had another great idea for a use of adaptive queries. We often do this
in our MonoRail actions:&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ShowCourseEnrollments([EntityParameter]
User user) { &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (CourseEnrollment ce &lt;span class="kwrd"&gt;in&lt;/span&gt; user.CourseEnrollments)
{ &lt;span class="rem"&gt;// Do something with ce.Course.Number&lt;/span&gt; } }&lt;/pre&gt;
&lt;p&gt;
This probably requires a bit of explanation. Basically, when someone goes to the url &lt;em&gt;/Controller/ShowCourseEnrollments.rails?user=1&lt;/em&gt;,
MR's databinding (and our EntityParameter binder) will do an NHibernate session.Load&amp;lt;User&amp;gt;(1).
At this time, the db hasn't been hit. As soon as we start enumerating user.CourseEnrollments,
we're select N+1ing. Furthermore, we could potentially be doing another fetch for
ce.Course. The solution to this is to either change your mapping to always fetch these
things (bad idea anyone?) or to do something like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;user = UserDao.FetchUserWithEnrollments(user.Id);&lt;/pre&gt;
&lt;p&gt;
Well, what if adaptive queries kicked in at the databind, and instead of adding that
method to your dao, you just got what you needed? Sure, YAGNI, but You Are Gonna Want
It... if I or someone ever implements it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=6164281b-37de-46f3-8206-0233e604b6b4" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,6164281b-37de-46f3-8206-0233e604b6b4.aspx</comments>
      <category>development</category>
      <category>ideas</category>
      <category>nhibernate</category>
      <category>open source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=91c63cff-4af1-4e2b-992c-b88de2fba6e0</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,91c63cff-4af1-4e2b-992c-b88de2fba6e0.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,91c63cff-4af1-4e2b-992c-b88de2fba6e0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=91c63cff-4af1-4e2b-992c-b88de2fba6e0</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
Ayende posted a great comment with some questions about adaptive fetching. Here are
his questions and my responses:
</p>
          <p>
            <em>Let us assume this:<br />
customers = LoadCustomers();<br />
for customer in customers:<br />
if customer.IsImportant:<br />
print customer.Birthday<br />
else:<br />
print customer.CurrentCharge<br />
What would the adaptive fetching do in this case?<br />
Assume that you have started with mostly unimportant customers and then moved to important
customers?<br />
The amount of queries that would be generated is prohibitive.</em>
          </p>
          <p>
In this scenario, adaptive fetching would generate a query that pulled IsImportant
and CurrentCharge for unimportant customers. As soon as a single Important customer
ran through this code, the query would change to fetch IsImportant, CurrentCharge
and Birthday. It would also immediately lazy load *all* missing properties from the
original query for all customers originally queried, and continue to track accessed
properties. That's only one additional query for each differing codepath, and that's
only the first time its hit. From that point on, until the query was reset (version
upgrade, app restart if it's not persisted, manually, etc), then you would have all
you needed for all codepaths.
</p>
          <p>
            <em>Another problem that you have here is that you do a query like:<br />
"select u.Name, u.Email from Customer" and the query actually returns you Name,Email,
Address, Photo.<br />
That violates the law of least surprise fairly drastically.</em>
          </p>
          <p>
I don't see adaptive queries having that syntax. I'm thinking more along the lines
of "select ??? from customer"  or "select what  i need from customer", etc.
Specifying what you're looking for puts you right back into projections. If you want
to be specific, use projections.
</p>
          <p>
            <em>Finally, I think that a much easier solution than trying to eek a few more milliseconds
from a DB query is not to go to the DB at all. Utilize NH's caching abilities, and
you'll get a significant performance benefit for little cost.</em>
          </p>
          <p>
I agree that most of the time you'd get more benefit from caching than squeezing some
time out of the db. I just think it gets us one step closer to making mapping between
objects and the db more friendly to both sides of the map, and gives developers a
tool that works automatically "for free". I'm sure it'd also make DBAs happier that
their devs aren't just doing select * all the time.
</p>
          <p>
            <em>Now, if you still want adaptive fetching, the best way to get it is to help build
the HQL Parser, which would generate a human workable AST.</em>
          </p>
          <p>
Agreed. I guess if we work on rewriting chunks of NHibernate the code would become
manageable eh? So yeah, I'll stop blathering and try and start contributing.
</p>
        </blockquote>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=91c63cff-4af1-4e2b-992c-b88de2fba6e0" />
      </body>
      <title>Optimizing NHibernate pt. 4</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,91c63cff-4af1-4e2b-992c-b88de2fba6e0.aspx</guid>
      <link>http://blog.eleutian.com/2007/08/27/OptimizingNHibernatePt4.aspx</link>
      <pubDate>Mon, 27 Aug 2007 06:44:50 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
Ayende posted a great comment with some questions about adaptive fetching. Here are
his questions and my responses:
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Let us assume this:&lt;br&gt;
customers = LoadCustomers();&lt;br&gt;
for customer in customers:&lt;br&gt;
if customer.IsImportant:&lt;br&gt;
print customer.Birthday&lt;br&gt;
else:&lt;br&gt;
print customer.CurrentCharge&lt;br&gt;
What would the adaptive fetching do in this case?&lt;br&gt;
Assume that you have started with mostly unimportant customers and then moved to important
customers?&lt;br&gt;
The amount of queries that would be generated is prohibitive.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
In this scenario, adaptive fetching would generate a query that pulled IsImportant
and CurrentCharge for unimportant customers. As soon as a single Important customer
ran through this code, the query would change to fetch IsImportant, CurrentCharge
and Birthday. It would also immediately lazy load *all* missing properties from the
original query for all customers originally queried, and continue to track accessed
properties. That's only one additional query for each differing codepath, and that's
only the first time its hit. From that point on, until the query was reset (version
upgrade, app restart if it's not persisted, manually, etc), then you would have all
you needed for all codepaths.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Another problem that you have here is that you do a query like:&lt;br&gt;
"select u.Name, u.Email from Customer" and the query actually returns you Name,Email,
Address, Photo.&lt;br&gt;
That violates the law of least surprise fairly drastically.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I don't see adaptive queries having that syntax. I'm thinking more along the lines
of "select ??? from customer"&amp;nbsp; or "select what&amp;nbsp; i need from customer", etc.
Specifying what you're looking for puts you right back into projections. If you want
to be specific, use projections.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Finally, I think that a much easier solution than trying to eek a few more milliseconds
from a DB query is not to go to the DB at all. Utilize NH's caching abilities, and
you'll get a significant performance benefit for little cost.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I agree that most of the time you'd get more benefit from caching than squeezing some
time out of the db. I just think it gets us one step closer to making mapping between
objects and the db more friendly to both sides of the map, and gives developers a
tool that works automatically "for free". I'm sure it'd also make DBAs happier that
their devs aren't just doing select * all the time.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Now, if you still want adaptive fetching, the best way to get it is to help build
the HQL Parser, which would generate a human workable AST.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
Agreed. I guess if we work on rewriting chunks of NHibernate the code would become
manageable eh? So yeah, I'll stop blathering and try and start contributing.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=91c63cff-4af1-4e2b-992c-b88de2fba6e0" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,91c63cff-4af1-4e2b-992c-b88de2fba6e0.aspx</comments>
      <category>development</category>
      <category>nhibernate</category>
      <category>open source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=069e6084-79aa-4340-8608-ea9aaa9199b7</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,069e6084-79aa-4340-8608-ea9aaa9199b7.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,069e6084-79aa-4340-8608-ea9aaa9199b7.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=069e6084-79aa-4340-8608-ea9aaa9199b7</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Oren <a href="http://ayende.com/Blog/archive/2007/08/26/Optimizing-NHibernate.aspx">replied</a> to
my <a href="http://blog.eleutian.com/2007/08/25/NHibernateIsGreatUnlessYouWantToOptimize.aspx">original
post</a> about features I feel that NHibernate is missing. I think he may have
misinterpreted some of my wants so I'll run through and reply in order.
</p>
        <p>
          <strong>Lazy Field Initialization</strong>
        </p>
        <p>
Obviously grabbing each field as it was accessed would be ludicrous. Lazy field initialization
is simply something that needs to be there in order for partial object queries to
work properly. It's the same idea as deciding whether or not to fetch a child collection.
You query for it if you need it, you don't if you don't, and if you end up using the
child collection when you didn't fetch it, it will get lazy loaded. I'm of the
opinion that lazy loads are a smell, and if you hit them in anything but a border
case, you're doing something wrong and you should be fetching what you need.  
</p>
        <p>
I'm fine with specifying defaults on fields (don't load Photo unless I ask for it
explicitly), but Lazy Field Initialization really would shine in partial
queries.
</p>
        <p>
          <strong>Partial object queries</strong>
        </p>
        <p>
UserSummaryDetails is not User. Let's say I have a ReminderService that has a SendMailTo
method that takes a User and a message. If all that method needs is that User's name
and email address, why should I allow NHibernate to query for the entire user? It's
just wasteful. Furthermore, it'd be rather tedious to have multiple Value Objects
like UserSummaryDetails in your codebase just to support the different scenarios you
want. 
</p>
        <p>
If you add Lazy Field Initialization and real partial object queries to the mix, you'd
be able to query for a group of Users that just have email and name populated, and
if you happen to need address, it'd requery for all of the user's addresses. You can
even take it further and requery for all of the fields missing from the original user
query. NHibernate should also probably slap you on the wrist (by logging it). I think
I'm going to find a way to start logging all lazy loads so that we can investigate
them.
</p>
        <p>
Using objects like UserSummaryDetails eliminates a good chunk of the usefulness of
the domain. That is,  you can no longer use the methods on the User object itself
or any domain services that would consume that object.
</p>
        <p>
So really what I want is the syntax I mentioned in my original post, which would just
reuse NHibernate's field access strategy to set the appropriate fields. Any fields
that were not set at that time would be flagged for lazy loading. On flush, only those
fields that were set would be compared. Combine this with <a href="http://blog.eleutian.com/2007/06/01/AdaptiveFetchingStrategiesInORMs.aspx">Adaptive
Fetching Strategies</a> and you've got a very powerful, self optimizing and flexible
query mechanism that leaves you free to use your Domain as you wish.
</p>
        <p>
          <strong>Read Only Queries</strong>
        </p>
        <p>
Maybe it would make sense to give two sessions to each Dao, one of them that handled
read only queries and one that handled read write queries. That's a good idea, but
I still think it's something that should be built in (Hibernate does it). <br /></p>
        <p>
          <strong>Join Qualifiers</strong>
        </p>
        <p>
I don't remember what the query was, but there was a reporting query I was trying
to do involving multiple left joins that I just couldn't get to work with HQL. I was
able to do it in SQL no problem by qualifying one of the joins, but every attempt
in HQL hit a wall. Good point about having to support multiple db's. It's still something
I'd like to see eventually.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=069e6084-79aa-4340-8608-ea9aaa9199b7" />
      </body>
      <title>Optimizing NHibernate pt. 2</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,069e6084-79aa-4340-8608-ea9aaa9199b7.aspx</guid>
      <link>http://blog.eleutian.com/2007/08/26/OptimizingNHibernatePt2.aspx</link>
      <pubDate>Sun, 26 Aug 2007 00:48:04 GMT</pubDate>
      <description>&lt;p&gt;
Oren &lt;a href="http://ayende.com/Blog/archive/2007/08/26/Optimizing-NHibernate.aspx"&gt;replied&lt;/a&gt;&amp;nbsp;to
my &lt;a href="http://blog.eleutian.com/2007/08/25/NHibernateIsGreatUnlessYouWantToOptimize.aspx"&gt;original
post&lt;/a&gt;&amp;nbsp;about features I feel that NHibernate is missing. I think he may have
misinterpreted some of my wants so I'll run through and reply in order.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Lazy Field Initialization&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Obviously grabbing each field as it was accessed would be ludicrous. Lazy field initialization
is simply something that needs to be there in order for partial object queries to
work properly. It's the same idea as deciding whether or not to fetch a child collection.
You query for it if you need it, you don't if you don't, and if you end up using the
child collection when you didn't fetch it,&amp;nbsp;it will get lazy loaded. I'm of the
opinion that lazy loads are a smell, and if you hit them in anything but a border
case, you're doing something wrong and you should be fetching what you need.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
I'm fine with specifying defaults on fields (don't load Photo unless I ask for it
explicitly), but&amp;nbsp;Lazy Field Initialization&amp;nbsp;really would shine in partial
queries.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Partial object queries&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
UserSummaryDetails is not User. Let's say I have a ReminderService that has a SendMailTo
method that takes a User and a message. If all that method needs is that User's name
and email address, why should I allow NHibernate to query for the entire user? It's
just wasteful. Furthermore, it'd be rather tedious to have multiple Value Objects
like UserSummaryDetails in your codebase just to support the different scenarios you
want. 
&lt;/p&gt;
&lt;p&gt;
If you add Lazy Field Initialization and real partial object queries to the mix, you'd
be able to query for a group of Users that just have email and name populated, and
if you happen to need address, it'd requery for all of the user's addresses. You can
even take it further and requery for all of the fields missing from the original user
query. NHibernate should also probably slap you on the wrist (by logging it). I think
I'm going to find a way to start logging all lazy loads so that we can investigate
them.
&lt;/p&gt;
&lt;p&gt;
Using objects like UserSummaryDetails eliminates a good chunk of the usefulness of
the domain. That is,&amp;nbsp; you can no longer use the methods on the User object itself
or any domain services that would consume that object.
&lt;/p&gt;
&lt;p&gt;
So really what I want is the syntax I mentioned in my original post, which would just
reuse NHibernate's field access strategy to set the appropriate fields. Any fields
that were not set at that time would be flagged for lazy loading. On flush, only those
fields that were set would be compared. Combine this with &lt;a href="http://blog.eleutian.com/2007/06/01/AdaptiveFetchingStrategiesInORMs.aspx"&gt;Adaptive
Fetching Strategies&lt;/a&gt;&amp;nbsp;and you've got a very powerful, self optimizing and flexible
query mechanism that leaves you free to use your Domain as you wish.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Read Only Queries&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Maybe it would make sense to give two sessions to each Dao, one of them that handled
read only queries and one that handled read write queries. That's a good idea, but
I still think it's something that should be built in (Hibernate does it).&amp;nbsp;&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Join Qualifiers&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I don't remember what the query was, but there was a reporting query I was trying
to do involving multiple left joins that I just couldn't get to work with HQL. I was
able to do it in SQL no problem by qualifying one of the joins, but every attempt
in HQL hit a wall. Good point about having to support multiple db's. It's still something
I'd like to see eventually.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=069e6084-79aa-4340-8608-ea9aaa9199b7" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,069e6084-79aa-4340-8608-ea9aaa9199b7.aspx</comments>
      <category>development</category>
      <category>nhibernate</category>
      <category>open source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=3eb51e05-e80d-4bff-bf68-69be9d5140a0</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,3eb51e05-e80d-4bff-bf68-69be9d5140a0.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,3eb51e05-e80d-4bff-bf68-69be9d5140a0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=3eb51e05-e80d-4bff-bf68-69be9d5140a0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.amazon.com/Server-2005-Database-Design-Optimization/dp/1590595297/ref=pd_bbs_sr_2/104-6956634-1071903?ie=UTF8&amp;s=books&amp;qid=1188067817&amp;sr=1-2" atomicselection="true">
            <img alt="sqldavidson" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/NHibernateisgrea.unlessyouwanttooptimize_B063/sqldavidson_1.gif" align="right" />
          </a> First
I want to admit something somewhat embarrassing: I don't know enough about databases.
Until coming here, I've always had a DBA that could handle that. I've started to realize
now how important it is for all developers to have a much better understanding about
databases than I do. So I picked up <a href="http://www.amazon.com/Server-2005-Database-Design-Optimization/dp/1590595297/ref=pd_bbs_sr_2/104-6956634-1071903?ie=UTF8&amp;s=books&amp;qid=1188067817&amp;sr=1-2">this
book</a>. I was really looking to better understand optimization, especially knowing
when to create indexes and optimizing queries. Though the book only had a few chapters
on the subject, I felt like it greatly increased my understanding on the subject.
</p>
        <p>
A few months ago I wrote about <a href="http://blog.eleutian.com/2007/06/01/AdaptiveFetchingStrategiesInORMs.aspx">Adaptive
Fetching Strategies</a>. At that time I had little knowledge about covering indexes
and how they improve performance. Oh well, it's just more reason something like that
should be implemented. Before we even get to that however, NHibernate needs to have
a few features added to it in order to be useful in scenarios where performance is
important.
</p>
        <p>
Remember folks, <a href="http://weblogs.asp.net/jgalloway/archive/2007/07/18/the-real-reason-select-queries-are-bad-index-coverage.aspx">SELECT
* is bad</a>. NHibernate is for effectively always doing a SELECT * when you're asking
for an object when it comes to index coverage. Yes, it specifies columns so it doesn't
have <strong>all</strong> the problems associated with SELECT *, but it is still decidedly
less performant than querying for only what you want. Compound this with the extra
overhead the unneeded columns add during a flush, and you've got a pretty compelling
argument to only query for what you need.
</p>
        <p>
OK, so you've decided that you only want to query for what you need and you're using
NHibernate. Well, you can do that... kind of. Not much differently than you can if
you just used ADO.NET and DataSets though. NHibernate doesn't support lazy field initialization.
This means that if you query for only username and email address from your user table,
you don't get back a User. You get back a List of object[]'s. Arguably a list of object[]'s
is less functional than a DataSet. Combine this with the fact that you're querying
using HQL, which has a subset of functionality, predictability, and therefore optimizability
of real SQL, and you start to see a big hole in whole ORM thing... if you want to
optimize your pages. Yes, HQL does make writing queries more pretty and more in the
domain, but it would be nice if it supported things like "on".
</p>
        <p>
Obviously optimizing too early is evil, but we've had several pages now where we've
needed to optimize, and just resorted to querying for a table of values. No longer
are we querying for objects, that's just not performant enough. Especially in display
scenarios where you're not making changes to anything as NHibernate lacks the ability
to do read-only queries, so you end up with that (very) expensive flush unless you
change your flushing strategy to be manual (awkward) or you detach your objects from
the session (also awkward).
</p>
        <p>
So in short, I feel NHibernate (and any ORM for that matter) needs the following features
to really be optimization friendly:
</p>
        <ul>
          <li>
Lazy field initialization 
</li>
          <li>
Querying for partial objects: <strong>select u(Username, Email) from User u</strong></li>
          <li>
Read-only queries that do not get flushed. 
</li>
          <li>
Join qualifiers (on in T-SQL)</li>
        </ul>
        <p>
And yeah, I know it's open source and I could just do it myself, but I have nightmares
about that codebase, and I hardly have the time to implement such large features.
All I have the time to do is complain and wish :)
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=3eb51e05-e80d-4bff-bf68-69be9d5140a0" />
      </body>
      <title>NHibernate is great... unless you want to optimize</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,3eb51e05-e80d-4bff-bf68-69be9d5140a0.aspx</guid>
      <link>http://blog.eleutian.com/2007/08/25/NHibernateIsGreatUnlessYouWantToOptimize.aspx</link>
      <pubDate>Sat, 25 Aug 2007 19:28:29 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.amazon.com/Server-2005-Database-Design-Optimization/dp/1590595297/ref=pd_bbs_sr_2/104-6956634-1071903?ie=UTF8&amp;amp;s=books&amp;amp;qid=1188067817&amp;amp;sr=1-2" atomicselection="true"&gt;&lt;img alt="sqldavidson" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/NHibernateisgrea.unlessyouwanttooptimize_B063/sqldavidson_1.gif" align="right"&gt;&lt;/a&gt; First
I want to admit something somewhat embarrassing: I don't know enough about databases.
Until coming here, I've always had a DBA that could handle that. I've started to realize
now how important it is for all developers to have a much better understanding about
databases than I do. So I picked up &lt;a href="http://www.amazon.com/Server-2005-Database-Design-Optimization/dp/1590595297/ref=pd_bbs_sr_2/104-6956634-1071903?ie=UTF8&amp;amp;s=books&amp;amp;qid=1188067817&amp;amp;sr=1-2"&gt;this
book&lt;/a&gt;. I was really looking to better understand optimization, especially knowing
when to create indexes and optimizing queries. Though the book only had a few chapters
on the subject, I felt like it greatly increased my understanding on the subject.
&lt;/p&gt;
&lt;p&gt;
A few months ago I wrote about &lt;a href="http://blog.eleutian.com/2007/06/01/AdaptiveFetchingStrategiesInORMs.aspx"&gt;Adaptive
Fetching Strategies&lt;/a&gt;. At that time I had little knowledge about covering indexes
and how they improve performance. Oh well, it's just more reason something like that
should be implemented. Before we even get to that however, NHibernate needs to have
a few features added to it in order to be useful in scenarios where performance is
important.
&lt;/p&gt;
&lt;p&gt;
Remember folks, &lt;a href="http://weblogs.asp.net/jgalloway/archive/2007/07/18/the-real-reason-select-queries-are-bad-index-coverage.aspx"&gt;SELECT
* is bad&lt;/a&gt;. NHibernate is for effectively always doing a SELECT * when you're asking
for an object when it comes to index coverage. Yes, it specifies columns so it doesn't
have &lt;strong&gt;all&lt;/strong&gt; the problems associated with SELECT *, but it is still decidedly
less performant than querying for only what you want. Compound this with the extra
overhead the unneeded columns add during a flush, and you've got a pretty compelling
argument to only query for what you need.
&lt;/p&gt;
&lt;p&gt;
OK, so you've decided that you only want to query for what you need and you're using
NHibernate. Well, you can do that... kind of. Not much differently than you can if
you just used ADO.NET and DataSets though. NHibernate doesn't support lazy field initialization.
This means that if you query for only username and email address from your user table,
you don't get back a User. You get back a List of object[]'s. Arguably a list of object[]'s
is less functional than a DataSet. Combine this with the fact that you're querying
using HQL, which has a subset of functionality, predictability, and therefore optimizability
of real SQL, and you start to see a big hole in whole ORM thing... if you want to
optimize your pages. Yes, HQL does make writing queries more pretty and more in the
domain, but it would be nice if it supported things like "on".
&lt;/p&gt;
&lt;p&gt;
Obviously optimizing too early is evil, but we've had several pages now where we've
needed to optimize, and&amp;nbsp;just resorted to querying for a table of values. No longer
are we querying for objects, that's just not performant enough. Especially in display
scenarios where you're not making changes to anything as NHibernate lacks the ability
to do read-only queries, so you end up with that (very) expensive flush unless you
change your flushing strategy to be manual (awkward) or you detach your objects from
the session (also awkward).
&lt;/p&gt;
&lt;p&gt;
So in short, I feel NHibernate (and any ORM for that matter) needs the following features
to really be optimization friendly:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Lazy field initialization 
&lt;li&gt;
Querying for partial objects: &lt;strong&gt;select u(Username, Email) from User u&lt;/strong&gt; 
&lt;li&gt;
Read-only queries that do not get flushed. 
&lt;li&gt;
Join qualifiers (on in T-SQL)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
And yeah, I know it's open source and I could just do it myself, but I have nightmares
about that codebase, and I hardly have the time to implement such large features.
All I have the time to do is complain and&amp;nbsp;wish&amp;nbsp;:)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=3eb51e05-e80d-4bff-bf68-69be9d5140a0" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,3eb51e05-e80d-4bff-bf68-69be9d5140a0.aspx</comments>
      <category>annoyances</category>
      <category>development</category>
      <category>nhibernate</category>
      <category>open source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=08512caa-c081-46a6-9e60-444e053dae5b</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,08512caa-c081-46a6-9e60-444e053dae5b.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,08512caa-c081-46a6-9e60-444e053dae5b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=08512caa-c081-46a6-9e60-444e053dae5b</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There's a good back and forth going on about something I've been thinking about for
quite a while. <a href="http://scruffylookingcatherder.com">Jacob Proffitt</a> is
basically claiming that DI's <a href="http://scruffylookingcatherder.com/archive/2007/08/07/dependency-injection.aspx">primary
benefit</a> is mockability in unit tests and that we should all but ditch it in favor
of using TypeMock to mock our tests. Meanwhile, <a href="http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx">Ayende</a> and <a href="http://kohari.org/2007/08/15/defending-dependency-injection/">Nate
Kohari</a> have been defending DI with reasons like "getting low coupling
between our objects" (<a href="http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx">Ayende</a>)
and "simply put, dependency injection makes your code easier to change"
(<a href="http://kohari.org/2007/08/15/defending-dependency-injection/">Nate</a>). 
</p>
        <p>
Well, I think I'm just going to have to agree with both of them... to an extent. I
agree that DI promotes loose coupling, but I disagree that TypeMock is too powerful
(even though I said just that in our <a href="http://www.hanselman.com/blog/HanselminutesPodcast55MonoRailAsAlternativeASPNET.aspx">podcast
with Hanselman</a>, I'm allowed to change my mind). I think that DI has its place,
and is not a Silver Bullet. There have been a number of times when I was refactoring
a class, pulling out a method object, or moving something to a a sub service (in my
attempts to adhere to the Single Responsibility Principle) that I've questioned whether
or not that new class warrants all of the following:
</p>
        <ol>
          <li>
Having an interface.</li>
          <li>
Being added to the container and being injected.</li>
          <li>
Writing new tests for just that, even though tests were already passing and will continue
to pass for its parent object with the same coverage %.</li>
          <li>
Being mocked out of the original tests.</li>
        </ol>
        <p>
The problem with doing #4 with a framework like Rhino Mocks is that it requires you
to do #3 (obviously) as well as #1 &amp; #2 . You can't mock unless your methods are
virtual or you're interfaced and that mock is injected. With TypeMock I can do #3
and #4 without worrying about adding my new class to the container. So why not add
it to the container? Well, because a great deal of the time You Aren't Gonna Need
It. I think once one other class takes a dependency on that service you have enough
justification to control its creation in one place by adding it to the container.
However, a great deal of the time, these one off objects you create to increase readability
and maintainability in your code just end up either changing the way you test your
objects in that you're testing more than one class at a time, or you end up with an
interface and constructor argument explosion <strong>solely</strong> so that you can
mock them in tests. In situations like that, Jacob is right--the only real benefit
is mockability. Maybe we should consider using something like TypeMock in these situations?
Just because something is powerful doesn't mean its evil. We just need to exercise
more caution and use it only in times its warranted. 
</p>
        <p>
That said, I've never actually used TypeMock and this is all purely conjecture based
on what I've read about it. I just think that we can learn something from the Ruby
guys and from people with the viewpoints of <a href="http://www.elilopian.com/">Eli</a> and
Jacob, as long as everyone realizes that neither sides of the camp is producing Silver
Bullets. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=08512caa-c081-46a6-9e60-444e053dae5b" />
      </body>
      <title>Dependency Injection, Type Mock, Windmills and other stuff</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,08512caa-c081-46a6-9e60-444e053dae5b.aspx</guid>
      <link>http://blog.eleutian.com/2007/08/18/DependencyInjectionTypeMockWindmillsAndOtherStuff.aspx</link>
      <pubDate>Sat, 18 Aug 2007 20:11:35 GMT</pubDate>
      <description>&lt;p&gt;
There's a good back and forth going on about something I've been thinking about for
quite a while. &lt;a href="http://scruffylookingcatherder.com"&gt;Jacob Proffitt&lt;/a&gt;&amp;nbsp;is
basically claiming that DI's &lt;a href="http://scruffylookingcatherder.com/archive/2007/08/07/dependency-injection.aspx"&gt;primary
benefit&lt;/a&gt; is mockability in unit tests and that we should all but ditch it in favor
of using TypeMock to mock our tests. Meanwhile, &lt;a href="http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx"&gt;Ayende&lt;/a&gt;&amp;nbsp;and &lt;a href="http://kohari.org/2007/08/15/defending-dependency-injection/"&gt;Nate
Kohari&lt;/a&gt;&amp;nbsp;have been&amp;nbsp;defending DI with reasons like "getting low coupling
between our objects" (&lt;a href="http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx"&gt;Ayende&lt;/a&gt;)
and "simply put,&amp;nbsp;dependency injection&amp;nbsp;makes your code easier to change"
(&lt;a href="http://kohari.org/2007/08/15/defending-dependency-injection/"&gt;Nate&lt;/a&gt;). 
&lt;/p&gt;
&lt;p&gt;
Well, I think I'm just going to have to agree with both of them... to an extent. I
agree that DI promotes loose coupling, but I disagree that TypeMock is too powerful
(even though I said just that in our &lt;a href="http://www.hanselman.com/blog/HanselminutesPodcast55MonoRailAsAlternativeASPNET.aspx"&gt;podcast
with Hanselman&lt;/a&gt;, I'm allowed to change my mind). I think that DI has its place,
and is not a Silver Bullet. There have been a number of times when I was refactoring
a class, pulling out a method object, or moving something to a a sub service (in my
attempts to adhere to the Single Responsibility Principle) that I've questioned whether
or not that new class warrants all of the following:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Having an interface.&lt;/li&gt;
&lt;li&gt;
Being added to the container and being injected.&lt;/li&gt;
&lt;li&gt;
Writing new tests for just that, even though tests were already passing and will continue
to pass for its parent object with the same coverage %.&lt;/li&gt;
&lt;li&gt;
Being mocked out of the original tests.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The problem with doing #4 with a framework like Rhino Mocks is that it requires you
to do #3 (obviously) as well as #1 &amp;amp; #2 . You can't mock unless your methods are
virtual or you're interfaced and that mock is injected. With TypeMock I can do #3
and #4 without worrying about adding my new class to the container. So why not add
it to the container? Well, because a great deal of the time You Aren't Gonna Need
It. I think once one other class takes a dependency on that service you have enough
justification to control its creation in one place by adding it to the container.
However, a great deal of the time, these one off objects you create to increase readability
and maintainability in your code just end up either changing the way you test your
objects in that you're testing more than one class at a time, or you end up with an
interface and constructor argument explosion &lt;strong&gt;solely&lt;/strong&gt; so that you can
mock them in tests. In situations like that, Jacob is right--the only real benefit
is mockability. Maybe we should consider using something like TypeMock in these situations?
Just because something is powerful doesn't mean its evil. We just need to exercise
more caution and use it only in times its warranted. 
&lt;/p&gt;
&lt;p&gt;
That said, I've never actually used TypeMock and this is all purely conjecture based
on what I've read about it. I just think that we can learn something from the Ruby
guys and from people with the viewpoints of &lt;a href="http://www.elilopian.com/"&gt;Eli&lt;/a&gt; and
Jacob, as long as everyone realizes that neither sides of the camp is producing Silver
Bullets. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=08512caa-c081-46a6-9e60-444e053dae5b" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,08512caa-c081-46a6-9e60-444e053dae5b.aspx</comments>
      <category>development</category>
      <category>inversion of control</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=47a0b56f-612e-4b42-9370-038f3141cfd5</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,47a0b56f-612e-4b42-9370-038f3141cfd5.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,47a0b56f-612e-4b42-9370-038f3141cfd5.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=47a0b56f-612e-4b42-9370-038f3141cfd5</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Jacob posted about the <a href="http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx">AutoMockingContainer</a> several
months ago. At that time we didn't really use it, it was just kind of an implementation
of an idea. Well, we've finally started using it in some side projects (<a href="http://blog.eleutian.com/2007/07/30/ReSharperTestDriveV01.aspx">Resharper.TestDrive</a> for
example), and I must say... wow. It is most definitely the way to instantiate your
subject under test most of the time. Why? 
</p>
        <ol>
          <li>
            <strong>It decouples your tests from your constructors. </strong>This means that if
you have multiple TestFixtures for a class and you want to add a new service to your
constructor, you don't have to change a thing in your tests. 
</li>
          <li>
            <strong>It simplifies your tests.</strong> Things are just cleaner when you're
not having to create all your mock services to inject into your subject under test. 
</li>
          <li>
            <strong>It helps reinforce good mock usage. </strong>The default mock strategy is
dynamic mocks. You can override that if you want to, but most tests should (in my
opinion) be written with dynamic mocks. Like Dave <a href="http://laribee.com/blog/2007/08/04/one-mock-per-test/trackback/">talks
about</a> you only really want to set actual expectations on zero or one mock
at a time. Everything else should be more stub-like.</li>
        </ol>
        <p>
I've started to use a base class for all my tests. Let's take a look at <a href="http://blog.eleutian.com/2007/07/30/ReSharperTestDriveV01.aspx">ReSharper.TestDrive's</a> test
base class:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">abstract</span>
          <span class="kwrd">class</span> AutoMockingTests
{ <span class="kwrd">private</span> MockRepository _mocks; <span class="kwrd">private</span> AutoMockingContainer
_container; <span class="kwrd">protected</span> MockRepository Mocks { get { <span class="kwrd">return</span> _mocks;
} } <span class="kwrd">protected</span> AutoMockingContainer Container { get { <span class="kwrd">return</span> _container;
} } [SetUp] <span class="kwrd">public</span><span class="kwrd">void</span> BaseSetup()
{ _mocks = <span class="kwrd">new</span> MockRepository(); _container = <span class="kwrd">new</span> AutoMockingContainer(_mocks);
_container.Initialize(); Setup(); } <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> Setup(); <span class="kwrd">public</span> T
Create&lt;T&gt;() { <span class="kwrd">return</span> _container.Create&lt;T&gt;();
} <span class="kwrd">public</span> T Mock&lt;T&gt;() <span class="kwrd">where</span> T
: <span class="kwrd">class</span> { <span class="kwrd">return</span> _container.Get&lt;T&gt;();
} <span class="kwrd">public</span><span class="kwrd">void</span> Provide&lt;TService,
TImplementation&gt;() { _container.AddComponent(<span class="kwrd">typeof</span>(TImplementation).FullName, <span class="kwrd">typeof</span>(TService), <span class="kwrd">typeof</span>(TImplementation));
} <span class="kwrd">public</span><span class="kwrd">void</span> Provide&lt;TService&gt;(<span class="kwrd">object</span> instance)
{ _container.Kernel.AddComponentInstance(instance.GetType().FullName, <span class="kwrd">typeof</span>(TService),
instance); } } </pre>
        <p>
So what's a test look like with this base class? Let's borrow Dave's <a href="http://laribee.com/blog/2007/08/04/one-mock-per-test/">example</a>.
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> SearchPresenterTests
: AutoMockingTests { <span class="kwrd">private</span> SearchPresenter _presenter; <span class="kwrd">private</span> SearchResultDTO
_fakeResults; <span class="kwrd">public</span><span class="kwrd">override</span><span class="kwrd">void</span> Setup()
{ <span class="kwrd">this</span>._fakeResults = <span class="kwrd">new</span> SearchResultDTO(); <span class="kwrd">this</span>._presenter
= Create&lt;SearchPresenter&gt;(); } [Test] <span class="kwrd">public</span><span class="kwrd">void</span> Can_search_for_customers_by_number_of_orders()
{ <span class="kwrd">using</span> (_mocks.Record()) { Expect .Call(Mock&lt;ISearchService&gt;().GetCustomersByOrderCount(42))
.Return(<span class="kwrd">this</span>._fakeResults); } <span class="kwrd">using</span> (_mocks.Playback())
{ _presenter.SearchByOrderCount(42); } } [Test] <span class="kwrd">public</span><span class="kwrd">void</span> Search_results_are_displayed_to_the_user()
{ <span class="kwrd">using</span> (_mocks.Record()) { mockView.SearchResults = _fakeResults;
SetupResult .For(Mock&lt;ISearchService&gt;().GetCustomersByOrderCount(42)) .Return(_fakeResults);
} <span class="kwrd">using</span> (_mocks.Playback()) { presenter.SearchByOrderCount(42);
} } } </pre>
        <p>
Not bad eh? You can do some more complicated things too. Let's say all your presenters
take a hub service called PresenterServices. Rather than mocking it and its child
services and setting up expectations for each of the children you can just use the
real one and do this:
</p>
        <pre class="csharpcode">      Provide&lt;IPresenterService&gt;(Create&lt;PresenterService&gt;());
      <span class="kwrd">this</span>._presenter
= Create&lt;SearchPresenter&gt;();</pre>
        <p>
Now you can refer to all your hub's child services with the Mock&lt;T&gt;() method.
</p>
        <p>
Ok, so if you made it this far you probably want to check it out for yourself. Thanks
to <a href="http://www.ayende.com/blog">Ayende</a>, the AMC it is now part of <a href="http://sourceforge.net/projects/rhino-tools/">Rhino.Tools</a> so
you can check it out (<tt>svn co <a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk">https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk</a> rhino-tools</tt>)
 and build it yourself or just grab the current trunk build with all the dependencies <a href="/download/Rhino.Testing.zip">here</a>.
Hope Oren doesn't mind me building and linking this... ;)
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=47a0b56f-612e-4b42-9370-038f3141cfd5" />
      </body>
      <title>Using the AutoMockingContainer</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,47a0b56f-612e-4b42-9370-038f3141cfd5.aspx</guid>
      <link>http://blog.eleutian.com/2007/08/05/UsingTheAutoMockingContainer.aspx</link>
      <pubDate>Sun, 05 Aug 2007 19:47:22 GMT</pubDate>
      <description>&lt;p&gt;
Jacob posted about the &lt;a href="http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx"&gt;AutoMockingContainer&lt;/a&gt;&amp;nbsp;several
months ago. At that time we didn't really use it, it was just kind of an implementation
of an idea. Well, we've finally started using it in some side projects (&lt;a href="http://blog.eleutian.com/2007/07/30/ReSharperTestDriveV01.aspx"&gt;Resharper.TestDrive&lt;/a&gt;&amp;nbsp;for
example), and I must say... wow. It is most definitely the way to instantiate your
subject under test most of the time. Why? 
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It decouples your tests from your constructors. &lt;/strong&gt;This means that if
you have multiple TestFixtures for a class and you want to add a new service to your
constructor, you don't have to change a thing in your tests. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It simplifies your tests.&lt;/strong&gt; Things are just&amp;nbsp;cleaner when you're
not having to create all your mock services to inject into your subject under test. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It helps reinforce good mock usage. &lt;/strong&gt;The default mock strategy is
dynamic mocks. You can override that if you want to, but most tests should (in my
opinion) be written with dynamic mocks. Like Dave &lt;a href="http://laribee.com/blog/2007/08/04/one-mock-per-test/trackback/"&gt;talks
about&lt;/a&gt;&amp;nbsp;you only really want to set actual expectations on zero or one mock
at a time. Everything else should be more stub-like.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
I've started to use a base class for all my tests. Let's take a look at &lt;a href="http://blog.eleutian.com/2007/07/30/ReSharperTestDriveV01.aspx"&gt;ReSharper.TestDrive's&lt;/a&gt;&amp;nbsp;test
base class:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AutoMockingTests
{ &lt;span class="kwrd"&gt;private&lt;/span&gt; MockRepository _mocks; &lt;span class="kwrd"&gt;private&lt;/span&gt; AutoMockingContainer
_container; &lt;span class="kwrd"&gt;protected&lt;/span&gt; MockRepository Mocks { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _mocks;
} } &lt;span class="kwrd"&gt;protected&lt;/span&gt; AutoMockingContainer Container { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _container;
} } [SetUp] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; BaseSetup()
{ _mocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); _container = &lt;span class="kwrd"&gt;new&lt;/span&gt; AutoMockingContainer(_mocks);
_container.Initialize(); Setup(); } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup(); &lt;span class="kwrd"&gt;public&lt;/span&gt; T
Create&amp;lt;T&amp;gt;() { &lt;span class="kwrd"&gt;return&lt;/span&gt; _container.Create&amp;lt;T&amp;gt;();
} &lt;span class="kwrd"&gt;public&lt;/span&gt; T Mock&amp;lt;T&amp;gt;() &lt;span class="kwrd"&gt;where&lt;/span&gt; T
: &lt;span class="kwrd"&gt;class&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _container.Get&amp;lt;T&amp;gt;();
} &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Provide&amp;lt;TService,
TImplementation&amp;gt;() { _container.AddComponent(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TImplementation).FullName, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TService), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TImplementation));
} &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Provide&amp;lt;TService&amp;gt;(&lt;span class="kwrd"&gt;object&lt;/span&gt; instance)
{ _container.Kernel.AddComponentInstance(instance.GetType().FullName, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TService),
instance); } } &lt;/pre&gt;
&lt;p&gt;
So what's a test look like with this base class? Let's borrow Dave's &lt;a href="http://laribee.com/blog/2007/08/04/one-mock-per-test/"&gt;example&lt;/a&gt;.
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SearchPresenterTests
: AutoMockingTests { &lt;span class="kwrd"&gt;private&lt;/span&gt; SearchPresenter _presenter; &lt;span class="kwrd"&gt;private&lt;/span&gt; SearchResultDTO
_fakeResults; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()
{ &lt;span class="kwrd"&gt;this&lt;/span&gt;._fakeResults = &lt;span class="kwrd"&gt;new&lt;/span&gt; SearchResultDTO(); &lt;span class="kwrd"&gt;this&lt;/span&gt;._presenter
= Create&amp;lt;SearchPresenter&amp;gt;(); } [Test] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_search_for_customers_by_number_of_orders()
{ &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Record()) { Expect .Call(Mock&amp;lt;ISearchService&amp;gt;().GetCustomersByOrderCount(42))
.Return(&lt;span class="kwrd"&gt;this&lt;/span&gt;._fakeResults); } &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Playback())
{ _presenter.SearchByOrderCount(42); } } [Test] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Search_results_are_displayed_to_the_user()
{ &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Record()) { mockView.SearchResults = _fakeResults;
SetupResult .For(Mock&amp;lt;ISearchService&amp;gt;().GetCustomersByOrderCount(42)) .Return(_fakeResults);
} &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Playback()) { presenter.SearchByOrderCount(42);
} } } &lt;/pre&gt;
&lt;p&gt;
Not bad eh? You can do some more complicated things too. Let's say all your presenters
take a hub service called PresenterServices. Rather than mocking it and its child
services and setting up expectations for each of the children you can just use the
real one and do this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;      Provide&amp;lt;IPresenterService&amp;gt;(Create&amp;lt;PresenterService&amp;gt;());
      &lt;span class="kwrd"&gt;this&lt;/span&gt;._presenter
= Create&amp;lt;SearchPresenter&amp;gt;();&lt;/pre&gt;
&lt;p&gt;
Now you can refer to all your hub's child services with the Mock&amp;lt;T&amp;gt;() method.
&lt;/p&gt;
&lt;p&gt;
Ok, so if you made it this far you probably want to check it out for yourself. Thanks
to &lt;a href="http://www.ayende.com/blog"&gt;Ayende&lt;/a&gt;, the AMC&amp;nbsp;it is now part of &lt;a href="http://sourceforge.net/projects/rhino-tools/"&gt;Rhino.Tools&lt;/a&gt;&amp;nbsp;so
you can check it out (&lt;tt&gt;svn co &lt;a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk"&gt;https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk&lt;/a&gt; rhino-tools&lt;/tt&gt;)
&amp;nbsp;and build it yourself or just grab the current trunk build with all the dependencies&amp;nbsp;&lt;a href="/download/Rhino.Testing.zip"&gt;here&lt;/a&gt;.
Hope Oren doesn't mind me building and linking this... ;)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=47a0b56f-612e-4b42-9370-038f3141cfd5" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,47a0b56f-612e-4b42-9370-038f3141cfd5.aspx</comments>
      <category>development</category>
      <category>inversion of control</category>
      <category>test driven development</category>
      <category>tips</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=4a53babb-a43d-4c2a-8851-88f9fc2e671c</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,4a53babb-a43d-4c2a-8851-88f9fc2e671c.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,4a53babb-a43d-4c2a-8851-88f9fc2e671c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=4a53babb-a43d-4c2a-8851-88f9fc2e671c</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Writing ReSharper.TestDrive was kind of an exercise in tiny classes for me. I didn't
TDD the whole thing because so much of it was just experimenting with ReSharper's
mostly undocumented API and EnvDTE, though I did TDD a good portion of it after my
initial spike. After I got a working prototype implemented I spent a lot of time
refactoring it into tiny classes that for the most part follow the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single
responsibility principle</a>. 
</p>
        <p>
As this is the most code I've ever thrown out into the public at any one time and
it was a bit of an experiment for me, I wanted to take this chance to ask the community
to review my code. If you have the time, feel free to look over the code and tell
me what you think. Likes/dislikes/hates/loves anything is fair game--feel free to
rip on it. Maybe we'll get some interesting discussion out of it.
</p>
        <p>
          <a href="http://blog.eleutian.com/download/ReSharper.TestDrive.v.0.1.Source.zip">Source</a>
        </p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=4a53babb-a43d-4c2a-8851-88f9fc2e671c" />
      </body>
      <title>Request for Code Review - ReSharper.TestDrive</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,4a53babb-a43d-4c2a-8851-88f9fc2e671c.aspx</guid>
      <link>http://blog.eleutian.com/2007/07/30/RequestForCodeReviewReSharperTestDrive.aspx</link>
      <pubDate>Mon, 30 Jul 2007 17:54:52 GMT</pubDate>
      <description>&lt;p&gt;
Writing ReSharper.TestDrive was kind of an exercise in tiny classes for me. I didn't
TDD the whole thing because so much of it was just experimenting with ReSharper's
mostly undocumented API and EnvDTE, though I did TDD a good portion of it after my
initial spike.&amp;nbsp;After I got a working prototype implemented I spent a lot of time
refactoring it into tiny classes that for the most part follow the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single
responsibility principle&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
As this is the most code I've ever thrown out into the public at any one time and
it was a bit of an experiment for me, I wanted to take this chance to ask the community
to review my code. If you have the time, feel free to look over the code and tell
me what you think. Likes/dislikes/hates/loves anything is fair game--feel free to
rip on it. Maybe we'll get some interesting discussion out of it.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/ReSharper.TestDrive.v.0.1.Source.zip"&gt;Source&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=4a53babb-a43d-4c2a-8851-88f9fc2e671c" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,4a53babb-a43d-4c2a-8851-88f9fc2e671c.aspx</comments>
      <category>development</category>
      <category>source</category>
      <category>test driven development</category>
      <category>tools</category>
      <category>resharper</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=9ee7efcc-83eb-4791-8f45-026344c2a9d8</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,9ee7efcc-83eb-4791-8f45-026344c2a9d8.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,9ee7efcc-83eb-4791-8f45-026344c2a9d8.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9ee7efcc-83eb-4791-8f45-026344c2a9d8</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <strong>Note: </strong>You must have ReSharper 3.0.1 in order for this to work.
</p>
        <p>
When you're doing TDD you'll create two classes every time you need one. You'll create
one for the class itself and you'll create one for the tests for that class. <a href="http://www.jetbrains.com/resharper/">ReSharper</a> makes
it a little bit easier by allowing you to write your tests and then alt+enter on your
class under test to create it. Unfortunately it will create the class in your test
project and not your project under test. It may actually create it in that same file
(I don't remember) which means you have to Move to File. Then you have to drag it
over to the project under test and/or change the namespace. Pretty obnoxious for something
we have to do so often.
</p>
        <p>
So... I decided to write a ReSharper plugin to do just that. It'll also create tests
from a class under test (just in case you cheated and created your class first). Heck,
it'll even create all the folders you need to. 
</p>
        <p>
This current version makes a few assumptions about your structure and it's not
configurable at all unless you actually hit the code. Here are the assumptions it
makes:
</p>
        <ul>
          <li>
The tests for Project.Foo live in Project.Foo.Tests. 
</li>
          <li>
Test classes have the "Tests" suffix. 
</li>
          <li>
Test classes live in the same namespace as the classes under test. 
</li>
          <li>
The tests for ClassFoo are in ClassFooTests.</li>
        </ul>
        <p>
I lied when I said it wasn't configurable at all. After you've used it for the first
time it will have created ReSharper file templates that you can edit to customize
what is generated when you create a test or a class under test. Just go to ReSharper&gt;Options&gt;Templates&gt;FileTemplates&gt;User
Templates&gt;TestDrive.
</p>
        <p>
To install it just extract it somewhere and run install.cmd, or just copy the dll
to your %APPDATA%\JetBrains\ReSharper\v3.0\vs8.0\Plugins\Resharper.TestDrive (obviously
you'll need ReSharper installed).
</p>
        <p>
To use it, just have the cursor somewhere within a class that doesn't have a test,
or a test that doesn't have a class under test and hit Alt+Enter, select Create X
Tests... or create X... and hit enter. In order for the light bulb/alt+enter to show
up it will need to be able to find an associated project to the one you're in (Sample.Project.Tests&lt;-&gt;Sample.Project).
</p>
        <p>
I've got more plans for this but I wanted to get it out there to see what you all
thought. Oh, and in case you're wondering, Bunker is just a really light nearly feature-free
IoC container that Jacob wrote in a day for another project we're working on.
</p>
        <p>
          <a href="/download/ReSharper.TestDrive.v.0.1.zip">Binaries</a>
          <br />
          <a href="/download/ReSharper.TestDrive.v.0.1.Source.zip">Source</a>
        </p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=9ee7efcc-83eb-4791-8f45-026344c2a9d8" />
      </body>
      <title>ReSharper.TestDrive v0.1</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,9ee7efcc-83eb-4791-8f45-026344c2a9d8.aspx</guid>
      <link>http://blog.eleutian.com/2007/07/30/ReSharperTestDriveV01.aspx</link>
      <pubDate>Mon, 30 Jul 2007 17:14:15 GMT</pubDate>
      <description>&lt;p&gt;
&lt;strong&gt;Note: &lt;/strong&gt;You must have ReSharper 3.0.1 in order for this to work.
&lt;/p&gt;
&lt;p&gt;
When you're doing TDD you'll create two classes every time you need one. You'll create
one for the class itself and you'll create one for the tests for that class. &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt;&amp;nbsp;makes
it a little bit easier by allowing you to write your tests and then alt+enter on your
class under test to create it. Unfortunately it will create the class in your test
project and not your project under test. It may actually create it in that same file
(I don't remember) which means you have to Move to File. Then you have to drag it
over to the project under test and/or change the namespace. Pretty obnoxious for something
we have to do so often.
&lt;/p&gt;
&lt;p&gt;
So... I decided to write a ReSharper plugin to do just that. It'll also create tests
from a class under test (just in case you cheated and created your class first). Heck,
it'll even create all the folders you need to. 
&lt;/p&gt;
&lt;p&gt;
This current version makes a&amp;nbsp;few assumptions about your structure and it's not
configurable at all unless you actually hit the code. Here are the assumptions it
makes:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The tests for Project.Foo live in Project.Foo.Tests. 
&lt;/li&gt;
&lt;li&gt;
Test classes have the "Tests" suffix. 
&lt;/li&gt;
&lt;li&gt;
Test classes live in the same namespace as the classes under test. 
&lt;/li&gt;
&lt;li&gt;
The tests for ClassFoo are in ClassFooTests.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
I lied when I said it wasn't configurable at all. After you've used it for the first
time it will have created ReSharper file templates that you can edit to customize
what is generated when you create a test or a class under test. Just go to ReSharper&amp;gt;Options&amp;gt;Templates&amp;gt;FileTemplates&amp;gt;User
Templates&amp;gt;TestDrive.
&lt;/p&gt;
&lt;p&gt;
To install it just extract it somewhere and run install.cmd, or just copy the dll
to your %APPDATA%\JetBrains\ReSharper\v3.0\vs8.0\Plugins\Resharper.TestDrive (obviously
you'll need ReSharper installed).
&lt;/p&gt;
&lt;p&gt;
To use it, just have the cursor somewhere within a class that doesn't have a test,
or a test that doesn't have a class under test and hit Alt+Enter, select Create X
Tests... or create X... and hit enter. In order for the light bulb/alt+enter to show
up it will need to be able to find an associated project to the one you're in (Sample.Project.Tests&amp;lt;-&amp;gt;Sample.Project).
&lt;/p&gt;
&lt;p&gt;
I've got more plans for this but I wanted to get it out there to see what you all
thought. Oh, and in case you're wondering, Bunker is just a really light nearly&amp;nbsp;feature-free
IoC container that Jacob wrote in a day for another project we're working on.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="/download/ReSharper.TestDrive.v.0.1.zip"&gt;Binaries&lt;/a&gt;
&lt;br&gt;
&lt;a href="/download/ReSharper.TestDrive.v.0.1.Source.zip"&gt;Source&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=9ee7efcc-83eb-4791-8f45-026344c2a9d8" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,9ee7efcc-83eb-4791-8f45-026344c2a9d8.aspx</comments>
      <category>development</category>
      <category>resharper</category>
      <category>source</category>
      <category>test driven development</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=f16bd97a-8bae-4e5c-8e21-99c86b789efd</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,f16bd97a-8bae-4e5c-8e21-99c86b789efd.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,f16bd97a-8bae-4e5c-8e21-99c86b789efd.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f16bd97a-8bae-4e5c-8e21-99c86b789efd</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So I recently started using the Early Access edition of <a href="http://studios.thoughtworks.com/">ThoughtWorks</a>' <a href="http://studios.thoughtworks.com/mingle-project-intelligence">Mingle</a>.
Let me start out by saying I'm impressed. Mingle is a very clean, very powerful and
most importantly, very flexible approach to project management. We all have our approach
to managing our projects and Mingle allows you to handle many of those methods. I've
spent a few days over the last week configuring our Mingle project to map to our newly
developing process.  It hasn't been entirely smooth sailing, but it's slightly
less than an R1 product so I won't complain... too much :) Here's a list of my thoughts
thus far:
</p>
        <ol>
          <li>
            <strong>Installation</strong> - I've done an install on both Windows and Linux. Both
were relatively painless. The Linux install documentation was missing a few steps
but that has since been fixed. I wish that Mingle could run on IIS and/or Apache.
It's a bit annoying that I have to run it on a separate port than my primary web server,
though Jacob tells me we can proxy a port with Apache, so that'd be nice. 
</li>
          <li>
            <strong>Creating a new project - </strong>The existing templates are nice to allow
you to see what Mingle is capable of. I wouldn't recommend creating your real project
using one of the existing templates as it leaves you with a lot of undeletable properties
you don't need. Start out by creating three test projects, one using each of the three
templates and play around with them until you know what you want to go into yours.
Then create yours from a Blank Project. 
</li>
          <li>
            <strong>Card properties - </strong>It's really nice to be able to specify exactly
what properties you want to be on each of your cards. The property creation UI could
use some help though, it'd be nice if we could specify the values we want at the same
time we're creating the property--but that's just a minor nitpick. 
</li>
          <li>
            <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/wiki.jpg" atomicselection="true">
              <img style="border-width: 0px;" alt="wiki" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/wiki_thumb.jpg" align="right" border="0" height="174" width="240" />
            </a>
            <strong>Wiki
- </strong>Mingle allows for you to create custom wiki pages that have nifty things
like bar graphs and pie charts and tables. My biggest complaint about the wiki is
lack of documentation. Not all of the samples in the documentation seem to work and
the syntax is a little confusing... plus, if you type something wrong the error messages
you get back are less than useless. Fortunately I've managed to get most of the things
I want in our overview wiki, but it took much longer than it should have. Also, there's
no way at the moment to do a burndown chart which would certainly be a nice thing
to have for a lot of the agile methodologies out there. 
</li>
          <li>
            <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/list.jpg" atomicselection="true">
              <img style="border: 0px none ;" alt="list" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/list_thumb.jpg" align="right" border="0" height="89" width="240" />
            </a>
            <strong>List
view - </strong>Mingle has some really simple filtering options for its two views.
You can basically choose a value for each of your properties (or any) and add tags
you'd like to filter by as well. Once you have a view you like, you can save that
view. If you really like the view you can add it as a tab to the top for the whole
team to see. Unfortunately there are some limitations: There's no way to give
an OR for two property values and there's no way to specify that you only want to
see cards with a property that has a null value. I'm hoping they will add these things
eventually. There's also no way to specify "Current User" in the view... which makes
views like "My Bugs" out of reach. Also, there's currently a max page size of 25,
so if you want to do batch operations you can only do them 25 at a time. That's pretty
annoying. In the meantime I can get *most* of my views the way I want them, but
not all of them. 
</li>
          <li>
            <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/grid.jpg" atomicselection="true">
              <img style="border-width: 0px;" alt="grid" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/grid_thumb.jpg" align="right" border="0" height="89" width="240" />
            </a>
            <strong>Grid
view</strong> - OK, this is pretty cool. You can pick a property to group by, and
pick a property to color by. Of course you can specify the same filters as you can
in the list (so the same limitations). What's nice though, is you can drag those cards
around into different values for that grouped property. You can move a Story Card
from New to Open just by dragging it. Pretty slick if you ask me. This view allows
you to make a number of cool views. You can create an Assignment view that lets you
drag unassigned cards to ready and willing developers. You can create an Iteration
planning view that allows you to drag cards around to different Iterations. Or even
an estimation view so you can drag your cards to different story point values. If
you need more detail on a card, you can always just click it. It'll pop up a nice
little window with more information so you don't have to navigate away from the page.
All in all pretty fancy. It would be nice if the lanes were colored... if you have
a long list it'd be easier to tell where you're dragging to when you're at the
bottom. Oh, and where's the sort? I should be able to sort the cards in the grid. 
</li>
          <li>
            <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/transitions.jpg" atomicselection="true">
              <img style="border: 0px none ;" alt="transitions" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/transitions_thumb.jpg" align="right" border="0" height="143" width="240" />
            </a>
            <strong>Transitions</strong> -
These are cool too. These allow you to add buttons to cards that meet specific preconditions.
Clicking that button will set the properties you specify in the transition. Pretty
slick to kind of workflow your process. Unfortunately there are a few things missing
from these as well. You can't specify "Current User" in either the preconditions or
the set area. This means you can't do things like automatically set the Approved By
field when something is Approved. You also can't say that only the user who a Card
is Assigned To can Resolve it. Just adding that feature would make it so much more
powerful. The other thing that is lacking is that the only way to "trigger" these
transitions is to actually click on the transition button. This means that dragging
things around your swim lanes won't trigger these. Yes, that complicates things but
it'd be nice to have some sort of trigger criteria... or even a view that shows "transitionable"
cards that you can drag to transition them. Obviously I'm rambling now, but I think
this is a cool idea and can be expanded upon. 
</li>
          <li>
            <a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/svn.jpg" atomicselection="true">
              <img style="border: 0px none ;" alt="svn" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/svn_thumb.jpg" align="right" border="0" height="123" width="240" />
            </a>
            <strong> SVN
Integration</strong> - Right now we use Trac as a Wiki and to explore our SVN revisions.
Mingle's SVN integration looks pretty slick but is kind of broken. Take a look at
this diff to the right. See all that white? Why is that white? It's not blank lines
when you do a svn diff... where's my context? I don't know if this is a bug or what,
but it's annoying. Other than that, Mingle's SVN integration is reasonable. I like
the fact that you can define keywords so that when you add things like "#245" or "card
245" in your commit log it will automatically reference that commit with the specified
card. 
</li>
          <li>
            <strong>Support</strong> - Eh... It seems the best point of contact with support is
either email (which I haven't had a high enough priority issue to try) or their <a href="https://75.40.150.117/forums/1">forum</a>.
The forum is painfully slow most of the day (read: unusable, but who knows if that's
a routing issue between me and them or just their forum server/software) and doesn't
have a ton of activity. A few of the bugs I reported were acknowledged via email so
that's good, but who knows how seriously the suggestions are taken. There's only one
guy moderating the forum so I'm sure he's busy, but it would be nice to see a bit
more activity and feedback. 
</li>
          <li>
            <strong>Overall impressions</strong> - It's slick. It's flexible. It's... a little
slow. Loading our Bug Board view just now took almost 5.71 seconds and 51 requests.
Eek. Granted I'm not local right now, but it's not much faster when I'm on my Lan.
That said, we're using it. We moved all our bugs off of <a href="http://www.fogcreek.com/FogBugz/">FogBugz</a> and
we're going to give Mingle a shot for now.  It's free for the first 5 users,
so why not? 
</li>
          <li>
            <strong>Wish list</strong> - 
</li>
        </ol>
        <ul>
          <li>
The greatest thing about <a href="http://www.fogcreek.com/FogBugz/">FogBugz</a> is
its email integration. It can check a mail account you specify and automatically create
tickets for messages in that inbox. You can reply to the senders of that mail via
FogBugz interface and you can receive more correspondence through email from the original
sender. It's pretty slick and great for support. Right now we're just using FogBugz
for this purpose. To handle support requests. If something is upgraded to a bug we'll
move it to Mingle... but the copy/paste operation will probably get tedious, so it'd
be nice to have this functionality built into Mingle as well. 
</li>
          <li>
I want to be able to show/hide properties based on another property such as Type.
If my Type is Bug, I should see Bug Status and not Story Status. Right now all cards
have all properties even if they aren't relevant. It's fine that they have them, because
I could change the type, but I don't want to see them. 
</li>
          <li>
I also want to create Backlogs--Priority Queues. Having just a property with a fixed
# of #s for Priority is annoying at best. It's much more natural to keep things in
a sorted order. I want to be able to create a new backlog with a specific filter and
then drag my cards into an order that I want. I then want to be able to sort in that
order. That way I can pop things off of that stack in that order and as new cards
come in I can insert them. I'm very tempted to write this feature on my own... 
</li>
          <li>
Triggers--I mentioned this before, but it's worth mentioning again. I'd like to be
able to specify what happens when I drag a card from one lane to another in a Grid
view. I'd also like to be able to gate that dragging... something can't be dragged
to Ready for Development until it has an Estimate set. Now that I think more about
it, I think the latter would be more useful. I want to be able to specify constraints
on the Cards that will be enforced by all views and editing techniques.</li>
        </ul>
        <p>
Alright, that's more than enough rambling for now. I'd strongly urge you all to go
take a look at Mingle. It's got a ton of potential and I think it's definitely headed
in the right direction. Remember, it's free for Open Source and it's free for the
first five users of a Commercial project. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=f16bd97a-8bae-4e5c-8e21-99c86b789efd" />
      </body>
      <title>Mingle: First Impressions</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,f16bd97a-8bae-4e5c-8e21-99c86b789efd.aspx</guid>
      <link>http://blog.eleutian.com/2007/07/17/MingleFirstImpressions.aspx</link>
      <pubDate>Tue, 17 Jul 2007 02:56:44 GMT</pubDate>
      <description>&lt;p&gt;
So I recently started using the Early Access edition of &lt;a href="http://studios.thoughtworks.com/"&gt;ThoughtWorks&lt;/a&gt;' &lt;a href="http://studios.thoughtworks.com/mingle-project-intelligence"&gt;Mingle&lt;/a&gt;.
Let me start out by saying I'm impressed. Mingle is a very clean, very powerful and
most importantly, very flexible approach to project management. We all have our approach
to managing our projects and Mingle allows you to handle many of those methods. I've
spent a few days over the last week configuring our Mingle project to map to our newly
developing process.&amp;nbsp; It hasn't been entirely smooth sailing, but it's slightly
less than an R1 product so I won't complain... too much :) Here's a list of my thoughts
thus far:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Installation&lt;/strong&gt; - I've done an install on both Windows and Linux. Both
were relatively painless. The Linux install documentation was missing a few steps
but that has since been fixed. I wish that Mingle could run on IIS and/or Apache.
It's a bit annoying that I have to run it on a separate port than my primary web server,
though Jacob tells me we can proxy a port with Apache, so that'd be nice. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creating a new project - &lt;/strong&gt;The existing templates are nice to allow
you to see what Mingle is capable of. I wouldn't recommend creating your real project
using one of the existing templates as it leaves you with a lot of undeletable properties
you don't need. Start out by creating three test projects, one using each of the three
templates and play around with them until you know what you want to go into yours.
Then create yours from a Blank Project. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Card properties - &lt;/strong&gt;It's really nice to be able to specify exactly
what properties you want to be on each of your cards. The property creation UI could
use some help though, it'd be nice if we could specify the values we want at the same
time we're creating the property--but that's just a minor nitpick. 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/wiki.jpg" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="wiki" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/wiki_thumb.jpg" align="right" border="0" height="174" width="240"&gt;&lt;/a&gt;&lt;strong&gt;Wiki
- &lt;/strong&gt;Mingle allows for you to create custom wiki pages that have nifty things
like bar graphs and pie charts and tables. My biggest complaint about the wiki is
lack of documentation. Not all of the samples in the documentation seem to work and
the syntax is a little confusing... plus, if you type something wrong the error messages
you get back are less than useless. Fortunately I've managed to get most of the things
I want in our overview wiki, but it took much longer than it should have. Also, there's
no way at the moment to do a burndown chart which would certainly be a nice thing
to have for a lot of the agile methodologies out there. 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/list.jpg" atomicselection="true"&gt;&lt;img style="border: 0px none ;" alt="list" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/list_thumb.jpg" align="right" border="0" height="89" width="240"&gt;&lt;/a&gt;&lt;strong&gt;List
view - &lt;/strong&gt;Mingle has some really simple filtering options for its two views.
You can basically choose a value for each of your properties (or any) and add tags
you'd like to filter by as well. Once you have a view you like, you can save that
view. If you really like the view you can add it as a tab to the top for the whole
team to see. Unfortunately there are some limitations:&amp;nbsp;There's no way to give
an OR for two property values and there's no way to specify that you only want to
see cards with a property that has a null value. I'm hoping they will add these things
eventually. There's also no way to specify "Current User" in the view... which makes
views like "My Bugs" out of reach. Also, there's currently a max page size of 25,
so if you want to do batch operations you can only do them 25 at a time. That's pretty
annoying.&amp;nbsp;In the meantime I can get *most* of my views the way I want them, but
not all of them. 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/grid.jpg" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="grid" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/grid_thumb.jpg" align="right" border="0" height="89" width="240"&gt;&lt;/a&gt;&lt;strong&gt;Grid
view&lt;/strong&gt; - OK, this is pretty cool. You can pick a property to group by, and
pick a property to color by. Of course you can specify the same filters as you can
in the list (so the same limitations). What's nice though, is you can drag those cards
around into different values for that grouped property. You can move a Story Card
from New to Open just by dragging it. Pretty slick if you ask me. This view allows
you to make a number of cool views. You can create an Assignment view that lets you
drag unassigned cards to ready and willing developers. You can create an Iteration
planning view that allows you to drag cards around to different Iterations. Or even
an estimation view so you can drag your cards to different story point values. If
you need more detail on a card, you can always just click it. It'll pop up a nice
little window with more information so you don't have to navigate away from the page.
All in all pretty fancy. It would be nice if the lanes were colored... if you have
a long list it'd be&amp;nbsp;easier to tell where you're dragging to when you're at the
bottom. Oh, and where's the sort? I should be able to sort the cards in the grid. 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/transitions.jpg" atomicselection="true"&gt;&lt;img style="border: 0px none ;" alt="transitions" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/transitions_thumb.jpg" align="right" border="0" height="143" width="240"&gt;&lt;/a&gt; &lt;strong&gt;Transitions&lt;/strong&gt; -
These are cool too. These allow you to add buttons to cards that meet specific preconditions.
Clicking that button will set the properties you specify in the transition. Pretty
slick to kind of workflow your process. Unfortunately there are a few things missing
from these as well. You can't specify "Current User" in either the preconditions or
the set area. This means you can't do things like automatically set the Approved By
field when something is Approved. You also can't say that only the user who a Card
is Assigned To can Resolve it. Just adding that feature would make it so much more
powerful. The other thing that is lacking is that the only way to "trigger" these
transitions is to actually click on the transition button. This means that dragging
things around your swim lanes won't trigger these. Yes, that complicates things but
it'd be nice to have some sort of trigger criteria... or even a view that shows "transitionable"
cards that you can drag to transition them. Obviously I'm rambling now, but I think
this is a cool idea and can be expanded upon. 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/svn.jpg" atomicselection="true"&gt;&lt;img style="border: 0px none ;" alt="svn" src="http://blog.eleutian.com/content/binary/WindowsLiveWriter/MingleFirstImpressions_11620/svn_thumb.jpg" align="right" border="0" height="123" width="240"&gt;&lt;/a&gt;&lt;strong&gt; SVN
Integration&lt;/strong&gt; - Right now we use Trac as a Wiki and to explore our SVN revisions.
Mingle's SVN integration looks pretty slick but is kind of broken. Take a look at
this diff to the right. See all that white? Why is that white? It's not blank lines
when you do a svn diff... where's my context? I don't know if this is a bug or what,
but it's annoying. Other than that, Mingle's SVN integration is reasonable. I like
the fact that you can define keywords so that when you add things like "#245" or "card
245" in your commit log it will automatically reference that commit with the specified
card. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Support&lt;/strong&gt; - Eh... It seems the best point of contact with support is
either email (which I haven't had a high enough priority issue to try) or their &lt;a href="https://75.40.150.117/forums/1"&gt;forum&lt;/a&gt;.
The forum is painfully slow most of the day (read: unusable, but who knows if that's
a routing issue between me and them or just their forum server/software) and doesn't
have a ton of activity. A few of the bugs I reported were acknowledged via email so
that's good, but who knows how seriously the suggestions are taken. There's only one
guy moderating the forum so I'm sure he's busy, but it would be nice to see a bit
more activity and feedback. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overall impressions&lt;/strong&gt; - It's slick. It's flexible. It's... a little
slow. Loading our Bug Board view just now took almost&amp;nbsp;5.71 seconds and 51 requests.
Eek. Granted I'm not local right now, but it's not much faster when I'm on my Lan.
That said, we're using it. We moved all our bugs off of &lt;a href="http://www.fogcreek.com/FogBugz/"&gt;FogBugz&lt;/a&gt; and
we're going to give Mingle a shot for now.&amp;nbsp; It's free for the first 5 users,
so why not? 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wish list&lt;/strong&gt; - 
&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;
The greatest thing about &lt;a href="http://www.fogcreek.com/FogBugz/"&gt;FogBugz&lt;/a&gt;&amp;nbsp;is
its email integration. It can check a mail account you specify and automatically create
tickets for messages in that inbox. You can reply to the senders of that mail via
FogBugz interface and you can receive more correspondence through email from the original
sender. It's pretty slick and great for support. Right now we're just using FogBugz
for this purpose. To handle support requests. If something is upgraded to a bug we'll
move it to Mingle... but the copy/paste operation will probably get tedious, so it'd
be nice to have this functionality built into Mingle as well. 
&lt;/li&gt;
&lt;li&gt;
I want to be able to show/hide properties based on another property such as Type.
If my Type is Bug, I should see Bug Status and not Story Status. Right now all cards
have all properties even if they aren't relevant. It's fine that they have them, because
I could change the type, but I don't want to see them. 
&lt;/li&gt;
&lt;li&gt;
I also want to create Backlogs--Priority Queues. Having just a property with a fixed
# of #s for Priority is annoying at best. It's much more natural to keep things in
a sorted order. I want to be able to create a new backlog with a specific filter and
then drag my cards into an order that I want. I then want to be able to sort in that
order. That way I can pop things off of that stack in that order and as new cards
come in I can insert them. I'm very tempted to write this feature on my own... 
&lt;/li&gt;
&lt;li&gt;
Triggers--I mentioned this before, but it's worth mentioning again. I'd like to be
able to specify what happens when I drag a card from one lane to another in a Grid
view. I'd also like to be able to gate that dragging... something can't be dragged
to Ready for Development until it has an Estimate set. Now that I think more about
it, I think the latter would be more useful. I want to be able to specify constraints
on the Cards that will be enforced by all views and editing techniques.&lt;/li&gt;
&lt;/ul&gt;
&gt;
&lt;p&gt;
Alright, that's more than enough rambling for now. I'd strongly urge you all to go
take a look at Mingle. It's got a ton of potential and I think it's definitely headed
in the right direction. Remember, it's free for Open Source and it's free for the
first five users of a Commercial project. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=f16bd97a-8bae-4e5c-8e21-99c86b789efd" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,f16bd97a-8bae-4e5c-8e21-99c86b789efd.aspx</comments>
      <category>development</category>
      <category>reviews</category>
      <category>mingle</category>
      <category>agile</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=1c184996-30c1-4c36-a270-9ed5f9405d71</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,1c184996-30c1-4c36-a270-9ed5f9405d71.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,1c184996-30c1-4c36-a270-9ed5f9405d71.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1c184996-30c1-4c36-a270-9ed5f9405d71</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Generally when I'm testing something that has dependencies my test fixture looks something
like this:
</p>
        <pre class="csharpcode">  [TestFixture]
  <span class="kwrd">public</span><span class="kwrd">class</span> FooTests
{ <span class="kwrd">private</span> MockRepository _mocks; <span class="kwrd">private</span> Foo
_foo; <span class="kwrd">private</span> IBar _bar; [SetUp] <span class="kwrd">public</span><span class="kwrd">void</span> Setup()
{ _mocks = <span class="kwrd">new</span> MockRepository(); _bar = _mocks.DynamicMock&lt;IBar&gt;();
_foo = <span class="kwrd">new</span> Foo(_bar); } [Test] <span class="kwrd">public</span><span class="kwrd">void</span> Test()
{ <span class="kwrd">using</span> (_mocks.Unordered()) { SetupResult.For(_bar.ProvideService()).Returns(1);
} _mocks.ReplayAll(); Assert.IsTrue(_foo.DoSomething()); _mocks.VerifyAll(); } } </pre>
        <p>
Granted, I'm starting to use our <a href="http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx">AutoMocking
Container</a> more and more, but that's not the point of this post. As an aside, <a href="http://www.ayende.com/blog">Ayende</a> has
some more <a href="http://www.ayende.com/Blog/archive/2007/06/08/The-Auto-Mocking-Container.aspx">examples</a> on
its usage and has <a href="http://rhino-tools.svn.sourceforge.net/viewvc/rhino-tools/trunk/Rhino.Testing/">added
it to Rhino.Tools</a>. 
</p>
        <p>
Anyways, back to the subject at hand. The above works great if nothing really happens
in your Foo constructor. But what happens if part of the constructor is to call a
method on IBar? Well surely you could just move the construction to the Test method:
</p>
        <pre class="csharpcode">    [Test]
    <span class="kwrd">public</span><span class="kwrd">void</span> Test()
{ <span class="kwrd">using</span> (_mocks.Unordered()) { _bar.DoSomeSetup(); SetupResult.For(_bar.ProvideService()).Returns(1);
} _mocks.ReplayAll(); _foo = <span class="kwrd">new</span> FooTests(_bar); Assert.IsTrue(_foo.DoSomething());
_mocks.VerifyAll(); } </pre>
        <p>
That works, but what if you have more than one test? Well, you could just do the same
thing in every test, but that's an annoying amount of repeated code that doesn't add
any value. You can't simply Extract Method on it because you'll most likely need
to setup other expectations for other scenarios you're testing. So... now I do this:
</p>
        <pre class="csharpcode">    [Test]
    <span class="kwrd">public</span><span class="kwrd">void</span> Test()
{ SetupMocks(<span class="kwrd">delegate</span>() { SetupResult.For(_bar.ProvideService()).Returns(1);
}); Assert.IsTrue(_foo.DoSomething()); _mocks.VerifyAll(); } <span class="kwrd">public</span><span class="kwrd">void</span> SetupMocks(Block
block) { <span class="kwrd">using</span> (_mocks.Unordered()) { _bar.DoSomeSetup(); <span class="kwrd">if</span> (block
!= <span class="kwrd">null</span>) block(); } _mocks.ReplayAll(); _foo = <span class="kwrd">new</span> FooTests(_bar);
} </pre>
        <p>
And this:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">delegate</span>
          <span class="kwrd">void</span> Block();</pre>
        <p>
Funny what a little Ruby exposure will do to you...
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=1c184996-30c1-4c36-a270-9ed5f9405d71" />
      </body>
      <title>Boilerplate Test Fixtures with Mocks</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,1c184996-30c1-4c36-a270-9ed5f9405d71.aspx</guid>
      <link>http://blog.eleutian.com/2007/06/27/BoilerplateTestFixturesWithMocks.aspx</link>
      <pubDate>Wed, 27 Jun 2007 03:36:55 GMT</pubDate>
      <description>&lt;p&gt;
Generally when I'm testing something that has dependencies my test fixture looks something
like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;  [TestFixture]
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; FooTests
{ &lt;span class="kwrd"&gt;private&lt;/span&gt; MockRepository _mocks; &lt;span class="kwrd"&gt;private&lt;/span&gt; Foo
_foo; &lt;span class="kwrd"&gt;private&lt;/span&gt; IBar _bar; [SetUp] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()
{ _mocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); _bar = _mocks.DynamicMock&amp;lt;IBar&amp;gt;();
_foo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Foo(_bar); } [Test] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test()
{ &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { SetupResult.For(_bar.ProvideService()).Returns(1);
} _mocks.ReplayAll(); Assert.IsTrue(_foo.DoSomething()); _mocks.VerifyAll(); } } &lt;/pre&gt;
&lt;p&gt;
Granted, I'm starting to use our &lt;a href="http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx"&gt;AutoMocking
Container&lt;/a&gt; more and more, but that's not the point of this post. As an aside, &lt;a href="http://www.ayende.com/blog"&gt;Ayende&lt;/a&gt; has
some more &lt;a href="http://www.ayende.com/Blog/archive/2007/06/08/The-Auto-Mocking-Container.aspx"&gt;examples&lt;/a&gt; on
its usage and has &lt;a href="http://rhino-tools.svn.sourceforge.net/viewvc/rhino-tools/trunk/Rhino.Testing/"&gt;added
it to Rhino.Tools&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
Anyways, back to the subject at hand. The above works great if nothing really happens
in your Foo constructor. But what happens if part of the constructor is to call a
method on IBar? Well surely you could just move the construction to the Test method:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;    [Test]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test()
{ &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { _bar.DoSomeSetup(); SetupResult.For(_bar.ProvideService()).Returns(1);
} _mocks.ReplayAll(); _foo = &lt;span class="kwrd"&gt;new&lt;/span&gt; FooTests(_bar); Assert.IsTrue(_foo.DoSomething());
_mocks.VerifyAll(); } &lt;/pre&gt;
&lt;p&gt;
That works, but what if you have more than one test? Well, you could just do the same
thing in every test, but that's an annoying amount of repeated code that doesn't add
any value. You can't simply Extract Method&amp;nbsp;on it because you'll most likely need
to setup other expectations for other scenarios you're testing. So... now I do this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;    [Test]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test()
{ SetupMocks(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;() { SetupResult.For(_bar.ProvideService()).Returns(1);
}); Assert.IsTrue(_foo.DoSomething()); _mocks.VerifyAll(); } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetupMocks(Block
block) { &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { _bar.DoSomeSetup(); &lt;span class="kwrd"&gt;if&lt;/span&gt; (block
!= &lt;span class="kwrd"&gt;null&lt;/span&gt;) block(); } _mocks.ReplayAll(); _foo = &lt;span class="kwrd"&gt;new&lt;/span&gt; FooTests(_bar);
} &lt;/pre&gt;
&lt;p&gt;
And this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Block();&lt;/pre&gt;
&lt;p&gt;
Funny what a little Ruby exposure will do to you...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=1c184996-30c1-4c36-a270-9ed5f9405d71" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,1c184996-30c1-4c36-a270-9ed5f9405d71.aspx</comments>
      <category>development</category>
      <category>test driven development</category>
      <category>tips</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=cdad99d5-9f28-4331-b144-fb0c6239177d</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,cdad99d5-9f28-4331-b144-fb0c6239177d.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,cdad99d5-9f28-4331-b144-fb0c6239177d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=cdad99d5-9f28-4331-b144-fb0c6239177d</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Shortly after I posted my <a href="http://blog.eleutian.com/PermaLink,guid,e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa.aspx">original
EleutianControllerTests</a>, <a href="http://codebetter.com/blogs/scott.bellware/">Scott
Bellware</a>, <a href="http://hammett.castleproject.org/">Hamilton</a> and myself
chatted about the subject, and Hamilton decided to help out by making some of the
previously internal methods public. Context still isn't settable directly however
(maybe we should have him change that too...), so you still have to do a little bit
of magic to get your context in there. There's actually more code to do this now then
when I just set it w/ reflection, but it's arguably more "correct" and doesn't involve
"reflection" and you can mock more (like logging and such). Anyways, here's the <a href="http://blog.eleutian.com/download/EleutianControllerTests.cs.txt">code</a>:
</p>
        <p>
 
</p>
        <pre class="csharpcode">
          <span class="kwrd">using</span> System; <span class="kwrd">using</span> System.Collections; <span class="kwrd">using</span> System.Collections.Specialized; <span class="kwrd">using</span> System.IO; <span class="kwrd">using</span> System.ComponentModel.Design; <span class="kwrd">using</span> System.Security.Principal; <span class="kwrd">using</span> System.Web; <span class="kwrd"></span><span class="kwrd">using</span> Castle.MonoRail.Framework; <span class="kwrd">using</span> Castle.MonoRail.Framework.Internal; <span class="kwrd">using</span> Castle.Core.Logging; <span class="kwrd">using</span> Castle.MonoRail.Framework.Services; <span class="kwrd">using</span> NUnit.Framework; <span class="kwrd">using</span> Rhino.Mocks; <span class="kwrd">public</span><span class="kwrd">class</span> EleutianControllerTests
{ <span class="preproc">#region</span> Constants <span class="kwrd">private</span><span class="kwrd">const</span><span class="kwrd">string</span> ApplicationPhysicalPath
= <span class="str">"Q:\\PhysicalPath"</span>; <span class="preproc">#endregion</span><span class="preproc">#region</span> Member
Data <span class="kwrd">protected</span><span class="kwrd">string</span> _virtualDirectory
= String.Empty; <span class="kwrd">protected</span> MockRepository _mocks; <span class="kwrd">protected</span> IRailsEngineContext
_context; <span class="kwrd">protected</span> IRequest _request; <span class="kwrd">protected</span> IResponse
_response; <span class="kwrd">protected</span> IServerUtility _serverUtility; <span class="kwrd">protected</span> IDictionary
_session; <span class="kwrd">protected</span> Flash _flash; <span class="kwrd">protected</span> IViewEngineManager
_viewEngineManager; <span class="kwrd">protected</span> ControllerMetaDescriptor _descriptor; <span class="kwrd">protected</span> NameValueCollection
_parameters; <span class="preproc">#endregion</span><span class="preproc">#region</span> Test
Setup and Teardown Methods [SetUp] <span class="kwrd">public</span><span class="kwrd">virtual</span><span class="kwrd">void</span> Setup()
{ _mocks = <span class="kwrd">new</span> MockRepository(); _viewEngineManager = _mocks.CreateMock&lt;IViewEngineManager&gt;();
_descriptor = _mocks.CreateMock&lt;ControllerMetaDescriptor&gt;(); _context = _mocks.CreateMock&lt;MockRailsEngineContext&gt;(_viewEngineManager,
_descriptor); _request = _mocks.DynamicMock&lt;IRequest&gt;(); _response = _mocks.DynamicMock&lt;IResponse&gt;();
_serverUtility = _mocks.DynamicMock&lt;IServerUtility&gt;(); _session = _mocks.DynamicMock&lt;IDictionary&gt;();
_flash = <span class="kwrd">new</span> Flash(); _parameters = <span class="kwrd">new</span> NameValueCollection();
} <span class="kwrd">protected</span><span class="kwrd">void</span> InitializeController(Controller
controller, <span class="kwrd">string</span> areaName, <span class="kwrd">string</span> controllerName, <span class="kwrd">string</span> actionName)
{ controller.InitializeControllerState(areaName, controllerName, actionName); controller.InitializeFieldsFromServiceProvider(_context);
InitializeRailsContext(areaName, controllerName, actionName); } <span class="kwrd">protected</span><span class="kwrd">void</span> InitializeRailsContext(<span class="kwrd">string</span> areaName, <span class="kwrd">string</span> controllerName, <span class="kwrd">string</span> actionName)
{ SetupResult.For(_context.UrlInfo).Return( <span class="kwrd">new</span> UrlInfo(<span class="str">"eleutian.com"</span>, <span class="str">"www"</span>,
_virtualDirectory, <span class="str">"http"</span>, 80, Path.Combine(Path.Combine(areaName,
controllerName), actionName), areaName, controllerName, actionName, <span class="str">"rails"</span>));
SetupResult.For(_context.Server).Return(_serverUtility); SetupResult.For(_context.Flash).Return(_flash);
SetupResult.For(_context.ApplicationPath).Return(<span class="str">"/"</span>); SetupResult.For(_context.Request).Return(_request);
SetupResult.For(_context.Response).Return(_response); SetupResult.For(_context.ApplicationPhysicalPath).Return(ApplicationPhysicalPath);
SetupResult.For(_request.Params).Return(_parameters); SetupResult.For(_context.Session).Return(_session);
} <span class="preproc">#endregion</span> } <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">class</span> MockRailsEngineContext
: IRailsEngineContext { <span class="preproc">#region</span> Member Data <span class="kwrd">private</span> IViewEngineManager
_viewEngineManager; <span class="kwrd">private</span> IControllerDescriptorProvider
_controllerDescriptorProvider; <span class="preproc">#endregion</span><span class="preproc">#region</span> Properties <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> Transfer(<span class="kwrd">string</span> path, <span class="kwrd">bool</span> preserveForm); <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">string</span> RequestType
{ get; } <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">string</span> Url
{ get; } <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">string</span> UrlReferrer
{ get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> HttpContext
UnderlyingContext { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> NameValueCollection
Params { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> IDictionary
Session { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> IRequest
Request { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> IResponse
Response { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> ITrace
Trace { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> ICacheProvider
Cache { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> Flash
Flash { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> IPrincipal
CurrentUser { get; set; } <span class="kwrd">public</span><span class="kwrd">abstract</span> Exception
LastException { get; set; } <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">string</span> ApplicationPath
{ get; } <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">string</span> ApplicationPhysicalPath
{ get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> UrlInfo
UrlInfo { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> IServerUtility
Server { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> IDictionary
Items { get; } <span class="kwrd">public</span><span class="kwrd">abstract</span> Controller
CurrentController { get; set; } <span class="preproc">#endregion</span><span class="preproc">#region</span> Constructors <span class="kwrd">public</span> MockRailsEngineContext(IViewEngineManager
viewEngineManager, ControllerMetaDescriptor descriptor) { _viewEngineManager = viewEngineManager;
_controllerDescriptorProvider = <span class="kwrd">new</span> ControllerDescriptorProviderStub(descriptor);
} <span class="preproc">#endregion</span><span class="preproc">#region</span> Methods <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> AddService(Type
serviceType, <span class="kwrd">object</span> serviceInstance); <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> AddService(Type
serviceType, <span class="kwrd">object</span> serviceInstance, <span class="kwrd">bool</span> promote); <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> AddService(Type
serviceType, ServiceCreatorCallback callback); <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> AddService(Type
serviceType, ServiceCreatorCallback callback, <span class="kwrd">bool</span> promote); <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> RemoveService(Type
serviceType); <span class="kwrd">public</span><span class="kwrd">abstract</span><span class="kwrd">void</span> RemoveService(Type
serviceType, <span class="kwrd">bool</span> promote); <span class="kwrd">public</span><span class="kwrd">object</span> GetService(Type
serviceType) { <span class="kwrd">if</span> (<span class="kwrd">typeof</span>(IViewEngineManager).Equals(serviceType))
{ <span class="kwrd">return</span> _viewEngineManager; } <span class="kwrd">else</span><span class="kwrd">if</span> (<span class="kwrd">typeof</span>(IControllerDescriptorProvider).Equals(serviceType))
{ <span class="kwrd">return</span> _controllerDescriptorProvider; } <span class="kwrd">else</span><span class="kwrd">if</span> (<span class="kwrd">typeof</span>(ILoggerFactory).Equals(serviceType))
{ <span class="kwrd">return</span><span class="kwrd">null</span>; } <span class="kwrd">else</span><span class="kwrd">if</span> (<span class="kwrd">typeof</span>(IUrlBuilder).Equals(serviceType))
{ <span class="kwrd">return</span><span class="kwrd">new</span> DefaultUrlBuilder();
} <span class="kwrd">return</span><span class="kwrd">null</span>; } <span class="preproc">#endregion</span><span class="preproc">#region</span> Classes <span class="kwrd">private</span><span class="kwrd">class</span> ControllerDescriptorProviderStub
: IControllerDescriptorProvider { <span class="kwrd">private</span> ControllerMetaDescriptor
_descriptor; <span class="kwrd">public</span> ControllerDescriptorProviderStub(ControllerMetaDescriptor
descriptor) { _descriptor = descriptor; } <span class="kwrd">public</span> ControllerMetaDescriptor
BuildDescriptor(Controller controller) { <span class="kwrd">return</span> _descriptor;
} <span class="kwrd">public</span> ControllerMetaDescriptor BuildDescriptor(Type controllerType)
{ <span class="kwrd">return</span> _descriptor; } <span class="kwrd">public</span><span class="kwrd">void</span> Service(IServiceProvider
provider) { } } <span class="preproc">#endregion</span> } </pre>
        <a href="http://blog.eleutian.com/download/EleutianControllerTests.cs.txt">Source</a>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=cdad99d5-9f28-4331-b144-fb0c6239177d" />
      </body>
      <title>EleutianControllerTests 2.0</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,cdad99d5-9f28-4331-b144-fb0c6239177d.aspx</guid>
      <link>http://blog.eleutian.com/2007/03/28/EleutianControllerTests20.aspx</link>
      <pubDate>Wed, 28 Mar 2007 22:51:30 GMT</pubDate>
      <description>&lt;p&gt;
Shortly after I posted my &lt;a href="http://blog.eleutian.com/PermaLink,guid,e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa.aspx"&gt;original
EleutianControllerTests&lt;/a&gt;, &lt;a href="http://codebetter.com/blogs/scott.bellware/"&gt;Scott
Bellware&lt;/a&gt;, &lt;a href="http://hammett.castleproject.org/"&gt;Hamilton&lt;/a&gt; and myself
chatted about the subject, and Hamilton decided to help out by making some of the
previously internal methods public. Context still isn't settable directly however
(maybe we should have him change that too...), so you still have to do a little bit
of magic to get your context in there. There's actually more code to do this now then
when I just set it w/ reflection, but it's arguably more "correct" and doesn't involve
"reflection" and you can mock more (like logging and such). Anyways, here's the &lt;a href="http://blog.eleutian.com/download/EleutianControllerTests.cs.txt"&gt;code&lt;/a&gt;:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Specialized; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.IO; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.ComponentModel.Design; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Principal; &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web; &lt;span class="kwrd"&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MonoRail.Framework; &lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MonoRail.Framework.Internal; &lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Core.Logging; &lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MonoRail.Framework.Services; &lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework; &lt;span class="kwrd"&gt;using&lt;/span&gt; Rhino.Mocks; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EleutianControllerTests
{ &lt;span class="preproc"&gt;#region&lt;/span&gt; Constants &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ApplicationPhysicalPath
= &lt;span class="str"&gt;"Q:\\PhysicalPath"&lt;/span&gt;; &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Member
Data &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _virtualDirectory
= String.Empty; &lt;span class="kwrd"&gt;protected&lt;/span&gt; MockRepository _mocks; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IRailsEngineContext
_context; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IRequest _request; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IResponse
_response; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IServerUtility _serverUtility; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IDictionary
_session; &lt;span class="kwrd"&gt;protected&lt;/span&gt; Flash _flash; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IViewEngineManager
_viewEngineManager; &lt;span class="kwrd"&gt;protected&lt;/span&gt; ControllerMetaDescriptor _descriptor; &lt;span class="kwrd"&gt;protected&lt;/span&gt; NameValueCollection
_parameters; &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Test
Setup and Teardown Methods [SetUp] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()
{ _mocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); _viewEngineManager = _mocks.CreateMock&amp;lt;IViewEngineManager&amp;gt;();
_descriptor = _mocks.CreateMock&amp;lt;ControllerMetaDescriptor&amp;gt;(); _context = _mocks.CreateMock&amp;lt;MockRailsEngineContext&amp;gt;(_viewEngineManager,
_descriptor); _request = _mocks.DynamicMock&amp;lt;IRequest&amp;gt;(); _response = _mocks.DynamicMock&amp;lt;IResponse&amp;gt;();
_serverUtility = _mocks.DynamicMock&amp;lt;IServerUtility&amp;gt;(); _session = _mocks.DynamicMock&amp;lt;IDictionary&amp;gt;();
_flash = &lt;span class="kwrd"&gt;new&lt;/span&gt; Flash(); _parameters = &lt;span class="kwrd"&gt;new&lt;/span&gt; NameValueCollection();
} &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeController(Controller
controller, &lt;span class="kwrd"&gt;string&lt;/span&gt; areaName, &lt;span class="kwrd"&gt;string&lt;/span&gt; controllerName, &lt;span class="kwrd"&gt;string&lt;/span&gt; actionName)
{ controller.InitializeControllerState(areaName, controllerName, actionName); controller.InitializeFieldsFromServiceProvider(_context);
InitializeRailsContext(areaName, controllerName, actionName); } &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeRailsContext(&lt;span class="kwrd"&gt;string&lt;/span&gt; areaName, &lt;span class="kwrd"&gt;string&lt;/span&gt; controllerName, &lt;span class="kwrd"&gt;string&lt;/span&gt; actionName)
{ SetupResult.For(_context.UrlInfo).Return( &lt;span class="kwrd"&gt;new&lt;/span&gt; UrlInfo(&lt;span class="str"&gt;"eleutian.com"&lt;/span&gt;, &lt;span class="str"&gt;"www"&lt;/span&gt;,
_virtualDirectory, &lt;span class="str"&gt;"http"&lt;/span&gt;, 80, Path.Combine(Path.Combine(areaName,
controllerName), actionName), areaName, controllerName, actionName, &lt;span class="str"&gt;"rails"&lt;/span&gt;));
SetupResult.For(_context.Server).Return(_serverUtility); SetupResult.For(_context.Flash).Return(_flash);
SetupResult.For(_context.ApplicationPath).Return(&lt;span class="str"&gt;"/"&lt;/span&gt;); SetupResult.For(_context.Request).Return(_request);
SetupResult.For(_context.Response).Return(_response); SetupResult.For(_context.ApplicationPhysicalPath).Return(ApplicationPhysicalPath);
SetupResult.For(_request.Params).Return(_parameters); SetupResult.For(_context.Session).Return(_session);
} &lt;span class="preproc"&gt;#endregion&lt;/span&gt; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MockRailsEngineContext
: IRailsEngineContext { &lt;span class="preproc"&gt;#region&lt;/span&gt; Member Data &lt;span class="kwrd"&gt;private&lt;/span&gt; IViewEngineManager
_viewEngineManager; &lt;span class="kwrd"&gt;private&lt;/span&gt; IControllerDescriptorProvider
_controllerDescriptorProvider; &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Properties &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Transfer(&lt;span class="kwrd"&gt;string&lt;/span&gt; path, &lt;span class="kwrd"&gt;bool&lt;/span&gt; preserveForm); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; RequestType
{ get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Url
{ get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; UrlReferrer
{ get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; HttpContext
UnderlyingContext { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; NameValueCollection
Params { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; IDictionary
Session { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; IRequest
Request { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; IResponse
Response { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; ITrace
Trace { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; ICacheProvider
Cache { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; Flash
Flash { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; IPrincipal
CurrentUser { get; set; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; Exception
LastException { get; set; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ApplicationPath
{ get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ApplicationPhysicalPath
{ get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; UrlInfo
UrlInfo { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; IServerUtility
Server { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; IDictionary
Items { get; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; Controller
CurrentController { get; set; } &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Constructors &lt;span class="kwrd"&gt;public&lt;/span&gt; MockRailsEngineContext(IViewEngineManager
viewEngineManager, ControllerMetaDescriptor descriptor) { _viewEngineManager = viewEngineManager;
_controllerDescriptorProvider = &lt;span class="kwrd"&gt;new&lt;/span&gt; ControllerDescriptorProviderStub(descriptor);
} &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Methods &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddService(Type
serviceType, &lt;span class="kwrd"&gt;object&lt;/span&gt; serviceInstance); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddService(Type
serviceType, &lt;span class="kwrd"&gt;object&lt;/span&gt; serviceInstance, &lt;span class="kwrd"&gt;bool&lt;/span&gt; promote); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddService(Type
serviceType, ServiceCreatorCallback callback); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddService(Type
serviceType, ServiceCreatorCallback callback, &lt;span class="kwrd"&gt;bool&lt;/span&gt; promote); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RemoveService(Type
serviceType); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RemoveService(Type
serviceType, &lt;span class="kwrd"&gt;bool&lt;/span&gt; promote); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetService(Type
serviceType) { &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IViewEngineManager).Equals(serviceType))
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; _viewEngineManager; } &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IControllerDescriptorProvider).Equals(serviceType))
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; _controllerDescriptorProvider; } &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ILoggerFactory).Equals(serviceType))
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;; } &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IUrlBuilder).Equals(serviceType))
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; DefaultUrlBuilder();
} &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;; } &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Classes &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ControllerDescriptorProviderStub
: IControllerDescriptorProvider { &lt;span class="kwrd"&gt;private&lt;/span&gt; ControllerMetaDescriptor
_descriptor; &lt;span class="kwrd"&gt;public&lt;/span&gt; ControllerDescriptorProviderStub(ControllerMetaDescriptor
descriptor) { _descriptor = descriptor; } &lt;span class="kwrd"&gt;public&lt;/span&gt; ControllerMetaDescriptor
BuildDescriptor(Controller controller) { &lt;span class="kwrd"&gt;return&lt;/span&gt; _descriptor;
} &lt;span class="kwrd"&gt;public&lt;/span&gt; ControllerMetaDescriptor BuildDescriptor(Type controllerType)
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; _descriptor; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Service(IServiceProvider
provider) { } } &lt;span class="preproc"&gt;#endregion&lt;/span&gt; } &lt;/pre&gt;
&lt;a href="http://blog.eleutian.com/download/EleutianControllerTests.cs.txt"&gt;Source&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=cdad99d5-9f28-4331-b144-fb0c6239177d" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,cdad99d5-9f28-4331-b144-fb0c6239177d.aspx</comments>
      <category>development</category>
      <category>monorail</category>
      <category>open source</category>
      <category>source</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=669975bb-30f4-4527-b716-3ff7cecb41c2</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,669975bb-30f4-4527-b716-3ff7cecb41c2.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,669975bb-30f4-4527-b716-3ff7cecb41c2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=669975bb-30f4-4527-b716-3ff7cecb41c2</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <pre class="csharpcode">
          <span class="kwrd">using</span> NUnit.Framework;
[TestFixture] <span class="kwrd">public</span><span class="kwrd">class</span> OperatorExplicitTests
{ <span class="kwrd">public</span><span class="kwrd">interface</span> IFoo { } <span class="kwrd">public</span><span class="kwrd">sealed</span><span class="kwrd">class</span> Foo
: IFoo { } <span class="kwrd">public</span><span class="kwrd">class</span> Bar :
IFoo { <span class="kwrd">private</span> Foo _foo = <span class="kwrd">new</span> Foo(); <span class="kwrd">public</span><span class="kwrd">static</span><span class="kwrd">explicit</span><span class="kwrd">operator</span> Foo(Bar
bar) { <span class="kwrd">return</span> bar._foo; } } [Test] <span class="rem">//
Passes</span><span class="kwrd">public</span><span class="kwrd">void</span> TestCastFromClass()
{ Bar bar = <span class="kwrd">new</span> Bar(); Foo foo = (Foo)bar; } [Test] <span class="rem">//
Passes</span><span class="kwrd">public</span><span class="kwrd">void</span> TestCastFromInterfaceToNormal()
{ IFoo bar = <span class="kwrd">new</span> Bar(); Foo foo = (Foo)(Bar)bar; } [Test] <span class="rem">//
Throws System.InvalidCastException: Unable to cast object of type 'Bar' to type 'Foo'.</span><span class="kwrd">public</span><span class="kwrd">void</span> TestCastFromInterface()
{ IFoo bar = <span class="kwrd">new</span> Bar(); Foo foo = (Foo)bar; } }</pre>
        <p>
Ugh, see what happens when you try to be sneaky? Is this a bug? A limitation? 
</p>
        <p>
If you're curious why I care, we have a custom implentation of IDbCommand that wraps
a SqlCommand and <a href="https://svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SqlClientBatchingBatcher.cs">this
code</a> expects it to be a SqlCommand... no fooling it I guess :/
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=669975bb-30f4-4527-b716-3ff7cecb41c2" />
      </body>
      <title>Operator explicit fubar'd</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,669975bb-30f4-4527-b716-3ff7cecb41c2.aspx</guid>
      <link>http://blog.eleutian.com/2007/03/28/OperatorExplicitFubard.aspx</link>
      <pubDate>Wed, 28 Mar 2007 07:15:48 GMT</pubDate>
      <description>&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework; [TestFixture] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; OperatorExplicitTests
{ &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IFoo { } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Foo
: IFoo { } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Bar :
IFoo { &lt;span class="kwrd"&gt;private&lt;/span&gt; Foo _foo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Foo(); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;explicit&lt;/span&gt; &lt;span class="kwrd"&gt;operator&lt;/span&gt; Foo(Bar
bar) { &lt;span class="kwrd"&gt;return&lt;/span&gt; bar._foo; } } [Test] &lt;span class="rem"&gt;//
Passes&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestCastFromClass()
{ Bar bar = &lt;span class="kwrd"&gt;new&lt;/span&gt; Bar(); Foo foo = (Foo)bar; } [Test] &lt;span class="rem"&gt;//
Passes&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestCastFromInterfaceToNormal()
{ IFoo bar = &lt;span class="kwrd"&gt;new&lt;/span&gt; Bar(); Foo foo = (Foo)(Bar)bar; } [Test] &lt;span class="rem"&gt;//
Throws System.InvalidCastException: Unable to cast object of type 'Bar' to type 'Foo'.&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestCastFromInterface()
{ IFoo bar = &lt;span class="kwrd"&gt;new&lt;/span&gt; Bar(); Foo foo = (Foo)bar; } }&lt;/pre&gt;
&lt;p&gt;
Ugh, see what happens when you try to be sneaky? Is this a bug? A limitation? 
&lt;/p&gt;
&lt;p&gt;
If you're curious why I care, we have a custom implentation of IDbCommand that wraps
a SqlCommand and &lt;a href="https://svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SqlClientBatchingBatcher.cs"&gt;this
code&lt;/a&gt;&amp;nbsp;expects it to be a SqlCommand... no fooling it I guess :/
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=669975bb-30f4-4527-b716-3ff7cecb41c2" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,669975bb-30f4-4527-b716-3ff7cecb41c2.aspx</comments>
      <category>annoyances</category>
      <category>development</category>
      <category>nhibernate</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=35648029-dd21-4aa0-ab5c-85124a895150</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,35648029-dd21-4aa0-ab5c-85124a895150.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,35648029-dd21-4aa0-ab5c-85124a895150.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=35648029-dd21-4aa0-ab5c-85124a895150</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
        <p>
          <b>Update:</b> Hamilton <a href="http://groups.google.com/group/castle-project-commits/msg/8d5c61dbde43d8d9?hl=en&amp;">committed</a> my
patch, so if you're running off trunk you won't need to do this anymore. Thanks Hamilton!<br /></p>
        <p>
Currently MonoRail uses ResourceSets wrapped up in an implementation of IResource
to provide Resources to views. Unfortunately, <strong>ResourceSets do not cascade
when it comes to resource resolution.</strong>  ResourceManager.GetResourceSet
does have a flag called tryParents, but all that does is try less and less specific
cultures until it finds a match. In other words, if you have two resource files:<br />
Foo.ko.resx: defines A, B<br />
Foo.resx: defines A, B, C 
</p>
        <p>
If you locale is ko-KR, calling RseourceManager.GetResourceSet will yield a ResourceSet
that maps to Foo.ko.resx, so asking for B will work, but asking for C will not. A
ResourceSet only knows about itself. 
</p>
        <p>
ResourceManagers on the other hand, are perfectly capable of handling this cascade,
which is quite necessary in at least our localized app, as we do not want to have
to define strings in all languages for everything. So with ResourceManagers, asking
for A, B, and C all behave as expected, preferring the most specified culture and
cascading down as necessary. 
</p>
        <p>
Below is an implementation of an IResourceFactory that spits out wrapped ResourceManagers
instead of ResourceSets. 
</p>
        <p>
To use it you'll need to add this to your web.config:<br /></p>
        <pre class="csharpcode">
          <span class="kwrd">&lt;</span>
          <span class="html">monorail</span>
          <span class="kwrd">&gt;</span>
          <span class="kwrd">&lt;</span>
          <span class="html">services</span>
          <span class="kwrd">&gt;</span>
          <span class="kwrd">&lt;</span>
          <span class="html">service</span>
          <span class="attr">id</span>
          <span class="kwrd">="ResourceFactory"</span>
          <span class="attr">type</span>
          <span class="kwrd">="Eleutian.Shared.MonoRail.ResourceManagerFactory,
Eleutian.Shared"</span>
          <span class="kwrd">/&gt;</span>
          <span class="kwrd">&lt;/</span>
          <span class="html">services</span>
          <span class="kwrd">&gt;</span>
          <span class="kwrd">&lt;/</span>
          <span class="html">monorail</span>
          <span class="kwrd">&gt;</span>
        </pre>
        <p>
          <a href="/download/ResourceManagerFactory.zip">Source</a>
        </p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=35648029-dd21-4aa0-ab5c-85124a895150" />
      </body>
      <title>ResourceSets vs ResourceManagers in MonoRail</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,35648029-dd21-4aa0-ab5c-85124a895150.aspx</guid>
      <link>http://blog.eleutian.com/2007/03/27/ResourceSetsVsResourceManagersInMonoRail.aspx</link>
      <pubDate>Tue, 27 Mar 2007 12:13:12 GMT</pubDate>
      <description>&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; Hamilton &lt;a href="http://groups.google.com/group/castle-project-commits/msg/8d5c61dbde43d8d9?hl=en&amp;amp;"&gt;committed&lt;/a&gt; my
patch, so if you're running off trunk you won't need to do this anymore. Thanks Hamilton!&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Currently MonoRail uses ResourceSets wrapped up in an implementation of IResource
to provide Resources to views. Unfortunately, &lt;strong&gt;ResourceSets do not cascade
when it comes to resource resolution.&lt;/strong&gt;&amp;nbsp;&amp;nbsp;ResourceManager.GetResourceSet
does have a flag called tryParents, but all that does is try less and less specific
cultures until it finds a match. In other words, if you have two resource files:&lt;br&gt;
Foo.ko.resx: defines A, B&lt;br&gt;
Foo.resx: defines A, B, C 
&lt;/p&gt;
&lt;p&gt;
If you locale is ko-KR, calling RseourceManager.GetResourceSet will yield a ResourceSet
that maps to Foo.ko.resx, so asking for B will work, but asking for C will not. A
ResourceSet only knows about itself. 
&lt;/p&gt;
&lt;p&gt;
ResourceManagers on the other hand, are perfectly capable of handling this cascade,
which is quite necessary in at least our localized app, as we do not want to have
to define strings in all languages for everything. So with ResourceManagers, asking
for A, B, and C all behave as expected, preferring the most specified culture and
cascading down as necessary. 
&lt;/p&gt;
&lt;p&gt;
Below is an implementation of an IResourceFactory that spits out wrapped ResourceManagers
instead of ResourceSets. 
&lt;/p&gt;
&lt;p&gt;
To use it you'll need to add this to your web.config:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;monorail&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;services&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;service&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="ResourceFactory"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="Eleutian.Shared.MonoRail.ResourceManagerFactory,
Eleutian.Shared"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;services&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;monorail&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;a href="/download/ResourceManagerFactory.zip"&gt;Source&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=35648029-dd21-4aa0-ab5c-85124a895150" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,35648029-dd21-4aa0-ab5c-85124a895150.aspx</comments>
      <category>development</category>
      <category>localization</category>
      <category>monorail</category>
      <category>open source</category>
      <category>source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=0d492114-3c0c-478a-9f65-8d8072b48ff4</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,0d492114-3c0c-478a-9f65-8d8072b48ff4.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,0d492114-3c0c-478a-9f65-8d8072b48ff4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=0d492114-3c0c-478a-9f65-8d8072b48ff4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm sure you've all heard that the best developers are lazy and/or <a href="http://www.codinghorror.com/blog/archives/000373.html">dumb</a>.
I certainly agree with that. Also, you know that writing repetitive code and repeatedly
following tedious steps is bad and a waste of time. 
</p>
        <p>
OK, so we know that those things are bad... but how do you identify them? Often tedious
steps and repetitive code is taught as the way you do things... so you just do them
in that way without asking questions. Other times, we just don't know that there is
an easier way. We're developers--we're creative, smart, inventive, and we should be
able to tell when something can and should be done in a better way. 
</p>
        <p>
Let me give you an example of some code I came across some time ago:
</p>
        <pre class="csharpcode">
          <span class="kwrd">Public</span> userstring <span class="kwrd">As</span><span class="kwrd">String</span> = <span class="str">"U000000"</span><span class="kwrd">Private</span><span class="kwrd">Function</span> GetUserIDStr(<span class="kwrd">ByVal</span> userid <span class="kwrd">As</span><span class="kwrd">Integer</span>) <span class="kwrd">As</span><span class="kwrd">String</span><span class="rem">'Returns
String:</span><span class="rem">' User Info FileName</span><span class="kwrd">Dim</span> source <span class="kwrd">As</span><span class="kwrd">String</span> =
userid.ToString() <span class="rem">'Dim destination As Char() = {"U"c, "0"c, "0"c,
"0"c, "0"c, "0"c, "0"c}</span><span class="kwrd">Dim</span> destination <span class="kwrd">As</span><span class="kwrd">String</span> = <span class="kwrd">Me</span>.userstring <span class="kwrd">Dim</span> sourcelen <span class="kwrd">As</span><span class="kwrd">Integer</span> =
source.Length() <span class="kwrd">Dim</span> destindex <span class="kwrd">As</span><span class="kwrd">Integer</span> =
7 - sourcelen source.CopyTo(0, destination, destindex, sourcelen) <span class="kwrd">Return</span> destination <span class="kwrd">End</span> Function</pre>
        <p>
Obviously there are several things... off... with this code. Let's discuss some of
them. The first is the original Dim destination that is now commented out. This is
an example of someone partially applying what I'm talking about. They realized that
typing out that array was unnecessarily complicated so they did something about it.
That's great, that's what we need to do. Unfortunately, they stopped there. I don't
think we should stop there. We should look at the code, say, wow, this isn't really
doing anything but formatting a number into a string. There has to be a better way
to do this! In this case there certainly is... all of that code can be replaced with
something along the lines of: 
</p>
        <pre class="csharpcode">
          <span class="kwrd">Return</span>
          <span class="kwrd">String</span>.Format('U{0:D06}',
userid)</pre>
        <p>
Which is easier to read? Write? Maintain? The answer is obvious... even if you didn't
know about String.Format, you should know when you're writing that code that there
has to be a better way. Google is your friend.
</p>
        <p>
What if there isn't a better way? Can you think of a better way? Ask yourself these
questions:
</p>
        <ul>
          <li>
Are you going to be doing this more than a few times? 
</li>
          <li>
Are other people on your team going to be doing it? 
</li>
          <li>
Are you looking for a break from your normal work? 
</li>
        </ul>
        <p>
If the answer to any of those questions is yes, take some time to write or come up
with a better way. You're a developer. You develop. Remember, you can write software
for yourself and your teams that will make writing software more enjoyable and faster.
And then, when you're done, share it! Share it with the world, make development easier
for everyone. 
</p>
        <p>
I'll give you another example. I've <a href="http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx">already
blogged about it</a>, but it's relevant to this post so I'll rehash here. In order
to localize a string in <a href="http://www.castleproject.org/MonoRail/">MonoRail</a> you
must do the following:
</p>
        <ol>
          <li>
Create a new resx file.</li>
          <li>
Add a Resource attribute to your controller mapping the resx to a key in the
PropertyBag.</li>
          <li>
For each string you want to localize, add a key/value pair to the resx file.</li>
          <li>
Replace the strings in your view template with your resource key.</li>
        </ol>
        <p>
Hm. Every controller? Every string? Talk about context switching. Not to mention the
fact that you can't see the actual English string in your view template, so any modifications
to the English string require the same context switching. It didn't take me long to
decide that was far too tedious for myself and my team. 
</p>
        <p>
It probably took me 2-3 days overall to write both the <a href="http://blog.eleutian.com/PermaLink,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx">ASP.NET
preprocessor</a> and <a href="http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx">adapt
it to MonoRail/Brail</a>. Will I ever get 2-3 whole days back? Who knows. Will my
entire team in total? More likely. Does it save us countless context switches, speed
up our development, and make localization trivial? Absolutely. So was it worth it?
Absolutely. 
</p>
        <p>
And before you ask, yes I plan on taking my own advice and sharing it... eventually.
Oh and keep in mind, this sort of thinking can and should be applied to everything
you do, including but not limited to code, tools, and process. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=0d492114-3c0c-478a-9f65-8d8072b48ff4" />
      </body>
      <title>Knowing when things could be easier</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,0d492114-3c0c-478a-9f65-8d8072b48ff4.aspx</guid>
      <link>http://blog.eleutian.com/2007/03/24/KnowingWhenThingsCouldBeEasier.aspx</link>
      <pubDate>Sat, 24 Mar 2007 23:46:12 GMT</pubDate>
      <description>&lt;p&gt;
I'm sure you've all heard that the best developers are lazy and/or &lt;a href="http://www.codinghorror.com/blog/archives/000373.html"&gt;dumb&lt;/a&gt;.
I certainly agree with that. Also, you know that writing repetitive code and repeatedly
following tedious steps is bad and a waste of time. 
&lt;/p&gt;
&lt;p&gt;
OK, so we know that those things are bad... but how do you identify them? Often tedious
steps and repetitive code is taught as the way you do things... so you just do them
in that way without asking questions. Other times, we just don't know that there is
an easier way. We're developers--we're creative, smart, inventive, and we should be
able to tell when something can and should be done in a better way.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Let me give you an example of some code I came across some time ago:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; userstring &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; = &lt;span class="str"&gt;"U000000"&lt;/span&gt; &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; GetUserIDStr(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; userid &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; &lt;span class="rem"&gt;'Returns
String:&lt;/span&gt; &lt;span class="rem"&gt;' User Info FileName&lt;/span&gt; &lt;span class="kwrd"&gt;Dim&lt;/span&gt; source &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; =
userid.ToString() &lt;span class="rem"&gt;'Dim destination As Char() = {"U"c, "0"c, "0"c,
"0"c, "0"c, "0"c, "0"c}&lt;/span&gt; &lt;span class="kwrd"&gt;Dim&lt;/span&gt; destination &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; = &lt;span class="kwrd"&gt;Me&lt;/span&gt;.userstring &lt;span class="kwrd"&gt;Dim&lt;/span&gt; sourcelen &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; =
source.Length() &lt;span class="kwrd"&gt;Dim&lt;/span&gt; destindex &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; =
7 - sourcelen source.CopyTo(0, destination, destindex, sourcelen) &lt;span class="kwrd"&gt;Return&lt;/span&gt; destination &lt;span class="kwrd"&gt;End&lt;/span&gt; Function&lt;/pre&gt;
&lt;p&gt;
Obviously there are several things... off... with this code. Let's discuss some of
them. The first is the original Dim destination that is now commented out. This is
an example of someone partially applying what I'm talking about. They realized that
typing out that array was unnecessarily complicated so they did something about it.
That's great, that's what we need to do. Unfortunately, they stopped there. I don't
think we should stop there. We should look at the code, say, wow, this isn't really
doing anything but formatting a number into a string. There has to be a better way
to do this! In this case there certainly is... all of that code can be replaced with
something along the lines of: 
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;.Format('U{0:D06}',
userid)&lt;/pre&gt;
&lt;p&gt;
Which is easier to read? Write? Maintain? The answer is obvious... even if you didn't
know about String.Format, you should know when you're writing that code that there
has to be a better way. Google is your friend.
&lt;/p&gt;
&lt;p&gt;
What if there isn't a better way? Can you think of a better way? Ask yourself these
questions:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Are you going to be doing this more than a few times? 
&lt;/li&gt;
&lt;li&gt;
Are other people on your team going to be doing it? 
&lt;/li&gt;
&lt;li&gt;
Are you looking for a break from your normal work? 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
If the answer to any of those questions is yes, take some time to write or come up
with&amp;nbsp;a better way. You're a developer. You develop. Remember, you can write software
for yourself and your teams that will make writing software more enjoyable and faster.
And then, when you're done, share it! Share it with the world, make development easier
for everyone. 
&lt;/p&gt;
&lt;p&gt;
I'll give you another example. I've &lt;a href="http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx"&gt;already
blogged about it&lt;/a&gt;, but it's relevant to this post so I'll rehash here. In order
to&amp;nbsp;localize a string in &lt;a href="http://www.castleproject.org/MonoRail/"&gt;MonoRail&lt;/a&gt; you
must do the following:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new resx file.&lt;/li&gt;
&lt;li&gt;
Add a Resource&amp;nbsp;attribute to your controller mapping the resx to a key in the
PropertyBag.&lt;/li&gt;
&lt;li&gt;
For each string you want to localize, add a key/value pair to the resx file.&lt;/li&gt;
&lt;li&gt;
Replace the strings in your view template with your resource key.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Hm. Every controller? Every string? Talk about context switching. Not to mention the
fact that you can't see the actual English string in your view template, so any modifications
to the English string require the same context switching. It didn't take me long to
decide that was far too tedious for myself and my team. 
&lt;/p&gt;
&lt;p&gt;
It probably took me 2-3 days overall to write both the &lt;a href="http://blog.eleutian.com/PermaLink,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx"&gt;ASP.NET
preprocessor&lt;/a&gt; and &lt;a href="http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx"&gt;adapt
it to MonoRail/Brail&lt;/a&gt;. Will I ever get 2-3 whole days back? Who knows. Will my
entire team in total? More likely. Does it save us countless context switches, speed
up our development, and make localization trivial? Absolutely. So was it worth it?
Absolutely. 
&lt;/p&gt;
&lt;p&gt;
And before you ask, yes I plan on taking my own advice and sharing it... eventually.
Oh and keep in mind, this sort of thinking can and should be applied to everything
you do, including but not limited to code, tools, and process. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=0d492114-3c0c-478a-9f65-8d8072b48ff4" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,0d492114-3c0c-478a-9f65-8d8072b48ff4.aspx</comments>
      <category>development</category>
      <category>productivity</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=632c968b-0164-48ed-8903-ac0d03b91c48</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,632c968b-0164-48ed-8903-ac0d03b91c48.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,632c968b-0164-48ed-8903-ac0d03b91c48.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=632c968b-0164-48ed-8903-ac0d03b91c48</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In a <a href="http://blog.eleutian.com/PermaLink,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx">previous
post</a> I talked about how we use automatically generated interfaces to wrap our
PropertyBag's. You can also use this technique in your ViewComponents to clean up
that code quite a lot. We used to have code like this:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> SomeComponent
: ViewComponent { <span class="kwrd">public</span><span class="kwrd">override</span><span class="kwrd">void</span> Initialize()
{ <span class="kwrd">base</span>.Initialize(); User user = (User)<span class="kwrd">this</span>.ComponentParams[<span class="str">"User"</span>]; <span class="kwrd">float</span><span class="kwrd">value</span> = <span class="kwrd">this</span>.Service.CalculateThatValue(user); <span class="kwrd">this</span>.ComponentParams[<span class="str">"SomeCalculatedValue"</span>]
= <span class="kwrd">value</span>; } }</pre>
        <p>
Now we can make an interface:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">interface</span> ISomeComponentView
{ User User { get; set; } <span class="kwrd">float</span> SomeCalculatedValue { get;
set; } } </pre>
        <p>
And ask the code we wrote for the PropertyBag to give us a hand in cleaning things
up:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> SomeComponent
: ViewComponent { <span class="kwrd">public</span><span class="kwrd">override</span><span class="kwrd">void</span> Initialize()
{ <span class="kwrd">base</span>.Initialize(); ISomeComponentView view = <span class="kwrd">this</span>.ViewFactory.ResolveView&lt;ISomeComponentView&gt;(<span class="kwrd">this</span>.ComponentParams); <span class="kwrd">float</span><span class="kwrd">value</span> = <span class="kwrd">this</span>.Service.CalculateThatValue(view.User);
view.SomeCalculatedValue = <span class="kwrd">value</span>; } }</pre>
        <p>
Much better! If you're looking for an implementation of that ViewFactory service,
Lee Henson <a href="http://groups.google.com/group/castle-project-devel/browse_thread/thread/d476f43c539d1e7d">tossed
one together</a> shortly after our post. He's also added some other, pretty nitfy
features since then. Like the ability to specify a prefix with an attribute for the
generated dictionary keys. As he mentions in his post, you can use it for anything
(and probably should) that uses strings as keys into an IDictionary (Session, Flash,
etc..). I'm planning on checking it in to CastleContrib soon, until then, grab it
off the list. Thanks Lee!
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=632c968b-0164-48ed-8903-ac0d03b91c48" />
      </body>
      <title>ViewFactory Updates and ViewComponents</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,632c968b-0164-48ed-8903-ac0d03b91c48.aspx</guid>
      <link>http://blog.eleutian.com/2007/03/22/ViewFactoryUpdatesAndViewComponents.aspx</link>
      <pubDate>Thu, 22 Mar 2007 17:21:27 GMT</pubDate>
      <description>&lt;p&gt;
In a &lt;a href="http://blog.eleutian.com/PermaLink,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx"&gt;previous
post&lt;/a&gt; I talked about how we use automatically generated interfaces to wrap our
PropertyBag's. You can also use this technique in your ViewComponents to clean up
that code quite a lot. We used to have code like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SomeComponent
: ViewComponent { &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Initialize()
{ &lt;span class="kwrd"&gt;base&lt;/span&gt;.Initialize(); User user = (User)&lt;span class="kwrd"&gt;this&lt;/span&gt;.ComponentParams[&lt;span class="str"&gt;"User"&lt;/span&gt;]; &lt;span class="kwrd"&gt;float&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;this&lt;/span&gt;.Service.CalculateThatValue(user); &lt;span class="kwrd"&gt;this&lt;/span&gt;.ComponentParams[&lt;span class="str"&gt;"SomeCalculatedValue"&lt;/span&gt;]
= &lt;span class="kwrd"&gt;value&lt;/span&gt;; } }&lt;/pre&gt;
&lt;p&gt;
Now we can make an interface:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ISomeComponentView
{ User User { get; set; } &lt;span class="kwrd"&gt;float&lt;/span&gt; SomeCalculatedValue { get;
set; } } &lt;/pre&gt;
&lt;p&gt;
And ask the code we wrote for the PropertyBag to give us a hand in cleaning things
up:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SomeComponent
: ViewComponent { &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Initialize()
{ &lt;span class="kwrd"&gt;base&lt;/span&gt;.Initialize(); ISomeComponentView view = &lt;span class="kwrd"&gt;this&lt;/span&gt;.ViewFactory.ResolveView&amp;lt;ISomeComponentView&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt;.ComponentParams); &lt;span class="kwrd"&gt;float&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;this&lt;/span&gt;.Service.CalculateThatValue(view.User);
view.SomeCalculatedValue = &lt;span class="kwrd"&gt;value&lt;/span&gt;; } }&lt;/pre&gt;
&lt;p&gt;
Much better! If you're looking for an implementation of that ViewFactory service,
Lee Henson &lt;a href="http://groups.google.com/group/castle-project-devel/browse_thread/thread/d476f43c539d1e7d"&gt;tossed
one together&lt;/a&gt; shortly after our post. He's also added some other, pretty nitfy
features since then. Like the ability to specify a prefix with an attribute for the
generated dictionary keys. As he mentions in his post, you can use it for anything
(and probably should) that uses strings as keys into an IDictionary (Session, Flash,
etc..). I'm planning on checking it in to CastleContrib soon, until then, grab it
off the list. Thanks Lee!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=632c968b-0164-48ed-8903-ac0d03b91c48" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,632c968b-0164-48ed-8903-ac0d03b91c48.aspx</comments>
      <category>development</category>
      <category>monorail</category>
      <category>open source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=cc358a7d-70aa-425e-93b1-4e02ce65c497</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,cc358a7d-70aa-425e-93b1-4e02ce65c497.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,cc358a7d-70aa-425e-93b1-4e02ce65c497.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=cc358a7d-70aa-425e-93b1-4e02ce65c497</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.martinfowler.com/">Martin Fowler</a> has a great post entitled <a href="http://www.martinfowler.com/articles/mocksArentStubs.html">Mocks
Aren't Stubs</a>. Go ahead and read/skim through that if you haven't. Of the two categories
of TDDers he defines, I am definitely a mockist. That is, I almost always "use a mock
for any object with interesting behavior". I do use Mocks and Stubs, but those stubs
are generally just Mocks set up to be stubs (as it's easier than stubbing a class
myself.) That said, I do have a few ground rules for when I write tests with <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino
Mocks</a> that sort fall into the whole <a href="http://www.hedgate.net/articles/2006/11/17/one-assertion-per-test-should-come-natural">one</a><a href="http://weblogs.asp.net/rosherove/archive/2006/10/04/Avoid-multiple-asserts-in-a-single-unit-test_3A00_-revisited.aspx">assertion</a><a href="http://dotavery.com/blog/archive/2006/09/25/21263.aspx">per
test</a> topic and the mocks vs. stubs thing. Here they are:
</p>
        <ol>
          <li>
Name TestMethods MethodOrPropertyName_Scenario_ExpectedResult. I stole this from someone,
but I can't remember who. If you know what post that was, let me know so I can link
it. An example is: CalculateCost_WhenProductIsNull_ThrowsException. I just think
this makes your intent very clear and readable. 
</li>
          <li>
Never use _mocks.Ordered() unless you are actually testing the order in which something
is called... and if you are, test only that! 
</li>
          <li>
Unless you're testing that something gets called once and only once (expensive method?
caching?), use Repeat.Any(). Who cares if it gets called again? 
</li>
          <li>
Prefer DynamicMock over CreateMock (standard mocks). Here's an example. You're testing
that your controller sets FirstName on a view. You write that test and implement the
feature. Two days later you need to add EmailAddress to that view, so you write your
test to ensure that it is added. You implement it and run your tests... now, if you
used a standard mock, your original FirstName test would fail! With a DynamicMock
however, you have no tests to "fix" (tests should rarely need to be fixed because
you added something to the class that has no bearing on the originally tested scenario) 
</li>
          <li>
Don't use VerifyAll. What!?!? That's right, half the time I use Verify(someMock) and
the other half I don't even Verify anything (as I can verify the result of a method
called). Your tests should be complete enough that it shouldn't matter if ObjectA
called ObjectB as long as ObjectA gives you the correct result.</li>
        </ol>
        <p>
Remember, Verification, Ordering, setting a fixed # of repeats, requiring every method/property
to be "expected", etc, are all Assertions. I'm not steadfast on the whole one assertion
per test thing, as sometimes a few assertions make sense. As long as you at least
think about what your test would be named if you put every assertion in the method
name of the test, I think that a lot of these rules would make more sense.... Would
you rather diagnose a break in SomeMethod_WhenSomeObjectIsGreen_CallsFooTwiceThenCallsBarWithResultOfFoo<br />
OnceThenSetsColorAndShapeAndResultOfFoo() or SomeMethod_WhenSomeObjectIsGreen_SetsColor()?
:)
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=cc358a7d-70aa-425e-93b1-4e02ce65c497" />
      </body>
      <title>Loosely-coupled Mocks</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,cc358a7d-70aa-425e-93b1-4e02ce65c497.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/27/LooselycoupledMocks.aspx</link>
      <pubDate>Tue, 27 Feb 2007 20:13:04 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.martinfowler.com/"&gt;Martin Fowler&lt;/a&gt; has a great post entitled &lt;a href="http://www.martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks
Aren't Stubs&lt;/a&gt;. Go ahead and read/skim through that if you haven't. Of the two categories
of TDDers he defines, I am definitely a mockist. That is, I almost always "use a mock
for any object with interesting behavior". I do use Mocks and Stubs, but those stubs
are generally just Mocks set up to be stubs (as it's easier than stubbing a class
myself.) That said, I do have a few ground rules for when I write tests with &lt;a href="http://ayende.com/projects/rhino-mocks.aspx"&gt;Rhino
Mocks&lt;/a&gt; that sort fall into the whole &lt;a href="http://www.hedgate.net/articles/2006/11/17/one-assertion-per-test-should-come-natural"&gt;one&lt;/a&gt; &lt;a href="http://weblogs.asp.net/rosherove/archive/2006/10/04/Avoid-multiple-asserts-in-a-single-unit-test_3A00_-revisited.aspx"&gt;assertion&lt;/a&gt; &lt;a href="http://dotavery.com/blog/archive/2006/09/25/21263.aspx"&gt;per
test&lt;/a&gt; topic and the mocks vs. stubs thing. Here they are:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Name TestMethods MethodOrPropertyName_Scenario_ExpectedResult. I stole this from someone,
but I can't remember who. If you know what post that was, let me know so I can link
it.&amp;nbsp;An example is: CalculateCost_WhenProductIsNull_ThrowsException. I just think
this makes your intent very clear and readable. 
&lt;/li&gt;
&lt;li&gt;
Never use _mocks.Ordered() unless you are actually testing the order in which something
is called... and if you are, test only that! 
&lt;/li&gt;
&lt;li&gt;
Unless you're testing that something gets called once and only once (expensive method?
caching?), use Repeat.Any(). Who cares if it gets called again? 
&lt;/li&gt;
&lt;li&gt;
Prefer DynamicMock over CreateMock (standard mocks). Here's an example. You're testing
that your controller sets FirstName on a view. You write that test and implement the
feature. Two days later you need to add EmailAddress to that view, so you write your
test to ensure that it is added. You implement it and run your tests... now, if you
used a standard mock, your original FirstName test would fail! With a DynamicMock
however, you have no tests to "fix" (tests should rarely need to be fixed because
you added something to the class that has no bearing on the originally tested scenario) 
&lt;/li&gt;
&lt;li&gt;
Don't use VerifyAll. What!?!? That's right, half the time I use Verify(someMock) and
the other half I don't even Verify anything (as I can verify the result of a method
called). Your tests should be complete enough that it shouldn't matter if ObjectA
called ObjectB as long as ObjectA gives you the correct result.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Remember, Verification, Ordering, setting a fixed # of repeats, requiring every method/property
to be "expected", etc, are all Assertions. I'm not steadfast on the whole one assertion
per test thing, as sometimes a few assertions make sense. As long as you at least
think about what your test would be named if you put every assertion in the method
name of the test, I think that a lot of these rules would make more sense....&amp;nbsp;Would
you rather diagnose a break in&amp;nbsp;SomeMethod_WhenSomeObjectIsGreen_CallsFooTwiceThenCallsBarWithResultOfFoo&lt;br&gt;
OnceThenSetsColorAndShapeAndResultOfFoo() or SomeMethod_WhenSomeObjectIsGreen_SetsColor()?
:)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=cc358a7d-70aa-425e-93b1-4e02ce65c497" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,cc358a7d-70aa-425e-93b1-4e02ce65c497.aspx</comments>
      <category>development</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://weblogs.asp.net/rosherove/archive/2007/02/25/why-you-should-think-about-ootp-object-oriented-testable-programming.aspx">Roy
Osherove brings up</a> several good points with regards to the preached design
methodology of old and the new testable design some of us have come to know and love
lately. 
</p>
        <p>
I think that TDD really helps to eliminate a large number of these issues. If you
test everything yourself, then inherently your API is testable. Unfortunately, as <a href="http://weblogs.asp.net/jcogley/archive/2007/02/25/will-tdd-become-mainstream.aspx">Jonathan
Cogley points out</a>, TDD is not yet <a href="http://en.wikipedia.org/wiki/Mainstream">mainstream</a>.
</p>
        <p>
A nice side effect of having a testable API or application is that often times your
code is also extensible. The thing that blocks it from being fully testable and extensible
are those darn sealed and internal keywords, and of course, not making things virtual
that really should be. Even <a href="http://www.castleproject.org">open source projects</a> are <a href="https://svn.castleproject.org/svn/castle/trunk/MonoRail/Castle.MonoRail.Framework/Controller.cs">guilty</a> of
not having testable or extensible enough APIs. Yes, Castle MonoRail provides <a href="http://www.castleproject.org/monorail/documentation/v1rc2/usersguide/Generated_AbstractMRTestCase.html">AbstractMRTestCase</a>,
but that actually uses ASP.NET and is way more heavy than I like.
</p>
        <p>
Here is our base class for Controller Tests. This allows you to unit test nearly every
aspect of your controller, without ever hitting ASP.NET. Notice the lovely and necessary
use of reflection? In order to mock the context, you'll want to call InitializeController
and pass it the controller you're testing, but be sure to do it in your _mocks.Unordered
block (we almost ALWAYS use mocks.Unordered... it makes tests more readable and
ensures that you're tests aren't too tightly coupled with your implementation, but
that's the subject for another post.)
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> EleutianControllerTests
{ <span class="preproc">#region</span> Constants <span class="kwrd">private</span><span class="kwrd">const</span><span class="kwrd">string</span> ApplicationPhysicalPath
= <span class="str">"Q:\\PhysicalPath"</span>; <span class="preproc">#endregion</span><span class="preproc">#region</span> Member
Data <span class="kwrd">protected</span><span class="kwrd">string</span> _virtualDirectory
= String.Empty; <span class="kwrd">protected</span> MockRepository _mocks; <span class="kwrd">protected</span> IControllerServices
_controllerServices; <span class="kwrd">protected</span> IRailsEngineContext _context; <span class="kwrd">protected</span> IRequest
_request; <span class="kwrd">protected</span> IResponse _response; <span class="kwrd">protected</span> IServerUtility
_serverUtility; <span class="kwrd">protected</span> IDictionary _session; <span class="kwrd">protected</span> Flash
_flash; <span class="kwrd">protected</span> NameValueCollection _parameters; <span class="preproc">#endregion</span><span class="preproc">#region</span> Test
Setup and Teardown Methods [SetUp] <span class="kwrd">public</span><span class="kwrd">virtual</span><span class="kwrd">void</span> Setup()
{ _mocks = <span class="kwrd">new</span> MockRepository(); _context = _mocks.CreateMock&lt;IRailsEngineContext&gt;();
_request = _mocks.DynamicMock&lt;IRequest&gt;(); _response = _mocks.DynamicMock&lt;IResponse&gt;();
_serverUtility = _mocks.DynamicMock&lt;IServerUtility&gt;(); _session = _mocks.DynamicMock&lt;IDictionary&gt;();
_flash = <span class="kwrd">new</span> Flash(); _parameters = <span class="kwrd">new</span> NameValueCollection();
} <span class="kwrd">protected</span><span class="kwrd">void</span> InitializeController(Controller
controller, <span class="kwrd">string</span> areaName, <span class="kwrd">string</span> controllerName, <span class="kwrd">string</span> actionName)
{ BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Instance
| BindingFlags.NonPublic | BindingFlags.InvokeMethod; MethodInfo method = controller.GetType().GetMethod(<span class="str">"InitializeControllerState"</span>,
bindingFlags); method.Invoke(controller, <span class="kwrd">new</span><span class="kwrd">object</span>[]
{ areaName, controllerName, actionName }); FieldInfo field = controller.GetType().GetField(<span class="str">"context"</span>,
bindingFlags); field.SetValue(controller, _context); field = controller.GetType().GetField(<span class="str">"serviceProvider"</span>,
bindingFlags); field.SetValue(controller, _context); InitializeRailsContext(areaName,
controllerName, actionName); } <span class="kwrd">protected</span><span class="kwrd">void</span> InitializeRailsContext(<span class="kwrd">string</span> areaName, <span class="kwrd">string</span> controllerName, <span class="kwrd">string</span> actionName)
{ Expect.Call(_context.UrlInfo).Return( <span class="kwrd">new</span> UrlInfo(<span class="str">"eleutian.com"</span>, <span class="str">"www"</span>,
_virtualDirectory, <span class="str">"http"</span>, 80, Path.Combine(Path.Combine(areaName,
controllerName), actionName), areaName, controllerName, actionName, <span class="str">"rails"</span>)).Repeat.Any();
Expect.Call(_context.GetService(<span class="kwrd">typeof</span>(IUrlBuilder))).Return(<span class="kwrd">new</span> DefaultUrlBuilder()).Repeat.Any();
Expect.Call(_context.Server).Return(_serverUtility).Repeat.Any(); Expect.Call(_context.Flash).Return(_flash).Repeat.Any();
Expect.Call(_context.ApplicationPath).Return(<span class="str">"/"</span>).Repeat.Any();
Expect.Call(_context.Request).Return(_request).Repeat.Any(); Expect.Call(_context.Response).Return(_response).Repeat.Any();
Expect.Call(_context.ApplicationPhysicalPath).Return(ApplicationPhysicalPath).Repeat.Any();
Expect.Call(_request.Params).Return(_parameters).Repeat.Any(); Expect.Call(_context.Session).Return(_session).Repeat.Any();
} <span class="preproc">#endregion</span> } </pre>
        <p>
Oh, and back to the original point of the post. Please, please don't mark things internal
or sealed unless you have a very good reason to do it. And no, I don't think that
"We can't afford to support it" is a good enough reason. Also, "We're afraid the user
might break something" is DEFINITELY not a good enough reason. We know what we're
getting into when we extend APIs... we're all developers here.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa" />
      </body>
      <title>Object Oriented Design, Testability and Extensibility</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/26/ObjectOrientedDesignTestabilityAndExtensibility.aspx</link>
      <pubDate>Mon, 26 Feb 2007 00:16:47 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://weblogs.asp.net/rosherove/archive/2007/02/25/why-you-should-think-about-ootp-object-oriented-testable-programming.aspx"&gt;Roy
Osherove brings up&lt;/a&gt;&amp;nbsp;several good points with regards to the preached design
methodology of old and the new testable design some of us have come to know and love
lately. 
&lt;/p&gt;
&lt;p&gt;
I think that TDD really helps to eliminate a large number of these issues. If you
test everything yourself, then inherently your API is testable. Unfortunately, as &lt;a href="http://weblogs.asp.net/jcogley/archive/2007/02/25/will-tdd-become-mainstream.aspx"&gt;Jonathan
Cogley points out&lt;/a&gt;, TDD is not yet &lt;a href="http://en.wikipedia.org/wiki/Mainstream"&gt;mainstream&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
A nice side effect of having a testable API or application is that often times your
code is also extensible. The thing that blocks it from being fully testable and extensible
are those darn sealed and internal keywords, and of course, not making things virtual
that really should be. Even &lt;a href="http://www.castleproject.org"&gt;open source projects&lt;/a&gt; are &lt;a href="https://svn.castleproject.org/svn/castle/trunk/MonoRail/Castle.MonoRail.Framework/Controller.cs"&gt;guilty&lt;/a&gt;&amp;nbsp;of
not having testable or extensible enough APIs.&amp;nbsp;Yes, Castle MonoRail provides &lt;a href="http://www.castleproject.org/monorail/documentation/v1rc2/usersguide/Generated_AbstractMRTestCase.html"&gt;AbstractMRTestCase&lt;/a&gt;,
but that actually uses ASP.NET and is way more heavy than I like.
&lt;/p&gt;
&lt;p&gt;
Here is our base class for Controller Tests. This allows you to unit test nearly every
aspect of your controller, without ever hitting ASP.NET. Notice the lovely and necessary
use of reflection? In order to mock the context, you'll want to call InitializeController
and pass it the controller you're testing, but be sure to do it in your _mocks.Unordered
block (we&amp;nbsp;almost ALWAYS use mocks.Unordered... it makes tests more readable and
ensures that you're tests aren't too tightly coupled with your implementation, but
that's the subject for another post.)
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EleutianControllerTests
{ &lt;span class="preproc"&gt;#region&lt;/span&gt; Constants &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ApplicationPhysicalPath
= &lt;span class="str"&gt;"Q:\\PhysicalPath"&lt;/span&gt;; &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Member
Data &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _virtualDirectory
= String.Empty; &lt;span class="kwrd"&gt;protected&lt;/span&gt; MockRepository _mocks; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IControllerServices
_controllerServices; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IRailsEngineContext _context; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IRequest
_request; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IResponse _response; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IServerUtility
_serverUtility; &lt;span class="kwrd"&gt;protected&lt;/span&gt; IDictionary _session; &lt;span class="kwrd"&gt;protected&lt;/span&gt; Flash
_flash; &lt;span class="kwrd"&gt;protected&lt;/span&gt; NameValueCollection _parameters; &lt;span class="preproc"&gt;#endregion&lt;/span&gt; &lt;span class="preproc"&gt;#region&lt;/span&gt; Test
Setup and Teardown Methods [SetUp] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()
{ _mocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); _context = _mocks.CreateMock&amp;lt;IRailsEngineContext&amp;gt;();
_request = _mocks.DynamicMock&amp;lt;IRequest&amp;gt;(); _response = _mocks.DynamicMock&amp;lt;IResponse&amp;gt;();
_serverUtility = _mocks.DynamicMock&amp;lt;IServerUtility&amp;gt;(); _session = _mocks.DynamicMock&amp;lt;IDictionary&amp;gt;();
_flash = &lt;span class="kwrd"&gt;new&lt;/span&gt; Flash(); _parameters = &lt;span class="kwrd"&gt;new&lt;/span&gt; NameValueCollection();
} &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeController(Controller
controller, &lt;span class="kwrd"&gt;string&lt;/span&gt; areaName, &lt;span class="kwrd"&gt;string&lt;/span&gt; controllerName, &lt;span class="kwrd"&gt;string&lt;/span&gt; actionName)
{ BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Instance
| BindingFlags.NonPublic | BindingFlags.InvokeMethod; MethodInfo method = controller.GetType().GetMethod(&lt;span class="str"&gt;"InitializeControllerState"&lt;/span&gt;,
bindingFlags); method.Invoke(controller, &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[]
{ areaName, controllerName, actionName }); FieldInfo field = controller.GetType().GetField(&lt;span class="str"&gt;"context"&lt;/span&gt;,
bindingFlags); field.SetValue(controller, _context); field = controller.GetType().GetField(&lt;span class="str"&gt;"serviceProvider"&lt;/span&gt;,
bindingFlags); field.SetValue(controller, _context); InitializeRailsContext(areaName,
controllerName, actionName); } &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeRailsContext(&lt;span class="kwrd"&gt;string&lt;/span&gt; areaName, &lt;span class="kwrd"&gt;string&lt;/span&gt; controllerName, &lt;span class="kwrd"&gt;string&lt;/span&gt; actionName)
{ Expect.Call(_context.UrlInfo).Return( &lt;span class="kwrd"&gt;new&lt;/span&gt; UrlInfo(&lt;span class="str"&gt;"eleutian.com"&lt;/span&gt;, &lt;span class="str"&gt;"www"&lt;/span&gt;,
_virtualDirectory, &lt;span class="str"&gt;"http"&lt;/span&gt;, 80, Path.Combine(Path.Combine(areaName,
controllerName), actionName), areaName, controllerName, actionName, &lt;span class="str"&gt;"rails"&lt;/span&gt;)).Repeat.Any();
Expect.Call(_context.GetService(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IUrlBuilder))).Return(&lt;span class="kwrd"&gt;new&lt;/span&gt; DefaultUrlBuilder()).Repeat.Any();
Expect.Call(_context.Server).Return(_serverUtility).Repeat.Any(); Expect.Call(_context.Flash).Return(_flash).Repeat.Any();
Expect.Call(_context.ApplicationPath).Return(&lt;span class="str"&gt;"/"&lt;/span&gt;).Repeat.Any();
Expect.Call(_context.Request).Return(_request).Repeat.Any(); Expect.Call(_context.Response).Return(_response).Repeat.Any();
Expect.Call(_context.ApplicationPhysicalPath).Return(ApplicationPhysicalPath).Repeat.Any();
Expect.Call(_request.Params).Return(_parameters).Repeat.Any(); Expect.Call(_context.Session).Return(_session).Repeat.Any();
} &lt;span class="preproc"&gt;#endregion&lt;/span&gt; } &lt;/pre&gt;
&lt;p&gt;
Oh, and back to the original point of the post. Please, please don't mark things internal
or sealed unless you have a very good reason to do it. And no, I don't think that
"We can't afford to support it" is a good enough reason. Also, "We're afraid the user
might break something" is DEFINITELY not a good enough reason. We know what we're
getting into when we extend APIs... we're all developers here.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,e9abf4e0-10f6-4c28-8494-b79ebb0d5ffa.aspx</comments>
      <category>development</category>
      <category>monorail</category>
      <category>open source</category>
      <category>source</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=762249da-e25a-4503-8f20-c6d59b1a69bc</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=762249da-e25a-4503-8f20-c6d59b1a69bc</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been writing a lot of tests lately because our code coverage isn't up to where
we'd like it. NCover+NCoverExplorer is a pretty awesome and while it does <a href="http://www.ncover.org/COMMUNITY/ShowPost.aspx?PostID=436">have
its quirks</a> it has been incredibly useful. A lot of my adventures in writing
tests is creating tons of RhinoMocks so that I can pass them to constructors
and that sort of thing. So I got a strange idea, and I'm not sure how I feel about
it necessarily and figured I'd propose it to all of you out there and get your thoughts.
</p>
        <p>
Basically, 99% of the time in a services unit test, all the interfaces passed to the
service will be mocks, so we end up with lots of functions that look like this:
</p>
        <pre class="csharpcode">[SetUp]
<span class="kwrd">public</span><span class="kwrd">void</span> Setup()
{ _service1 = _mocks.CreateMock&lt;IService1&gt;(); _service2 = _mocks.CreateMock&lt;IService2&gt;();
_service3 = _mocks.CreateMock&lt;IService3&gt;(); _serviceWeAreTesting = <span class="kwrd">new</span> DefaultServiceWeAreTesting(_service1,
_service2, _service3); }</pre>
        <p>
If we do this a lot for DefaultServiceWeAreTesting, it's often refactored to a single
location. All in all, it can be very repetitive to create all the mocks. So, as an
experiment I decided to leverage the power of the IoC container, in our case Windsor,
to take care of a lot of the mundane for me. In the end I end up with this code:
</p>
        <pre class="csharpcode">[SetUp]
<span class="kwrd">public</span><span class="kwrd">void</span> Setup()
{ _mocks = <span class="kwrd">new</span> MockRepository(); _container = <span class="kwrd">new</span> AutoMockingContainer(_mocks);
_service = _container.Create&lt;DefaultServiceWeAreTesting&gt;(); }</pre>
        <p>
AutoMockingContainer is simply a WindsorContainer with a custom facility and dependency
resolver that supplies a RhinoMock for any interface that is resolved. We've also
moved the setup code into a base class AutoMockingTests that has the container and
mock repository. We can then write code that uses the mocks from the container:
</p>
        <pre class="csharpcode">[Test]
<span class="kwrd">public</span><span class="kwrd">void</span> DoWork_Always_AsksOtherServices()
{ <span class="kwrd">using</span> (_mocks.Unordered()) { _container.Get&lt;Service1&gt;().DoWork();
_container.Get&lt;Service2&gt;().DoWork(); _container.Get&lt;Service3&gt;().DoWork();
} _mocks.ReplayAll(); _service.DoWork(); _mocks.VerifyAll(); }</pre>
        <p>
Of course, you don't have to always do the Get call on the container, you can do that
in the Setup and use member variables, etc... the point is, the mocked services are
created for us. We can also mark other types so that they aren't tested, and take
advantage of the IoC and injection that we use in our application to speed up writing
and maintaining tests. The implementation I've uploaded allows you to associate a
particular strategy with service dependencies. They are:
</p>
        <ul>
          <li>
StubbedStrategy: A mock will be created, then for each property a GetValue expectation
(with Repeat.Any) will be added to return a value for that property. The value will
be retrieved like any other dependency (it's strategy will be gotten, and the value
from that)</li>
          <li>
DynamicStrategy (default): A DynamicMock will be created for the dependency.</li>
          <li>
StandardStrategy: A standard mock from CreateMock.</li>
          <li>
NonMockedStrategy: Automatically set for any service that is registered normally with
the container - a standard container resolve.</li>
        </ul>
        <p>
So, pros and cons? To get things started:
</p>
        <p>
          <strong>Pros</strong>
        </p>
        <ol>
          <li>
Can be easier to maintain and write tests. Adding a dependency or changing the constructor
breaks tests in ways that the production environment can usually handle just fine. 
</li>
          <li>
Automatic testing the services construction via the IoC container.</li>
        </ol>
        <p>
          <strong>Cons</strong>
        </p>
        <ol>
          <li>
Speed/peformance? Does making the container such an integral part of the test fixtures
hurt performance? Setup has to be longer when we're creating the container the way
we are. Does this matter? Only as long as the time saved when writing tests is larger
than the time it takes to run them, which I'm sure is the case...</li>
        </ol>
        <p>
What do you think? So far we really like the idea, or at least think it's interesting.
Here are the sources:
</p>
        <p>
          <a href="http://blog.eleutian.com/download/AutoMockingContainer.zip">Source and Binaries</a>
        </p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=762249da-e25a-4503-8f20-c6d59b1a69bc" />
      </body>
      <title>Tests + AutoMocking + IoC Container = ?</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx</link>
      <pubDate>Fri, 23 Feb 2007 03:04:26 GMT</pubDate>
      <description>&lt;p&gt;
I've been writing a lot of tests lately because our code coverage isn't up to where
we'd like it. NCover+NCoverExplorer is a pretty awesome and while it does&amp;nbsp;&lt;a href="http://www.ncover.org/COMMUNITY/ShowPost.aspx?PostID=436"&gt;have
its quirks&lt;/a&gt;&amp;nbsp;it has been incredibly useful. A lot of my adventures in writing
tests is creating tons of RhinoMocks so that I&amp;nbsp;can pass them to constructors
and that sort of thing. So I got a strange idea, and I'm not sure how I feel about
it necessarily and figured I'd propose it to all of you out there and get your thoughts.
&lt;/p&gt;
&lt;p&gt;
Basically, 99% of the time in a services unit test, all the interfaces passed to the
service will be mocks, so we end up with lots of functions that look like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;[SetUp]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()
{ _service1 = _mocks.CreateMock&amp;lt;IService1&amp;gt;(); _service2 = _mocks.CreateMock&amp;lt;IService2&amp;gt;();
_service3 = _mocks.CreateMock&amp;lt;IService3&amp;gt;(); _serviceWeAreTesting = &lt;span class="kwrd"&gt;new&lt;/span&gt; DefaultServiceWeAreTesting(_service1,
_service2, _service3); }&lt;/pre&gt;
&lt;p&gt;
If we do this a lot for DefaultServiceWeAreTesting, it's often refactored to a single
location. All in all, it can be very repetitive to create all the mocks. So, as an
experiment I decided to leverage the power of the IoC container, in our case Windsor,
to take care of a lot of the mundane for me. In the end I end up with this code:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;[SetUp]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Setup()
{ _mocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); _container = &lt;span class="kwrd"&gt;new&lt;/span&gt; AutoMockingContainer(_mocks);
_service = _container.Create&amp;lt;DefaultServiceWeAreTesting&amp;gt;(); }&lt;/pre&gt;
&lt;p&gt;
AutoMockingContainer is simply a WindsorContainer with a custom facility and dependency
resolver that supplies a RhinoMock for any interface that is resolved. We've also
moved the setup code into a base class AutoMockingTests that has the container and
mock repository. We can then write code that uses the mocks from the container:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;[Test]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoWork_Always_AsksOtherServices()
{ &lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { _container.Get&amp;lt;Service1&amp;gt;().DoWork();
_container.Get&amp;lt;Service2&amp;gt;().DoWork(); _container.Get&amp;lt;Service3&amp;gt;().DoWork();
} _mocks.ReplayAll(); _service.DoWork(); _mocks.VerifyAll(); }&lt;/pre&gt;
&lt;p&gt;
Of course, you don't have to always do the Get call on the container, you can do that
in the Setup and use member variables, etc... the point is, the mocked services are
created for us. We can also mark other types so that they aren't tested, and take
advantage of the IoC and injection that we use in our application to speed up writing
and maintaining tests. The implementation I've uploaded allows you to associate a
particular strategy with service dependencies. They are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
StubbedStrategy: A mock will be created, then for each property a GetValue expectation
(with Repeat.Any) will be added to return a value for that property. The value will
be retrieved like any other dependency (it's strategy will be gotten, and the value
from that)&lt;/li&gt;
&lt;li&gt;
DynamicStrategy (default): A DynamicMock will be created for the dependency.&lt;/li&gt;
&lt;li&gt;
StandardStrategy: A standard mock from CreateMock.&lt;/li&gt;
&lt;li&gt;
NonMockedStrategy: Automatically set for any service that is registered normally with
the container - a standard container resolve.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So, pros and cons? To get things started:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Pros&lt;/strong&gt;
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Can be easier to maintain and write tests. Adding a dependency or changing the constructor
breaks tests in ways that the production environment can usually handle just fine. 
&lt;/li&gt;
&lt;li&gt;
Automatic testing the services construction via the IoC container.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;strong&gt;Cons&lt;/strong&gt;
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Speed/peformance? Does making the container such an integral part of the test fixtures
hurt performance? Setup has to be longer when we're creating the container the way
we are. Does this matter? Only as long as the time saved when writing tests is larger
than the time it takes to run them, which I'm sure is the case...&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
What do you think? So far we really like the idea, or at least think it's interesting.
Here are the sources:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/AutoMockingContainer.zip"&gt;Source and Binaries&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=762249da-e25a-4503-8f20-c6d59b1a69bc" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx</comments>
      <category>development</category>
      <category>inversion of control</category>
      <category>source</category>
      <category>test driven development</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm pleased to announce that I've committed Castle.Tools.CodeGenerator into the <a href="https://svn.castleproject.org/svn/castlecontrib/codegenerator/trunk">CastleContrib</a> repository.
What's in trunk also includes some patches posted on the mailing list by Chris Ortman
and some other suggestions. It also includes some bug fixes I made. We've deleted
the code generator from our repository, so any changes we make will be to the castle
repository. Eleutian.Tools.CodeGenerator is no more, long live Castle.Tools.CodeGenerator!
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4" />
      </body>
      <title>Our CodeGenerator in CastleContrib!</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/22/OurCodeGeneratorInCastleContrib.aspx</link>
      <pubDate>Thu, 22 Feb 2007 20:03:57 GMT</pubDate>
      <description>&lt;p&gt;
I'm pleased to announce that I've committed Castle.Tools.CodeGenerator into the &lt;a href="https://svn.castleproject.org/svn/castlecontrib/codegenerator/trunk"&gt;CastleContrib&lt;/a&gt; repository.
What's in trunk also includes some patches posted on the mailing list by Chris Ortman
and some other suggestions. It also includes some bug fixes I made. We've deleted
the code generator from our repository, so any changes we make will be to the castle
repository. Eleutian.Tools.CodeGenerator is no more, long live Castle.Tools.CodeGenerator!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,ec25af40-fdc7-4e37-ae3e-10a27f2d7fe4.aspx</comments>
      <category>development</category>
      <category>news</category>
      <category>open source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=b454e935-2813-4bb9-b8f3-a724a2a81458</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,b454e935-2813-4bb9-b8f3-a724a2a81458.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,b454e935-2813-4bb9-b8f3-a724a2a81458.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=b454e935-2813-4bb9-b8f3-a724a2a81458</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
        <p>
I spent yesterday and today refactoring the code for the Controller Action/View map
generator tool. I made some improvements, wrote some tests and I'm pleased to announce
the first release. I saw Brian had implemented his as an MsBuild task and I liked
that idea, so that's how ours runs now. First, things aren't as "drop in and
go" as I would like because it's still evolving in and around our, already large,
code base. It's my hope that over time the tool evolves as peoples needs change, etc...
we can add some more configuration, that sort of thing. I'm more than willing to hear
suggestions and such. Anway, here we go...
</p>
        <p>
So in order to get yourself up and running, you'll need to do a few things. First,
you'll need to add the task to your MsBuild. Simply open your csproj and add the following:
</p>
        <pre class="csharpcode">
          <span class="kwrd">&lt;</span>
          <span class="html">UsingTask</span>
          <span class="attr">TaskName</span>
          <span class="kwrd">="Eleutian.Tools.CodeGenerator.MsBuild.GenerateMonoRailSiteTreeTask"</span>
          <span class="attr">AssemblyFile</span>
          <span class="kwrd">="Eleutian.Tools.CodeGenerator.dll"</span>
          <span class="kwrd">/&gt;</span>
          <span class="kwrd">&lt;</span>
          <span class="html">ItemGroup</span>
          <span class="kwrd">&gt;</span>
          <span class="kwrd"> &lt;</span>
          <span class="html">ViewSources</span>
          <span class="attr">Include</span>
          <span class="kwrd">="../YourViewsDirectory/**/*.brail"</span>
          <span class="kwrd">&gt;</span>
          <span class="kwrd"> &lt;</span>
          <span class="html">InProject</span>
          <span class="kwrd">&gt;</span>false<span class="kwrd">&lt;/</span><span class="html">InProject</span><span class="kwrd">&gt;</span><span class="kwrd"> &lt;/</span><span class="html">ViewSources</span><span class="kwrd">&gt;</span><span class="kwrd">&lt;/</span><span class="html">ItemGroup</span><span class="kwrd">&gt;</span><span class="kwrd">&lt;</span><span class="html">ItemGroup</span><span class="kwrd">&gt;</span><span class="kwrd"> &lt;</span><span class="html">ControllerSources</span><span class="attr">Include</span><span class="kwrd">="Controllers/**/*.cs"</span><span class="kwrd">&gt;</span><span class="kwrd"> &lt;</span><span class="html">InProject</span><span class="kwrd">&gt;</span>false<span class="kwrd">&lt;/</span><span class="html">InProject</span><span class="kwrd">&gt;</span><span class="kwrd"> &lt;/</span><span class="html">ControllerSources</span><span class="kwrd">&gt;</span><span class="kwrd">&lt;/</span><span class="html">ItemGroup</span><span class="kwrd">&gt;</span><span class="kwrd">&lt;</span><span class="html">Target</span><span class="attr">Name</span><span class="kwrd">="BeforeBuild"</span><span class="attr">Inputs</span><span class="kwrd">="@(Compile)"</span><span class="attr">Outputs</span><span class="kwrd">="$(ProjectDir)\SiteMap.generated.cs"</span><span class="kwrd">&gt;</span><span class="kwrd"> &lt;</span><span class="html">GenerateMonoRailSiteTreeTask</span><span class="attr">File</span><span class="kwrd">="SiteMap.generated.cs"</span><span class="attr">Namespace</span><span class="kwrd">="YourNamespace.SiteMap"</span><span class="attr">ControllerSources</span><span class="kwrd">="@(ControllerSources)"</span><span class="attr">ViewSources</span><span class="kwrd">="@(ViewSources)"</span><span class="kwrd">&gt;</span><span class="kwrd"> &lt;/</span><span class="html">GenerateMonoRailSiteTreeTask</span><span class="kwrd">&gt;</span><span class="kwrd">&lt;/</span><span class="html">Target</span><span class="kwrd">&gt;</span></pre>
        <p>
You'll notice a few things in here. ViewSources is just the collection of view files
(be it .vm or .brail, etc..). It will use the paths of those files to add the View
nodes into the map. ControllerSources is the C# source files for your controllers
only, specifying this helps speed up parsing. I'll get to why in a bit. And for the
Sources attribute on the task you should specify all the sources in the assembly,
which @(Compile) works great for.
</p>
        <p>
With the task added, you should be able to build and have a SiteMap.generated.cs file,
now you'll need to tweak your "base" Controller class. If you're like us, you have
a Controller class that you made yourself that all of your Controller's inherit from.
In that class you'll need to add some code like the following:
</p>
        <pre class="csharpcode">
          <span class="kwrd">private</span> ICodeGeneratorServices _services; <span class="kwrd">public</span> ICodeGeneratorServices
CodeGeneratorServices { get { <span class="kwrd">return</span> _services; } } <span class="kwrd">public</span> RootAreaNode
Site { get { <span class="kwrd">return</span><span class="kwrd">new</span> RootAreaNode(<span class="kwrd">this</span>.ControllerServices);
} } <span class="kwrd">protected</span><span class="kwrd">virtual</span><span class="kwrd">void</span> PerformGeneratedInitialize()
{ _services = <span class="kwrd">new</span> DefaultCodeGeneratorServices( <span class="kwrd">new</span> DefaultControllerReferenceFactory(), <span class="kwrd">new</span> AspDotNetRedirectService()
); _services.Controller = <span class="kwrd">this</span>; } </pre>
        <p>
First is the member variable, _services. You can do this however you like, but it's
consumed by the ControllerAction/ViewReference classes to do various likes. CodeGeneratorServices
is a property the partial controller code uses to create the MyActions and MyViews
node instances. Site is just an easy way to get the top, root level node. PerformGeneratorInitialize
is overridden in the partial controller classes to add the MyViews and MyActions nodes
to the PropertyBag so they are available in the views. So there you go. If I'm not
missing anything, that should do the trick. Let me know if I did and I'll append to
this post.
</p>
        <p>
Oh, the reason for specifying the ControllerSources is to cut down on the number of
classes/types we visit when generating the internal "tree" that the source code is
generated from. At first glance it seems like you should just be able to parse the
ControllerSources only. We coudln't really do that because we needed to gather information
on the other types in the assembly so we'd be able to use them in the arguments of
our actions, etc... 
</p>
        <p>
I have to stress that the code is relatively new, having undergone a major refactor
to isolate it from the rest of our project. I make no promises that I won't have to
upload a new copy, with a fix. :)
</p>
        <p>
          <a href="http://blog.eleutian.com/download/Eleutian.Tools.CodeGenerator-20070220.zip">Source
and Binaries</a>
        </p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=b454e935-2813-4bb9-b8f3-a724a2a81458" />
      </body>
      <title>Eleutian.Tools.CodeGenerator Source</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,b454e935-2813-4bb9-b8f3-a724a2a81458.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/21/EleutianToolsCodeGeneratorSource.aspx</link>
      <pubDate>Wed, 21 Feb 2007 02:36:46 GMT</pubDate>
      <description>&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
I spent yesterday and today refactoring the code for the Controller Action/View map
generator tool. I made some improvements, wrote some tests and I'm pleased to announce
the first release. I saw Brian had implemented his as an MsBuild task and I liked
that idea, so that's how ours runs now.&amp;nbsp;First, things aren't as "drop in and
go" as I would like because it's still evolving in and around our, already large,
code base. It's my hope that over time the tool evolves as peoples needs change, etc...
we can add some more configuration, that sort of thing. I'm more than willing to hear
suggestions and such. Anway, here we go...
&lt;/p&gt;
&lt;p&gt;
So in order to get yourself up and running, you'll need to do a few things. First,
you'll need to add the task to your MsBuild. Simply open your csproj and add the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UsingTask&lt;/span&gt; &lt;span class="attr"&gt;TaskName&lt;/span&gt;&lt;span class="kwrd"&gt;="Eleutian.Tools.CodeGenerator.MsBuild.GenerateMonoRailSiteTreeTask"&lt;/span&gt; &lt;span class="attr"&gt;AssemblyFile&lt;/span&gt;&lt;span class="kwrd"&gt;="Eleutian.Tools.CodeGenerator.dll"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ViewSources&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="../YourViewsDirectory/**/*.brail"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;&lt;/span&gt;&lt;span class="html"&gt;InProject&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;false&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;InProject&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ViewSources&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ControllerSources&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="Controllers/**/*.cs"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;&lt;/span&gt;&lt;span class="html"&gt;InProject&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;false&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;InProject&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ControllerSources&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Target&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="BeforeBuild"&lt;/span&gt; &lt;span class="attr"&gt;Inputs&lt;/span&gt;&lt;span class="kwrd"&gt;="@(Compile)"&lt;/span&gt; &lt;span class="attr"&gt;Outputs&lt;/span&gt;&lt;span class="kwrd"&gt;="$(ProjectDir)\SiteMap.generated.cs"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GenerateMonoRailSiteTreeTask&lt;/span&gt; &lt;span class="attr"&gt;File&lt;/span&gt;&lt;span class="kwrd"&gt;="SiteMap.generated.cs"&lt;/span&gt; &lt;span class="attr"&gt;Namespace&lt;/span&gt;&lt;span class="kwrd"&gt;="YourNamespace.SiteMap"&lt;/span&gt; &lt;span class="attr"&gt;ControllerSources&lt;/span&gt;&lt;span class="kwrd"&gt;="@(ControllerSources)"&lt;/span&gt; &lt;span class="attr"&gt;ViewSources&lt;/span&gt;&lt;span class="kwrd"&gt;="@(ViewSources)"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt; &amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;GenerateMonoRailSiteTreeTask&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Target&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
You'll notice a few things in here. ViewSources is just the collection of view files
(be it .vm or .brail, etc..). It will use the paths of those files to add the View
nodes into the map. ControllerSources is the C# source files for your controllers
only, specifying this helps speed up parsing. I'll get to why in a bit. And for the
Sources attribute on the task you should specify all the sources in the assembly,
which @(Compile) works great for.
&lt;/p&gt;
&lt;p&gt;
With the task added, you should be able to build and have a SiteMap.generated.cs file,
now you'll need to tweak your "base" Controller class. If you're like us, you have
a Controller class that you made yourself that all of your Controller's inherit from.
In that class you'll need to add some code like the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; ICodeGeneratorServices _services; &lt;span class="kwrd"&gt;public&lt;/span&gt; ICodeGeneratorServices
CodeGeneratorServices { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _services; } } &lt;span class="kwrd"&gt;public&lt;/span&gt; RootAreaNode
Site { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; RootAreaNode(&lt;span class="kwrd"&gt;this&lt;/span&gt;.ControllerServices);
} } &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PerformGeneratedInitialize()
{ _services = &lt;span class="kwrd"&gt;new&lt;/span&gt; DefaultCodeGeneratorServices( &lt;span class="kwrd"&gt;new&lt;/span&gt; DefaultControllerReferenceFactory(), &lt;span class="kwrd"&gt;new&lt;/span&gt; AspDotNetRedirectService()
); _services.Controller = &lt;span class="kwrd"&gt;this&lt;/span&gt;; } &lt;/pre&gt;
&lt;p&gt;
First is the member variable, _services. You can do this however you like, but it's
consumed by the ControllerAction/ViewReference classes to do various likes. CodeGeneratorServices
is a property the partial controller code uses to create the MyActions and MyViews
node instances. Site is just an easy way to get the top, root level node. PerformGeneratorInitialize
is overridden in the partial controller classes to add the MyViews and MyActions nodes
to the PropertyBag so they are available in the views. So there you go. If I'm not
missing anything, that should do the trick. Let me know if I did and I'll append to
this post.
&lt;/p&gt;
&lt;p&gt;
Oh, the reason for specifying the ControllerSources is to cut down on the number of
classes/types we visit when generating the internal "tree" that the source code is
generated from. At first glance it seems like you should just be able to parse the
ControllerSources only. We coudln't really do that because we needed to gather information
on the other types in the assembly so we'd be able to use them in the arguments of
our actions, etc...&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
I have to stress that the code is relatively new, having undergone a major refactor
to isolate it from the rest of our project. I make no promises that I won't have to
upload a new copy, with a fix. :)
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/Eleutian.Tools.CodeGenerator-20070220.zip"&gt;Source
and Binaries&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=b454e935-2813-4bb9-b8f3-a724a2a81458" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,b454e935-2813-4bb9-b8f3-a724a2a81458.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>monorail</category>
      <category>source</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=e6b41de9-34c0-48e7-b440-72a7f0ca48de</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e6b41de9-34c0-48e7-b440-72a7f0ca48de</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Hello everyone. I just wanted to let those who responded to the string literal posts know
what was "going on". Basically, we're still trying to find a way that we can release
the source to some of our tools. I apologize for dragging my feet. I manage
to keep pretty busy. Heh.
</p>
        <p>
I was catching up on the castle-project's mailing list and found <a href="http://groups.google.com/group/castle-project-devel/browse_frm/thread/aa65cfb118250109">this
post</a> by Lee Henson. He's tossed together an implementation of a PropertyBag
wrapper generator and posted the source. It's very similar to our approach. One main
difference is that we do our generation at runtime so we don't have to generate and
reference another assembly. We have an IViewFactory interface and a PropertyBagViewFactory
implementation that hands them out. In tests we have an IViewFactory that pulls the
views out of the MockRepository. 
</p>
        <p>
Incidentally, our code that does this lies with some other code for generating
Control.Invoke proxies for our System.Windows.Forms view interfaces. It seems we're
not the only people who do that particular type of generation. Rüdiger Klaehn
has an article <a href="http://www.codeproject.com/csharp/threadsafeforms.asp?print=true">here</a> about
it. I strongly suggest reading it if you do any kind of SWF work.
</p>
        <p>
In the thread I found a link to <a href="http://www.brianromanko.com/2007/02/monorail-code-generation.html">Brian
Romanko's</a> post about his implementation of the controller action/view SiteMap,
as well as the PropertyBag wrapper. So just in case anybody gets impatient they can
find something to tinker with over there. While his implementation is different from
ours, the end result is very similar. I like his idea of generating a property for
actions to refer to them without parameters.
</p>
        <p>
I mentioned that for the SiteMap code generation we used CodeDom. CodeDom is
great and means we don't have to use StringBuilder to construct C#. It also abstracts
the language away. For those who are interested in this kind of code inspection/generation,
I strongly suggest taking a look at <a href="http://wiki.sharpdevelop.net/default.aspx/SharpDevelop.NRefactory">N
Refactory</a>, it's a great library for parsing C#/VB.NET source. It's what we use
and I've been incredibly happy with it. The only real hurdle when doing that kind
or work is type resolution. I'll try and make a post about that in the near future. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e6b41de9-34c0-48e7-b440-72a7f0ca48de" />
      </body>
      <title>We're Glad Everybody Hates String Literals</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/19/WereGladEverybodyHatesStringLiterals.aspx</link>
      <pubDate>Mon, 19 Feb 2007 03:04:16 GMT</pubDate>
      <description>&lt;p&gt;
Hello everyone. I just wanted to let those who responded to the string literal posts&amp;nbsp;know
what was "going on". Basically, we're still trying to find a way that we can release
the source to some of our tools.&amp;nbsp;I apologize for dragging&amp;nbsp;my feet. I manage
to keep pretty busy. Heh.
&lt;/p&gt;
&lt;p&gt;
I was catching up on the castle-project's mailing list and found &lt;a href="http://groups.google.com/group/castle-project-devel/browse_frm/thread/aa65cfb118250109"&gt;this
post&lt;/a&gt;&amp;nbsp;by Lee Henson. He's tossed together an implementation of a PropertyBag
wrapper generator and posted the source. It's very similar to our approach. One main
difference is that we do our generation at runtime so we don't have to generate and
reference another assembly. We have an IViewFactory interface and a PropertyBagViewFactory
implementation that hands them out. In tests we have an IViewFactory that pulls the
views out of the MockRepository. 
&lt;/p&gt;
&lt;p&gt;
Incidentally, our code that does&amp;nbsp;this lies with some other code for generating
Control.Invoke proxies for our System.Windows.Forms view interfaces. It seems&amp;nbsp;we're
not the only&amp;nbsp;people who do that particular type of generation. Rüdiger Klaehn
has an article &lt;a href="http://www.codeproject.com/csharp/threadsafeforms.asp?print=true"&gt;here&lt;/a&gt; about
it. I strongly suggest reading&amp;nbsp;it if you do any kind of SWF work.
&lt;/p&gt;
&lt;p&gt;
In the thread I found a link to &lt;a href="http://www.brianromanko.com/2007/02/monorail-code-generation.html"&gt;Brian
Romanko's&lt;/a&gt;&amp;nbsp;post about his implementation of the controller&amp;nbsp;action/view&amp;nbsp;SiteMap,
as well as the PropertyBag wrapper. So just in case anybody gets impatient they can
find something to tinker with over there. While his implementation is different from
ours, the end result is very similar. I like his idea of generating a property for
actions to refer to them without parameters.
&lt;/p&gt;
&lt;p&gt;
I mentioned that for the SiteMap code&amp;nbsp;generation we used CodeDom. CodeDom is
great and means we don't have to use StringBuilder to construct C#. It also abstracts
the language away. For those who are interested in this kind of code inspection/generation,
I strongly suggest taking a look at &lt;a href="http://wiki.sharpdevelop.net/default.aspx/SharpDevelop.NRefactory"&gt;N
Refactory&lt;/a&gt;, it's a great library for parsing C#/VB.NET source. It's what we use
and I've been incredibly happy with it. The only real hurdle when doing that kind
or work is type resolution. I'll try and make a post about that in the near future. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e6b41de9-34c0-48e7-b440-72a7f0ca48de" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>emit</category>
      <category>source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=78155ba4-e525-4c2f-ada4-c5b504aca510</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,78155ba4-e525-4c2f-ada4-c5b504aca510.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,78155ba4-e525-4c2f-ada4-c5b504aca510.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=78155ba4-e525-4c2f-ada4-c5b504aca510</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <div style="width: 500px; text-align: right;">
          <a href="http://beta.zooomr.com/photos/34263@Z01/738808/" title="Zooomr Photo Sharing :: Photo Sharing">
            <img src="http://static.zooomr.com/images/738808_20195e6ec1.jpg" alt="workspace" style="border: 1px solid rgb(0, 0, 0);" border="0" height="375" width="500" />
          </a>
          <span style="float: left;">
          </span> Hosted
on <strong>Zooom<span style="color: rgb(158, 174, 21);">r</span></strong></div>
        <p>
          <strong>1 <a href="http://accessories.dell.com/sna/productdetail.aspx?c=us&amp;l=en&amp;s=bsd&amp;cs=04&amp;sku=222-0863">3007WFP</a> +
2 <a href="http://accessories.dell.com/sna/productdetail.aspx?c=us&amp;l=en&amp;s=bsd&amp;cs=04&amp;sku=320-4687">2007FP</a> = 4960x1600</strong>
        </p>
        <p>
Need I say more?
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=78155ba4-e525-4c2f-ada4-c5b504aca510" />
      </body>
      <title>Developer Velocity Tip #5</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,78155ba4-e525-4c2f-ada4-c5b504aca510.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/15/DeveloperVelocityTip5.aspx</link>
      <pubDate>Thu, 15 Feb 2007 19:14:50 GMT</pubDate>
      <description>&lt;div style="width: 500px; text-align: right;"&gt;&lt;a href="http://beta.zooomr.com/photos/34263@Z01/738808/" title="Zooomr Photo Sharing :: Photo Sharing"&gt;&lt;img src="http://static.zooomr.com/images/738808_20195e6ec1.jpg" alt="workspace" style="border: 1px solid rgb(0, 0, 0);" border="0" height="375" width="500"&gt;&lt;/a&gt;&lt;span style="float: left;"&gt;&lt;/span&gt; Hosted
on &lt;strong&gt;Zooom&lt;span style="color: rgb(158, 174, 21);"&gt;r&lt;/span&gt;&lt;/strong&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;strong&gt;1 &lt;a href="http://accessories.dell.com/sna/productdetail.aspx?c=us&amp;amp;l=en&amp;amp;s=bsd&amp;amp;cs=04&amp;amp;sku=222-0863"&gt;3007WFP&lt;/a&gt;&amp;nbsp;+
2&amp;nbsp;&lt;a href="http://accessories.dell.com/sna/productdetail.aspx?c=us&amp;amp;l=en&amp;amp;s=bsd&amp;amp;cs=04&amp;amp;sku=320-4687"&gt;2007FP&lt;/a&gt;&amp;nbsp;=&amp;nbsp;4960x1600&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Need I say more?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=78155ba4-e525-4c2f-ada4-c5b504aca510" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,78155ba4-e525-4c2f-ada4-c5b504aca510.aspx</comments>
      <category>development</category>
      <category>hardware</category>
      <category>media</category>
      <category>productivity</category>
      <category>tips</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=1ebd5525-406b-4d18-83ba-a643a238f32d</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,1ebd5525-406b-4d18-83ba-a643a238f32d.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,1ebd5525-406b-4d18-83ba-a643a238f32d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1ebd5525-406b-4d18-83ba-a643a238f32d</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
        <p>
So I was recently at a good friend's birthday gig. He's actually my old lead at Microsoft.
Anyways, some of our friends that are still at Microsoft were at the party and I got
a chance to talk to one of them for a while about various things... primarily open
source, TDD, Inversion of Control and Mock objects. He was interested in <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino
Mocks</a> and wanted me to email him about it. Instead of just doing that I threw
together a good sized email. Here it is (slightly modified): 
</p>
        <p>
Good to see you again. As we talked about, there has been quite a shift in the way
we write programs... that is a shift towards a much more testable, more maintainable
type of programming. I know there are teams at Microsoft that have embraced recent
changes... some have gone agile, most use some form of continuous integration, and
some have written Inversion of Control containers (the Composite UI Application Block
by the patterns &amp; practices group has a container in it), but I'm sure most teams
aren't employing all of these practices and could benefit from at least some of them. 
</p>
        <p>
In a number of teams, I think that there are a lot of improvements that can be made...
even without shipping anything that's open source. 
</p>
        <p>
Firstly, you should take another serious look at Test Driven Development. I've attached
an <a href="http://www.google.com/url?sa=t&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fcollaboration.csc.ncsu.edu%2Flaurie%2FPapers%2FTDDpaperv8.pdf&amp;ei=HzPQRYj8NJPqgwPysajCAw&amp;usg=__C0l8jqVoY5i3vK_CuJqNLJgrg1c=&amp;sig2=KIffsn2PZK7QEtNU9HhA3w">interesting
study on TDD</a>, and you may want to skim the <a href="http://www.microsoft.com/MSPress/books/6778.aspx">MS
Press book on the subject</a>. 
</p>
        <p>
Also, the following blogs have some real good info on it:<br /><a href="http://codebetter.com/search/SearchResults.aspx?q=tdd&amp;s=127">Jeremy D.
Miller's TDD Posts</a><br /><a href="http://www.ayende.com/Blog/category/515.aspx">Ayende's TDD Posts</a></p>
        <p>
A big part of TDD and Unit Testing in general is being able to remove not only your
ephemeral dependencies, but also the rest of your dependencies. That way you know,
when something fails, exactly what class caused the failure... it's the class being
tested. In order to get rid of the dependencies, you need to replace them. In order
to replace them, you need two things. The first is a replacement. The second is a
method in which to inject the replacement. Mocks and stubs can handle the first task.
I mentioned that we use <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino
Mocks</a>: 
</p>
        <p>
          <a href="http://www.ayende.com/Blog/category/487.aspx">Ayende's Rhino Mocks Posts</a>
        </p>
        <p>
The library is great, it makes for very readable code and it's pretty darn flexible. 
</p>
        <p>
The second item can be a bit more complicated. The thing you usually see when class
Foo depends on class Bar is this: 
</p>
        <pre class="csharpcode">
          <span class="kwrd">class</span> Foo { <span class="kwrd">private</span> Bar
_bar = <span class="kwrd">new</span> Bar(); <span class="kwrd">public</span><span class="kwrd">string</span> DoStuff()
{ <span class="kwrd">return</span> _bar.DoSomeOtherStuff() ? <span class="str">"yes"</span> : <span class="str">"no"</span> }
} </pre>
        <p>
Now if you're going to test Foo, you'll also be testing Bar. You can't avoid that
the way it's written. Now, it's good to test Foo talking to Bar eventually, but that's
what integration tests are for... not Unit tests. So what do you do? This: 
</p>
        <pre class="csharpcode">
          <span class="kwrd">interface</span> IBar { <span class="kwrd">bool</span> DoSomeOtherStuff();
} <span class="kwrd">class</span> Foo { <span class="kwrd">private</span> IBar _bar; <span class="kwrd">public</span> Foo(IBar
bar) { _bar = bar; } <span class="kwrd">public</span><span class="kwrd">string</span> DoStuff()
{ <span class="kwrd">return</span> _bar.DoSomeOtherStuff() ? <span class="str">"yes"</span> : <span class="str">"no"</span> }
} </pre>
        <p>
Subtle difference, but this allows you to throw any implementation of IBar (including
a mock) into Foo. That allows you to write a test like this: 
</p>
        <pre class="csharpcode">[Test]
<span class="kwrd">void</span> DoStuff_WhenBarReturnsTrue_ReturnsYes()
{ MockRepository mocks = <span class="kwrd">new</span> MockRepository(); IBar bar
= mocks.CreateMock&lt;IBar&gt;(); Foo foo = <span class="kwrd">new</span> Foo(bar);
Expect.Call(bar.DoSomeOtherStuff()).Returns(<span class="kwrd">true</span>); mocks.ReplayAll();
Assert.Equals(<span class="str">"yes"</span>, foo.DoStuff()); } </pre>
        <p>
Now even if Bar changes, or Bar doesn't even exist, this test will still pass. 
</p>
        <p>
Here's the definition from <a href="http://www.martinfowler.com/articles/injection.html">Martin
Fowler of Dependency Injection/Inversion of Control</a></p>
        <p>
Unfortunately, real programs are a lot more complicated than just one class depending
on another... dependency chains are generally pretty deep, and one class can depend
on 5 classes, each of which depends on 2, each of which has a different lifetime...
per web request, transient, singleton, etc. It would be quite a pain if you had to
instantiate everything like this:<br />
Foo foo = new Foo(new Bar(new Blah(), Yadda.Instance)); 
</p>
        <p>
Fortunately, it's not terribly complicated to write a Container that can handle all
of this for you... so all you do is something like:<br />
Foo foo = container.Resolve&lt;Foo&gt;(); 
</p>
        <p>
There are several: 
</p>
        <ul>
          <li>
The <a href="http://www.codeplex.com/smartclient/Wiki/View.aspx?title=Composite%20UI%20Application%20Block&amp;referringTitle=Home">CAB</a> contains
one 
</li>
          <li>
            <a href="http://www.castleproject.org/container/gettingstarted/index.html">Castle
Windsor</a>
          </li>
          <li>
            <a href="http://www.springframework.net/">Spring.NET</a>
          </li>
        </ul>
        <p>
And then I went on to mention how long this mail was getting and that I was going
to post it to this blog.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=1ebd5525-406b-4d18-83ba-a643a238f32d" />
      </body>
      <title>The way we write programs</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,1ebd5525-406b-4d18-83ba-a643a238f32d.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/12/TheWayWeWritePrograms.aspx</link>
      <pubDate>Mon, 12 Feb 2007 09:33:16 GMT</pubDate>
      <description>&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So I was recently at a good friend's birthday gig. He's actually my old lead at Microsoft.
Anyways, some of our friends that are still at Microsoft were at the party and I got
a chance to talk to one of them for a while about various things... primarily open
source, TDD, Inversion of Control and Mock objects. He was interested in &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino
Mocks&lt;/a&gt;&amp;nbsp;and wanted me to email him about it. Instead of just doing that I threw
together a good sized email. Here it is (slightly modified): 
&lt;/p&gt;
&lt;p&gt;
Good to see you again. As we talked about, there has been quite a shift in the way
we write programs... that is a shift towards a much more testable, more maintainable
type of programming. I know there are teams at Microsoft that have embraced recent
changes... some have gone agile, most use some form of continuous integration, and
some have written Inversion of Control containers (the Composite UI Application Block
by the patterns &amp;amp; practices group has a container in it), but I'm sure most teams
aren't employing all of these practices and could benefit from at least some of them. 
&lt;/p&gt;
&lt;p&gt;
In a number of teams, I think that there are a lot of improvements that can be made...
even without shipping anything that's open source. 
&lt;/p&gt;
&lt;p&gt;
Firstly, you should take another serious look at Test Driven Development. I've attached
an &lt;a href="http://www.google.com/url?sa=t&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fcollaboration.csc.ncsu.edu%2Flaurie%2FPapers%2FTDDpaperv8.pdf&amp;amp;ei=HzPQRYj8NJPqgwPysajCAw&amp;amp;usg=__C0l8jqVoY5i3vK_CuJqNLJgrg1c=&amp;amp;sig2=KIffsn2PZK7QEtNU9HhA3w"&gt;interesting
study on TDD&lt;/a&gt;, and you may want to skim the &lt;a href="http://www.microsoft.com/MSPress/books/6778.aspx"&gt;MS
Press book on the subject&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
Also, the following blogs have some real good info on it:&lt;br&gt;
&lt;a href="http://codebetter.com/search/SearchResults.aspx?q=tdd&amp;amp;s=127"&gt;Jeremy D.
Miller's TDD Posts&lt;/a&gt;
&lt;br&gt;
&lt;a href="http://www.ayende.com/Blog/category/515.aspx"&gt;Ayende's TDD Posts&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
A big part of TDD and Unit Testing in general is being able to remove not only your
ephemeral dependencies, but also the rest of your dependencies. That way you know,
when something fails, exactly what class caused the failure... it's the class being
tested. In order to get rid of the dependencies, you need to replace them. In order
to replace them, you need two things. The first is a replacement. The second is a
method in which to inject the replacement. Mocks and stubs can handle the first task.
I mentioned that we use &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino
Mocks&lt;/a&gt;: 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.ayende.com/Blog/category/487.aspx"&gt;Ayende's Rhino Mocks Posts&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The library is great, it makes for very readable code and it's pretty darn flexible. 
&lt;/p&gt;
&lt;p&gt;
The second item can be a bit more complicated. The thing you usually see when class
Foo depends on class Bar is this: 
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Foo { &lt;span class="kwrd"&gt;private&lt;/span&gt; Bar
_bar = &lt;span class="kwrd"&gt;new&lt;/span&gt; Bar(); &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DoStuff()
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; _bar.DoSomeOtherStuff() ? &lt;span class="str"&gt;"yes"&lt;/span&gt; : &lt;span class="str"&gt;"no"&lt;/span&gt; }
} &lt;/pre&gt;
&lt;p&gt;
Now if you're going to test Foo, you'll also be testing Bar. You can't avoid that
the way it's written. Now, it's good to test Foo talking to Bar eventually, but that's
what integration tests are for... not Unit tests. So what do you do? This: 
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt; IBar { &lt;span class="kwrd"&gt;bool&lt;/span&gt; DoSomeOtherStuff();
} &lt;span class="kwrd"&gt;class&lt;/span&gt; Foo { &lt;span class="kwrd"&gt;private&lt;/span&gt; IBar _bar; &lt;span class="kwrd"&gt;public&lt;/span&gt; Foo(IBar
bar) { _bar = bar; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DoStuff()
{ &lt;span class="kwrd"&gt;return&lt;/span&gt; _bar.DoSomeOtherStuff() ? &lt;span class="str"&gt;"yes"&lt;/span&gt; : &lt;span class="str"&gt;"no"&lt;/span&gt; }
} &lt;/pre&gt;
&lt;p&gt;
Subtle difference, but this allows you to throw any implementation of IBar (including
a mock) into Foo. That allows you to write a test like this: 
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;[Test]
&lt;span class="kwrd"&gt;void&lt;/span&gt; DoStuff_WhenBarReturnsTrue_ReturnsYes()
{ MockRepository mocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); IBar bar
= mocks.CreateMock&amp;lt;IBar&amp;gt;(); Foo foo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Foo(bar);
Expect.Call(bar.DoSomeOtherStuff()).Returns(&lt;span class="kwrd"&gt;true&lt;/span&gt;); mocks.ReplayAll();
Assert.Equals(&lt;span class="str"&gt;"yes"&lt;/span&gt;, foo.DoStuff()); } &lt;/pre&gt;
&lt;p&gt;
Now even if Bar changes, or Bar doesn't even exist, this test will still pass. 
&lt;/p&gt;
&lt;p&gt;
Here's the definition from &lt;a href="http://www.martinfowler.com/articles/injection.html"&gt;Martin
Fowler of Dependency Injection/Inversion of Control&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Unfortunately, real programs are a lot more complicated than just one class depending
on another... dependency chains are generally pretty deep, and one class can depend
on 5 classes, each of which depends on 2, each of which has a different lifetime...
per web request, transient, singleton, etc. It would be quite a pain if you had to
instantiate everything like this:&lt;br&gt;
Foo foo = new Foo(new Bar(new Blah(), Yadda.Instance)); 
&lt;/p&gt;
&lt;p&gt;
Fortunately, it's not terribly complicated to write a Container that can handle all
of this for you... so all you do is something like:&lt;br&gt;
Foo foo = container.Resolve&amp;lt;Foo&amp;gt;(); 
&lt;/p&gt;
&lt;p&gt;
There are several: 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The &lt;a href="http://www.codeplex.com/smartclient/Wiki/View.aspx?title=Composite%20UI%20Application%20Block&amp;amp;referringTitle=Home"&gt;CAB&lt;/a&gt;&amp;nbsp;contains
one 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.castleproject.org/container/gettingstarted/index.html"&gt;Castle
Windsor&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.springframework.net/"&gt;Spring.NET&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
And then I went on to mention how long this mail was getting and that I was going
to post it to this blog.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=1ebd5525-406b-4d18-83ba-a643a238f32d" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,1ebd5525-406b-4d18-83ba-a643a238f32d.aspx</comments>
      <category>development</category>
      <category>inversion of control</category>
      <category>tools</category>
      <category>test driven development</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=153d741a-e567-4ca4-974d-8c35c25c3872</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,153d741a-e567-4ca4-974d-8c35c25c3872.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,153d741a-e567-4ca4-974d-8c35c25c3872.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=153d741a-e567-4ca4-974d-8c35c25c3872</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It amazes me how often developers lack fundamental competencies. Things that every
developer should at LEAST know about, somewhat understand and know the capabilities
of (i.e. when they're presented with a problem that is easily solvable using something
such as... oh, regular expressions, they can at least google the steps to a solution.)
What I'm trying to say is:
</p>
        <p>
          <strong>Understand and make use of <a href="http://en.wikipedia.org/wiki/Regular_expression">regular
expressions</a> whenever it is appropriate.</strong>
        </p>
        <p>
Unfortunately, if you're reading this blog, you probably read several other blogs,
so you've already been exposed to regex's. If that's the case, the tip for you is:
</p>
        <p>
          <strong>Think of more ways you can use regular expressions, understand them even more,
oh and tell a friend/coworker who doesn't subscribe to 30 dev blogs and doesn't understand/make
use of regular expressions about them.</strong>
        </p>
        <p>
There are <strong><a href="http://www.exforsys.com/content/view/1749/360/">several</a> <a href="http://www.regular-expressions.info/">resources</a> <a href="http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=523">for</a> <a href="http://msdn2.microsoft.com/en-us/library/hs600312.aspx">regular</a> <a href="http://gnosis.cx/publish/programming/regular_expressions.html">expressions</a>. </strong></p>
        <p>
          <a href="http://weblogs.asp.net/rosherove/">Roy Osherove</a> has come up with
some pretty killer tools for regex:
</p>
        <ul>
          <li>
            <a href="http://tools.osherove.com/CoolTools/Regulazy/tabid/182/Default.aspx">Regulazy</a>
          </li>
          <li>
            <a href="http://tools.osherove.com/Default.aspx?tabid=185">The Regulator</a>
          </li>
          <li>
            <a href="http://tools.osherove.com/Default.aspx?tabid=187">Regex Visualizer Kit</a>
          </li>
        </ul>
        <p>
What do you do with them? Well, there are tons of uses for them, but here's a
quick list of some of the basic things they're used for:
</p>
        <ul>
          <li>
Find things - Duh. 
</li>
          <li>
Replace things - tons of cool stuff you can do here with and without <a href="http://www.regular-expressions.info/named.html">capture
groups</a>. Here's a naive example:<pre class="csharpcode">Foo bar
Blah yadda
Etc etc
<span class="rem">//
search: (\w*) (\w*) replace: \1 \2 = new \1();</span><span class="rem">// becomes:</span> Foo
bar = <span class="kwrd">new</span> Foo(); Blah yadda = <span class="kwrd">new</span> Blah();
Etc etc = <span class="kwrd">new</span> Etc(); </pre></li>
          <li>
Parse things - obviously we love to <a href="http://blog.eleutian.com/PermaLink,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx">parse</a><a href="http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx">things</a>.</li>
        </ul>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=153d741a-e567-4ca4-974d-8c35c25c3872" />
      </body>
      <title>Developer Velocity Tip #4</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,153d741a-e567-4ca4-974d-8c35c25c3872.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/11/DeveloperVelocityTip4.aspx</link>
      <pubDate>Sun, 11 Feb 2007 18:33:10 GMT</pubDate>
      <description>&lt;p&gt;
It amazes me how often developers lack fundamental competencies. Things that every
developer should at LEAST know about, somewhat understand and know the capabilities
of (i.e. when they're presented with a problem that is easily solvable using something
such as... oh, regular expressions, they can at least google the steps to a solution.)
What I'm trying to say is:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Understand and make use of &lt;a href="http://en.wikipedia.org/wiki/Regular_expression"&gt;regular
expressions&lt;/a&gt;&amp;nbsp;whenever it is appropriate.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Unfortunately, if you're reading this blog, you probably read several other blogs,
so you've already been exposed to regex's. If that's the case, the tip for you is:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Think of more ways you can use regular expressions, understand them even more,
oh and tell a friend/coworker who doesn't subscribe to 30 dev blogs and doesn't understand/make
use of regular expressions about them.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
There are&amp;nbsp;&lt;strong&gt;&lt;a href="http://www.exforsys.com/content/view/1749/360/"&gt;several&lt;/a&gt;&amp;nbsp;&lt;a href="http://www.regular-expressions.info/"&gt;resources&lt;/a&gt;&amp;nbsp;&lt;a href="http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=523"&gt;for&lt;/a&gt;&amp;nbsp;&lt;a href="http://msdn2.microsoft.com/en-us/library/hs600312.aspx"&gt;regular&lt;/a&gt;&amp;nbsp;&lt;a href="http://gnosis.cx/publish/programming/regular_expressions.html"&gt;expressions&lt;/a&gt;. &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://weblogs.asp.net/rosherove/"&gt;Roy Osherove&lt;/a&gt;&amp;nbsp;has come up with
some pretty killer tools for regex:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://tools.osherove.com/CoolTools/Regulazy/tabid/182/Default.aspx"&gt;Regulazy&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://tools.osherove.com/Default.aspx?tabid=185"&gt;The Regulator&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://tools.osherove.com/Default.aspx?tabid=187"&gt;Regex Visualizer Kit&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
What do you do with them? Well, there are tons of uses for them,&amp;nbsp;but here's a
quick list of some of the basic things they're used for:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Find things&amp;nbsp;- Duh. 
&lt;/li&gt;
&lt;li&gt;
Replace things&amp;nbsp;- tons of cool stuff you can do here with and without &lt;a href="http://www.regular-expressions.info/named.html"&gt;capture
groups&lt;/a&gt;. Here's a naive example:&lt;pre class="csharpcode"&gt;Foo bar
Blah yadda
Etc etc
&lt;span class="rem"&gt;//
search: (\w*) (\w*) replace: \1 \2 = new \1();&lt;/span&gt; &lt;span class="rem"&gt;// becomes:&lt;/span&gt; Foo
bar = &lt;span class="kwrd"&gt;new&lt;/span&gt; Foo(); Blah yadda = &lt;span class="kwrd"&gt;new&lt;/span&gt; Blah();
Etc etc = &lt;span class="kwrd"&gt;new&lt;/span&gt; Etc(); &lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Parse things - obviously we love to &lt;a href="http://blog.eleutian.com/PermaLink,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx"&gt;parse&lt;/a&gt; &lt;a href="http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx"&gt;things&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=153d741a-e567-4ca4-974d-8c35c25c3872" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,153d741a-e567-4ca4-974d-8c35c25c3872.aspx</comments>
      <category>development</category>
      <category>productivity</category>
      <category>tips</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=a01352a7-f69e-4c03-81b0-7f627d56821b</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,a01352a7-f69e-4c03-81b0-7f627d56821b.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,a01352a7-f69e-4c03-81b0-7f627d56821b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=a01352a7-f69e-4c03-81b0-7f627d56821b</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've seen several people say the following when talking about <a href="http://testdriven.net/">TestDriven.NET</a>:
"It's so easy, just right click, and click run tests!" Maybe I'm just too lazy, but
what's easy about moving my hand to the mouse, right clicking, finding the appropriate
menu item and clicking it?
</p>
        <p>
          <strong>Bind a key to Run Test(s) and Run Test(s) with Debugger (I use Ctrl+Shift+R
and Ctrl+Shift+D)</strong>
        </p>
        <p>
Just look for TestDriven.NET.RunTests and TestDriven.NET.Debugger in your keyboard
settings. I personally use <a href="http://www.jetbrains.com/resharper/features/unitTesting.html">Resharper's
Unit Testing feature</a> (except in our 32bit apps on our x64 machines... they're
still fixing that bug), but it's all the same. Bind a key to it. Real easy to finish
up a test, hit Ctrl+Shift+R, and away you go. No arm movement required!
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=a01352a7-f69e-4c03-81b0-7f627d56821b" />
      </body>
      <title>Developer Velocity Tip #3</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,a01352a7-f69e-4c03-81b0-7f627d56821b.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/10/DeveloperVelocityTip3.aspx</link>
      <pubDate>Sat, 10 Feb 2007 00:46:23 GMT</pubDate>
      <description>&lt;p&gt;
I've seen several people say the following when talking about &lt;a href="http://testdriven.net/"&gt;TestDriven.NET&lt;/a&gt;:
"It's so easy, just right click, and click run tests!" Maybe I'm just too lazy, but
what's easy about moving my hand to the mouse, right clicking, finding the appropriate
menu item and clicking it?
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Bind a key to Run Test(s) and Run Test(s) with Debugger (I use Ctrl+Shift+R
and Ctrl+Shift+D)&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Just look for TestDriven.NET.RunTests and TestDriven.NET.Debugger in your keyboard
settings. I personally use &lt;a href="http://www.jetbrains.com/resharper/features/unitTesting.html"&gt;Resharper's
Unit Testing feature&lt;/a&gt;&amp;nbsp;(except in our 32bit apps on our x64 machines... they're
still fixing that bug), but it's all the same. Bind a key to it. Real easy to finish
up a test, hit Ctrl+Shift+R, and away you go. No arm movement required!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=a01352a7-f69e-4c03-81b0-7f627d56821b" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,a01352a7-f69e-4c03-81b0-7f627d56821b.aspx</comments>
      <category>development</category>
      <category>productivity</category>
      <category>tips</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=7b230899-0c36-4cbe-bb3b-00c373b8f950</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,7b230899-0c36-4cbe-bb3b-00c373b8f950.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,7b230899-0c36-4cbe-bb3b-00c373b8f950.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=7b230899-0c36-4cbe-bb3b-00c373b8f950</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've mentioned several tools that we use here, many of which cost money.  We're
very lucky here that we can have most of the tools that we want, I know it pains the
check writers--"What? Another $300s per dev?!?!" but thankfully they understand that
it helps us immensely. I can't help but notice that occasionally people will mention
on their blogs that they really like a tool, but that their company won't buy it for
them. I think that usually, that shows a bit of shortsightedness in their management. 
</p>
        <p>
Developers are expensive and impatient. Good developers are often even more expensive
and even more impatient. Tools help developers write more code faster, better, and
with less distraction. We'll use <a href="http://www.jetbrains.com/resharper/index.html">ReSharper</a> as
an example to try and calculate true cost of the tool. 
</p>
        <p>
Let's say a developer's salary is $60,000 per year, that's about $240/day, or $30/hr.
Say you build 10 times in an hour, half those times, you accidently typo'd something
so you have to rebuild. We'll say fixing/rebuilding costs you 2 minutes per hour.
That's 16 minutes per day or $8/day, $40/week, or about $2000/year. All for losing
2 minutes per hour. It's worse than that too, because 2 minutes is a gross exaageration,
and that 2 minutes is during a developer's most productive time... the time they actually
spend producing. 
</p>
        <p>
Now what if, for $300 you could have something that would warn you of impending build
errors, so that you could fix them before you actually built? What if it had keybindings
(I use Ctrl+Shift+N and Ctrl+Shift+P) to skip to the next error so you can quickly
correct it? What if it saved you not only those 2 minutes per hour, but also gave
you several other nifty <a href="http://www.jetbrains.com/resharper/features/index.html">features</a> that
saved you time with navigating, formatting, and several other things. Would you spend
$300 to save $2000 per year? I would hope the answer is yes. Many other tools share
this same productivity boost to cost ratio.
</p>
        <p>
Does this mean developers should have carte blanche for tools? I don't think so. There
still needs to be some filter in place, some time for evaluation, and someone who
is in tune with both a developer's need for a tool and the company's need not to spend
money senselessly, but please, managers, seriously consider requests for tools. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=7b230899-0c36-4cbe-bb3b-00c373b8f950" />
      </body>
      <title>The importance of development tools</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,7b230899-0c36-4cbe-bb3b-00c373b8f950.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/10/TheImportanceOfDevelopmentTools.aspx</link>
      <pubDate>Sat, 10 Feb 2007 00:33:41 GMT</pubDate>
      <description>&lt;p&gt;
I've mentioned several tools that we use here, many of which cost money.&amp;nbsp; We're
very lucky here that we can have most of the tools that we want, I know it pains the
check writers--"What? Another $300s per dev?!?!" but thankfully they understand that
it helps us immensely. I can't help but notice that occasionally people will mention
on their blogs that they really like a tool, but that their company won't buy it for
them. I think that usually, that shows a bit of shortsightedness in their management. 
&lt;/p&gt;
&lt;p&gt;
Developers are expensive and impatient. Good developers are often even more expensive
and even more impatient. Tools help developers write more code faster, better, and
with less distraction. We'll use &lt;a href="http://www.jetbrains.com/resharper/index.html"&gt;ReSharper&lt;/a&gt;&amp;nbsp;as
an example to try and calculate true cost of the tool. 
&lt;/p&gt;
&lt;p&gt;
Let's say a developer's salary is $60,000 per year, that's about $240/day, or $30/hr.
Say you build 10 times in an hour, half those times, you accidently typo'd something
so you have to rebuild. We'll say fixing/rebuilding costs you 2 minutes per hour.
That's 16 minutes per day or&amp;nbsp;$8/day, $40/week, or about $2000/year. All for losing
2 minutes per hour. It's worse than that too, because 2 minutes is a gross exaageration,
and that 2 minutes is during a developer's most productive time... the time they actually
spend producing. 
&lt;/p&gt;
&lt;p&gt;
Now what if, for $300 you could have something that would warn you of impending build
errors, so that you could fix them before you actually built? What if it had keybindings
(I use Ctrl+Shift+N and Ctrl+Shift+P) to skip to the next error so you can quickly
correct it? What if it saved you not only those 2 minutes per hour, but also gave
you several other nifty &lt;a href="http://www.jetbrains.com/resharper/features/index.html"&gt;features&lt;/a&gt;&amp;nbsp;that
saved you time with navigating, formatting, and several other things. Would you spend
$300 to save $2000 per year? I would hope the answer is yes. Many other tools share
this same productivity boost to cost ratio.
&lt;/p&gt;
&lt;p&gt;
Does this mean developers should have carte blanche for tools? I don't think so. There
still needs to be some filter in place, some time for evaluation, and someone who
is in tune with both a developer's need for a tool and the company's need not to spend
money senselessly, but please, managers, seriously consider requests for tools. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=7b230899-0c36-4cbe-bb3b-00c373b8f950" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,7b230899-0c36-4cbe-bb3b-00c373b8f950.aspx</comments>
      <category>development</category>
      <category>productivity</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=5d450fee-04a2-479e-bdbc-6be71e3a2ce1</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,5d450fee-04a2-479e-bdbc-6be71e3a2ce1.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,5d450fee-04a2-479e-bdbc-6be71e3a2ce1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=5d450fee-04a2-479e-bdbc-6be71e3a2ce1</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This goes hand in hand with the first tip, so I figured I should post it now.
</p>
        <p>
          <strong>Get <a href="http://weblogs.asp.net/gmilano/archive/2006/05/10/446010.aspx">Gaston
Milano's</a> <a href="http://download.deklarit.com/files/gmilano/coolcommands40.zip">Cool
Commands 4.0</a> and bind a key to Collapse All Projects (I use Ctrl+Alt+C)</strong>
        </p>
        <p>
There are plenty of other useful gems in CoolCommands (e.g. copy/paste project reference
is very useful), but the one I use several times a day is Collapse All Projects. If
you've got 40 projects, collapsing them all makes selecting the 36 projects you want
to unload much easier.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=5d450fee-04a2-479e-bdbc-6be71e3a2ce1" />
      </body>
      <title>Developer Velocity Tip #2</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,5d450fee-04a2-479e-bdbc-6be71e3a2ce1.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/06/DeveloperVelocityTip2.aspx</link>
      <pubDate>Tue, 06 Feb 2007 16:45:14 GMT</pubDate>
      <description>&lt;p&gt;
This goes hand in hand with the first tip, so I figured I should post it now.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Get &lt;a href="http://weblogs.asp.net/gmilano/archive/2006/05/10/446010.aspx"&gt;Gaston
Milano's&lt;/a&gt;&amp;nbsp;&lt;a href="http://download.deklarit.com/files/gmilano/coolcommands40.zip"&gt;Cool
Commands 4.0&lt;/a&gt;&amp;nbsp;and bind a key to Collapse All Projects (I use Ctrl+Alt+C)&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
There are plenty of other useful gems in CoolCommands (e.g. copy/paste project reference
is very useful), but the one I use several times a day is Collapse All Projects. If
you've got 40 projects, collapsing them all makes selecting the 36 projects you want
to unload much easier.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=5d450fee-04a2-479e-bdbc-6be71e3a2ce1" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,5d450fee-04a2-479e-bdbc-6be71e3a2ce1.aspx</comments>
      <category>development</category>
      <category>productivity</category>
      <category>tips</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=e0d5de46-e58c-4d7b-b3c9-48c041505552</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,e0d5de46-e58c-4d7b-b3c9-48c041505552.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,e0d5de46-e58c-4d7b-b3c9-48c041505552.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e0d5de46-e58c-4d7b-b3c9-48c041505552</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I don't like waiting for things. I don't like typing the same thing over and over
again for days, weeks or even months on end. I do as much as I can to speed my production
of code up. I want the bottleneck to be in my mind, not in my fingers or my cpu. I
figured I'd share some of the things I've learned along the way. Hopefully, a few
of the tips will grab your interest and make coding just a little bit less tedious
for you. So without further blathering, onto the first tip.
</p>
        <p>
          <strong>Bind keys to Unload Project and Reload Project (I use Ctrl+Alt+U and Ctrl+Alt+R)
and liberally unload projects you aren't mucking with.</strong>
        </p>
        <p>
This may not apply to you, but here at Eleutian we have about 40 projects to build.
Client, server, tools, shared libraries, generators, preprocessors, etc and of course
tests for all of them. If you have all 40 projects open, just the dirty check during
a build can take up a good amount of time. Generally you're only working on a few
projects at a time, so go ahead and unload the rest (you can shift and ctrl select
multiple projects to unload or reload). Every build you do (if you're doing TDD, you're
dong several) will take significantly less time. Less time between build/test runs
is a huge win. Adding the keybinding just saves you from fishing through the 100 item
project context menu.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e0d5de46-e58c-4d7b-b3c9-48c041505552" />
      </body>
      <title>Developer Velocity Tip #1</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,e0d5de46-e58c-4d7b-b3c9-48c041505552.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/06/DeveloperVelocityTip1.aspx</link>
      <pubDate>Tue, 06 Feb 2007 16:36:42 GMT</pubDate>
      <description>&lt;p&gt;
I don't like waiting for things. I don't like typing the same thing over and over
again for days, weeks or even months on end. I do as much as I can to speed my production
of code up. I want the bottleneck to be in my mind, not in my fingers or my cpu. I
figured I'd share some of the things I've learned along the way. Hopefully, a few
of the tips will grab your interest and make coding just a little bit less tedious
for you. So without further blathering, onto the first tip.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Bind keys to Unload Project and Reload Project (I use Ctrl+Alt+U and Ctrl+Alt+R)
and liberally unload projects you aren't mucking with.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
This may not apply to you, but here at Eleutian we have about 40 projects to build.
Client, server, tools, shared libraries, generators, preprocessors, etc and of course
tests for all of them. If you have all 40 projects open, just the dirty check during
a build can take up a good amount of time. Generally you're only working on a few
projects at a time, so go ahead and unload the rest (you can shift and ctrl select
multiple projects to unload or reload). Every build you do (if you're doing TDD, you're
dong several) will take significantly less time. Less time between build/test runs
is a huge win. Adding the keybinding just saves you from fishing through the 100 item
project context menu.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e0d5de46-e58c-4d7b-b3c9-48c041505552" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,e0d5de46-e58c-4d7b-b3c9-48c041505552.aspx</comments>
      <category>development</category>
      <category>productivity</category>
      <category>tips</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=cc69a4d9-b640-416f-a02a-c889aa97c996</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,cc69a4d9-b640-416f-a02a-c889aa97c996.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,cc69a4d9-b640-416f-a02a-c889aa97c996.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=cc69a4d9-b640-416f-a02a-c889aa97c996</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
        <p>
As promised, here's the plugin I was talking about and the <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino
Mocks</a> templates.
</p>
        <p>
          <a href="http://blog.eleutian.com/download/CSharp_Root_Rhino.Mocks.xml">Rhino Mocks
Templates</a> - Import these in your Template config.
</p>
        <p>
          <a href="http://blog.eleutian.com/download/CR_AddClassAttribute.zip">Plugin</a> -
Drop this in your "%PROGRAMFILES%\Developer Express Inc\DXCore for Visual Studio .NET\2.0\Bin\Plugins"
Oh, and yeah I know it's named after only one of the commands in there, but I was
too lazy after I added the other commands to rename the project. You'll see the new
commands in the Template configuration in the Command dropdown.
</p>
        <p>
          <a href="http://blog.eleutian.com/download/src-CR_AddClassAttribute.zip">Plugin Source</a> -
I know, not the source you want from us right now and it's pretty ugly, but it's
a start, right? 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=cc69a4d9-b640-416f-a02a-c889aa97c996" />
      </body>
      <title>CodeRush Plugin/Plugin Source/Templates</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,cc69a4d9-b640-416f-a02a-c889aa97c996.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/03/CodeRushPluginPluginSourceTemplates.aspx</link>
      <pubDate>Sat, 03 Feb 2007 02:00:18 GMT</pubDate>
      <description>&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
As promised, here's the plugin I was talking about and the &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino
Mocks&lt;/a&gt;&amp;nbsp;templates.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/CSharp_Root_Rhino.Mocks.xml"&gt;Rhino Mocks
Templates&lt;/a&gt; - Import these in your Template config.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/CR_AddClassAttribute.zip"&gt;Plugin&lt;/a&gt;&amp;nbsp;-
Drop this in your "%PROGRAMFILES%\Developer Express Inc\DXCore for Visual Studio .NET\2.0\Bin\Plugins"
Oh, and yeah I know it's named after only one of the commands in there, but I was
too lazy after I added the other commands to rename the project. You'll see the new
commands in the Template configuration in the Command dropdown.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.eleutian.com/download/src-CR_AddClassAttribute.zip"&gt;Plugin Source&lt;/a&gt;&amp;nbsp;-
I know, not the source you want from us&amp;nbsp;right now and it's pretty ugly, but it's
a start, right? 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=cc69a4d9-b640-416f-a02a-c889aa97c996" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,cc69a4d9-b640-416f-a02a-c889aa97c996.aspx</comments>
      <category>development</category>
      <category>productivity</category>
      <category>tools</category>
      <category>coderush</category>
      <category>source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=1b638dc0-d44c-419a-af9b-4e1c039fece2</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,1b638dc0-d44c-419a-af9b-4e1c039fece2.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,1b638dc0-d44c-419a-af9b-4e1c039fece2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1b638dc0-d44c-419a-af9b-4e1c039fece2</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I mentioned before that we love templates in <a href="http://www.devexpress.com/Products/NET/IDETools/CodeRush/Index.xml">CodeRush</a> and
that they're incredibly customizable. I wanted to give a few examples of that
and share one of the plugins I wrote for it.
</p>
        <p>
We use <a href="http://www.ayende.com/blog">Ayende's</a> <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino
Mocks</a> quite a bit. We're also lazy, so I came up with a set of templates
for it:
</p>
        <ul>
          <li>
tfm - Expands to a TestFixture that includes a MockRepository already. 
</li>
          <li>
scm/sdm - Takes whatever is in your clipboard and expands to this: <pre class="csharpcode">someClass = _mocks.CreateMock&lt;SomeClass&gt;();</pre></li>
          <li>
=cm/=dm - Similar to scm/sdm, but for when you don't have anything in your clipboard 
</li>
          <li>
mu/mo - Creates a using(_mocks.[Un]ordered()) block 
</li>
          <li>
mra - ReplayAll 
</li>
          <li>
mv/mva - Verify or VerifyAll 
</li>
          <li>
ec - Expect.Call().Return();</li>
        </ul>
        <p>
One thing I noticed while I was using these is that sometimes I wanted to use them
after I'd already written some code that would be in the expansion... for example,
say I've already got a class called SomeClassTests but I want to add the TestFixture
attribute, add the MockRepository instance variable, etc. Before I had to delete the
class and do a tfm on a blank slate. Instead, I spent a few minutes writing a
template command plugin that allows you to add an attribute to the class your cursor
is in. That enabled me to do what you see in the video below. 
</p>
        <p>
Also, say I've already typed the method I was going to set a rhino expectation on.
Normally, I'd just have to go to the beginning of the line, type Expect.Call(, go
to the end of the line, type the rest. So to solve this, I wrote a few commands: DeleteSemicolon
(this one seems a bit buggy w/ the latest <a href="http://www.devexpress.com/Products/NET/IDETools/CodeRush/Index.xml">CodeRush</a>),
GotoBeginningOfLine, and GotoEndOfLine. Now ec, ae, an, ann, etc can all be written
to add code around code I've already written as you'll see in the video below.
</p>
        <p>
Bear with me on this video, it's my first attempt at something like this. If anyone
can suggest something better than <a href="http://www.youtube.com">YouTube</a> and
a screen recorder for stuff like this that's still free, I'd appreciate it. I don't
like being limited to 320x240.<br /></p>
        <p>
Also, the astute will notice that I'm not running <a href="http://www.jetbrains.com/resharper/">Resharper</a> on
this machine. This is my home machine and once again I'm not quite sure about the
stability of it (it likes to crash every time I close VS, it breaks some autocomplete
scenarios, etc). There are some autocomplete issues in this video, but i think that's
just because I had multiple classes named the same thing in this project. 
<br /></p>
        <p>
I tried to go slowly so that you can see the templates before I expand them so that
hopefully you can get a feel as to how easy it is to write code when you've got templates
this powerful. I'm going to upload the source for the plugins as well as my <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino
Mocks</a> templates a bit later today.
</p>
        <embed src="http://www.youtube.com/v/3XIPeT0rAvQ" type="application/x-shockwave-flash" height="350" width="600">
          <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=1b638dc0-d44c-419a-af9b-4e1c039fece2" />
        </embed>
      </body>
      <title>Rhino Mocks CodeRush Templates and Plugins</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,1b638dc0-d44c-419a-af9b-4e1c039fece2.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/02/RhinoMocksCodeRushTemplatesAndPlugins.aspx</link>
      <pubDate>Fri, 02 Feb 2007 18:04:00 GMT</pubDate>
      <description>&lt;p&gt;
I mentioned before that we love templates in &lt;a href="http://www.devexpress.com/Products/NET/IDETools/CodeRush/Index.xml"&gt;CodeRush&lt;/a&gt; and
that they're incredibly customizable.&amp;nbsp;I wanted to give a few examples of that
and share one of the plugins I wrote for it.
&lt;/p&gt;
&lt;p&gt;
We use &lt;a href="http://www.ayende.com/blog"&gt;Ayende's&lt;/a&gt;&amp;nbsp;&lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino
Mocks&lt;/a&gt;&amp;nbsp;quite a bit. We're also lazy, so I came up with a set of templates
for it:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
tfm - Expands to a TestFixture that includes a MockRepository already. 
&lt;/li&gt;
&lt;li&gt;
scm/sdm - Takes whatever is in your clipboard and expands to this: &lt;pre class="csharpcode"&gt;someClass = _mocks.CreateMock&amp;lt;SomeClass&amp;gt;();&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
=cm/=dm - Similar to scm/sdm, but for when you don't have anything in your clipboard 
&lt;/li&gt;
&lt;li&gt;
mu/mo - Creates a using(_mocks.[Un]ordered()) block 
&lt;/li&gt;
&lt;li&gt;
mra - ReplayAll 
&lt;/li&gt;
&lt;li&gt;
mv/mva - Verify or VerifyAll 
&lt;/li&gt;
&lt;li&gt;
ec - Expect.Call().Return();&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
One thing I noticed while I was using these is that sometimes I wanted to use them
after I'd already written some code that would be in the expansion... for example,
say I've already got a class called SomeClassTests but I want to add the TestFixture
attribute, add the MockRepository instance variable, etc. Before I had to delete the
class and do a tfm on a blank slate.&amp;nbsp;Instead, I spent a few minutes writing a
template command plugin that allows you to add an attribute to the class your cursor
is in.&amp;nbsp;That enabled me to do what you see in the video below. 
&lt;/p&gt;
&lt;p&gt;
Also, say I've already typed the method I was going to set a rhino expectation on.
Normally, I'd just have to go to the beginning of the line, type Expect.Call(, go
to the end of the line, type the rest. So to solve this, I wrote a few commands: DeleteSemicolon
(this one seems a bit buggy w/ the latest &lt;a href="http://www.devexpress.com/Products/NET/IDETools/CodeRush/Index.xml"&gt;CodeRush&lt;/a&gt;),
GotoBeginningOfLine, and GotoEndOfLine.&amp;nbsp;Now ec, ae, an, ann, etc can all be written
to add code around code I've already written as you'll see in the video below.
&lt;/p&gt;
&lt;p&gt;
Bear with me on this video, it's my first attempt at something like this. If anyone
can suggest something better than &lt;a href="http://www.youtube.com"&gt;YouTube&lt;/a&gt; and
a screen recorder for stuff like this that's still free, I'd appreciate it. I don't
like being limited to 320x240.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Also, the astute will notice that I'm not running &lt;a href="http://www.jetbrains.com/resharper/"&gt;Resharper&lt;/a&gt; on
this machine. This is my home machine and once again I'm not quite sure about the
stability of it (it likes to crash every time I close VS, it breaks some autocomplete
scenarios, etc). There are some autocomplete issues in this video, but i think that's
just because I had multiple classes named the same thing in this project. 
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
I tried to go slowly so that you can see the templates before I expand them so that
hopefully you can get a feel as to how easy it is to write code when you've got templates
this powerful. I'm going to upload the source for the plugins as well as my &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino
Mocks&lt;/a&gt; templates a bit later today.
&lt;/p&gt;
&lt;embed src="http://www.youtube.com/v/3XIPeT0rAvQ" type="application/x-shockwave-flash" height="350" width="600"&gt;&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=1b638dc0-d44c-419a-af9b-4e1c039fece2" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,1b638dc0-d44c-419a-af9b-4e1c039fece2.aspx</comments>
      <category>coderush</category>
      <category>development</category>
      <category>productivity</category>
      <category>tools</category>
      <category>media</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=ba5b573d-9863-456f-8e0d-be8871883eca</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ba5b573d-9863-456f-8e0d-be8871883eca</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I figured I might as well continue on the string literal kick I started with my last
post and talk about another situation where we've eliminated string literals. Take
the following code from inside a MonoRail action method:
</p>
        <pre class="csharpcode">PropertyBag[<span class="str">"User"</span>] = ourUser; PropertyBag[<span class="str">"TwoStates"</span>]
= <span class="kwrd">new</span><span class="kwrd">string</span>[] { <span class="str">"WA"</span>, <span class="str">"CA"</span> };
PropertyBag[<span class="str">"IsAdmin"</span>] = <span class="kwrd">true</span>;</pre>
        <p>
String literals, used as keys into dictionaries carry a cetain degree of code smell
(for us) and we try to avoid them. It reminds me too much code that does something
similar, but in the opposite direction:
</p>
        <pre class="csharpcode">
          <span class="kwrd">int</span> id = Int32.Parse(<span class="kwrd">this</span>.Params[<span class="str">"id"</span>])</pre>
        <p>
Thankfully, this kind of code is eliminated when using MonoRail and its SmartDispatchController.
We found ourselves thinking of how the same code would look in a SWF application.
We would be populating views, only those views would be interfaces that the various
forms/controls implemented. Well, this is exactly what we wanted in MonoRail, to wrap
the PropertyBag with an interface!
</p>
        <p>
After a few hours playing with Reflection.Emit, I had a code generator that would
take an interface:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">interface</span> IEditMyProfileView
{ <span class="kwrd">string</span> Name { get; set; } DateTime Birthday { get; set;
} IList&lt;TimeZone&gt; TimeZones { get; set; } } </pre>
        <p>
And produce a class like the following:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> EditMyProfileViewPropertyBagManipulator
: IEditMyProfileView { <span class="kwrd">private</span> IDictionary _bag; <span class="kwrd">public</span> EditMyProfileViewPropertyBagManipulator(IDictionary
bag) { _bag = bag; } <span class="kwrd">public</span><span class="kwrd">string</span> Name
{ get { <span class="kwrd">return</span> (<span class="kwrd">string</span>)_bag[<span class="str">"Name"</span>];
} set { _bag[<span class="str">"Name"</span>] = <span class="kwrd">value</span>; }
} <span class="kwrd">public</span> DateTime Birthday { get { <span class="kwrd">return</span> (DateTime)_bag[<span class="str">"Birthday"</span>];
} set { _bag[<span class="str">"Birthday"</span>] = <span class="kwrd">value</span>;
} } <span class="kwrd">public</span> IList&lt;TimeZone&gt; TimeZones { get { <span class="kwrd">return</span> (IList&lt;TimeZone&gt;)_bag[<span class="str">"TimeZones"</span>];
} set { _bag[<span class="str">"TimeZones"</span>] = <span class="kwrd">value</span>;
} } } </pre>
        <p>
This has the advantage of keeping our interactions with the PropertyBag as type safe
as they can be. Changing the interface, breaks the build. Where as changing the type
of a value inserted into the PropertyBag will (hopefully) only break tests if even
that. This is another example of us trying to turn run-time failures into compile-time
failures.
</p>
        <p>
This kind of thing also makes tests more elegant. Instead of:
</p>
        <pre class="csharpcode">Assert.Equals(<span class="str">"Jacob"</span>, PropertyBag[<span class="str">"Name"</span>]);</pre>
        <p>
In our controller tests, we can continue to leverage the use of RhinoMocks to ensure
the views are properly initialized:
</p>
        <pre class="csharpcode">
          <span class="kwrd">using</span> (_mocks.Unordered()) { _view.Name
= <span class="str">"Jacob"</span>; } _mocks.ReplayAll(); _controller.Action(); _mocks.Verify(_view);</pre>
        <p>
All we've done here is created a mock from the view interface, rather than emitting
the wrapper class. Again, another added benefit is if the view changes, compiling
the tests will also break. The sooner things break after a change the better, and
the build is pretty soon. Although, with ReSharper, it's nice to see red squiggles
appear as that's even sooner.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=ba5b573d-9863-456f-8e0d-be8871883eca" />
      </body>
      <title>We Still Hate String Literals</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</guid>
      <link>http://blog.eleutian.com/2007/01/31/WeStillHateStringLiterals.aspx</link>
      <pubDate>Wed, 31 Jan 2007 04:31:07 GMT</pubDate>
      <description>&lt;p&gt;
I figured I might as well continue on the string literal kick I started with my last
post and talk about another situation where we've eliminated string literals. Take
the following code from inside a MonoRail action method:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;PropertyBag[&lt;span class="str"&gt;"User"&lt;/span&gt;] = ourUser; PropertyBag[&lt;span class="str"&gt;"TwoStates"&lt;/span&gt;]
= &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"WA"&lt;/span&gt;, &lt;span class="str"&gt;"CA"&lt;/span&gt; };
PropertyBag[&lt;span class="str"&gt;"IsAdmin"&lt;/span&gt;] = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;
String literals, used as keys into dictionaries carry a cetain degree of code smell
(for us) and we try to avoid them. It reminds me too much code that does something
similar, but in the opposite direction:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt; id = Int32.Parse(&lt;span class="kwrd"&gt;this&lt;/span&gt;.Params[&lt;span class="str"&gt;"id"&lt;/span&gt;])&lt;/pre&gt;
&lt;p&gt;
Thankfully, this kind of code is eliminated when using MonoRail and its SmartDispatchController.
We found ourselves thinking of how the same code would look in a SWF application.
We would be populating views, only those views would be interfaces that the various
forms/controls implemented. Well, this is exactly what we wanted in MonoRail, to wrap
the PropertyBag with an interface!
&lt;/p&gt;
&lt;p&gt;
After a few hours playing with Reflection.Emit, I had a code generator that would
take an interface:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IEditMyProfileView
{ &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; } DateTime Birthday { get; set;
} IList&amp;lt;TimeZone&amp;gt; TimeZones { get; set; } } &lt;/pre&gt;
&lt;p&gt;
And produce a class like the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EditMyProfileViewPropertyBagManipulator
: IEditMyProfileView { &lt;span class="kwrd"&gt;private&lt;/span&gt; IDictionary _bag; &lt;span class="kwrd"&gt;public&lt;/span&gt; EditMyProfileViewPropertyBagManipulator(IDictionary
bag) { _bag = bag; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name
{ get { &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;)_bag[&lt;span class="str"&gt;"Name"&lt;/span&gt;];
} set { _bag[&lt;span class="str"&gt;"Name"&lt;/span&gt;] = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }
} &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime Birthday { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; (DateTime)_bag[&lt;span class="str"&gt;"Birthday"&lt;/span&gt;];
} set { _bag[&lt;span class="str"&gt;"Birthday"&lt;/span&gt;] = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; IList&amp;lt;TimeZone&amp;gt; TimeZones { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; (IList&amp;lt;TimeZone&amp;gt;)_bag[&lt;span class="str"&gt;"TimeZones"&lt;/span&gt;];
} set { _bag[&lt;span class="str"&gt;"TimeZones"&lt;/span&gt;] = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
} } } &lt;/pre&gt;
&lt;p&gt;
This has the advantage of keeping our interactions with the PropertyBag as type safe
as they can be. Changing the interface, breaks the build. Where as changing the type
of a value inserted into the PropertyBag will (hopefully) only break tests if even
that. This is another example of us trying to turn run-time failures into compile-time
failures.
&lt;/p&gt;
&lt;p&gt;
This kind of thing also makes tests more elegant. Instead of:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;Assert.Equals(&lt;span class="str"&gt;"Jacob"&lt;/span&gt;, PropertyBag[&lt;span class="str"&gt;"Name"&lt;/span&gt;]);&lt;/pre&gt;
&lt;p&gt;
In our controller tests, we can continue to leverage the use of RhinoMocks to ensure
the views are properly initialized:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { _view.Name
= &lt;span class="str"&gt;"Jacob"&lt;/span&gt;; } _mocks.ReplayAll(); _controller.Action(); _mocks.Verify(_view);&lt;/pre&gt;
&lt;p&gt;
All we've done here is created a mock from the view interface, rather than emitting
the wrapper class. Again, another added benefit is if the view changes, compiling
the tests will also break. The sooner things break after a change the better, and
the build is pretty soon. Although, with ReSharper, it's nice to see red squiggles
appear as that's even sooner.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=ba5b573d-9863-456f-8e0d-be8871883eca" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>monorail</category>
      <category>emit</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=088e158b-cd3c-4920-8df5-a3628013cc1d</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,088e158b-cd3c-4920-8df5-a3628013cc1d.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,088e158b-cd3c-4920-8df5-a3628013cc1d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=088e158b-cd3c-4920-8df5-a3628013cc1d</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was trying to think of a way to introduce this post, and I realize that here at
Eleutian, we hate strings. We don't hate all strings, it's mostly string literals
that we hate. Not even all string literals. But most of them. A good portion of the
work we do to make our code more solid involves removing string literals. Anybody
familiar with MonoRail has seen lines similar to the following:
</p>
        <pre class="csharpcode">RenderView(<span class="str">"Home"</span>, <span class="str">"Action"</span>);</pre>
        <pre class="csharpcode">
          <span class="kwrd">&lt;</span>
          <span class="html">a</span>
          <span class="attr">href</span>
          <span class="kwrd">="${siteRoot}/Home/Action.rails"</span>
          <span class="kwrd">&gt;</span>...<span class="kwrd">&lt;/</span><span class="html">a</span><span class="kwrd">&gt;</span> ${UrlHelper.LinkTo("Home",
"Action")}</pre>
        <p>
And so on, see all those string literals that refer to things that aren't really string
literals? At first glance, we can get rid of the Controller string by doing something
with typeof(HomeController).Name and chopping off the Controller suffix. That'd
be an ugly static method call everywhere and still leaves "Action" to be dealt with.
We want things to break if HomeController changes names and we want to know where
HomeController is referenced.
</p>
        <p>
          <strong>One of our rules is to turn potential run-time errors into potential
compile time errors if at all possible.</strong>
        </p>
        <p>
It's funny, but the compiler is our first test, and a good one at that. We found ourselves
using those controller names, area names, and action names in a variety of situations
- redirections, view rendering, generating url's in the views, etc... Anytime I see
this I'm annoyed:
</p>
        <pre class="csharpcode">Redirect(<span class="str">"AnotherAction.rails?parameter=34"</span>);</pre>
        <p>
or even worse:
</p>
        <pre>Dictionary query = <span class="kwrd">new</span> Dictionary(); query[<span class="str">"parameter"</span>]
= 34; Redirect(<span class="str">"AnotherAction.rails"</span>, query);</pre>
        <p>
You'll notice that our life would be much easier if methods were first-class objects.
They aren't in C# 2.0... or even in 3.0 (without lambda functions) and so
this is where we are.
</p>
        <p>
Aaron and I talked things over, complaining on end about how frustrating things were.
Our problem boiled down to turning the controller action's into something we could
reference, so we decided to try a little code generation magic. We wanted to be able
to write code like the following:
</p>
        <pre class="csharpcode">MyActions.Action().Render()

Site.AdministrativeArea.Home.Action(24, <span class="str">"Jacob"</span>).Redirect()</pre>
        <p>
And so on. In order to do this we have a tool that runs as a pre-build step that does
the following:
</p>
        <ul>
          <li>
Walks the source tree, parsing *.cs files, looking for controllers. We have to parse
source, we can't use reflection because the same source files we're parsing will be
using this generated code. 
</li>
          <li>
Look for public methods (the actions) on the controller. 
</li>
          <li>
Generates a class (HomeControllerNode) that has a method, with the same prototype
as the action, that returns a ControllerActionReference:</li>
        </ul>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">partial</span>
          <span class="kwrd">class</span> HomeControllerNode
{ <span class="kwrd">private</span> IControllerServices _services; <span class="kwrd">public</span> HomeControllerNode(IControllerServices
services) { <span class="kwrd">this</span>._services = services; } <span class="kwrd">public</span><span class="kwrd">virtual</span> HomeControllerViewsNode
Views { get { <span class="kwrd">return</span><span class="kwrd">new</span> HomeControllerViewsNode(<span class="kwrd">this</span>._services);
} } [System.Diagnostics.DebuggerNonUserCodeAttribute()] <span class="kwrd">public</span><span class="kwrd">virtual</span> ControllerActionReference
SomeAction() { <span class="kwrd">return</span><span class="kwrd">this</span>._services.ControllerReferenceFactory.CreateActionReference(<span class="kwrd">this</span>._services.Controller, <span class="kwrd">typeof</span>(HomeController), <span class="str">"Home"</span>, <span class="str">"Administrative"</span>, <span class="str">"SomeAction"</span>, <span class="kwrd">new</span> ActionArgument[0]);
} [System.Diagnostics.DebuggerNonUserCodeAttribute()] <span class="kwrd">public</span><span class="kwrd">virtual</span> ControllerActionReference
AnotherAction(<span class="kwrd">string</span> name) { <span class="kwrd">return</span><span class="kwrd">this</span>._services.ControllerReferenceFactory.CreateActionReference(<span class="kwrd">this</span>._services.Controller, <span class="kwrd">typeof</span>(HomeController), <span class="str">"Home"</span>, <span class="str">"Administrative"</span>, <span class="str">"AnotherAction"</span>, <span class="kwrd">new</span> ActionArgument[]
{ <span class="kwrd">new</span> ActionArgument(<span class="str">"name"</span>, <span class="kwrd">typeof</span>(<span class="kwrd">string</span>),
name)}); } } }</pre>
        <ul>
          <li>
Generates a stub class for area's with properties for each controller in that area
(they return HomeControllerNode instances) We introduce a default root area that all
top level controllers and other areas are children of:</li>
        </ul>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">partial</span>
          <span class="kwrd">class</span> AdministrativeAreaNode
{ <span class="kwrd">private</span> IControllerServices _services; <span class="kwrd">private</span> HomeControllerNode
_home; <span class="kwrd">public</span> AdministrativeAreaNode(IControllerServices
services) { <span class="kwrd">this</span>._services = services; <span class="kwrd">this</span>._home
= <span class="kwrd">new</span> HomeControllerNode(<span class="kwrd">this</span>._services);
} <span class="kwrd">public</span><span class="kwrd">virtual</span> HomeControllerNode
Home { get { <span class="kwrd">return</span><span class="kwrd">this</span>._home;
} } }</pre>
        <ul>
          <li>
Walks the Views subdirectory looking for *.brail files mapping them onto their controllers. 
</li>
          <li>
Generates a class (HomeControllerViewsNode) with methods for each view that return
a ControllerViewReference.</li>
        </ul>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">partial</span>
          <span class="kwrd">class</span> HomeControllerViewsNode
{ <span class="kwrd">private</span> IControllerServices _services; <span class="kwrd">public</span> HomeControllerViewsNode(IControllerServices
services) { <span class="kwrd">this</span>._services = services; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] <span class="kwrd">public</span><span class="kwrd">virtual</span> ControllerViewReference
SomeAction { get { <span class="kwrd">return</span><span class="kwrd">this</span>._services.ControllerReferenceFactory.CreateViewReference(<span class="kwrd">this</span>._services.Controller, <span class="kwrd">typeof</span>(HomeController), <span class="str">"Home"</span>, <span class="str">"Administrative"</span>, <span class="str">"SomeAction"</span>);
} } }</pre>
        <ul>
          <li>
Generates a partial class for the controller class itself with two properties - MyViews
and MyActions that return the ControllerNode and ControllerViewsNode instances for
that controller.</li>
        </ul>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">partial</span>
          <span class="kwrd">class</span> HomeController
{ <span class="kwrd">public</span><span class="kwrd">virtual</span> HomeControllerNode
MyActions { get { <span class="kwrd">return</span><span class="kwrd">new</span> HomeControllerNode(<span class="kwrd">this</span>.ControllerServices);
} } <span class="kwrd">public</span><span class="kwrd">virtual</span> HomeControllerViewsNode
MyViews { get { <span class="kwrd">return</span><span class="kwrd">new</span> HomeControllerViewsNode(<span class="kwrd">this</span>.ControllerServices);
} } <span class="kwrd">protected</span><span class="kwrd">override</span><span class="kwrd">void</span> PerformGeneratedInitialize()
{ <span class="kwrd">base</span>.PerformGeneratedInitialize(); <span class="kwrd">this</span>.PropertyBag[<span class="str">"MyViews"</span>]
= <span class="kwrd">this</span>.MyViews; <span class="kwrd">this</span>.PropertyBag[<span class="str">"MyActions"</span>]
= <span class="kwrd">this</span>.MyActions; } }</pre>
        <p>
In the generated code, we pass a reference to an IControllerServices implementation
down through the hierarchy, which provides the ControllerReferenceFactory that creates
the ControllerActionReference and ControllerViewReference objects. Our EleutianController
class has a property on it <em>-</em> Site, that returns the RootAreaNode so the top
of the site can be reached from anywhere. This is also always placed into the controller's
PropertyBag, along with MyActions and MyViews. The PerformGeneratedInitialize method,
that's called in our base class so that each controller can add it's own MyViews and
MyActions, which don't live in the base class.
</p>
        <p>
Our ControllerActionReference class has Redirect and Transfer methods as well
as a Url property. So now we can do the following:
</p>
        <pre class="csharpcode">
          <span class="kwrd">&lt;</span>
          <span class="html">a</span>
          <span class="attr">href</span>
          <span class="kwrd">="${Site.HomeController.Action(200,
true).Url}"</span>
          <span class="kwrd">&gt;</span>...<span class="kwrd">&lt;/</span><span class="html">a</span><span class="kwrd">&gt;</span></pre>
        <p>
ControllerViewReference has a Render method that does the appropriate RenderView call
on the controller. Now we've accomplished a lot of things:
</p>
        <ol>
          <li>
No more string literals. If we rename something, the build fails. Our views will fail
when they're opened and not when links are followed, it is pretty easy to test that
a view compiles. 
</li>
          <li>
All URLs for redirections/transfers are always well formed and include necessary parameters
with proper type checking. 
</li>
          <li>
Provided ourselves with Intellisense when building URLs in tests and controllers.
(Sexy? Yes.) 
</li>
        </ol>
        <p>
Speaking of testing, in our controller tests we have a custom IControllerReferenceFactory
that returns mock ControllerActionReference instances (via RhinoMocks) So now redirections,
transfers and view renderings are just mocked method calls:
</p>
        <pre class="csharpcode">
          <span class="kwrd">using</span> (_mocks.Unordered()) { _controller.MyViews.AnotherView.Render();
} _mocks.ReplayAll(); _controller.SomeActionThatRendersAnotherView(); _mocks.VerifyAll();</pre>
        <p>
Which is much cleaner than string comparisons on <font face="Courier New">_controller.SelectedViewName</font>,
especially if a redirect with parameters is expected in an action. This has simplified
our life and given us incredible peace of mind, well worth the time to implement -
which was relatively simple. Whew.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=088e158b-cd3c-4920-8df5-a3628013cc1d" />
      </body>
      <title>We Hate String Literals</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,088e158b-cd3c-4920-8df5-a3628013cc1d.aspx</guid>
      <link>http://blog.eleutian.com/2007/01/30/WeHateStringLiterals.aspx</link>
      <pubDate>Tue, 30 Jan 2007 02:45:08 GMT</pubDate>
      <description>&lt;p&gt;
I was trying to think of a way to introduce this post, and I realize that here at
Eleutian, we hate strings. We don't hate all strings, it's mostly string literals
that we hate. Not even all string literals. But most of them. A good portion of the
work we do to make our code more solid involves removing string literals. Anybody
familiar with MonoRail has seen&amp;nbsp;lines similar to the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;RenderView(&lt;span class="str"&gt;"Home"&lt;/span&gt;, &lt;span class="str"&gt;"Action"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="${siteRoot}/Home/Action.rails"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;...&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; ${UrlHelper.LinkTo("Home",
"Action")}&lt;/pre&gt;
&lt;p&gt;
And so on, see all those string literals that refer to things that aren't really string
literals? At first glance, we can get rid of the Controller string by doing something
with typeof(HomeController).Name&amp;nbsp;and chopping off the Controller suffix. That'd
be an ugly static method call everywhere and still leaves "Action" to be dealt with.
We want things to break if HomeController changes names and we want to know where
HomeController is referenced.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;One of our&amp;nbsp;rules is to&amp;nbsp;turn potential run-time errors into potential
compile time errors if at all possible.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
It's funny, but the compiler is our first test, and a good one at that. We found ourselves
using those controller names, area names, and action names in a variety of situations
- redirections, view rendering, generating url's in the views, etc... Anytime I see
this I'm annoyed:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;Redirect(&lt;span class="str"&gt;"AnotherAction.rails?parameter=34"&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;
or even worse:
&lt;/p&gt;
&lt;pre&gt;Dictionary query = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary(); query[&lt;span class="str"&gt;"parameter"&lt;/span&gt;]
= 34; Redirect(&lt;span class="str"&gt;"AnotherAction.rails"&lt;/span&gt;, query);&lt;/pre&gt;
&lt;p&gt;
You'll notice that our life would be much easier if methods were first-class objects.
They aren't in C# 2.0... or even&amp;nbsp;in 3.0&amp;nbsp;(without lambda functions) and so
this is where we are.
&lt;/p&gt;
&lt;p&gt;
Aaron and I talked things over, complaining on end about how frustrating things were.
Our problem boiled down to turning the controller action's into something we could
reference, so we decided to try a little code generation magic. We wanted to be able
to write code like the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;MyActions.Action().Render()

Site.AdministrativeArea.Home.Action(24, &lt;span class="str"&gt;"Jacob"&lt;/span&gt;).Redirect()&lt;/pre&gt;
&lt;p&gt;
And so on. In order to do this we have a tool that runs as a pre-build step that does
the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Walks the source tree, parsing *.cs files, looking for controllers. We have to parse
source, we can't use reflection because the same source files we're parsing will be
using this generated code. 
&lt;/li&gt;
&lt;li&gt;
Look for public methods (the actions) on the controller. 
&lt;/li&gt;
&lt;li&gt;
Generates a class (HomeControllerNode) that has a method, with the same prototype
as the action, that returns a ControllerActionReference:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HomeControllerNode
{ &lt;span class="kwrd"&gt;private&lt;/span&gt; IControllerServices _services; &lt;span class="kwrd"&gt;public&lt;/span&gt; HomeControllerNode(IControllerServices
services) { &lt;span class="kwrd"&gt;this&lt;/span&gt;._services = services; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; HomeControllerViewsNode
Views { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HomeControllerViewsNode(&lt;span class="kwrd"&gt;this&lt;/span&gt;._services);
} } [System.Diagnostics.DebuggerNonUserCodeAttribute()] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; ControllerActionReference
SomeAction() { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._services.ControllerReferenceFactory.CreateActionReference(&lt;span class="kwrd"&gt;this&lt;/span&gt;._services.Controller, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(HomeController), &lt;span class="str"&gt;"Home"&lt;/span&gt;, &lt;span class="str"&gt;"Administrative"&lt;/span&gt;, &lt;span class="str"&gt;"SomeAction"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; ActionArgument[0]);
} [System.Diagnostics.DebuggerNonUserCodeAttribute()] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; ControllerActionReference
AnotherAction(&lt;span class="kwrd"&gt;string&lt;/span&gt; name) { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._services.ControllerReferenceFactory.CreateActionReference(&lt;span class="kwrd"&gt;this&lt;/span&gt;._services.Controller, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(HomeController), &lt;span class="str"&gt;"Home"&lt;/span&gt;, &lt;span class="str"&gt;"Administrative"&lt;/span&gt;, &lt;span class="str"&gt;"AnotherAction"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; ActionArgument[]
{ &lt;span class="kwrd"&gt;new&lt;/span&gt; ActionArgument(&lt;span class="str"&gt;"name"&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;),
name)}); } } }&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
Generates a stub class for area's with properties for each controller in that area
(they return HomeControllerNode instances) We introduce a default root area that all
top level controllers and other areas are children of:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AdministrativeAreaNode
{ &lt;span class="kwrd"&gt;private&lt;/span&gt; IControllerServices _services; &lt;span class="kwrd"&gt;private&lt;/span&gt; HomeControllerNode
_home; &lt;span class="kwrd"&gt;public&lt;/span&gt; AdministrativeAreaNode(IControllerServices
services) { &lt;span class="kwrd"&gt;this&lt;/span&gt;._services = services; &lt;span class="kwrd"&gt;this&lt;/span&gt;._home
= &lt;span class="kwrd"&gt;new&lt;/span&gt; HomeControllerNode(&lt;span class="kwrd"&gt;this&lt;/span&gt;._services);
} &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; HomeControllerNode
Home { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._home;
} } }&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
Walks the Views subdirectory looking for *.brail files mapping them onto their controllers. 
&lt;/li&gt;
&lt;li&gt;
Generates a class (HomeControllerViewsNode) with methods for each view that return
a ControllerViewReference.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HomeControllerViewsNode
{ &lt;span class="kwrd"&gt;private&lt;/span&gt; IControllerServices _services; &lt;span class="kwrd"&gt;public&lt;/span&gt; HomeControllerViewsNode(IControllerServices
services) { &lt;span class="kwrd"&gt;this&lt;/span&gt;._services = services; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; ControllerViewReference
SomeAction { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._services.ControllerReferenceFactory.CreateViewReference(&lt;span class="kwrd"&gt;this&lt;/span&gt;._services.Controller, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(HomeController), &lt;span class="str"&gt;"Home"&lt;/span&gt;, &lt;span class="str"&gt;"Administrative"&lt;/span&gt;, &lt;span class="str"&gt;"SomeAction"&lt;/span&gt;);
} } }&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
Generates a partial class for the controller class itself with two properties - MyViews
and MyActions that return the ControllerNode and ControllerViewsNode instances for
that controller.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HomeController
{ &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; HomeControllerNode
MyActions { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HomeControllerNode(&lt;span class="kwrd"&gt;this&lt;/span&gt;.ControllerServices);
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; HomeControllerViewsNode
MyViews { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HomeControllerViewsNode(&lt;span class="kwrd"&gt;this&lt;/span&gt;.ControllerServices);
} } &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PerformGeneratedInitialize()
{ &lt;span class="kwrd"&gt;base&lt;/span&gt;.PerformGeneratedInitialize(); &lt;span class="kwrd"&gt;this&lt;/span&gt;.PropertyBag[&lt;span class="str"&gt;"MyViews"&lt;/span&gt;]
= &lt;span class="kwrd"&gt;this&lt;/span&gt;.MyViews; &lt;span class="kwrd"&gt;this&lt;/span&gt;.PropertyBag[&lt;span class="str"&gt;"MyActions"&lt;/span&gt;]
= &lt;span class="kwrd"&gt;this&lt;/span&gt;.MyActions; } }&lt;/pre&gt;
&lt;p&gt;
In the generated code, we pass a reference to&amp;nbsp;an IControllerServices implementation
down through the hierarchy, which provides the ControllerReferenceFactory that creates
the ControllerActionReference and ControllerViewReference objects. Our EleutianController
class has a property on it &lt;em&gt;-&lt;/em&gt; Site, that returns the RootAreaNode so the top
of the site can be reached from anywhere. This is also always placed into the controller's
PropertyBag, along with MyActions and MyViews. The PerformGeneratedInitialize method,
that's called in our base class so that each controller can add it's own MyViews and
MyActions, which don't live in the base class.
&lt;/p&gt;
&lt;p&gt;
Our ControllerActionReference class has Redirect and&amp;nbsp;Transfer methods as well
as a Url property. So now we can do the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="${Site.HomeController.Action(200,
true).Url}"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;...&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
ControllerViewReference has a Render method that does the appropriate RenderView call
on the controller. Now we've accomplished a lot of things:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
No more string literals. If we rename something, the build fails. Our views will fail
when they're opened and not when links are followed, it is pretty easy to test that
a view compiles. 
&lt;/li&gt;
&lt;li&gt;
All URLs for redirections/transfers are always well formed and include necessary parameters
with proper type checking. 
&lt;/li&gt;
&lt;li&gt;
Provided ourselves with Intellisense when building URLs in tests and controllers.
(Sexy? Yes.) 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Speaking of testing,&amp;nbsp;in our controller tests we have a custom IControllerReferenceFactory
that returns mock ControllerActionReference instances (via RhinoMocks) So now redirections,
transfers and view renderings are just mocked method calls:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { _controller.MyViews.AnotherView.Render();
} _mocks.ReplayAll(); _controller.SomeActionThatRendersAnotherView(); _mocks.VerifyAll();&lt;/pre&gt;
&lt;p&gt;
Which is much cleaner than string comparisons on &lt;font face="Courier New"&gt;_controller.SelectedViewName&lt;/font&gt;,
especially if a redirect with parameters is expected in an action. This has simplified
our life and given us incredible peace of mind, well worth the time to implement -
which was relatively simple. Whew.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=088e158b-cd3c-4920-8df5-a3628013cc1d" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,088e158b-cd3c-4920-8df5-a3628013cc1d.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>monorail</category>
      <category>productivity</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=8e87f832-d140-47bd-9a98-f7465c6ebfb0</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,8e87f832-d140-47bd-9a98-f7465c6ebfb0.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,8e87f832-d140-47bd-9a98-f7465c6ebfb0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8e87f832-d140-47bd-9a98-f7465c6ebfb0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
My team and I are whores when it comes to productivity tools. At the moment I have
and use so many in concer it's kind of amusing. I've actually spent a decent
amount of time working with the various tool authors to get their tools to play more
friendly so that I can use them together. Here's a rundown of some of the tools
I use every day:
</p>
        <ul>
          <li>
            <a title="CodeRush with Refactor! Pro" href="http://www.devexpress.com/Products/NET/IDETools/CodeRush/">CodeRush
with Refactor! Pro</a>
          </li>
          <ul>
            <li>
I'm pretty sure I couldn't happily write code without this, and I even disable
or neglect half the features. 
</li>
            <li>
I use and customize the templates heavily. They're so customizable its stupid.
You can literrally do just about anything, and if you can't do it, you can write a
plugin to help you do it and integrate it with any other template. I'll post the set
of plugins I wrote for CodeRush later.</li>
            <li>
The refactorings are great and rank high on the visual appeal, discoverability and
usability scales.</li>
          </ul>
          <li>
            <a title="ViEmu (Visual Studio vi/vim emulation)" href="http://www.viemu.com/">ViEmu
(Visual Studio vi/vim emulation)</a>
          </li>
          <ul>
            <li>
I almost took it upon myself to write this. When I worked at Microsoft, I submitted
requests to the Visual Studio team to write this. <a title="Jon's Blog" href="http://blog.ngedit.com/">Jon</a> came
to the rescue and implemented the now near-perfect ViEmu and he will forever be my
hero.</li>
            <li>
Vim is really so much faster than regular text editing it makes transferring code
from your mind to the screen much less of a barrier. If you haven't heard of it, used
it, given it a good shot, or even if you haven't fallen in love with it, I'd strongly
recommend doing all of those things. Here are some resources to get you started:</li>
            <ul>
              <li>
                <a href="http://jmcpherson.org/editing.html">Efficient Editing With vim</a>
              </li>
              <li>
                <a href="http://blog.ngedit.com/2005/06/03/the-vi-input-model/trackback/">The vi input
model</a>
              </li>
              <li>
                <a href="http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html">Graphical
vi/vim Cheat Sheet and Tutorial</a>
              </li>
            </ul>
            <li>
One quick tip: use Ctrl+[ instead of escape to get out of insert mode, it'll save
you at least 15 seconds a day!</li>
          </ul>
          <li>
            <a title="Resharper" href="http://www.jetbrains.com/resharper/">ReSharper</a>
          </li>
          <ul>
            <li>
This one's new to me. I've tried it twice in the past and it's only done bad things--crashed,
played badly with CodeRush or ViEmu, etc. The latest version however, seems to play
fine with everything I have installed, so it's going to stay. Yes, I use CodeRush
AND Resharper. I told you I was a productivity tool whore.</li>
            <li>
The biggest feature for me is the <a title="Error Highlighting and QuickFixes" href="http://www.jetbrains.com/resharper/features/highlighting.html">Error
Highlighting and QuickFixes</a>. Most of the refactorings CodeRush does (often better)
but there are a few that are pretty slick.</li>
            <li>
The navigation features are great too. Real fast and real usable, much better than
scrolling through our 40 project solution.</li>
          </ul>
        </ul>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=8e87f832-d140-47bd-9a98-f7465c6ebfb0" />
      </body>
      <title>Productivity Tools (part 1)</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,8e87f832-d140-47bd-9a98-f7465c6ebfb0.aspx</guid>
      <link>http://blog.eleutian.com/2007/01/28/ProductivityToolsPart1.aspx</link>
      <pubDate>Sun, 28 Jan 2007 18:54:23 GMT</pubDate>
      <description>&lt;p&gt;
My team and I are whores when it comes to productivity tools. At the moment I have
and use so many in concer it's kind of amusing.&amp;nbsp;I've actually spent a decent
amount of time working with the various tool authors to get their tools to play more
friendly so that I can use them together. Here's a rundown of some of&amp;nbsp;the tools
I use every day:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a title="CodeRush with Refactor! Pro" href="http://www.devexpress.com/Products/NET/IDETools/CodeRush/"&gt;CodeRush
with Refactor! Pro&lt;/a&gt;
&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;
I'm pretty sure&amp;nbsp;I couldn't happily write code without this, and I even disable
or neglect half the features. 
&lt;/li&gt;
&lt;li&gt;
I use and customize the templates heavily.&amp;nbsp;They're so customizable its stupid.
You can literrally do just about anything, and if you can't do it, you can write a
plugin to help you do it and integrate it with any other template. I'll post the set
of plugins I wrote for CodeRush later.&lt;/li&gt;
&lt;li&gt;
The refactorings are great and rank high on the visual appeal, discoverability and
usability scales.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
&lt;a title="ViEmu (Visual Studio vi/vim emulation)" href="http://www.viemu.com/"&gt;ViEmu
(Visual Studio vi/vim emulation)&lt;/a&gt;
&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;
I almost took it upon myself to write this.&amp;nbsp;When I worked at Microsoft, I submitted
requests to the Visual Studio team to write this. &lt;a title="Jon's Blog" href="http://blog.ngedit.com/"&gt;Jon&lt;/a&gt;&amp;nbsp;came
to the rescue and implemented the now near-perfect ViEmu and he will forever be my
hero.&lt;/li&gt;
&lt;li&gt;
Vim is really so much faster than regular text editing it makes transferring code
from your mind to the screen much less of a barrier. If you haven't heard of it, used
it, given it a good shot, or even if you haven't fallen in love with it, I'd strongly
recommend doing all of those things. Here are some resources to get you started:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://jmcpherson.org/editing.html"&gt;Efficient Editing With vim&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.ngedit.com/2005/06/03/the-vi-input-model/trackback/"&gt;The vi input
model&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html"&gt;Graphical
vi/vim Cheat Sheet and Tutorial&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
One quick tip: use Ctrl+[ instead of escape to get out of insert mode, it'll save
you at least 15 seconds a day!&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
&lt;a title="Resharper" href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt;
&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;
This one's new to me. I've tried it twice in the past and it's only done bad things--crashed,
played badly with CodeRush or ViEmu, etc. The latest version however, seems to play
fine with everything I have installed, so it's going to stay. Yes, I use CodeRush
AND Resharper. I told you I was a productivity tool whore.&lt;/li&gt;
&lt;li&gt;
The biggest feature for me is the &lt;a title="Error Highlighting and QuickFixes" href="http://www.jetbrains.com/resharper/features/highlighting.html"&gt;Error
Highlighting and QuickFixes&lt;/a&gt;. Most of the refactorings CodeRush does (often better)
but there are a few that are pretty slick.&lt;/li&gt;
&lt;li&gt;
The navigation features are great too. Real fast and real usable, much better than
scrolling through our 40 project solution.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=8e87f832-d140-47bd-9a98-f7465c6ebfb0" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,8e87f832-d140-47bd-9a98-f7465c6ebfb0.aspx</comments>
      <category>coderush</category>
      <category>development</category>
      <category>productivity</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=2888d63c-96c0-4545-b380-331fb94c16f2</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2888d63c-96c0-4545-b380-331fb94c16f2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Previously I mentioned that we switched to MonoRail for all of our new features. Well,
MonoRail doesn't make page localization any easier than Web Forms does.  As a
matter of fact, it actually breaks an important part of ASP.NET's localization (something
I should probably submit a patch for, heh).  Basically, the DefaultResourceFactory
in MonoRail creates IResources that wrap ResourceSets, which will not resolve resources
to their parent resource. In other words, if you have Res.ko-KR.resx which defines
String1 and Res.resx which defines String1 and String2, then calling GetString
on a ResourceSet when your locale is ko-KR with String1 will work, but String2 will
fail.  Calling GetString with any other locale, will return the String2 from
Res.resx. To get around this, we created an IResourceFactory that returns IResources
that wrap ResourceManagers instead of ResourceSets, which are capable of properly
traversing the resource tree.  I'll probably submit this code soon. 
</p>
        <p>
Anyways, in order to localize with MonoRail, you still need to create your resx file,
add an attribute to your controller to map that resx file to something in your view's
property bag, and create, name and initialize each string in your resx file.   
</p>
        <p>
What we did was create a new generator like the one mentioned in my previous post
that converts mybrail files to brail files.  All it does is convert this: 
</p>
        <pre class="csharpcode">${g.Test: Global Text} <span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span> ${v.ViewTest:
View Text} <span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span> ${c.ControllerTest:
Controller Text} <span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span></pre>
        <pre class="csharpcode">
        </pre>
        <p>
To this: 
</p>
        <pre class="csharpcode">${g.Test} <span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span> ${c.Localization_ViewTest} <span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span> ${c.ControllerTest} <span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span> </pre>
        <p>
Now a few things happened here:
</p>
        <ol>
          <li>
A file called Global.resx was created. 
</li>
          <li>
A file called Test.resx was created (Test is the name of the controller) 
</li>
          <li>
A resource called Test was added to Global.resx and initialized with the value "Global
Text" 
</li>
          <li>
A resource called ControllerTest was added to the Test.resx and initialized with the
value "Controller Text" 
</li>
          <li>
A resource called Localization_ViewTest (Localization is the name of the view) was
added to Test.resx and initialized with the value "View Text" 
</li>
          <li>
v.ViewTest was converted to c.Localization_ViewTest</li>
        </ol>
        <p>
Essentially, what you end up with, is one resx for your global strings, and one resx
per controller, with view specific strings being stored in the controller resx with
ViewName_ prefixed to them.  There's still one piece missing to the puzzle, which
is how do the views know what g and c are? Do we attribute each controller with
a g resource and a c resource? No... that would be too much work for us. Instead
what we do is define a new IControllerDescriptorProvider that wraps a DefaultControllerDescriptorProvider
and adds appropriate ResourceDescriptors to the Resources collection of the descriptor
built by the DefaultControllerDescriptorProvider.   
</p>
        <p>
There is some room for improvement, like gracefully handling the case where you define
and give a value for a resource twice in the same file or two seperate files, e.g.
${g.Test: Hi} ${g.Test: Hello}.  In this case, g.Test will be Hello.  In
the case where they're in seperate files, it'll depend on which one was saved last
(ew).  A decent way around this would be to detect this happening and pop
a message box if you try to do this.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2888d63c-96c0-4545-b380-331fb94c16f2" />
      </body>
      <title>MonoRail Brail preprocessing</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx</guid>
      <link>http://blog.eleutian.com/2007/01/27/MonoRailBrailPreprocessing.aspx</link>
      <pubDate>Sat, 27 Jan 2007 22:05:36 GMT</pubDate>
      <description>&lt;p&gt;
Previously I mentioned that we switched to MonoRail for all of our new features. Well,
MonoRail doesn't make page localization any easier than Web Forms does.&amp;nbsp; As a
matter of fact, it actually breaks an important part of ASP.NET's localization (something
I should probably submit a patch for, heh).&amp;nbsp; Basically, the DefaultResourceFactory
in MonoRail creates IResources that wrap ResourceSets, which will not resolve resources
to their parent resource. In other words, if you have Res.ko-KR.resx which defines
String1 and Res.resx&amp;nbsp;which defines String1 and String2, then calling GetString
on a ResourceSet when your locale is ko-KR with String1 will work, but String2 will
fail.&amp;nbsp; Calling GetString with any other locale, will return the String2 from
Res.resx.&amp;nbsp;To get around this, we created an IResourceFactory that returns IResources
that wrap ResourceManagers instead of ResourceSets, which are capable of properly
traversing the resource tree.&amp;nbsp; I'll probably submit this code soon.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Anyways, in order to localize with MonoRail, you still need to create your resx file,
add an attribute to your controller to map that resx file to something in your view's
property bag, and create, name and initialize each string in your resx file.&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
What we did was create a new generator like the one mentioned in my previous post
that converts mybrail files to brail files.&amp;nbsp; All it does is convert this:&amp;nbsp;
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;${g.Test: Global Text} &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; ${v.ViewTest:
View Text} &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; ${c.ControllerTest:
Controller Text} &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;&lt;/pre&gt;
&lt;p&gt;
To this:&amp;nbsp;
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;${g.Test} &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; ${c.Localization_ViewTest} &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; ${c.ControllerTest} &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;
Now a few things happened here:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
A file called Global.resx was created. 
&lt;/li&gt;
&lt;li&gt;
A file called Test.resx was created (Test is the name of the controller) 
&lt;/li&gt;
&lt;li&gt;
A resource called Test was added to Global.resx and initialized with the value "Global
Text" 
&lt;/li&gt;
&lt;li&gt;
A resource called ControllerTest was added to the Test.resx and initialized with the
value "Controller Text" 
&lt;/li&gt;
&lt;li&gt;
A resource called Localization_ViewTest (Localization is the name of the view) was
added to Test.resx and initialized with the value "View Text" 
&lt;/li&gt;
&lt;li&gt;
v.ViewTest was converted to c.Localization_ViewTest&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Essentially, what you end up with, is one resx for your global strings, and one resx
per controller, with view specific strings being stored in the controller resx with
ViewName_ prefixed to them.&amp;nbsp; There's still one piece missing to the puzzle, which
is how do the views know what g and c are?&amp;nbsp;Do we attribute each controller with
a g resource and a c resource?&amp;nbsp;No... that would be too much work for us.&amp;nbsp;Instead
what we do is define a new IControllerDescriptorProvider that wraps a DefaultControllerDescriptorProvider
and adds appropriate ResourceDescriptors to the Resources collection of the descriptor
built by the DefaultControllerDescriptorProvider.&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
There is some room for improvement, like gracefully handling the case where you define
and give a value for a resource twice in the same file or two seperate files, e.g.
${g.Test: Hi} ${g.Test: Hello}.&amp;nbsp; In this case, g.Test will be Hello.&amp;nbsp; In
the case where they're in seperate files, it'll depend on which one was saved last
(ew).&amp;nbsp; A&amp;nbsp;decent way around this would be to detect this happening and pop
a message box if you try to do this.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2888d63c-96c0-4545-b380-331fb94c16f2" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,2888d63c-96c0-4545-b380-331fb94c16f2.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>localization</category>
      <category>monorail</category>
      <category>productivity</category>
      <category>tools</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=e1f2cae9-7693-4935-9be9-927946cc6cd2</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e1f2cae9-7693-4935-9be9-927946cc6cd2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">One of the things we love to do is remove
tedium from code. That subject will probably be the topic of this post, and many of
our posts in the future I'm sure, as we've done a lot of things in that department. 
ASP.NET is lacking in a number of areas, as I'm sure most of you know.  It's
"strong point" is supposed to be its declarative nature--but that often leads to very
difficult to test pages.  Not only that, but it's declarative syntax leaves much
to be desired.  My primary complaint is with its verbosity.  There's nothing
simple about ASP.NET syntax.  Here's an example:<br /><br /><p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;">&lt;asp:HyperLink
runat=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"server"</span> ID=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"_lnkForgot"</span> NavigateUrl='~/ForgotPassword.aspx'
Text='&lt;%$ Resources: PageStrings, Login_lnkForgot_Text %&gt;' /&gt;</span></p><p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"></span></p>
Now, let's complain a little.  First, we've got that runat="server" tag. 
Who's idea was that?  I mean, I understand why it's there, but how often are
you going to want &lt;asp:Anything&gt; to be sent to the client in that form? 
Wouldn't it make more sense to just ASSUME that &lt;anything:Anything&gt; was runat="server"
and just allow us to specify runat="client" and strip that attribute when rendering
if we really want to send that to the client? 
<br /><br />
Next, we've got the ID tag.  I don't really have a problem with that, it's implemented
just fine.  Then there's the NavigateUrl.  Notice the ~.  I love that. 
It's something that ASP.NET did right. It makes it a breeze to make pages location
agnostic... unless of course you want to use a non-asp control.  Then you've
got to use this lovely syntax:<br /><br /><p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;">&lt;img
src='&lt;%= ResolveClientUrl(<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"~/Images/img.gif"</span>)
%&gt;' /&gt;</span></p><p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"></span></p>
Ah yes, that just screams beautiful.  Now would it have really been that difficult
to replace ~ with ApplicationPath and let ~~ by as ~ in attributes?  
<br /><br />
OK, let's move on to my favorite subject--Globalization/Localization.  ASP.NET
2.0 has come a long way on this front.  It's much easier to localize your pages
the way ASP.NET 2.0 wants you to than the way ASP.NET 1.0 wanted you to, especially
with the new meta:resourcekey attribute and automatic generation of resources. 
Unfortunately, it's still not as simple and elegant as it could be.  If you don't
want to use the meta:resource attribute (because you have a shared string) then you
have to use the lovely Resources: syntax seen up above, and you have to manually create
your resx file, and manually name and fill in your default locale value.  If
you want to localize a literal, you get to use wonderfully verbose &lt;asp:Localize&gt;
control.  
<br /><br />
Another complaint that isn't demonstrated in the example is with Databinding. 
The databinding syntax is pretty clean, I don't have any complaints about that, but
its implementation is a bit poor, mainly because of its use of uncached reflection. 
It's been a while since I benchmarked it, but if my memory serves me (and often it
doesn't) then it took about 30% longer to databind using the reflexive &lt;%# Field
%&gt; syntax than it did to use &lt;%# ((Class)Container.DataItem).Field %&gt;. 
Who wants to write all of the latter, especially with autocomplete in aspx pages being
flaky at best?<br /><br />
So how could ASP.NET be less verbose and more elegant? How could WE make it better
without being able to extend the many internal and sealed classes that make up the
webforms engine?<br /><br />
Preprocessing!<br /><br />
Here's an overview of what you do.<br /><ol><li>
Create a generator that extends Microsoft.CustomTool.BaseCodeGeneratorWithSite for
.aspx files and do your preprocessing here.<br /></li><li>
Create a wizard that extends Microsoft.VisualStudio.TemplateWizard.IWizard to set
up a file hiearchy like this: 
<ul><li>
Page.myaspx 
<ul><li>
Page.aspx 
<ul><li>
Page.aspx.cs 
</li><li>
Page.aspx.designer.cs</li></ul></li></ul></li></ul></li><li>
Create an ItemTemplate for your item that contains these files and sets your custom
tool to the generator you created in step one. 
</li><li>
Add a new myaspx to your project and make sure you don't ever modify the aspx directly
(this takes some getting used to)</li></ol>
So what are some things you could filter/generate/preprocess?<br /><ul><li>
Add runat="server" tags to all &lt;anything:anything&gt; nodes that don't have it. 
</li><li>
Replace "~/blah" with an appropriate call to ResolveClientUrl 
</li><li>
Do some localization magic</li></ul><p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"></span></p><p><font face="Courier New"><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;">&lt;asp:HyperLink
ID=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"_lnkForgot"</span> NavigateUrl='~/ForgotPassword.aspx'
Text=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"$:Forgot
your Username or Password?"</span> /&gt;</span></font></p><p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"></span></p>
          This is actually the markup that we wrote
to generate the markup at the beginning of this post.  See the $:?  That
tells the generator to create a resource in PageStrings.resx with the format Page_ID_Property
that has the value that appears after the $:.  You never need to touch that resx
file.  You can just write your markup, define your strings, and not be interrupted,
or even have to name your strings.  If you want to name your strings, say something
global.  Just do "$Name: This is a string" and if you want to use it later just
use "$Name".<br /><ul><li>
Make databinding strongly typed.  This requires a bit of help, as you can't easily
infer the type being bound by just inspecting the source.  Our solution was to
add a DataType="Class" attribute to whatever the repeating control was.  For
example: 
<p><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"></span><br /><font face="Courier New"><span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;">&lt;asp:Repeater
ID=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"repeater"</span> DataType=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"BoundClass"</span>&gt;<br />
  &lt;ItemTemplate&gt;<br />
    &lt;asp:Label ID=<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);">"textBox"</span> Text='&lt;%#@Name%&gt;'
/&gt;<br />
  &lt;/ItemTemplate&gt;<br />
&lt;/asp:Repeater&gt;</span></font></p></li></ul><blockquote><p>
Notice the @ in the &lt;%#%&gt; tag.  That gets replaced with ((BoundClass)Container.DataItem),
so you can do things like &lt;%# String.Format("{0} {1}", @FirstName, @LastName) %&gt;<br /><br />
So that's how we made ASP.NET more manageable.  Now, there are still several
issues with ASP.NET WebForms that caused us to eventually switch to MonoRail, which
also benefits from preprocessing, but I'll post on that later.
</p></blockquote><img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e1f2cae9-7693-4935-9be9-927946cc6cd2" /></body>
      <title>ASP.NET page preprocessing</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx</guid>
      <link>http://blog.eleutian.com/2007/01/27/ASPNETPagePreprocessing.aspx</link>
      <pubDate>Sat, 27 Jan 2007 16:47:03 GMT</pubDate>
      <description>One of the things we love to do is remove tedium from code. That subject will probably be the topic of this post, and many of our posts in the future I'm sure, as we've done a lot of things in that department.&amp;nbsp; ASP.NET is lacking in a number of areas, as I'm sure most of you know.&amp;nbsp; It's "strong point" is supposed to be its declarative nature--but that often leads to very difficult to test pages.&amp;nbsp; Not only that, but it's declarative syntax leaves much to be desired.&amp;nbsp; My primary complaint is with its verbosity.&amp;nbsp; There's nothing simple about ASP.NET syntax.&amp;nbsp; Here's an example:&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&amp;lt;asp:HyperLink
runat=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"server"&lt;/span&gt; ID=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"_lnkForgot"&lt;/span&gt; NavigateUrl='~/ForgotPassword.aspx'
Text='&amp;lt;%$ Resources: PageStrings, Login_lnkForgot_Text %&amp;gt;' /&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&lt;/span&gt;
&lt;/p&gt;
Now, let's complain a little.&amp;nbsp; First, we've got that runat="server" tag.&amp;nbsp;
Who's idea was that?&amp;nbsp; I mean, I understand why it's there, but how often are
you going to want &amp;lt;asp:Anything&amp;gt; to be sent to the client in that form?&amp;nbsp;
Wouldn't it make more sense to just ASSUME that &amp;lt;anything:Anything&amp;gt; was runat="server"
and just allow us to specify runat="client" and strip that attribute when rendering
if we really want to send that to the client? 
&lt;br&gt;
&lt;br&gt;
Next, we've got the ID tag.&amp;nbsp; I don't really have a problem with that, it's implemented
just fine.&amp;nbsp; Then there's the NavigateUrl.&amp;nbsp; Notice the ~.&amp;nbsp; I love that.&amp;nbsp;
It's something that ASP.NET did right. It makes it a breeze to make pages location
agnostic... unless of course you want to use a non-asp control.&amp;nbsp; Then you've
got to use this lovely syntax:&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&amp;lt;img
src='&amp;lt;%= ResolveClientUrl(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"~/Images/img.gif"&lt;/span&gt;)
%&amp;gt;' /&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&lt;/span&gt;
&lt;/p&gt;
Ah yes, that just screams beautiful.&amp;nbsp; Now would it have really been that difficult
to replace ~ with ApplicationPath and let ~~ by as ~ in attributes?&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
OK, let's move on to my favorite subject--Globalization/Localization.&amp;nbsp; ASP.NET
2.0 has come a long way on this front.&amp;nbsp; It's much easier to localize your pages
the way ASP.NET 2.0 wants you to than the way ASP.NET 1.0 wanted you to, especially
with the new meta:resourcekey attribute and automatic generation of resources.&amp;nbsp;
Unfortunately, it's still not as simple and elegant as it could be.&amp;nbsp; If you don't
want to use the meta:resource attribute (because you have a shared string) then you
have to use the lovely Resources: syntax seen up above, and you have to manually create
your resx file, and manually name and fill in your default locale value.&amp;nbsp; If
you want to localize a literal, you get to use wonderfully verbose &amp;lt;asp:Localize&amp;gt;
control.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
Another complaint that isn't demonstrated in the example is with Databinding.&amp;nbsp;
The databinding syntax is pretty clean, I don't have any complaints about that, but
its implementation is a bit poor, mainly because of its use of uncached reflection.&amp;nbsp;
It's been a while since I benchmarked it, but if my memory serves me (and often it
doesn't) then it took about 30% longer to databind using the reflexive &amp;lt;%# Field
%&amp;gt; syntax than it did to use &amp;lt;%# ((Class)Container.DataItem).Field %&amp;gt;.&amp;nbsp;
Who wants to write all of the latter, especially with autocomplete in aspx pages being
flaky at best?&lt;br&gt;
&lt;br&gt;
So how could ASP.NET be less verbose and more elegant? How could WE make it better
without being able to extend the many internal and sealed classes that make up the
webforms engine?&lt;br&gt;
&lt;br&gt;
Preprocessing!&lt;br&gt;
&lt;br&gt;
Here's an overview of what you do.&lt;br&gt;
&lt;ol&gt;
&lt;li&gt;
Create a generator that extends Microsoft.CustomTool.BaseCodeGeneratorWithSite for
.aspx files and do your preprocessing here.&lt;br&gt;
&lt;/li&gt;
&lt;li&gt;
Create a wizard that extends Microsoft.VisualStudio.TemplateWizard.IWizard to set
up a file hiearchy like this: 
&lt;ul&gt;
&lt;li&gt;
Page.myaspx 
&lt;ul&gt;
&lt;li&gt;
Page.aspx 
&lt;ul&gt;
&lt;li&gt;
Page.aspx.cs 
&lt;/li&gt;
&lt;li&gt;
Page.aspx.designer.cs&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Create an ItemTemplate for your item that contains these files and sets your custom
tool to the generator you created in step one. 
&lt;/li&gt;
&lt;li&gt;
Add a new myaspx to your project and make sure you don't ever modify the aspx directly
(this takes some getting used to)&lt;/li&gt;
&lt;/ol&gt;
So what are some things you could filter/generate/preprocess?&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
Add runat="server" tags to all &amp;lt;anything:anything&amp;gt; nodes that don't have it. 
&lt;/li&gt;
&lt;li&gt;
Replace "~/blah" with an appropriate call to ResolveClientUrl 
&lt;/li&gt;
&lt;li&gt;
Do some localization magic&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&amp;lt;asp:HyperLink
ID=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"_lnkForgot"&lt;/span&gt; NavigateUrl='~/ForgotPassword.aspx'
Text=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"$:Forgot
your Username or Password?"&lt;/span&gt; /&amp;gt;&lt;/span&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&lt;/span&gt;
&lt;/p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; This is actually the markup that we wrote
to generate the markup at the beginning of this post.&amp;nbsp; See the $:?&amp;nbsp; That
tells the generator to create a resource in PageStrings.resx with the format Page_ID_Property
that has the value that appears after the $:.&amp;nbsp; You never need to touch that resx
file.&amp;nbsp; You can just write your markup, define your strings, and not be interrupted,
or even have to name your strings.&amp;nbsp; If you want to name your strings, say something
global.&amp;nbsp; Just do "$Name: This is a string" and if you want to use it later just
use "$Name".&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
Make databinding strongly typed.&amp;nbsp; This requires a bit of help, as you can't easily
infer the type being bound by just inspecting the source.&amp;nbsp; Our solution was to
add a DataType="Class" attribute to whatever the repeating control was.&amp;nbsp; For
example: 
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&lt;/span&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&lt;span style="font-size: 11px; color: black; font-family: courier new; background-color: transparent;"&gt;&amp;lt;asp:Repeater
ID=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"repeater"&lt;/span&gt; DataType=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"BoundClass"&lt;/span&gt;&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;ItemTemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Label ID=&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: courier new; background-color: rgb(228, 228, 228);"&gt;"textBox"&lt;/span&gt; Text='&amp;lt;%#@Name%&amp;gt;'
/&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;/ItemTemplate&amp;gt;&lt;br&gt;
&amp;lt;/asp:Repeater&amp;gt;&lt;/span&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Notice the @ in the &amp;lt;%#%&amp;gt; tag.&amp;nbsp; That gets replaced with ((BoundClass)Container.DataItem),
so you can do things like &amp;lt;%# String.Format("{0} {1}", @FirstName, @LastName) %&amp;gt;&lt;br&gt;
&lt;br&gt;
So that's how we made ASP.NET more manageable.&amp;nbsp; Now, there are still several
issues with ASP.NET WebForms that caused us to eventually switch to MonoRail, which
also benefits from preprocessing, but I'll post on that later.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e1f2cae9-7693-4935-9be9-927946cc6cd2" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,e1f2cae9-7693-4935-9be9-927946cc6cd2.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>localization</category>
      <category>productivity</category>
      <category>tools</category>
    </item>
  </channel>
</rss>