<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.haacked.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.haacked.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">
    <channel>
        <title>you've been HAACKED</title>
        <link>http://haacked.com/Default.aspx</link>
        <description>...and you like it.</description>
        <language>en-US</language>
        <copyright>Haacked</copyright>
        <managingEditor>haacked@gmail.com</managingEditor>
        <generator>Subtext Version 2.0.0.43</generator>
        <image>
            <title>you've been HAACKED</title>
            <url>http://haacked.com/images/RSS2Image.gif</url>
            <link>http://haacked.com/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <geo:lat>34.03056</geo:lat><geo:long>-118.398043</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.5/</creativeCommons:license><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.haacked.com/haacked" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">527603</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://www.feedburner.com</feedburner:feedburnerHostname><item>
            <title>Streamlined BDD Using SubSpec for xUnit.NET</title>
            <category>TDD</category>
            <category>Software Development</category>
            <link>http://haacked.com/archive/2008/08/24/introducing-subspec.aspx</link>
            <description>&lt;p&gt;I admit, up until now I’ve largely ignored the &lt;a title="Introducing BDD" href="http://dannorth.net/introducing-bdd"&gt;BDD&lt;/a&gt; (Behavior Driven Development) Context/Specification style of writing unit tests. It’s been touted as a more approachable way to learn TDD (Test Driven Development) and as a more natural transition from user stories to the actual code design. I guess my hesitation to give it a second thought was that I felt I didn’t need a more approachable form of TDD.&lt;/p&gt;  &lt;p&gt;Recently, my Subtext partner in crime, &lt;a title="StevenHarman" href="http://stevenharman.net/"&gt;Steve Harman&lt;/a&gt;, urged me to take another fresh look at BDD Context/Specification style tests. I trust Steve’s opinion, so I took another look and in doing so, the benefits of this approach dawned on me. I realized that it wasn’t BDD itself I didn’t like, after all, I did enjoy &lt;a title="Minispec and IronRuby" href="http://haacked.com/archive/2008/04/09/my-first-ironruby-unit-test-spec-for-asp.net-mvc.aspx"&gt;writing specs using minispec and IronRuby&lt;/a&gt;. I realized that the part I didn’t really like was the .NET implementations of this style. Keep in mind that I do not claim to be an expert in TDD or BDD, I’m just a student and these are just my observations. I’m sure others will chime in and provide corrections that we can all learn from.&lt;/p&gt;  &lt;h3&gt;SpecUnit.NET example&lt;/h3&gt;  &lt;p&gt;For example, let’s take a look at one example pulled from the sample project of &lt;a title="Scott Bellware's Blog" href="http://blog.scottbellware.com/" rel="met"&gt;Scott Bellware’s&lt;/a&gt; &lt;a title="SpecUnit.NET" href="http://code.google.com/p/specunit-net/"&gt;SpecUnit.NET project&lt;/a&gt;, which provides extensions supporting the BDD-style use with .NET unit testing frameworks and has really pushed this space forward. I trimmed the name of the class slightly by removing a couple articles (“the” and “an”) so it would fit within the format of my blog post.&lt;/p&gt;  &lt;div class="dropshadow code"&gt;   &lt;div class="innerbox"&gt;     &lt;pre class="csharpcode"&gt;[Concern(&lt;span class="str"&gt;"Funds transfer"&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; when_transfering_amount_greater_than_balance_of_the_from_account
  : behaves_like_context_with_from_account_and_to_account
{
  &lt;span class="kwrd"&gt;private&lt;/span&gt; Exception _exception;

  &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; Because()
  {
    _exception = ((MethodThatThrows) &lt;span class="kwrd"&gt;delegate&lt;/span&gt;
    {
      _fromAccount.Transfer(2m, _toAccount);
    })
    .GetException();
  }

  [Observation]
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; should_not_allow_the_transfer()
  {
    _exception.ShouldNotBeNull();
  }

  [Observation]
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; should_raise_System_Exception()
  {
    _exception.ShouldBeOfType(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Exception));
  }
}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;The &lt;code&gt;Because&lt;/code&gt; method contains the code with the behavior we’re interested in testing. The two methods annotated with &lt;code&gt;Observation&lt;/code&gt; are the specifications. Notice that the names of the classes and methods are meant to be human readable. The output of running these tests remove underscores and reads like a specification document. It’s all very cool.&lt;/p&gt;

&lt;p class="clear"&gt;What I like about this approach is there’s a crisp focus on having each test class focused on a single behavior, in this case transferring a balance from one account to another. In the past, I might have written something like this as two test methods (which led to duplicating code or putting code in some generic &lt;code&gt;Setup&lt;/code&gt; method that seems detached from what I’m trying to test) or as one method with two asserts. This approach helps you think about how to organize tests along the lines of your objects’ responsibilities.&lt;/p&gt;

&lt;p&gt;What I don’t like about it, and I admit this is really just a nitpick, is that it looks like someone’s keyboard puked underscores all over the place. I feel like having to encapsulate each observation as a method adds a lot of syntactic overhead when I’m trying to read this class from top to bottom. Maybe that’s just something you get used to.&lt;/p&gt;

&lt;h3&gt;MSpec example&lt;/h3&gt;

&lt;p&gt;Switching gears, let’s look at a different example by &lt;a title="Aaron Jensen" href="http://codebetter.com/blogs/aaron.jensen/" rel="met"&gt;Aaron Jensen&lt;/a&gt;. This is an experiment in which he tried a very different approach. Look at this code sample…&lt;/p&gt;

&lt;div class="dropshadow code"&gt;
  &lt;div class="innerbox"&gt;
    &lt;pre class="csharpcode"&gt;[Description]   
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Transferring_between_from_account_and_to_account   
{   
  &lt;span class="kwrd"&gt;static&lt;/span&gt; Account fromAccount;   
  &lt;span class="kwrd"&gt;static&lt;/span&gt; Account toAccount;   
  
  Context before_each =()=&amp;gt;   
  {   
    fromAccount = &lt;span class="kwrd"&gt;new&lt;/span&gt; Account {Balance = 1m};   
    toAccount = &lt;span class="kwrd"&gt;new&lt;/span&gt; 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;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;There’s still the underscore porn, but it does read a little more like prose from top to bottom, if you can get yourself to ignore that funky operator right there. &lt;code&gt;=()=&amp;gt;&lt;/code&gt; Whoa! &lt;/p&gt;

&lt;p class="clear"&gt;When I complained to Steve about all the underscores in these various approaches, he suggested that being a fan of the more Zen-like Ruby language, the underscores didn’t bother him. I didn’t buy that as part of the aesthetic of Ruby is its clean DRY minimalism. Yes, it uses underscores, but it doesn’t generally abuse them. Let’s take a look at a BDD example using RSpec and Ruby. This is an example of a &lt;a title="Developing a Rails model using BDD and RSpec" href="http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1"&gt;spec in progress from Luke Redpath&lt;/a&gt;… (forgive the poor syntax highlighting. I need a ruby css stylesheet. :)&lt;/p&gt;

&lt;div class="dropshadow code"&gt;
  &lt;div class="innerbox"&gt;
    &lt;pre class="csharpcode"&gt;context &lt;span class="str"&gt;"A user (in general)"&lt;/span&gt; &lt;span class="kwrd"&gt;do&lt;/span&gt; 
  setup &lt;span class="kwrd"&gt;do&lt;/span&gt; 
    @user = User.&lt;span class="kwrd"&gt;new&lt;/span&gt; 
  end 

  specify &lt;span class="str"&gt;"should be invalid without a username"&lt;/span&gt; &lt;span class="kwrd"&gt;do&lt;/span&gt; 
    @user.should_not_be_valid 
    @user.errors.on(:username).should_equal &lt;span class="str"&gt;"is required"&lt;/span&gt; 
    @user.username = &lt;span class="str"&gt;'someusername'&lt;/span&gt; 
    @user.should_be_valid 
  end 

  specify &lt;span class="str"&gt;"should be invalid without an email"&lt;/span&gt; &lt;span class="kwrd"&gt;do&lt;/span&gt; 
    @user.should_not_be_valid 
    @user.errors.on(:email).should_equal &lt;span class="str"&gt;"is required"&lt;/span&gt; 
    @user.email = &lt;span class="str"&gt;'joe@bloggs.com'&lt;/span&gt; 
    @user.should_be_valid 
  end 
end&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;One thing to notice is that we’re not using separate methods and classes here. Ruby doesn’t force you to put code in classes. You can just execute a script top-to-bottom. In this case, the code sets up a context block and within that block there is a setup block and a couple of specify blocks. There’s no need to factor a specification into multiple classes and methods.&lt;/p&gt;

&lt;p class="clear"&gt;Also notice that the context and specifications are described using strings! Now we’re getting somewhere. If it’s meant to be human readable, why don’t we use strings instead of the underscore porn? On Twitter, many accused the ceremony and vagaries of C# of preventing this approach. While I agree that Ruby has less ceremony than C#, I also think C# doesn’t get its fair shake sometimes. We can certainly take a C# approach down to its bare metal with the least syntactic noise as possible, right?&lt;/p&gt;

&lt;h3&gt;SubSpec&lt;/h3&gt;

&lt;p&gt;So in true Program Manager at Microsoft fashion, I spec’d out a rough idea of the syntax I would like to use with BDD. I then showed it to &lt;a title="Brad Wilson's blog" href="http://bradwilson.typepad.com/" rel="friend met co-worker"&gt;Brad Wilson&lt;/a&gt; asking him how can I make this work in xUnit.net. In true Developer fashion, he ran with it and made it actually work. This blog post is the part where I try to take all the credit. That’s what PMs do at Microsoft, write specs, take credit for the hard work the developers do in bringing the specs to life. ;) (&lt;em&gt;I kid, I kid)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here’s an example using this syntax…&lt;/p&gt;

&lt;div class="dropshadow code"&gt;
  &lt;div class="innerbox"&gt;
    &lt;pre class="csharpcode"&gt;[Specification]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PushNullSpecifications()
{
  Stack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; stack = &lt;span class="kwrd"&gt;null&lt;/span&gt;;

  &lt;span class="str"&gt;"Given a new stack"&lt;/span&gt;.Context(() =&amp;gt; stack = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;());

  &lt;span class="str"&gt;"with null pushed into it"&lt;/span&gt;.Do(() =&amp;gt; stack.Push(&lt;span class="kwrd"&gt;null&lt;/span&gt;));

  &lt;span class="str"&gt;"the stack is not empty"&lt;/span&gt;.Assert(() =&amp;gt; Assert.False(stack.IsEmpty));
  &lt;span class="str"&gt;"the popped value is null"&lt;/span&gt;.Assert(() =&amp;gt; Assert.Null(stack.Pop()));
  &lt;span class="str"&gt;"Top returns null"&lt;/span&gt;.Assert(() =&amp;gt; Assert.Null(stack.Top));
}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;A few things to notice. First, the entire spec is in a single method, which reduces some of the syntactic noise of splitting the spec into multiple methods. Secondly, we’re using strings here to describe the specification and context, rather than method names with underscores for the human readable part.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lastly, &lt;em&gt;and most importantly&lt;/em&gt;, while it may look like we’re committing the sin of multiple asserts in a single test, this is not the case.&lt;/strong&gt; Via the power of the xUnit.NET extensibility model, Brad was able to generate a test per assertion. That’s why the &lt;code&gt;Assert&lt;/code&gt; method (should it be &lt;code&gt;Observe&lt;/code&gt; or &lt;code&gt;Fact&lt;/code&gt;?) takes in a lambda. We can return these closures to xUnit.net and it will wrap each one in a separate test. Here’s another look at the same method with some comments to highlight how similar this is to the previous examples. (&lt;em&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: I also changed the asserts to use the Should style extension methods to demonstrate what it could look like).&lt;/em&gt;&lt;/p&gt;

&lt;div class="dropshadow code"&gt;
  &lt;div class="innerbox"&gt;
    &lt;pre class="csharpcode"&gt;[Specification]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PushNullSpecifications()
{
  Stack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; stack = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
  &lt;span class="rem"&gt;//equivalent to before-each&lt;/span&gt;
  &lt;span class="str"&gt;"Given a new stack"&lt;/span&gt;.Context(() =&amp;gt; stack = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;());

  &lt;span class="rem"&gt;//equivalent to Because()&lt;/span&gt;
  &lt;span class="str"&gt;"with null pushed into it"&lt;/span&gt;.Do(() =&amp;gt; stack.Push(&lt;span class="kwrd"&gt;null&lt;/span&gt;));

  &lt;span class="rem"&gt;//Equivalent to [Observation]&lt;/span&gt;
  &lt;span class="str"&gt;"the stack is not empty"&lt;/span&gt;.Assert(() =&amp;gt; stack.IsEmpty.ShouldBeFalse());
  &lt;span class="rem"&gt;//Equivalent to [Observation]&lt;/span&gt;
  &lt;span class="str"&gt;"the popped value is null"&lt;/span&gt;.Assert(() =&amp;gt; stack.Pop().ShouldBeNull());
  &lt;span class="rem"&gt;//Equivalent to [Observation]&lt;/span&gt;
  &lt;span class="str"&gt;"Top returns null"&lt;/span&gt;.Assert(() =&amp;gt; stack.Top.ShouldBeNull());
}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;Keep in mind, at this point, this is a merely &lt;em&gt;proof-of-concept sample&lt;/em&gt; that will be included with the samples project in the next version of &lt;a title="xUnit.NET on CodePlex" href="http://www.codeplex.com/xunit"&gt;xUnit.NET&lt;/a&gt; and by the time you read this sentence, it may have changed already. You can download &lt;a title="xUnit.NET ChangeSet 22555" href="http://www.codeplex.com/xunit/SourceControl/DownloadSourceCode.aspx?changeSetId=22555"&gt;this particular changeset here&lt;/a&gt;.&lt;/p&gt;

&lt;p class="clear"&gt;The following is a screenshot of the HTML report generated by xUnit.NET when using this syntax that Brad sent me today.&lt;img title="subspec-report" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="463" alt="subspec-report" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/SubSpecBDDExtensionsforxUnit.net_980A/subspec-report_3.png" width="550" border="0" /&gt; &lt;/p&gt;

&lt;p class="clear"&gt;Despite it being a sample, I tried to give it a catchy name in case this is intriguing to others and worth iterating on to make it better (not to mention that I love putting the prefix “Sub” in &lt;a title="Subtext" href="http://subtextproject.com/"&gt;front&lt;/a&gt; of &lt;a title="Subkismet" href="http://subkismet.com/"&gt;everything&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Possible next steps would be to add all the &lt;code&gt;Woulda&lt;/code&gt;, &lt;code&gt;Coulda&lt;/code&gt;, &lt;code&gt;Shoulda&lt;/code&gt; extension methods so popular with this style of testing. For example, that would allow you to replace &lt;code&gt;Assert.False(stack.IsEmpty)&lt;/code&gt; with &lt;code&gt;stack.IsEmpty.ShouldBeFalse()&lt;/code&gt;. For those of you practicing BDD, I’d be interested in hearing your thoughts, objections, etc… concerning this approach.&lt;/p&gt;

&lt;p&gt;For completeness sake, here’s another syntax I proposed to Brad. He mentioned it was similar to something else he’s seen which he might port over to xUnit.net.&lt;/p&gt;

&lt;div class="dropshadow code"&gt;
  &lt;div class="innerbox"&gt;
    &lt;pre class="csharpcode"&gt;[Specification]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; When_Transferring_To_An_Account()
{
  Election e = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
  Account a = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
  Account b = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
 
  Where(&lt;span class="str"&gt;"both accounts have positive balances"&lt;/span&gt;, () =&amp;gt; {
    a = &lt;span class="kwrd"&gt;new&lt;/span&gt; Account { Balance = 1 };
    b = &lt;span class="kwrd"&gt;new&lt;/span&gt; Account { Balance = 2 };
  });
 
  When(&lt;span class="str"&gt;"transfer is made"&lt;/span&gt;, () =&amp;gt;
    
    a.Transfer(1, b)
  );
 
  It(&lt;span class="str"&gt;"debits account by amount transferred"&lt;/span&gt;, () =&amp;gt; a.Balance.ShouldBe(0));
  It(&lt;span class="str"&gt;"credits account by amount transferred"&lt;/span&gt;, () =&amp;gt; b.Balance.ShouldBe(3));
}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;For those of you completely new to BDD, check out &lt;a title="Behavior-Driven Development" href="http://www.code-magazine.com/Article.aspx?quickid=0805061"&gt;Scott Bellware’s Code Magazine article on the subject&lt;/a&gt;.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b27a48d1-0b4e-43e6-8472-8453455bbf84" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/tdd" rel="tag"&gt;tdd&lt;/a&gt;,&lt;a href="http://technorati.com/tags/bdd" rel="tag"&gt;bdd&lt;/a&gt;,&lt;a href="http://technorati.com/tags/xunit.net" rel="tag"&gt;xunit.net&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18526.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=gTB3lk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=gTB3lk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=a9Cm4k"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=a9Cm4k" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=WTllvK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=WTllvK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/24/introducing-subspec.aspx</guid>
            <pubDate>Sun, 24 Aug 2008 11:00:00 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18526.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/24/introducing-subspec.aspx#feedback</comments>
            <slash:comments>47</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18526.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Dealing With Denial of Service Attacks</title>
            <category>Personal</category>
            <link>http://haacked.com/archive/2008/08/22/dealing-with-denial-of-service-attacks.aspx</link>
            <description>&lt;p&gt;As Scott &lt;a title="Hacked!" href="http://www.hanselman.com/blog/HackedAndIDidntLikeItURLScanIsStepZero.aspx"&gt;wrote last week&lt;/a&gt;, using a punny title I have to admire, he and I (among many others) were both the subject of a DoS (Denial of Service) attack. Looking through my logs, it looks to actually be a DDoS (Distributed Denial of Service) attack coming from multiple IP addresses.&lt;/p&gt;  &lt;p&gt;The attack appears to actually be an attempt at a SQL Injection attack, but for his blog, which stores its data in XML files, that is entirely pointless. For my blog, which doesn’t do any inline SQL, it’s also mostly pointless. So far, the SQL injection part of the attack has failed, but it &lt;em&gt;has&lt;/em&gt; succeeded in pegging my CPU. Maybe that’s the actual intended goal. Only the attacker knows.&lt;/p&gt;  &lt;h3&gt;LogParser Queries&lt;/h3&gt;  &lt;p&gt;The first clue (besides my site being down) is that my log file for today is huge at 9:00 AM.&lt;/p&gt;  &lt;p&gt;&lt;img title="log-files" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="143" alt="log-files" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/DealingWithDenialofServiceAttacks_8743/log-files_3.png" width="283" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;The next step is to run some queries against my logs using the fantastic LogParser tool. This post, entitled &lt;em&gt;&lt;a title="Forensic Log Parsing" href="http://www.securityfocus.com/infocus/1712"&gt;Forensic Log Parsing with Microsoft’s LogParser&lt;/a&gt;&lt;/em&gt; is a great resource for constructing queries. The focus tends to be more on investigating an actual intrusion. The queries I need are to discover what kind of DoS attack I’m experiencing. Here’s the query I’m using so far…&lt;/p&gt;  &lt;pre&gt;&lt;code&gt;  logparser "SELECT c-ip, COUNT(*), STRLEN(cs-uri-query) as LENGTH, cs-uri-query 
  FROM C:\WINDOWS\system32\LogFiles\W3SVC1\&lt;strong&gt;ex080822.log&lt;/strong&gt; 
  GROUP BY Length, cs-uri-query, c-ip 
  HAVING Length &amp;gt; 500 
  ORDER BY LENGTH DESC" -rtp:-1 &amp;gt; long-query.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that I’m running this for a single log file for the day. I could use a wildcard and run this for all my log files. The very last snippet, &lt;em&gt;&amp;gt; long-query.txt&lt;/em&gt;, pipes the output to a text file. Here’s a snippet of one of the query strings I’m seeing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;?';DECLARE%20@S%20CHAR(4000);SET%20@S=CAST&lt;em&gt;&lt;strong&gt;…*snip*…&lt;/strong&gt;&lt;/em&gt;%20AS%20CHAR(4000));EXEC(@S);&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The length of these query strings are all very long. Interestingly enough, there’s no smooth transition in length. For example, there are no query strings of length 500 – 1000.&lt;/p&gt;

&lt;h3&gt;URL Scan&lt;/h3&gt;

&lt;p&gt;I then went and installed URLScan 3.0 Beta, which Scott wrote about, and went into the configuration file (located at &lt;em&gt;&lt;em&gt;&lt;code&gt;&lt;em&gt;&lt;em&gt;C:\WINDOWS\system32\inetsrv\urlscan\UrlScan.ini&lt;/em&gt;&lt;/em&gt;&lt;/code&gt;&lt;/em&gt;&lt;/em&gt; by default and changed the following setting near the bottom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  MaxQueryString=2048&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From its default of 2048 to another smaller value.&lt;/p&gt;

&lt;p&gt;The other setting I changed is to allow dots in the path because I have many URLs that contain dots.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  AllowDotInPath=1&lt;/code&gt;&lt;/pre&gt;



&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f74e6566-5344-46cb-b5ae-a580be4772f8" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/UrlScan" rel="tag"&gt;UrlScan&lt;/a&gt;,&lt;a href="http://technorati.com/tags/IIS" rel="tag"&gt;IIS&lt;/a&gt;,&lt;a href="http://technorati.com/tags/DoS" rel="tag"&gt;DoS&lt;/a&gt;,&lt;a href="http://technorati.com/tags/DDoS" rel="tag"&gt;DDoS&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Security" rel="tag"&gt;Security&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18525.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=wbGhdk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=wbGhdk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=rg5QDk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=rg5QDk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=HLzsSK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=HLzsSK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/22/dealing-with-denial-of-service-attacks.aspx</guid>
            <pubDate>Fri, 22 Aug 2008 17:00:37 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18525.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/22/dealing-with-denial-of-service-attacks.aspx#feedback</comments>
            <slash:comments>15</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18525.aspx</wfw:commentRss>
        </item>
        <item>
            <title>The ABCs of Alpha, Beta, CTP</title>
            <category>Software Development</category>
            <category>ASP.NET MVC</category>
            <category>ASP.NET</category>
            <link>http://haacked.com/archive/2008/08/15/understanding-beta.aspx</link>
            <description>&lt;p&gt;A commenter to &lt;a href="http://haacked.com/archive/2008/08/14/aspnetmvc-not-in-sp1.aspx"&gt;my last post&lt;/a&gt; asks the following question,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;What is the difference between a beta, a CTP, a fully-supported out of band release, an RTM feature, and a service pack? &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The answer you get will differ based on who you ask, but I’ll give you my two cents on what these terms mean.&lt;/p&gt;  &lt;h3&gt;Beta&lt;/h3&gt;  &lt;p&gt;Let’s start with Beta. A great starting point is this post by Jeff Atwood entitled &lt;a title="Alpha, Beta, and Sometimes Gamma" href="http://www.codinghorror.com/blog/archives/001159.html"&gt;Alpha, Beta, and Sometimes Gamma&lt;/a&gt;.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The software is complete enough for external testing -- that is, by groups outside the organization or community that developed the software. Beta software is usually feature complete, but may have known limitations or bugs. Betas are either closed (private) and limited to a specific set of users, or they can be open to the general public. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;With the ASP.NET MVC project, all features we plan to implement for RTM should be complete for our Beta. However, the Beta period can influence this and if it seems extremely important, we may take on small DCRs (Design Change Requests).&lt;/p&gt;  &lt;h3&gt;CTP&lt;/h3&gt;  &lt;p&gt;CTP stands for Community Technology Preview. It's generally an &lt;em&gt;incomplete&lt;/em&gt; preview of a new technology in progress. These usually come out before beta and are a way to gather feedback from the community during the development of a product. This is similar to an Alpha release per Jeff’s hierarchy, except that at Microsoft, we generally do put CTPs in a public location.&lt;/p&gt;  &lt;p&gt;With the ASP.NET MVC project, we no longer use the term CTP and simply use the term “Preview”. I think this is due to running out of our TLA (Three Letter Acronym) budget for the year. Our previews do still undergo a QA test pass and are released to the &lt;a href="http://asp.net/"&gt;ASP.NET website&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;Daily Builds / Interim Releases&lt;/h3&gt;  &lt;p&gt;The commenter didn’t ask about this, but I thought I would mention it. In many open source projects, you can get a daily build of the software directly from their source code repository. For example, with Subtext, if you want to grab the most recent build, you can go to our &lt;a title="Builds" href="http://build.subtextproject.com/builds/archive/"&gt;builds archive&lt;/a&gt;. A daily build is really for those who like to play with fire, as they usually are not tested, and could represent work in progress that is not even working at all.&lt;/p&gt;  &lt;p&gt;The closest thing the ASP.NET MVC team has to this is with our periodic “Interim releas”, a term we just made up, that is pushed out to &lt;a href="http://codeplex.com/aspnet"&gt;CodePlex&lt;/a&gt; and not placed on the ASP.NET website, because of the more mainstream nature of that site.&lt;/p&gt;  &lt;p&gt;As much as these CodePlex releases are for the cutting edge audience, being Microsoft, we can’t simply put daily builds out there and say you’re on your own. At least not yet. So these CodePlex builds are sanity checked by our QA team and by me, but they do not go under a full test pass like our Preview releases do. This is an area of experimentation for the ASP.NET team and so far, is proving successful.&lt;/p&gt;  &lt;h3&gt;Fully Supported Out-of-Band release&lt;/h3&gt;  &lt;p&gt;Internally, we usually call these OOB releases (pronounced “oob” like it’s spelled).&lt;/p&gt;  &lt;p&gt;A Fully Supported Out-of-Band release is a release that is not part of the Framework (i.e. it's not included in an installation of the .NET Framework), but is fully supported as if it were. For example, you can call up PSS (Microsoft's Tech Support) for support on a fully supported OOB release.&lt;/p&gt;  &lt;p&gt;One example of this was “Atlas” which later became Microsoft Ajax and was rolled into ASP.NET 3.5. ASP.NET MVC 1.0 will be an example of an OOB release.&lt;/p&gt;  &lt;h3&gt;RTM and RTW release&lt;/h3&gt;  &lt;p&gt;RTM stands for “Released to Manufacturing” and is a throwback to the days when software was mostly released as CDs. When a project went “Gold”, it was released to manufacturing who then burned a bunch of CDs and packaged them up to be put on store shelves. True, this still goes on today believe it or not, but this mode of delivery is on the decline for certain types of software.&lt;/p&gt;  &lt;p&gt;RTW is a related term that stands for “Released to Web” which is more descriptive of how software is actually shipped these days. For example, while we like to use the term RTM internally out of habit, ASP.NET MVC will actually be RTW.&lt;/p&gt;  &lt;h3&gt;Service Pack&lt;/h3&gt;  &lt;p&gt;A Service Pack (or SP) is simply an RTM (or RTW) release of fixes and/or improvements to some software. It used to be that SPs rarely included new features, but it seems to be the norm now that they do. Service Packs tend to include all the hotfixes and patches released since the product originally was released, which is convenient for the end user in not having to install every fix individually.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b7daf7f1-f5fe-45de-9c46-59e6155195c0" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/beta" rel="tag"&gt;beta&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ctp" rel="tag"&gt;ctp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/alpha" rel="tag"&gt;alpha&lt;/a&gt;,&lt;a href="http://technorati.com/tags/rtw" rel="tag"&gt;rtw&lt;/a&gt;,&lt;a href="http://technorati.com/tags/rtm" rel="tag"&gt;rtm&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18524.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=4VoSHk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=4VoSHk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=cuRRJk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=cuRRJk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=jYGO7K"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=jYGO7K" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/15/understanding-beta.aspx</guid>
            <pubDate>Fri, 15 Aug 2008 20:17:23 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18524.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/15/understanding-beta.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18524.aspx</wfw:commentRss>
        </item>
        <item>
            <title>ASP.NET MVC Is Not Part of ASP.NET 3.5 SP1</title>
            <category>ASP.NET MVC</category>
            <category>ASP.NET</category>
            <link>http://haacked.com/archive/2008/08/14/aspnetmvc-not-in-sp1.aspx</link>
            <description>&lt;p&gt;I wanted to clear up a bit of confusion I’ve seen around the web about ASP.NET MVC and the .NET Framework 3.5 Service Pack 1. ASP.NET MVC was &lt;em&gt;not&lt;/em&gt; released as part of SP1. I repeat, ASP.NET 3.5 SP1 does not include ASP.NET MVC.&lt;/p&gt;  &lt;p&gt;What &lt;em&gt;was&lt;/em&gt; released with SP1 was the ASP.NET Routing feature, which is in use by both ASP.NET MVC and Dynamic Data. The Routing feature is my first Framework RTM feature to ship at Microsoft! We also shipped a bunch of other features such as &lt;a title="Dynamic Data" href="http://blogs.msdn.com/scothu/archive/2008/08/11/dynamic-data-rtm-is-released.aspx"&gt;Dynamic Data&lt;/a&gt;, and this &lt;a title="Breaking Changes in ASP.NET 3.5 SP1" href="http://www.mostlylucid.net/archive/2008/08/14/know-issues--breaking-changes-in-asp.net-3.5-sp1.aspx"&gt;short list of breaking changes&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I hope that clears things up and I apologize for the confusion.&lt;/p&gt;  &lt;p&gt;And for my next feat, I’m going to try and &lt;em&gt;read your mind, oooooh!&lt;/em&gt; Right now, you’re thinking something along the lines of,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font style="background-color: #ffffff" face="Trebuchet MS" size="3"&gt;Ok, so ASP.NET MVC didn’t ship as part of SP1. When &lt;em&gt;is&lt;/em&gt; it going to ship?!&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Good question! Scott Hanselman &lt;a title="Quip" href="http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx#c40bae1e-c243-49dc-a172-41bca9e3edd9"&gt;once quipped&lt;/a&gt; that it would ship in a month that ends in “-ber”. He also &lt;a title="Not the same old 3.5 SP1 Post" href="http://www.hanselman.com/blog/HiddenGemsNotTheSameOld35SP1Post.aspx"&gt;recently quipped&lt;/a&gt;,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Anyway, Phil has always said that MVC is on its own schedule and will ship when its done. Possibly when &lt;a title="Duke Nukem Forever on Wikipedia" href="http://en.wikipedia.org/wiki/Duke_Nukem_Forever"&gt;Duke Nukem Forever&lt;/a&gt; ships. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;That Scott, he’s so full of quips. ;)&lt;/p&gt;  &lt;p&gt;In any case, he’s right in that MVC is pretty much on its own schedule since the first RTM version will be a fully supported out-of-band release, much like Atlas was back in the day.&lt;/p&gt;  &lt;p&gt;The MVC team really doesn’t want to rush the first release. We’re taking the time to do the best we can in laying the groundwork for future releases. My hope is that we’ll have very few, if any, moments where we we want to make a breaking change because we didn’t provide the right amount of extensibility.&lt;/p&gt;  &lt;p&gt;At the same time, we also really want to get ASP.NET MVC in your hands in an RTM form soon so you can start using it for your clients who are uncomfortable working with a beta technology. Trust me, we are not in the business of the “perpetual-beta” and are working towards an RTM. As Scott pointed out, our hope is to get it out before the end of the year. But as most of you know about how software scheduling works, anything can happen between now and tomorrow.&lt;/p&gt;  &lt;h3&gt;Metrics&lt;/h3&gt;  &lt;p&gt;As we move towards the tail end of the development cycle, we’ve been pushing hard to get our bug/approved change request count down, which I recently twittered about. I asked Carl, our tester, to print out an Excel graph of our bug count over time. It feels really good to walk by his office every day and see the line trending down towards zero (though occasionally, it ticks up a bit). I think it’s a huge motivator to try and fix and close out work items.&lt;/p&gt;  &lt;p&gt;At the same time, this graph is for our benefit only and not something we’re being evaluated on by any managers, which is extremely important. One of the dangers of any metric is that developers are smart and they’ll do what they can to optimize the metric. For example, the danger with this metric is that we might be tempted to not log feature requests and bugs. Joel Spolsky &lt;a title="Metrics" href="http://www.joelonsoftware.com/news/20020715.html"&gt;wrote about this phenomena&lt;/a&gt; when measuring the performance of knowledge workers a while back,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;But in the absence of 100% supervision, workers have an incentive to “work to the measurement,” concerning themselves solely with the measurement and not with the actual value or quality of their work. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Since we’re the only ones who care about this graph (nobody is looking over our shoulder) and QA is very motivated to find bugs, I think it’s a safe to use as a fun source of motivation. For the most part, watching the graph move towards zero feels good. Those are the metrics I like, the ones that inspire positive feelings among the team and a sense of forward motion and momentum. :)&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e7aeffb5-f1cd-4d9f-ade1-00a846dc122b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Tags:  		&lt;a href="http://technorati.com/tags/aspnetmvc/" title="aspnetmvc tag" rel="tag"&gt;aspnetmvc&lt;/a&gt; 		,  		&lt;a href="http://technorati.com/tags/aspnet/" title="aspnet tag" rel="tag"&gt;aspnet&lt;/a&gt; 		,  		&lt;a href="http://technorati.com/tags/schedule/" title="schedule tag" rel="tag"&gt;schedule&lt;/a&gt; 		&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18523.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=eCLJBk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=eCLJBk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=MIrKzk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=MIrKzk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=BTp6oK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=BTp6oK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/14/aspnetmvc-not-in-sp1.aspx</guid>
            <pubDate>Fri, 15 Aug 2008 03:27:02 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18523.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/14/aspnetmvc-not-in-sp1.aspx#feedback</comments>
            <slash:comments>15</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18523.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Filters in ASP.NET MVC CodePlex Preview 4</title>
            <category>ASP.NET</category>
            <category>ASP.NET MVC</category>
            <link>http://haacked.com/archive/2008/08/14/aspnetmvc-filters.aspx</link>
            <description>&lt;p&gt;In Preview 2 or Preview 3 of ASP.NET (I forget which), we introduced the concept of &lt;em&gt;Action Filters&lt;/em&gt;. Sounds much more exciting than your run-of-the-mill &lt;code&gt;LayOnTheCouchMunchingChipsWatchingInfomercialsFilter&lt;/code&gt;, that I originally proposed to the team. Thankfully, that was rejected.&lt;/p&gt;  &lt;p&gt;An action filter is an attribute you can slap on an action method in order to run some code before and after the action method executes. Typically, an action filter represents a &lt;a title="Cross-Cutting Concern on Wikipedia" href="http://en.wikipedia.org/wiki/Cross-cutting_concern"&gt;cross-cutting concern&lt;/a&gt; to your action method. Output caching is a good example of a cross-cutting concern.&lt;/p&gt;  &lt;p&gt;In &lt;a title="ASP.NET MVC on CodePlex" href="In CodePlex Preview 4 of ASP.NET MVC"&gt;CodePlex Preview 4 of ASP.NET MVC&lt;/a&gt;, we split out our action filters into four types of filters, each of which is an interface.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;code&gt;IAuthorizationFilter&lt;/code&gt; &lt;/li&gt;    &lt;li&gt;&lt;code&gt;IActionFilter&lt;/code&gt; &lt;/li&gt;    &lt;li&gt;&lt;code&gt;IResultFilter&lt;/code&gt; &lt;/li&gt;    &lt;li&gt;&lt;code&gt;IExceptionFilter&lt;/code&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;IAuthorizationFilter&lt;/h3&gt;  &lt;p&gt;Authorization filters run before any of the action filters and allow you to cancel the action. If you cancel the action, you can set the &lt;code&gt;ActionResult&lt;/code&gt; instance you want rendered in response to the current request.&lt;/p&gt;  &lt;p&gt;There should be very few cases (hopefully) that you need to write such a filter of your own. In those rare cases when you do, you’ll be glad to have this interface around.&lt;/p&gt;  &lt;h3&gt;IActionFilter&lt;/h3&gt;  &lt;p&gt;Action filters allow you to run code before and after an action method is called, but &lt;em&gt;before&lt;/em&gt; the result of the action method is executed. This effectively allows you to hook into the rendering of the view, for example.&lt;/p&gt;  &lt;p&gt;In the “before” method (&lt;code&gt;OnActionExecuting&lt;/code&gt;), you can cancel the action and even supply an action result of your own instead. If you cancel the action, no other filters higher up the stack will be executed and the invoker starts executing the “after” method for any action filter that had its “before” method called (except for the filter that canceled the action).&lt;/p&gt;  &lt;p&gt;In the after method (&lt;code&gt;OnActionExecuted&lt;/code&gt;) you can’t cancel the action (it already ran and we don’t have a &lt;code&gt;ITimeMachineFilter&lt;/code&gt; implemented yet), but you &lt;em&gt;can&lt;/em&gt; replace or modify the action result before it gets called.&lt;/p&gt;  &lt;p&gt;If an exception was thrown by another action filter or by the action method itself, you can examine the exception thrown from your filter. Your filter can specify that it can handle the exception (&lt;em&gt;seriously, only do this if your filter really can do this as it’s generally a bad thing to handle an exception you shouldn’t be handling&lt;/em&gt;), in which case the action result will still get executed. If the exception propagates up, the result will not get executed.&lt;/p&gt;  &lt;h3&gt;IResultFilter&lt;/h3&gt;  &lt;p&gt;Result filters are pretty much similar to action filters, except they run after the action method has executed, but before the result returned from the action method has been executed. The “before” method is called &lt;code&gt;OnResultExecuting&lt;/code&gt; and the “after” method is called &lt;code&gt;OnResultExecuted&lt;/code&gt;.&lt;/p&gt;  &lt;h3&gt;IExceptionFilter&lt;/h3&gt;  &lt;p&gt;The exception filters are all guaranteed to run after all of the action filters and result filters have run. Even if an exception filter indicates that it can handle the exception, it will still run. This is useful for logging scenarios in cases where you want a filter to always run no matter what happens so it can log exceptions etc…&lt;/p&gt;  &lt;p&gt;One interesting thing to note is that exception filters run after result filters. So what can you do from an exception filter? Well we give you one last ditch chance to render something to the user by allowing you to set the action result in the exception filter. If &lt;em&gt;that&lt;/em&gt; action result throws an exception, you’re &lt;a title="SOL from Urban Dictionary" href="http://www.urbandictionary.com/define.php?term=S.O.L."&gt;SOL&lt;/a&gt; and the exception filter does not handle that exception. Well, you’re not totally SOL. The normal ASP.NET &lt;a title="web.config customErrors" href="http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx"&gt;web.config settings for custom errors&lt;/a&gt; will kick in if you set them.&lt;/p&gt;  &lt;h3&gt;Writing Custom Filters&lt;/h3&gt;  &lt;p&gt;To write a custom filter, you simply need to create an attribute (aka a class that inherits from &lt;code&gt;FilterAttribute&lt;/code&gt;) that also implements one of the four interfaces I mentioned.&lt;/p&gt;  &lt;p&gt;It turns out that we think the most common case for custom filters will be those that implement &lt;code&gt;IActionFilter&lt;/code&gt; and/or &lt;code&gt;IResultFilter. &lt;/code&gt;To support the common case, we included a base attribute &lt;code&gt;ActionFilterAttribute&lt;/code&gt;, which inherits &lt;strong&gt;both&lt;/strong&gt; of these interfaces. Yeah, the name isn’t &lt;em&gt;exactly&lt;/em&gt; accurate, but we tend to think of action filters as really action and action result filters.&lt;/p&gt;  &lt;p&gt;For the other two filter types, we did not include a base attribute type for these. To write your own authorization filter, you simply implement &lt;code&gt;IAuthorizationFilter&lt;/code&gt;. For example, here’s a filter I wrote the other day which we will probably include in the &lt;code&gt;MvcFutures&lt;/code&gt; assembly. Apply this filter to an action and it will perform request validation of potentially insecure input. (&lt;em&gt;Side Note: This validation is on by default in ASP.NET WebForm applications, but not in ASP.NET MVC applications because it’s implemented by the Page class, which runs too late.&lt;/em&gt;)&lt;/p&gt;  &lt;div class="dropshadow code"&gt;   &lt;div class="innerbox"&gt;     &lt;pre class="csharpcode"&gt;[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, 
  Inherited = &lt;span class="kwrd"&gt;true&lt;/span&gt;, AllowMultiple = &lt;span class="kwrd"&gt;false&lt;/span&gt;)]
&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; ValidateInputAttribute : FilterAttribute
    , IAuthorizationFilter {
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnAuthorization(AuthorizationContext filterContext) {
    filterContext.HttpContext.Request.ValidateInput();
  }
}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p class="clear"&gt;While we did not include a base attribute for these filters, we did include concrete implementations of these interfaces. For example, the &lt;code&gt;AuthorizeAttribute&lt;/code&gt; is a concrete implementation of an authorization filter. You can (&lt;em&gt;er…will be able to&lt;/em&gt;) inherit from this attribute if you want, but you can also simply implement &lt;code&gt;IAuthorizationFilter&lt;/code&gt; yourself.&lt;/p&gt;

&lt;h3&gt;Why Four Filter Types?&lt;/h3&gt;

&lt;p&gt;We debated this a long time. We could have stuck with just the two interfaces: &lt;code&gt;IActionFilter&lt;/code&gt; and &lt;code&gt;IResultFilter&lt;/code&gt; and handled all cases. &lt;/p&gt;

&lt;p&gt;The problem we ran into is that for attributes that perform some sort of authentication check, you want to be absolutely sure it runs before any of the action filters. And it’s very easy to get this wrong by accident even if you know what you are doing.&lt;/p&gt;

&lt;p&gt;The type of thing we wanted to avoid was accidentally running the output cache filter before the the authorization filter. That’s a recipe for an information disclosure bug, potentially displaying information to someone who shouldn’t have access to see it such as photos of your hair piece collection (&lt;em&gt;Why do you have so man?&lt;/em&gt;). So we decided that there ought to be four distinct filter phases in the life of a controller action: Authorization, Action Execution, Result Execution, Exception Handling.&lt;/p&gt;

&lt;p&gt;If you write an authorization filter, it is guaranteed to run before any other action filters.&lt;/p&gt;

&lt;p&gt;Keep in mind though, that these phases merely help guide filter writers into doing the right thing. Because the MVC framework is all about leaving you in control, it is still possible to get it all wrong. For example, I could write a custom output caching filter that implements &lt;code&gt;IAuthorizationFilter&lt;/code&gt; and thus runs at the wrong time. &lt;strong&gt;Please don’t do this. &lt;/strong&gt;Code responsibly.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:87e2bdeb-7866-4cd6-9b04-9b850c24fa35" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/aspnetmvc" rel="tag"&gt;aspnetmvc&lt;/a&gt;,&lt;a href="http://technorati.com/tags/aspnet" rel="tag"&gt;aspnet&lt;/a&gt;,&lt;a href="http://technorati.com/tags/action+filters" rel="tag"&gt;action filters&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18522.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=1BPPfk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=1BPPfk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=tCT2Kk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=tCT2Kk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=IGKoUK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=IGKoUK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/14/aspnetmvc-filters.aspx</guid>
            <pubDate>Thu, 14 Aug 2008 13:00:00 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18522.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/14/aspnetmvc-filters.aspx#feedback</comments>
            <slash:comments>15</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18522.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Better URLs With Subtext and Windows Live Writer</title>
            <category>Subtext</category>
            <link>http://haacked.com/archive/2008/08/13/editing-post-slugs.aspx</link>
            <description>&lt;p&gt;One feature of Windows Live Writer that Subtext supports is the ability to edit your post slug? What is the URL slug associated with a blog post? What is the URL slug?&lt;/p&gt;  &lt;p&gt;Take a quick look in the address bar and you should notice that the URL ends with “&lt;strong&gt;editing-post-slugs&lt;/strong&gt;.aspx”. That bold part is the post slug. It’s a human friendly URL portion that identifies this blog post, as opposed to using some integer id.&lt;/p&gt;  &lt;p&gt;For a long time, Subtext had the ability to automatically convert your blog post title into friendlier URLs. However, as with most automatic efforts, there are cases where it falls a bit short. &lt;/p&gt;  &lt;p&gt;For example, suppose I started writing the following post with the following title:&lt;/p&gt;  &lt;p&gt;&lt;img title="editing-post-in-wlw" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="417" alt="editing-post-in-wlw" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/Yallwilllikeitreally_895F/editing-post-in-wlw_3.png" width="427" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;When I post it, the URL ends up being a bit ugly, though Subtext does give a good faith effort.&lt;/p&gt;  &lt;p&gt;&lt;img title="ugly-url-subtext" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="454" alt="ugly-url-subtext" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/Yallwilllikeitreally_895F/ugly-url-subtext_3.png" width="520" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;With Windows live writer, there’s a little double hash mark at the bottom that you can click to expand, providing more options. In the &lt;em&gt;Slug:&lt;/em&gt; field, enter a cleaner URL.&lt;/p&gt;  &lt;p&gt;&lt;img title="editing-slug-in-wlw" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="534" alt="editing-slug-in-wlw" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/Yallwilllikeitreally_895F/editing-slug-in-wlw_3.png" width="544" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Now when you publish this post, the URL will end with the slug that you specified. &lt;/p&gt;  &lt;p&gt;If you use the Subtext Web Admin to post, we’ve had this feature all along in the &lt;em&gt;Advanced Options&lt;/em&gt; section. It’s the &lt;em&gt;Entry Name&lt;/em&gt; field (which I think we should call &lt;em&gt;Entry Name Slug&lt;/em&gt; since Slug seems to be the standard term for this.&lt;/p&gt;  &lt;p&gt;Of course when we come out with our MVC version, we can get rid of that annoying &lt;em&gt;.aspx&lt;/em&gt; at the end. :)&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ea8b44d6-1ca4-417e-83c2-e341e63aebe2" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/subtext" rel="tag"&gt;subtext&lt;/a&gt;,&lt;a href="http://technorati.com/tags/windows+live+writer" rel="tag"&gt;windows live writer&lt;/a&gt;,&lt;a href="http://technorati.com/tags/wlw" rel="tag"&gt;wlw&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18521.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=w9gPXk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=w9gPXk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=EoPZmk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=EoPZmk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=SAqdRK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=SAqdRK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/13/editing-post-slugs.aspx</guid>
            <pubDate>Wed, 13 Aug 2008 17:05:07 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18521.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/13/editing-post-slugs.aspx#feedback</comments>
            <slash:comments>13</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18521.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Subtext 2.0 Released</title>
            <category>Personal</category>
            <link>http://haacked.com/archive/2008/08/10/subtext-2.0-released.aspx</link>
            <description>&lt;p&gt;It’s been a long time coming, but we are finally ready to release Subtext 2.0. As I &lt;a title="Subtext Awakens From Its Slumber" href="http://haacked.com/archive/2008/04/08/subtext-awakens-from-its-slumber.aspx"&gt;mentioned in April&lt;/a&gt; (was it that long ago!?), this is scaled down a bit from our original 2.0 plans. But even so, we have a lot of new goodness in here. It’s not just a bug fix release, though there are plenty of those too.&lt;/p&gt;  &lt;h3&gt;Highlights&lt;/h3&gt;  &lt;p&gt;With this release, Subtext has top notch support for Windows Live Writer thanks to some &lt;a title="My First OSS contribution" href="http://www.timheuer.com/blog/archive/2008/06/25/first-contribution-made-to-open-source-subtext.aspx"&gt;check-ins from Tim Heuer&lt;/a&gt;.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Enhanced MetaWeblog API implementation to support providing a "slug" URL name for the post.  This gives the user the option to use the default URL naming, the "auto-friendly" or now to override that with your own slug name. &lt;/li&gt;    &lt;li&gt;Fixed a bug in the SiteMap handler for blogs not hosted at root domains.  Would love people to test this out. &lt;/li&gt;    &lt;li&gt;Added support for WordPress API functions of: newPage, editPage, getPages, newCategory &lt;/li&gt;    &lt;li&gt;Simple modification to the Windows Live Writer manifest to prevent those who think they can future post :-) &lt;/li&gt;    &lt;li&gt;Tag-based RSS syndicator &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Other highlights&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;New CSS-Based Admin Design That Makes Better Use of Space &lt;/li&gt;    &lt;li&gt;Ability to set a separate skin for mobile devices &lt;/li&gt;    &lt;li&gt;Streamlined Installation Process. I tried to remove unnecessary steps and make this more robust. &lt;/li&gt;    &lt;li&gt;Support for Enclosures (See &lt;a title="Subtext 2.0 enclosures" href="http://codeclimber.net.nz/archive/2008/08/08/new-feature-in-subtext-2.0-enclosures.aspx"&gt;Simo’s great post on this&lt;/a&gt; for more details) &lt;/li&gt;    &lt;li&gt;CSS and JS optimizations (&lt;a title="CSS and JS optimizations" href="http://codeclimber.net.nz/archive/2008/08/09/new-feature-in-subtext-2.0-css-and-js-optimization.aspx"&gt;Simo has more interesting details on this&lt;/a&gt;). &lt;/li&gt;    &lt;li&gt;Setting a date in the future for publishing posts (again, &lt;a title="Publishing in the future" href="http://codeclimber.net.nz/archive/2008/08/10/new-feature-in-subtext-2.0-publish-in-the-future.aspx"&gt;Simo has more details&lt;/a&gt;). &lt;/li&gt;    &lt;li&gt;Login to your blog using OpenID, as well as use your blog as an OpenId Delegate &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Notes for new installations&lt;/h3&gt;  &lt;p&gt;The install package includes a default &lt;em&gt;Subtext2.0.mdf&lt;/em&gt; file for SQL 2005. If you plan to run your blog off of SQL Express, installation is as easy as copying the install files to your webroot. If you’re not using SQL Express, but plan to use SQL Server 2005, you can attach to the supplied .mdf file and use it as your database.&lt;/p&gt;  &lt;h3&gt;Notes for upgrading&lt;/h3&gt;  &lt;p&gt;In the app_data folder of the install package, feel free to delete the database files there. They only apply to new installs.&lt;/p&gt;  &lt;p&gt;We also include a zip file with just the SQL upgrade scripts. This is sometimes useful for those who run into problems with the upgrade procedure.&lt;/p&gt;  &lt;p&gt;Full upgrade notes are on the &lt;a title="Upgrading Subtext" href="http://www.subtextproject.com/Home/About/Docs/Upgrading/tabid/147/Default.aspx"&gt;Subtext project website&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;So what’s next for Subtext?&lt;/h3&gt;  &lt;p&gt;The Subtext team is fired up to get their feet wet using ASP.NET MVC, and I can’t blame them. So at this point, we’re starting preliminary planning work for Subtext 3.0, the next major version of Subtext which will be a ground-up rewrite pulling in as much code from 2.0 along the way of course.&lt;/p&gt;  &lt;p&gt;But that doesn’t mean we’re abandoning the 2.0 line immediately. I would expect to see several small incremental releases of the 2.* line even as we start on 3.0 with fires lit under our butts. Subtext 3.0 is in the very early stages of planning taking a long term look into the future.&lt;/p&gt;  &lt;p&gt;After all, there’s still a lot of infrastructure decisions to be made, as well as requirements gathering. In what ways do we want to be just like Subtext 2.0? In what ways do we want to completely change the architecture?&lt;/p&gt;  &lt;p&gt;Some of the decisions we need to make, just as a start:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Where do we host? Do we stick with SourceForge or go elsewhere? &lt;/li&gt;    &lt;li&gt;What data access layer/ORM tooling should we use? &lt;/li&gt;    &lt;li&gt;What DI framework do we choose? &lt;/li&gt;    &lt;li&gt;What do we use for communication and documentation? &lt;/li&gt;    &lt;li&gt;What should our database design look like? &lt;/li&gt;    &lt;li&gt;Should we change how we &lt;a title="Multi-tenancy" href="http://www.ayende.com/Blog/archive/2008/08/06/Multi-Tenancy.aspx"&gt;handle multi-tenancy&lt;/a&gt;? &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;In any case, it’s been a fun ride so far, and I hope we can keep our momentum going in producing a great blogging platform for ASP.NET.&lt;/p&gt;  &lt;p&gt;And before I forget, here’s the &lt;strong&gt;&lt;a title="Download Subtext 2.0" href="https://sourceforge.net/project/showfiles.php?group_id=137896"&gt;download page link&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3980fe75-d410-48c5-b845-8f82c1fb2ca1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Tags: &lt;a title="subtext tag" href="http://technorati.com/tags/subtext/" rel="tag"&gt;subtext&lt;/a&gt; &lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18518.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=s8qMFk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=s8qMFk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=C6NAhk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=C6NAhk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=I08OdK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=I08OdK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/10/subtext-2.0-released.aspx</guid>
            <pubDate>Sun, 10 Aug 2008 23:45:00 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18518.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/10/subtext-2.0-released.aspx#feedback</comments>
            <slash:comments>40</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18518.aspx</wfw:commentRss>
        </item>
        <item>
            <title>An Arbitrary Cycle Method For ASP.NET MVC</title>
            <category>ASP.NET</category>
            <category>ASP.NET MVC</category>
            <link>http://haacked.com/archive/2008/08/07/aspnetmvc_cycle.aspx</link>
            <description>&lt;p&gt;In his &lt;a title="Practical Review" href="http://www.joshuamcharles.com/blog/2008/08/a-practical-review-aspnet-mvc/"&gt;Practical Review of ASP.NET MVC&lt;/a&gt;, Josh Charles provides a helpful review of ASP.NET MVC from a Rails developer’s perspective. It seemed fair and balanced, and the end result is that there’s room for improvement, which we’re taking to heart.&lt;/p&gt;  &lt;p&gt;However, that’s not the part that caught my attention. He mentioned that he wrote a &lt;code&gt;cycle&lt;/code&gt; method but couldn’t write it as an extension method to &lt;code&gt;HtmlHelper&lt;/code&gt;.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;this was an instance method that would take two strings and return the one that it didn’t return the last time it was called. In my templates, I used this to change the classes for each row of data, to give them different background colors. I considered writing an extension method to the Html object used for other Html operations in the view page, but this method specifically required the use of an additional private variable, so that would not work. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you don’t mind cheating a bit, there is a way to write this as an extension method. And while we’re doing that, why stop at only two strings? Why not take an indefinite number? :)&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Cycle(&lt;span class="kwrd"&gt;this&lt;/span&gt; HtmlHelper html, &lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] strings) {
    var context = html.ViewContext.HttpContext;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; index = Convert.ToInt32(context.Items[&lt;span class="str"&gt;"cycle_index"&lt;/span&gt;]);

    &lt;span class="kwrd"&gt;string&lt;/span&gt; returnValue = strings[index % strings.Length];

    html.ViewContext.HttpContext.Items[&lt;span class="str"&gt;"cycle_index"&lt;/span&gt;] = ++index;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; returnValue;
}&lt;/pre&gt;

&lt;p&gt;Perhaps allowing an indefinite number of strings is overkill (who ever heard of a table with tri-color highlighting?) but I thought it was fun to do regardless. Here’s an example of usage with three different CSS styles:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    .first {background-color: #ddd;}
    .second {background-color: khaki;}
    .third {background-color: #fdd;}
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;style&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;table&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 5; i++) { &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;tr&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;="&amp;lt;%= Html.Cycle("&lt;/span&gt;&lt;span class="attr"&gt;first&lt;/span&gt;&lt;span class="kwrd"&gt;", "&lt;/span&gt;&lt;span class="attr"&gt;second&lt;/span&gt;&lt;span class="kwrd"&gt;", "&lt;/span&gt;&lt;span class="attr"&gt;third&lt;/span&gt;&lt;span class="kwrd"&gt;") %&amp;gt;"&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;td&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Stuff&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;td&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;tr&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt; } &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;table&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And the output...&lt;/p&gt;
&lt;style&gt;&lt;![CDATA[



    .first {background-color: #ddd;}
    .second {background-color: khaki;}
    .third {background-color: #fdd;}]]&gt;&lt;/style&gt;

&lt;table&gt;&lt;tbody&gt;
    &lt;tr class="first"&gt;
      &lt;td&gt;Stuff&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr class="second"&gt;
      &lt;td&gt;Stuff&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr class="third"&gt;
      &lt;td&gt;Stuff&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr class="first"&gt;
      &lt;td&gt;Stuff&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr class="second"&gt;
      &lt;td&gt;Stuff&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;With this, go forth and spread tri-color highlighted tables all over the web. Or if you’re really crazy player, go with four color highlighting!&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:47487eda-48f4-46cf-b398-2d22d1078a58" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/aspnetmvc" rel="tag"&gt;aspnetmvc&lt;/a&gt;,&lt;a href="http://technorati.com/tags/helpers" rel="tag"&gt;helpers&lt;/a&gt;,&lt;a href="http://technorati.com/tags/review" rel="tag"&gt;review&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18517.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=CEYpok"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=CEYpok" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=uOPk6k"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=uOPk6k" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=GhgB0K"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=GhgB0K" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/07/aspnetmvc_cycle.aspx</guid>
            <pubDate>Thu, 07 Aug 2008 16:33:12 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18517.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/07/aspnetmvc_cycle.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18517.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Hey Ma, I&amp;rsquo;m On TV!</title>
            <category>ASP.NET</category>
            <category>ASP.NET MVC</category>
            <link>http://haacked.com/archive/2008/08/06/haack_on_channel9.aspx</link>
            <description>&lt;p /&gt;  &lt;p&gt;&lt;img title="phil-in-office" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="184" alt="phil-in-office" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/HeyMaImOnTV_8D16/phil-in-office_3.jpg" width="244" align="right" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Recently, Adam Kinney came by my office to &lt;a title="MVC Preview 4" href="http://channel9.msdn.com/shows/Continuum/MVCPreview4/"&gt;interview me for a Channel 9&lt;/a&gt; episode discussing ASP.NET MVC CodePlex Preview 4.&lt;/p&gt;  &lt;p&gt;I’ve known Adam for a long time, even before he joined Microsoft. I think we met (in person) at Tech-Ed 2003.&lt;/p&gt;  &lt;p&gt;In any case, we talk a bit about ASP.NET MVC and Preview 4, all the while I tried very hard not to put my foot in my mouth. At the end there are some outtakes of me impersonating Scott Hanselman doing an impersonation of Sean Conery. That wasn’t to make fun of Scott, but totally out of love and respect. ;)&lt;/p&gt;  &lt;h3&gt;On Gaming&lt;/h3&gt;  &lt;p&gt;In the interview, I mentioned that I used to work at a skill gaming company called SkillJam which is no longer around. We had a tournament engine that allowed users to play games of skill for money. I worked on the back-end technologies such as our &lt;a title="Mobile Phone Gaming" href="http://haacked.com/archive/2005/06/08/mobile-phone-gaming.aspx"&gt;mobile gaming infrastructure&lt;/a&gt; which was well reviewed by &lt;a title="GameSpot Review" href="http://haacked.com/archive/2005/09/06/great-review-on-gamespot.aspx"&gt;GameSpot&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;SkillJam is no longer around as it was bought by &lt;a title="Fun Technologies" href="http://www.funtechnologies.com/"&gt;FUN Technologies&lt;/a&gt; (yes, I literally worked for “Fun” back then) and subsumed into &lt;a title="World Winner" href="http://www.worldwinner.com/"&gt;World Winner&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;In any case, one of the sharpest coworkers that I worked with there went out on his own to start a new game development company called &lt;a title="Casual Cafe" href="http://apps.facebook.com/casualcafe/"&gt;CasualCafe&lt;/a&gt;. This guy was the one who lived in the world of writing low-level C++ code, anti-cheat techniques, and game engines. If you are into the genre of “Casual Games” (such as Bejewelled, Solitaire, etc…) be sure to check it out.&lt;/p&gt;  &lt;h3&gt;Books on My Shelf&lt;/h3&gt;  &lt;p&gt;Via Twitter, &lt;a title="James Avery" href="http://infozerk.com/" rel="friend"&gt;James Avery&lt;/a&gt; was more interested in the books on my shelf than what I was saying (I don’t blame him). In case you were wondering, here’s a partial list:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="Facts and Fallacies" href="http://www.amazon.com/gp/product/0321117425?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0321117425"&gt;Facts and Fallacies of Software Engineering&lt;/a&gt; Highly recommended look at common myths and facts of software engineering. The facts are supported by research and data. &lt;/li&gt;    &lt;li&gt;&lt;a title="Code Complete 2nd Edition" href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0735619670"&gt;Code Complete&lt;/a&gt; Never leave home without it. Seriously. &lt;/li&gt;    &lt;li&gt;&lt;a title="Don't Make Me Think" href="http://www.amazon.com/gp/product/0321344758?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0321344758"&gt;Don’t Make Me Think!&lt;/a&gt; A classic on web usability. &lt;/li&gt;    &lt;li&gt;&lt;a title="UI for Programmers" href="http://www.amazon.com/gp/product/1893115941?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=1893115941"&gt;User Interface Design for Programmers&lt;/a&gt; A more general book on usability by Joel Spolsky. &lt;/li&gt;    &lt;li&gt;&lt;a title="FDG" href="http://www.amazon.com/gp/product/0321246756?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0321246756"&gt;Framework Design Guidelines&lt;/a&gt; Love it or hate it, the annotations alone are worthwhile. &lt;/li&gt;    &lt;li&gt;&lt;a title="Agile Web Development With Rails" href="http://www.amazon.com/gp/product/0977616630?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0977616630"&gt;Agile Web Development With Rails&lt;/a&gt; A bit outdated, but at the time, this helped me immensely getting up to speed at Koders. &lt;/li&gt;    &lt;li&gt;&lt;a title="Mastering Regular Expressions" href="http://www.amazon.com/gp/product/0596528124?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0596528124"&gt;Mastering Regular Expressions&lt;/a&gt; THE book on regular expressions. It lives up to its title. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are other books up there, but these are the ones I’ve read cover-to-cover and can recommend.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:7b95f08c-23f9-4feb-97de-233f1e517d2f" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/aspnetmvc" rel="tag"&gt;aspnetmvc&lt;/a&gt;,&lt;a href="http://technorati.com/tags/books" rel="tag"&gt;books&lt;/a&gt;,&lt;a href="http://technorati.com/tags/channel9" rel="tag"&gt;channel9&lt;/a&gt;&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18515.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=7CTzFk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=7CTzFk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=9EeTFk"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=9EeTFk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=yAn5UK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=yAn5UK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/06/haack_on_channel9.aspx</guid>
            <pubDate>Wed, 06 Aug 2008 23:39:47 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18515.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/06/haack_on_channel9.aspx#feedback</comments>
            <slash:comments>9</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18515.aspx</wfw:commentRss>
        </item>
        <item>
            <title>What Integrated Circuits Say About Testing Your Code</title>
            <category>TDD</category>
            <category>Software Development</category>
            <link>http://haacked.com/archive/2008/08/04/what-integrated-circuits-say-about-testing-your-code.aspx</link>
            <description>&lt;p&gt;A while back I talked about how &lt;a title="Testable code manages complexity" href="http://haacked.com/archive/2007/11/14/writing-testable-code-is-about-managing-complexity.aspx"&gt;testable code helps manage complexity&lt;/a&gt;. In that post, I mentioned one common rebuttal to certain design decisions made in code in order to make it more testable.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Why would I want to do XYZ just do improve testability?&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;img title="integrated-circuit" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="238" alt="integrated-circuit" src="http://haacked.com/images/haacked_com/WindowsLiveWriter/DesignForTestability_12B08/integrated-circuit_3.png" width="239" align="right" border="0" /&gt; Recently, I heard one variation of this comment in the comments to my post on &lt;a title="Unit Test Boundaries" href="http://haacked.com/archive/2008/07/22/unit-test-boundaries.aspx"&gt;unit test boundaries&lt;/a&gt;. Several people suggested that it’s fine to have unit tests access the database, after all, the code relies on data from the database, it should be tested. &lt;/p&gt;  &lt;p&gt;Implicit in this statement is the question, “&lt;em&gt;&lt;strong&gt;Why would I want to abstract away the data access just to improve testability?&lt;/strong&gt;”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Keep in mind, I never said you shouldn’t test your code’s interaction with the database. You absolutely should. I merely categorized that sort of test as a different sort of test - an integration test. You might still use your favorite unit testing framework to automate such a test, but I suggest trying to keep it in a separate test suite.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The authors of &lt;a href="http://www.amazon.com/gp/product/020161622X?ie=UTF8&amp;amp;tag=youvebeenhaac-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=020161622X"&gt;The Pragmatic Programmer: From Journeyman to Master&lt;/a&gt; have a great answer to this question with their comparison to Integrated Circuit's, which have features designed specifically to enable testability. &lt;/p&gt;  &lt;p&gt;The “&lt;a title="Design for Test on Wikipedia" href="http://en.wikipedia.org/wiki/Design_For_Test"&gt;Design For Test&lt;/a&gt;” Wikipedia entry refers to name as encompassing a range of design techniques for adding features to microelectronic hardware in order to make it testable. Examples of these techniques show up as early as the 1940s/50s. So designing for testability is not some whiz-bangy latest methodology flavor of the day the crazy kids are doing.&lt;/p&gt;  &lt;p&gt;One key benefit to these techniques is that components can be tested in relative isolation. You don’t have to place them into a product in order to test them, though at the same time, they can be tested while within the product.&lt;/p&gt;  &lt;p&gt;So in answer to the original question, I’d ask, &lt;strong&gt;“Why wouldn’t we design for testability?”&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I think this analogy illustrates one reason why I don’t want my unit tests talking to the database (apart from wanting the tests to run fast). Ideally, someone else down the road, new to the project, should be able to get the latest code from source control and run the unit tests immediately without having to go through the pains of setting up an environment with the correct database.&lt;/p&gt;  &lt;p&gt;Another benefit of abstracting away the database so that your code is testable and doesn’t cross boundaries is that your code is then not so dependent on a particular database. I used to argue that there’s no need to insulate your code from the particular database that you are using. I’ve never been on a project where the customer suddenly switches from SQL Server to Oracle. That sort of drastic change very rarely happens.&lt;/p&gt;  &lt;p&gt;But it turns out that I &lt;em&gt;have&lt;/em&gt; been on projects where we switched from SQL Server 6.5 to 7 (and from 7 to 2000 and so on). Upgrades can be nearly as drastic as choosing a different database vendor. Having your code isolated from your choice of database provides some nice peace of mind here.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:badb0488-6b77-4b78-8261-4839a82eb62c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Tags:  		&lt;a href="http://technorati.com/tags/IC/" title="IC tag" rel="tag"&gt;IC&lt;/a&gt; 		,  		&lt;a href="http://technorati.com/tags/Integrated+Circuit/" title="Integrated Circuit tag" rel="tag"&gt;Integrated Circuit&lt;/a&gt; 		,  		&lt;a href="http://technorati.com/tags/TDD/" title="TDD tag" rel="tag"&gt;TDD&lt;/a&gt; 		,  		&lt;a href="http://technorati.com/tags/Unit+Testing/" title="Unit Testing tag" rel="tag"&gt;Unit Testing&lt;/a&gt; 		&lt;/div&gt;&lt;img src="http://haacked.com/aggbug/18513.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.haacked.com/~f/haacked?a=3Qfmck"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=3Qfmck" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=3Cqh2k"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=3Cqh2k" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.haacked.com/~f/haacked?a=TWb2AK"&gt;&lt;img src="http://feeds.haacked.com/~f/haacked?i=TWb2AK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Haacked</dc:creator>
            <guid isPermaLink="false">http://haacked.com/archive/2008/08/04/what-integrated-circuits-say-about-testing-your-code.aspx</guid>
            <pubDate>Mon, 04 Aug 2008 18:00:00 GMT</pubDate>
            <wfw:comment>http://haacked.com/comments/18513.aspx</wfw:comment>
            <comments>http://haacked.com/archive/2008/08/04/what-integrated-circuits-say-about-testing-your-code.aspx#feedback</comments>
            <slash:comments>12</slash:comments>
            <wfw:commentRss>http://haacked.com/comments/commentRss/18513.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
