<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Grubbsian</title>
	<atom:link href="http://www.thegrubbsian.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thegrubbsian.com</link>
	<description>Writings on .NET, Ruby, JavaScript, HTML/CSS, Design, and all things Web.</description>
	<lastBuildDate>Sun, 01 Aug 2010 20:57:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Deep Validation with DataAnnotations</title>
		<link>http://www.thegrubbsian.com/2010/08/01/deep-validation-with-dataannotations/</link>
		<comments>http://www.thegrubbsian.com/2010/08/01/deep-validation-with-dataannotations/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 20:57:19 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=320</guid>
		<description><![CDATA[The following attribute will allow complex child properties to be validated when using the Validator.TryValidateObject in System.ComponentModel.DataAnnotations.]]></description>
			<content:encoded><![CDATA[<p>The following attribute will allow complex child properties to be validated when using the Validator.TryValidateObject in System.ComponentModel.DataAnnotations.</p>
<p><script src="http://gist.github.com/502736.js?file=ValidateChildAttribute.cs"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2010/08/01/deep-validation-with-dataannotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery.ganttView a Lightweight Gantt Chart for jQuery</title>
		<link>http://www.thegrubbsian.com/2010/06/11/jquery-ganttview-a-lightweight-gantt-chart-for-jquery/</link>
		<comments>http://www.thegrubbsian.com/2010/06/11/jquery-ganttview-a-lightweight-gantt-chart-for-jquery/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 16:05:16 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=311</guid>
		<description><![CDATA[I just wanted to drop a quick post about a new jQuery plugin that I&#8217;ve been working on for a recent project. As the title of the post implies it renders a simple HTML based Gantt chart using jQuery and the excellent date.js library. The screenshot below should give you an idea of what can [...]]]></description>
			<content:encoded><![CDATA[<p>I just wanted to drop a quick post about a new jQuery plugin that I&#8217;ve been working on for a recent project.  As the title of the post implies it renders a simple HTML based Gantt chart using jQuery and the excellent <a href="http://www.datejs.com/">date.js</a> library.  The screenshot below should give you an idea of what can be done with the plugin.<br />
<br/><br />
<img src="http://www.thegrubbsian.com/wp-content/uploads/2010/06/jquery-ganttview.png"/><br />
<br/><br />
The current version is fairly stable and has been tested in FireFox 3.6, Safari 4, Chrome 5, and IE8 and works well in all those browsers.  There are likely a few issues with it in IE7 and I wouldn&#8217;t even bother checking IE6, unless someone sends me a patch I have no interest in supporting IE6.<br />
<br/><br />
The source code and documentation can be found on GitHub: <a href="http://github.com/thegrubbsian/jquery.ganttView">http://github.com/thegrubbsian/jquery.ganttView</a><br />
<br/><br />
Let me know what you think and if there are any specific features you&#8217;d like to see added.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2010/06/11/jquery-ganttview-a-lightweight-gantt-chart-for-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>LINQ-to-SQL Sugar</title>
		<link>http://www.thegrubbsian.com/2009/12/21/linq-to-sql-sugar/</link>
		<comments>http://www.thegrubbsian.com/2009/12/21/linq-to-sql-sugar/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 04:33:15 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=262</guid>
		<description><![CDATA[Yes, yes, LINQ-to-SQL is probably, one day, going to be consumed or deprecated by it&#8217;s big brother the Entity Framework. However, for the current .NET platform (3.5) and likely for the next (4.0) it is still a viable ORM option in the Visual Studio box. There are lots of reasons to use LINQ-to-SQL and lots [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, yes, LINQ-to-SQL is probably, one day, going to be consumed or deprecated by it&#8217;s big brother the Entity Framework.  However, for the current .NET platform (3.5) and likely for the next (4.0) it is still a viable ORM option in the Visual Studio box.  There are lots of reasons to use LINQ-to-SQL and lots of reasons <em>not</em> to use it&#8230;that is for another post.  </p>
<p>What I do want to share is a set of extension methods that (for me) made using LINQ-to-SQL just a little bit nicer.  The title is indeed descriptive&#8230;these are &#8220;sugar&#8221; added to LINQ-to-SQL and don&#8217;t fundamentally improve how it works, just how the programmer works with the library.  I&#8217;m sure developers using LINQ-to-SQL have created scads of other such saccarine extensions to the framework&#8230;if you have some I&#8217;d be interested in hearing about them.</p>
<p>So here&#8217;s my LinqToSqlExtensions class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> LinqToSqlExtensions <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TEntity GetById<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Table<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> @<span style="color: #0600FF;">this</span>, <span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span>
        where TEntity <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SingleOrDefault</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IIdentified<span style="color: #000000;">&#41;</span>x<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">==</span> id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TEntity Upsert<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Table<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> @<span style="color: #0600FF;">this</span>, <span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span>
        where TEntity <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        var entity <span style="color: #008000;">=</span> @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SingleOrDefault</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IIdentified<span style="color: #000000;">&#41;</span>x<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">==</span> id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>entity <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            entity <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TEntity<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>entity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>entity <span style="color: #008000;">is</span> IAuditable<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IAuditable<span style="color: #000000;">&#41;</span> entity<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ModifiedDate</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> entity<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TEntity Upsert<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Table<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> @<span style="color: #0600FF;">this</span>, Func<span style="color: #008000;">&lt;</span>TEntity, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;</span> predicate<span style="color: #000000;">&#41;</span>
        where TEntity <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        var entity <span style="color: #008000;">=</span> @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SingleOrDefault</span><span style="color: #000000;">&#40;</span>predicate<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>entity <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            entity <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TEntity<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>entity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>entity <span style="color: #008000;">is</span> IAuditable<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IAuditable<span style="color: #000000;">&#41;</span> entity<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ModifiedDate</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> entity<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Delete<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Table<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> @<span style="color: #0600FF;">this</span>, TEntity entity<span style="color: #000000;">&#41;</span>
        where TEntity <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
        @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DeleteOnSubmit</span><span style="color: #000000;">&#40;</span>entity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> DeleteById<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Table<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> @<span style="color: #0600FF;">this</span>, <span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span>
        where TEntity <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
        var entity <span style="color: #008000;">=</span> @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetById</span><span style="color: #000000;">&#40;</span>id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>entity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> DeleteAll<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Table<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> @<span style="color: #0600FF;">this</span>, 
        IEnumerable<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span> entities<span style="color: #000000;">&#41;</span> where TEntity <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
        entities.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>e <span style="color: #008000;">=&gt;</span> @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> CommitChanges<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> DataContext @<span style="color: #0600FF;">this</span>, RepositoryContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        var changeSet <span style="color: #008000;">=</span> @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetChangeSet</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> changeSet.<span style="color: #0000FF;">Inserts</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>changeSet.<span style="color: #0000FF;">Inserts</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">is</span> IAuditable<span style="color: #000000;">&#41;</span> UpdateAuditProperties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IAuditable<span style="color: #000000;">&#41;</span> changeSet.<span style="color: #0000FF;">Inserts</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, context, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> changeSet.<span style="color: #0000FF;">Updates</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>changeSet.<span style="color: #0000FF;">Updates</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">is</span> IAuditable<span style="color: #000000;">&#41;</span> UpdateAuditProperties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IAuditable<span style="color: #000000;">&#41;</span> changeSet.<span style="color: #0000FF;">Updates</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, context, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        @<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> UpdateAuditProperties<span style="color: #008000;">&lt;</span>TEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>TEntity entity, RepositoryContext context, 
        <span style="color: #FF0000;">bool</span> isInsert<span style="color: #000000;">&#41;</span> where TEntity <span style="color: #008000;">:</span> IAuditable <span style="color: #000000;">&#123;</span>
&nbsp;
        entity.<span style="color: #0000FF;">ModifiedDate</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
        entity.<span style="color: #0000FF;">ModifiedBy</span> <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">UserID</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isInsert<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            entity.<span style="color: #0000FF;">CreatedDate</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            entity.<span style="color: #0000FF;">CreatedBy</span> <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">UserID</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here&#8217;s a rundown of the methods it creates and how to use them:</p>
<p><strong>GetById</strong><br />
Simple enough, allows you to avoid using the LINQ syntax to find an element by it&#8217;s identifier.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var thing <span style="color: #008000;">=</span> DataContext.<span style="color: #0000FF;">Things</span>.<span style="color: #0000FF;">GetById</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><strong>Upsert</strong><br />
This is probably the most convenient addition.  It simply checks to see whether the object exists (by it&#8217;s ID) if so it returns it if not it creates a new one and returns that.  It just avoids a lot of query-for-an-object-and-if-its-null-create-a-new-one-and-insert-it code.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var thing <span style="color: #008000;">=</span> DataContext.<span style="color: #0000FF;">Things</span>.<span style="color: #0000FF;">Upsert</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var anotherThing <span style="color: #008000;">=</span> DataContext.<span style="color: #0000FF;">Things</span>.<span style="color: #0000FF;">Upsert</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;That Thing&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><strong>Delete and DeleteById</strong><br />
Pretty obvious&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">DataContext.<span style="color: #0000FF;">Things</span>.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>thing<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DataContext.<span style="color: #0000FF;">Things</span>.<span style="color: #0000FF;">DeleteById</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><strong>CommitChanges</strong><br />
This is just a replacement for SubmitChanges that allows us to update some audit columns automagically.</p>
<p>Some of this magic is provided via a set of partial classes on those that LINQ-to-SQL generates that applies some additional interfaces to those classes.  So, I have a Partials.cs file that contains the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IAuditable <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> DateTime CreatedDate <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> DateTime UpdatedDate <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> CreatedBy <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> UpdatedBy <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IIdentified <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> ID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Thing <span style="color: #008000;">:</span> IIdentified, IAuditable <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OtherThing <span style="color: #008000;">:</span> IIdentified <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LastThing <span style="color: #008000;">:</span> IAuditable <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>As long as your generated class (and it&#8217;s related table) have properties of the same names as the IAuditable and IIdentified interfaces the LINQ-to-SQL extension methods can update them appropriately and in the background.  I could have accomplished much of this by just inheriting a new DataContext and adding some additional functionality, but doing it this way you can add these methods to even a closed assembly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/12/21/linq-to-sql-sugar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scriptability via the DLR and PostSharp</title>
		<link>http://www.thegrubbsian.com/2009/12/15/scriptability-via-the-dlr-and-postsharp/</link>
		<comments>http://www.thegrubbsian.com/2009/12/15/scriptability-via-the-dlr-and-postsharp/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 21:28:13 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[DLR]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=265</guid>
		<description><![CDATA[Making an application scriptable (particularly in a static language) has historically been difficult. With the advent of the DLR (Dynamic Language Runtime) on the .NET platform it becomes almost trivial to add scripting support to any application. For a recent project I needed the ability to add scripting hooks throughout the application and coupling the [...]]]></description>
			<content:encoded><![CDATA[<p>Making an application scriptable (particularly in a static language) has historically been difficult.  With the advent of the DLR (Dynamic Language Runtime) on the .NET platform it becomes almost trivial to add scripting support to any application.  For a recent project I needed the ability to add scripting hooks throughout the application and coupling the DLR with PostSharp AOP attributes made this effort pretty straightforward.  Here&#8217;s how it was done:</p>
<p><strong>Bring in the DLR</strong><br />
The DLR is implemented as a set of plain old DLL&#8217;s on top of the .NET Framework version 3.5&#8230;contrary to popular belief, you do not have to be running VS2010 or .NET 4 to use it.  Simply go to the CodePlex page for the DLR (<a href="http://dlr.codeplex.com">http://dlr.codeplex.com</a>), grab the source, and compile it.  There are solution files for VS2008 and VS2010.</p>
<p>For my purposes I used IronPython as the scripting language but IronRuby is available in the download as well.  To use the DLR your project will need to reference the following assemblies:</p>
<ul>
<li>Microsoft.Dynamic.dll</li>
<li>Microsoft.Scripting.Core.dll</li>
<li>Microsoft.Scripting.dll</li>
</ul>
<p>In order to use IronPython, you will also need to reference these:</p>
<ul>
<li>IronPython.dll</li>
<li>IronPython.Modules.dll</li>
<li>Microsoft.Scripting.ExtensionAttribute.dll</li>
</ul>
<p><strong>Register the Languages</strong><br />
Now that your project contains all the references it needs it&#8217;s time to set up the little bit of code needed to host the DLR.  First you&#8217;ll need to add a few things to your configuration file (App.config or Web.config depending on your project type&#8230;this works equally well with either).  Just add the following to the <configSections> node of your config file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;microsoft.scripting&quot;</span> </span>
<span style="color: #009900;">	<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Microsoft.Scripting.Hosting.Configuration.Section, Microsoft.Scripting, </span>
<span style="color: #009900;">		Version=0.9.6.20, Culture=neutral, PublicKeyToken=null&quot;</span> </span>
<span style="color: #009900;">	<span style="color: #000066;">requirePermission</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>And add the following somewhere in the root <configuration> node:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;microsoft.scripting<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;languages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language</span> <span style="color: #000066;">names</span>=<span style="color: #ff0000;">&quot;IronPython;Python;py&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">extensions</span>=<span style="color: #ff0000;">&quot;.py&quot;</span> <span style="color: #000066;">displayName</span>=<span style="color: #ff0000;">&quot;IronPython 2.6&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;IronPython.Runtime.PythonContext, IronPython, </span>
<span style="color: #009900;">				Version=2.6.10920.0, Culture=neutral, PublicKeyToken=null&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/languages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/microsoft.scripting<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This basically just tells the runtime what languages to register, what versions of those languages, and some info about file extensions, etc.</p>
<p><strong>Bring in PostSharp</strong><br />
Now I&#8217;m going to skip over to PostSharp for a minute.  If you&#8217;re not familiar with PostSharp you should run over to the website (<a href="http://www.postsharp.org">http://www.postsharp.org</a>) and check it out before continuing.  In short, it&#8217;s an AOP (Aspect Oriented Programming) library for .NET that allows easy injection of cross-cutting concerns through an application primarily via attributes.  The 10sec. version of how it works is this&#8230;it injects code in a post-compilation step.  This sounds scary, but it&#8217;s not.  In fact, this allows it to be extremely fast at runtime even if a small penalty is paid at compile-time.</p>
<p>In order to get PostSharp setup, it&#8217;s a simple download and install.  If you don&#8217;t want to install it on your machine, you can simply bin deploy it but it does require a tweak to your .proj file to get the post-compilation to work.  If you want to go that route, you can <a href="http://doc.postsharp.org/1.5/##PostSharp.HxS/UserGuide/Platform/EnablingPostSharp.html">find instructions here</a>.  At this point you will either follow those instructions or if you&#8217;ve installed directly, just include the following assemblies in your project:</p>
<ul>
<li>PostSharp.Laos.dll</li>
<li>PostSharp.Public.dll</li>
</ul>
<p><strong>The ScriptHook Attribute</strong><br />
PostSharp&#8217;s primary way of injecting an aspect is through attributes.  In our case we&#8217;re going to inherit from the OnMethodBoundaryAspect attribute in the PostSharp.Laos namespace.  This attribute exposes four virtual methods that allow injection of code at different points in the execution of a method:</p>
<ul>
<li>OnEntry</li>
<li>OnExit</li>
<li>OnException</li>
<li>OnSuccess</li>
</ul>
<p>Each of these methods accept a MethodExecutionEventArgs as an argument that provides access (including arguments) to the method on which the attribute is applied.  Here&#8217;s the ScriptHook attribute:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Serializable<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ScriptHookAttribute <span style="color: #008000;">:</span> OnMethodBoundaryAspect <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> ScriptEventTypes _scriptEventType<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ScriptEventAttribute<span style="color: #000000;">&#40;</span>ScriptEventTypes scriptEventType<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        _scriptEventType <span style="color: #008000;">=</span> scriptEventType<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnSuccess<span style="color: #000000;">&#40;</span>MethodExecutionEventArgs eventArgs<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        var args <span style="color: #008000;">=</span> eventArgs.<span style="color: #0000FF;">GetReadOnlyArgumentArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ScriptRunner.<span style="color: #0000FF;">ExecuteEvent</span><span style="color: #000000;">&#40;</span>_scriptEventType, args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>For now, ignore the ScriptRunner reference, but notice that I&#8217;ve created an enum (ScriptEventTypes) that distinguishes different &#8220;types&#8221; of scripts to be run, by types, I mean different usages.  At some point you can switch on the type of event that&#8217;s occurring and change which scripts are loaded or how they are handled, etc.  You would use the attribute like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>ScriptHook<span style="color: #000000;">&#40;</span>ScriptEventTypes.<span style="color: #0000FF;">ThingSaved</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SaveThing<span style="color: #000000;">&#40;</span>Thing t<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>ScriptHook<span style="color: #000000;">&#40;</span>ScriptEventTypes.<span style="color: #0000FF;">ThingDeleted</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> DeleteThing<span style="color: #000000;">&#40;</span>Thing t<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>The ScriptRunner</strong><br />
At this point we&#8217;ve got the DLR available and we have the PostSharp attribute that&#8217;s injecting our hooks.  Now we need something to execute our scripts.  Enter the ScriptRunner:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> ScriptRunner <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExecuteEvent<span style="color: #000000;">&#40;</span>ScriptEventTypes scriptEventType, <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> eventContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    IScriptEvent scriptEvent <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>scriptEventType<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">case</span> ScriptEventTypes.<span style="color: #0000FF;">ThingSaved</span><span style="color: #008000;">:</span>
	    scriptEvent <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ThingSavedScriptEvent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	    break<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">case</span> ScriptEventTypes.<span style="color: #0000FF;">ThingDeleted</span><span style="color: #008000;">:</span>
	    scriptEvent <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ThingDeletedScriptEvent<span style="color: #008000;">;</span>
	    break<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>scriptEvent <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
                scriptEvent.<span style="color: #0000FF;">ExecuteScripts</span><span style="color: #000000;">&#40;</span>eventContext<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ScriptExecutionExecption<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The runner is just a simple static class with a single ExecuteEvent method&#8230;it&#8217;s so simple in fact that it speaks for itself.  The one thing to note is the eventContext argument to the ExecuteEvent method.  If you look back at the ScriptHook attribute you can see that is&#8217; passing an object array from the MethodExecutionEventArgs instance.  This object array contains all the arguments to the executing method.  So for SaveThing() or DeleteThing() for example the eventContext array contains the instance of Thing that was passed.</p>
<p><strong>The IScriptEvent Classes</strong><br />
Lastly, we need something to actually grab the scripts and execute them in something like the context of the executing method.  So that&#8217;s where our example ThingSavedScriptEvent and ThingDeletedScriptEvent come in to play.  Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ThingSavedScriptEvent <span style="color: #008000;">:</span> IScriptEvent <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> ScriptRuntime _scriptRuntime<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IThingRepository _thingRepo<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ProjectCreatedScriptEvent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        _thingRepo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ThingRepository<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _scriptRuntime <span style="color: #008000;">=</span> ScriptRuntime.<span style="color: #0000FF;">CreateFromConfiguration</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ExecuteScripts<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> contextValues<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        var thing <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Thing<span style="color: #000000;">&#41;</span>contextValues.<span style="color: #0000FF;">SingleOrDefault</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">is</span> Thing<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		var scriptScope <span style="color: #008000;">=</span> _scriptRuntime.<span style="color: #0000FF;">CreateScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        scriptScope.<span style="color: #0000FF;">SetVariable</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;thingRepo&quot;</span>, _thingRepo<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        scriptScope.<span style="color: #0000FF;">SetVariable</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;thing&quot;</span>, thing<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        var script <span style="color: #008000;">=</span> GetScript<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        var scriptEngine <span style="color: #008000;">=</span> _scriptRuntime.<span style="color: #0000FF;">GetEngine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;IronPython&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        scriptEngine.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span>script, scriptScope<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> GetScript<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        var script <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
            code <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllText</span><span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Server</span>.<span style="color: #0000FF;">MapPath</span><span style="color: #000000;">&#40;</span>
                ConfigSettings.<span style="color: #0000FF;">ProjectTemplateStorePath</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;ThingSaved.py&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>FileNotFoundException<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> script<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The ScriptRuntime class is where the DLR magic happens.  We create an instance of it and then ask it for an engine that can execute IronPython scripts.  ScriptScope is a class used for constructing the scope that the script will have available to it.  Here for example we&#8217;re &#8220;passing&#8221; the &#8216;thingRepo&#8217; and &#8216;thing&#8217; variables into the script via the ScriptScope.  Lastly, we get the engine from the ScriptRuntime, pass it both the ScriptScope and the literal text of the script.  And that&#8217;s that&#8230;pretty easy eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/12/15/scriptability-via-the-dlr-and-postsharp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Reusable Test Scope Pattern</title>
		<link>http://www.thegrubbsian.com/2009/08/22/the-reusable-test-scope-pattern/</link>
		<comments>http://www.thegrubbsian.com/2009/08/22/the-reusable-test-scope-pattern/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 00:31:27 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=251</guid>
		<description><![CDATA[I’m almost positive someone has come up with this pattern before, but it was a big help on a recent project so I thought I’d share it. The basic goal of the pattern is to make the setup and teardown work of test classes reusable. We found this to cause particular pain for integration tests [...]]]></description>
			<content:encoded><![CDATA[<p>I’m almost positive someone has come up with this pattern before, but it was a big help on a recent project so I thought I’d share it.  The basic goal of the pattern is to make the setup and teardown work of test classes reusable.  We found this to cause particular pain for integration tests written against a database where multiple dependent records needed to be created before the actual test could be performed.  The second goal was that these externalized bits of code would be composable so that tests requiring multiple setup resources could create just the right ones.</p>
<p>Before really describing the workflow of the pattern, here are the basic components of our implementation:</p>
<p><b>The ITestScope Interface</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ITestScope <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">void</span> SetupScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">void</span> TeardownScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>These two methods (SetupScope and TeardownScope) are where we move all of the test setup and cleanup logic.  Any state that needs to be maintained between setup and teardown (i.e. needs to be available to the test itself) can be stored in public properties or fields on the class implementing ITestScope.</p>
<p><b>The TestBase Class</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> TestBase <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> ScopeSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> ScopeTeardown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Requiring that all test classes inherit from TestBase insures that at least developers will be considering the setup and cleanup of their tests.  The TestBase.ScopeSetup method would include instantiating and calling the TestScope.SetupScope methods for any scopes that are needed in the test class&#8230;likewise for teardown.</p>
<p><b>The TestFactory Class</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> TestFactory <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TRepository GetRepository<span style="color: #008000;">&lt;</span>TRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> where TRepository <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TController GetController<span style="color: #008000;">&lt;</span>TController<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> where TController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This isn&#8217;t directly related to the scope pattern, but it&#8217;s invaluable as a point where you can use a DI container in the test project.  All repository or controllers (or other resources) can be obtained from here.</p>
<p><b>An Example</b></p>
<p>So here&#8217;s an example test class that might use this pattern.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerTestScope <span style="color: #008000;">:</span> ITestScope <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> ICustomerRepository _customerRepo<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Customer TestCustomer <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> CustomerTestScope<span style="color: #000000;">&#40;</span>ICustomerRepository customerRepo<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_customerRepo <span style="color: #008000;">=</span> customerRepo<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SetupScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		TestCustomer <span style="color: #008000;">=</span> _customerRepo.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// Other complex test setup logic</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TeardownScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_customerRepo.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>TestCustomer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// The reverse of the complex setup logic</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>TestClass<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerRepositoryTests <span style="color: #008000;">:</span> TestBase <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> CustomerTestScope _customerScope<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> ICustomerRepository _customerRepo<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>TestInitialize<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ScopeSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		_customerRepo <span style="color: #008000;">=</span> TestFactory.<span style="color: #0000FF;">GetRepository</span><span style="color: #008000;">&lt;</span>ICustomerRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		_customerScope <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerTestScope<span style="color: #000000;">&#40;</span>_customerRepo<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		_customerScope.<span style="color: #0000FF;">SetupScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SaveCustomer_ShouldRenameCustomer_WhenNewNameGiven<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		_customerScope.<span style="color: #0000FF;">TestCustomer</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;New Name&quot;</span><span style="color: #008000;">;</span>
		_customerRepo.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span>_customerScope.<span style="color: #0000FF;">TestCustomer</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		var customer <span style="color: #008000;">=</span> _customerRepo.<span style="color: #0000FF;">GetById</span><span style="color: #000000;">&#40;</span>_customerScope.<span style="color: #0000FF;">TestCustomer</span>.<span style="color: #0000FF;">ID</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		Assert.<span style="color: #0000FF;">IsTrue</span><span style="color: #000000;">&#40;</span>customer.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;New Name&quot;</span>, 
			<span style="color: #666666;">&quot;SaveCustomer failed to update customer name.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>TestCleanup<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ScopeTeardown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_customerScope.<span style="color: #0000FF;">TeardownScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If additional scopes were needed then they could simple be added to the test class.  For example, if a ProductTestScope was also needed then it would be initialized just like the CustomerTestScope and torn down appropriately.  </p>
<p>Dependencies between scopes would be supplied via constructor parameters.  This does imply that the SetupScope methods be called in sequence and TeardownScope be called in reverse order.</p>
<p>That&#8217;s the pattern, pretty simple but now the logic for creating these setup conditions can be offloaded and reused in a simple way and we&#8217;ve made it easy to clean up that test data after the tests complete.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/08/22/the-reusable-test-scope-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thoughts on Legacy Code</title>
		<link>http://www.thegrubbsian.com/2009/08/01/thoughts-on-legacy-code/</link>
		<comments>http://www.thegrubbsian.com/2009/08/01/thoughts-on-legacy-code/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 19:57:31 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=223</guid>
		<description><![CDATA[I was listening to a recent Hanselminutes podcast episode in which Scott spoke with Michael Feathers of ObjectMentor on the topic of legacy code. They touched on a number of really effective techniques for approaching a legacy code-base and I wanted to echo some of their thoughts and add a few of my own. First, [...]]]></description>
			<content:encoded><![CDATA[<p>I was listening to a recent <a href="http://www.hanselminutes.com/">Hanselminutes</a> podcast <a href="http://www.hanselminutes.com/default.aspx?showID=183">episode</a> in which Scott spoke with <a href="http://www.michaelfeathers.com/">Michael Feathers</a> of <a href="http://www.objectmentor.com/">ObjectMentor</a> on the topic of <a href="http://en.wikipedia.org/wiki/Legacy_code">legacy code</a>.  They touched on a number of really effective techniques for approaching a legacy code-base and I wanted to echo some of their thoughts and add a few of my own.</p>
<p>First, the definition of legacy code (as was discussed in the show) is really much broader than the face value of the phrase itself.  In reality it&#8217;s harder to define &#8220;legacy code&#8221; than it is to know it when you see it.  Generally I know I&#8217;m dealing with legacy code when any of the following happen:</p>
<p>1. I&#8217;m fearful of making changes&#8230;the magic box where this code lives might get angry<br />
2. I&#8217;m confused and lost after more than a few clicks of &#8220;Go to Definition&#8221;<br />
3. I find few or no tests, or the quality of tests is poor<br />
4. There are tests but I get mixed or untrustworthy results from running them<br />
5. I have to write more lines of setup code to write a test than the lines of code that I want to test<br />
6. I can find nobody who will admit to understanding (or writing) this code</p>
<p>Doubtless there are more than just these indicators to tell you that you&#8217;re dealing with legacy code, but these are red-flags for sure.  So what are some high-level techniques that we can use to help bring legacy code into the present&#8230;well, here are a few:</p>
<p><b>Under The Rug</b><br />
We&#8217;ve all done it at home&#8230;there&#8217;s that last little bit of something that you missed with the broom.  So, you lift up the corner of the living room rug and kick it under there with your foot.  While you&#8217;re mom probably wouldn&#8217;t approve, sometimes with code this can be an acceptable approach &#8211; provided you acknowledge what you&#8217;re doing.  What I mean by sweeping it under the rug is that you wrap a section of &#8220;legacy code&#8221; in a bit of more modern, testable code.  In this way you can continue to use the legacy sections but a cleaner interface can be created for testing.</p>
<p><b>The Strangler Application</b><br />
This is a pattern that was introduced to me in the Hanselminutes podcast.  The basic premise is an extension of the &#8220;Under the Rug&#8221; concept.  But instead of just wrapping the code in a clean exterior, we actually take that opportunity to slowly &#8220;choke&#8221; out the old code.  </p>
<p>One technique that can be employed to slowly transform the codebase is using the &#8220;I&#8221; in SOLID&#8230;Interface Segregation.  Applying one or more interfaces to each legacy class allows you to incrementally swap out implementations with clean, new code.</p>
<p><b>Embrace Continual Design</b><br />
One thought that came out in the Hanselminutes episode was that design is never over.  And I think this is really important to realize.  Even in production/support mode, design is something that needs to be addressed.  When extending an application, are we setting it up for continued success and maintainability.  When re-factoring something or transforming it to un-legacy code, are we taking care that it won&#8217;t regress back into legacy code.  If a team is focused on continual design it&#8217;s less likely for code to become legacy in the first place.</p>
<p><b>Posthumous Test Coverage</b><br />
A third approach to legacy code is by testing the crap out of it.  One could even make the argument that this is a pre-requisite to the previous three approaches.  Spending a lot of time covering legacy code in tests has two major benefits.  First, it helps the developer really understand the intent of the code, it&#8217;s style, quirks, and patterns.  Secondly, it does what tests do best&#8230;helps make code changes safer.  If a codebase is adequately covered in tests then making changes becomes less scary and potential regression is reduced.</p>
<p><b>Forklift</b><br />
Lastly, there is one approach to legacy code that is rarely mentioned&#8230;and rightly so, it&#8217;s the most aggressive and can be the most costly.  Start over&#8230;sometimes it&#8217;s the best approach.</p>
<p>Once in my life I had to deal with a massive codebase that had been through many, many, cycles of attempted refactorings.  The years had left this poor application in a state of wild and unruly disrepair.  After discussing all the options with the client it became evident that actually starting over was the best (and in this case, cheapest) way to accomplish the goal.</p>
<p>Starting over can be daunting but if you attack it in a practical and measured way, and possibly mix it with the other techniques described here it can be liberating.  It&#8217;s important to have a good requirements phase when starting over, you can&#8217;t just look at legacy code and start re-writing it.  And you need to have all the trappings of a well staffed project in place as well, including strong QA.</p>
<p>~~</p>
<p>So those are some thoughts on legacy code.  As always, I&#8217;d appreciate hearing what everyone else thinks&#8230;what other techniques have you used?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/08/01/thoughts-on-legacy-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Martin: A Lightweight REST Framework Inspired by Sinatra and MVC</title>
		<link>http://www.thegrubbsian.com/2009/07/02/martin-a-framework-inspired-by-sinatra/</link>
		<comments>http://www.thegrubbsian.com/2009/07/02/martin-a-framework-inspired-by-sinatra/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 21:29:59 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Martin]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=228</guid>
		<description><![CDATA[Those of you who work in the Rails community will know something about Sinatra. It&#8217;s a lightweight web framework for building REST services in Ruby. I&#8217;m not a Ruby programmer (I know enough to be dangerous) but I really like the idea of Sinatra. The basic premise of Sinatra is that every method defines a [...]]]></description>
			<content:encoded><![CDATA[<p>Those of you who work in the Rails community will know something about Sinatra.  It&#8217;s a lightweight web framework for building REST services in Ruby.  I&#8217;m not a Ruby programmer (I know enough to be dangerous) but I really like the idea of Sinatra.  </p>
<p>The basic premise of Sinatra is that every method defines a route, and a route (URL + Verb) invokes a single method.  Ruby, being a dynamic language, makes this extremely easy.  Like I said I&#8217;m no Ruby guru but the built-in introspection of the language makes it easier to build a DSL that can describe these route methods.</p>
<p>Martin, isn&#8217;t at all a port of Sinatra.  Rather it&#8217;s inspired by the lightweight nature of Sinatra and it&#8217;s general pattern.  Martin relies on the new System.Web.Routing namespace in .NET 3.5 SP1 and will eventually ship with a DSL that will encapsulate defining your RouteTable.  Because it doesn&#8217;t have any external dependencies and really doesn&#8217;t need any templates it can plug into any ASP.NET or ASP.NET MVC project and live along side just fine*.</p>
<p>In Martin, the first step to creating a new action is defining it with the Config API.  At this point it&#8217;s excruciatingly simple.</p>
<p>In Global.asax:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    Config.<span style="color: #0000FF;">AddAction</span><span style="color: #008000;">&lt;</span>DateAction<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Show/CurrentDate&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>What this does is maps the URL: http://domain.com/Show/CurrentDate to the DateAction.Get() method.  Behind the scenes there is a factory class that&#8217;s constructing instances of DateAction, calling the Get() method, and then responding with the result to the request pipeline.</p>
<p>The DateAction class looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DateAction <span style="color: #008000;">:</span> ActionBase <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> IResponse Get<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> Text<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;The current date is {0}.&quot;</span>, DateTime.<span style="color: #0000FF;">Now</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There are four methods on ActionBase that can be overridden: Get(), Post(), Put(), and Delete().  Each method (obviously) corresponds to an HTTP Verb and only requests using that verb will invoke the appropriate method.  So, the same URL pattern can be mapped to two different methods provided that the request uses different verbs.</p>
<p>The ActionBase class provides a way to get access to any RouteData or Request items that might be part of the request.  Using the ValueOf<T>(String key) method you can find for example an ID that&#8217;s tacked on to the URL or a piece of data from the querystring or form collection.</p>
<p>~~</p>
<p>For now the project is in a very early stage, no real sample, no real documentation.  However, it&#8217;s so simple that reading through the handful of classes that make up the project is almost all the documentation you&#8217;ll need.  If you get a chance, poke around and let me know what you think&#8230;if you want to help make it better just let me know and I&#8217;ll get you commit rights to the Google Code repo.</p>
<p>Source at: <a href="http://code.google.com/p/martin-framework/">http://code.google.com/p/martin-framework/</a></p>
<p>* In the case of integration with ASP.NET MVC the routes defined by Martin and those your application might need could conflict so care must be taken to insure there are no collisions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/07/02/martin-a-framework-inspired-by-sinatra/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery Plugin: tablePager</title>
		<link>http://www.thegrubbsian.com/2009/06/05/jquery-plugin-tablepager/</link>
		<comments>http://www.thegrubbsian.com/2009/06/05/jquery-plugin-tablepager/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 18:38:18 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=211</guid>
		<description><![CDATA[Here&#8217;s a quick table pager plugin for jQuery. Unlike a number of the other plugins out there, this one doesn&#8217;t alter the DOM tree of the table itself it just hides the rows that are not available for the current page. In this way you can still get access to all the elements of the [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick table pager plugin for jQuery.  Unlike a number of the other plugins out there, this one doesn&#8217;t alter the DOM tree of the table itself it just hides the rows that are not available for the current page.  In this way you can still get access to all the elements of the table throughout the lifetime of the page.</p>
<p>Plugin Source:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">tablePager</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
        options = {
        firstBtn: Element,
        prevBtn: Element,
        nextBtn: Element,
        lastBtn: Element,
        indicator: Element,
        sizeSelect: Element,
        pageSize: Number,
        onPageChange: Callback,
        onPageSizeChange: Callback
        };
        */</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> tbl <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> pageSize<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> pageIndex <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> pageSize <span style="color: #339933;">=</span> opts.<span style="color: #660066;">pageSize</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rowCount <span style="color: #339933;">=</span> getRows<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> pageCount <span style="color: #339933;">=</span> getPageCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bindControlHandlers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            updatePaging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            updateIndicator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> getRows<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> tbl.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tbody tr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> bindControlHandlers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">firstBtn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	        opts.<span style="color: #660066;">firstBtn</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> 
		    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> changePage<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">prevBtn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		opts.<span style="color: #660066;">prevBtn</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> 
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> changePage<span style="color: #009900;">&#40;</span>pageIndex <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	    <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">nextBtn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		opts.<span style="color: #660066;">nextBtn</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> 
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> changePage<span style="color: #009900;">&#40;</span>pageIndex <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	    <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">lastBtn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		opts.<span style="color: #660066;">lastBtn</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> 
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> changePage<span style="color: #009900;">&#40;</span>pageCount <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	    <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">sizeSelect</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		opts.<span style="color: #660066;">sizeSelect</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;change&quot;</span><span style="color: #339933;">,</span> 
                    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> changePageSize<span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	    <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> getPageCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>rowCount <span style="color: #339933;">%</span> pageSize<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> 
		Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>rowCount <span style="color: #009966; font-style: italic;">/ pageSize) + 1 : (rowCount /</span> pageSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> changePage<span style="color: #009900;">&#40;</span>toIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>toIndex <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> toIndex <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span>pageCount <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                pageIndex <span style="color: #339933;">=</span> toIndex<span style="color: #339933;">;</span>
                updatePaging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                updateIndicator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">onPageChange</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    opts.<span style="color: #660066;">onPageChange</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>pageIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> changePageSize<span style="color: #009900;">&#40;</span>toSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            pageSize <span style="color: #339933;">=</span> toSize<span style="color: #339933;">;</span>
            pageCount <span style="color: #339933;">=</span> getPageCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            updatePaging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            updateIndicator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">onPageSizeChange</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                opts.<span style="color: #660066;">onPageSizeChange</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>pageSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> updateIndicator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opts.<span style="color: #660066;">indicator</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            	opts.<span style="color: #660066;">indicator</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>pageIndex <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #339933;">+</span> pageCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> updatePaging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> pageIndex <span style="color: #339933;">*</span> pageSize<span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> end <span style="color: #339933;">=</span> start <span style="color: #339933;">+</span> pageSize<span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> rows <span style="color: #339933;">=</span> getRows<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            rows.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;=</span> start <span style="color: #339933;">&amp;&amp;</span> i <span style="color: #339933;">&lt;</span> end<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span>rows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span>rows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                i<span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
            changePage<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>moveTo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> changePage<span style="color: #009900;">&#40;</span>moveTo <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            getPageSize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> pageSize<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            getPageIndex<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> pageIndex<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> pager <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#testTable&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tablePager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    firstBtn<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The element that pages to the first page</span>
    prevBtn<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.prev&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The element that pages to the previous page</span>
    nextBtn<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.next&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The element that pages to the next page</span>
    lastBtn<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The element that pages to the last page</span>
    indicator<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.curpage&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The input element that stores the current page (i.e. &quot;1/5&quot;)</span>
    sizeSelect<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.pagesize&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The select element that indicates the current page size</span>
    pageSize<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The initial page size</span>
    onPageChange<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>newIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Page change callback</span>
    onPageSizeChange<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>newSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// Page size change callback</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
pager.<span style="color: #660066;">changePage</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Causes the page to change from outside the plugin</span>
<span style="color: #003366; font-weight: bold;">var</span> pageIndex <span style="color: #339933;">=</span> pager.<span style="color: #660066;">getPageIndex</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Returns the current page index</span>
<span style="color: #003366; font-weight: bold;">var</span> pageSize <span style="color: #339933;">=</span> pager.<span style="color: #660066;">getPageSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Returns the current page size</span></pre></div></div>

<p>And here&#8217;s some example markup for the pager elements:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;span class=&quot;first&quot;&gt;First&lt;/span&gt;&amp;nbsp;&amp;nbsp;
&lt;span class=&quot;prev&quot;&gt;Prev&lt;/span&gt;&amp;nbsp;&amp;nbsp;
&lt;span class=&quot;next&quot;&gt;Next&lt;/span&gt;&amp;nbsp;&amp;nbsp;
&lt;span class=&quot;last&quot;&gt;Last&lt;/span&gt;
&lt;br&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; class=&quot;curpage&quot; /&gt;&amp;nbsp;&amp;nbsp;
&lt;select class=&quot;pagesize&quot;&gt;
    &lt;option value=&quot;5&quot;&gt;5&lt;/option&gt;
    &lt;option value=&quot;10&quot;&gt;10&lt;/option&gt;
    &lt;option value=&quot;20&quot;&gt;20&lt;/option&gt;
&lt;/select&gt;</pre></div></div>

<p>As you can see it&#8217;s really simple and flexible.  You can supply any elements you want to the pager.  If you don&#8217;t supply a first and last button elements for example then those will be ignored, if you don&#8217;t specify an indicator then that will be ignored too.  The most important thing is that your DOM stays intact, this allows you to continue to manipulate the &#8220;hidden&#8221; portions of the table (i.e. the stuff not on the current page) without having to access some crazy expando property that other pager plugins use to stash the un-shown portions of the table.</p>
<p>As always, I&#8217;d appreciate any feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/06/05/jquery-plugin-tablepager/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy Compression with ASP.NET MVC</title>
		<link>http://www.thegrubbsian.com/2009/05/02/easy-compression-with-aspnet-mvc/</link>
		<comments>http://www.thegrubbsian.com/2009/05/02/easy-compression-with-aspnet-mvc/#comments</comments>
		<pubDate>Sat, 02 May 2009 23:48:02 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=202</guid>
		<description><![CDATA[ASP.NET MVC already comes with great performance out of the box&#8230;due mostly to the lightweight nature of the framework. However, in the never-ending pursuit of faster load times I starting looking into what it would take to put together an Action Filter to compress the output of a controller action. Turns out, it&#8217;s pretty damn [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET MVC already comes with great performance out of the box&#8230;due mostly to the lightweight nature of the framework.  However, in the never-ending pursuit of faster load times I starting looking into what it would take to put together an Action Filter to compress the output of a controller action.  Turns out, it&#8217;s pretty damn easy.  Here&#8217;s what I came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CompressAttribute <span style="color: #008000;">:</span> ActionFilterAttribute <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext filterContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
            var encodingsAccepted <span style="color: #008000;">=</span> filterContext.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Headers</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Accept-Encoding&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>encodingsAccepted<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> return<span style="color: #008000;">;</span>
&nbsp;
            encodingsAccepted <span style="color: #008000;">=</span> encodingsAccepted.<span style="color: #0000FF;">ToLowerInvariant</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var response <span style="color: #008000;">=</span> filterContext.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Response</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>encodingsAccepted.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;deflate&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                response.<span style="color: #0000FF;">AppendHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-encoding&quot;</span>, <span style="color: #666666;">&quot;deflate&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                response.<span style="color: #0000FF;">Filter</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DeflateStream<span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">Filter</span>, CompressionMode.<span style="color: #0000FF;">Compress</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>encodingsAccepted.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;gzip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                response.<span style="color: #0000FF;">AppendHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-encoding&quot;</span>, <span style="color: #666666;">&quot;gzip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                response.<span style="color: #0000FF;">Filter</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GZipStream<span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">Filter</span>, CompressionMode.<span style="color: #0000FF;">Compress</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>And you can use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Compress<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ThingController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span> ... <span style="color: #000000;">&#125;</span></pre></div></div>

<p>or on a controller action:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ThingController<span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>Compress<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> ... <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see it really just sets a new filter for the response; GZipStream or DeflateStream depending on what the browser can handle and then sets the appropriate flag.  After doing some lightweight testing it looks like the compressed output is on average 18% of the original size.  My next task is to do some testing around how long the client takes to actually decompress the response.  My guess is that it&#8217;s pretty negligible though.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/05/02/easy-compression-with-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>NInject vs. StructureMap vs. I Made This</title>
		<link>http://www.thegrubbsian.com/2009/04/24/ninject-vs-structuremap-vs-i-made-this/</link>
		<comments>http://www.thegrubbsian.com/2009/04/24/ninject-vs-structuremap-vs-i-made-this/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 21:10:46 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=195</guid>
		<description><![CDATA[As I&#8217;ve mentioned before my team is working on a large scale ASP.NET MVC application and we&#8217;re using dependency injection to decouple our data access layer from the application. Initially we were using NInject as our IoC tool of choice. It&#8217;s lightweight and has a really nice configuration DSL and it&#8217;s pretty fast. The problem [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve mentioned before my team is working on a large scale ASP.NET MVC application and we&#8217;re using dependency injection to decouple our data access layer from the application.  Initially we were using NInject as our IoC tool of choice.  It&#8217;s lightweight and has a really nice configuration DSL and it&#8217;s pretty fast.</p>
<p>The problem arose when trying to get NInject&#8217;s scope caching to work.  It has this nifty feature where it will cache the objects it creates and you could specify the scope of that caching&#8230;for example SingletonBehavior, or OnPerSessionBehavior.  That&#8217;s great but I couldn&#8217;t make it work, regardless of the configuration it would always return a brand new object every time.</p>
<p>So then I went and checked out StructureMap.  It&#8217;s a little more complex to get set up and I personally don&#8217;t think it&#8217;s registration DSL is as nice as NInject&#8217;s but it works.  It has a similar caching feature where you can specify an Instance Scope of Application, Session, Request, Thread, etc.  Same problem though, no matter how I configured it the caching was always at the Application level.  So every session in the app would get back the same objects&#8230;not very secure on the data front.</p>
<p>Now, I am completely open to the possibility that I don&#8217;t know enough about either of these tools and that the roadblocks I ran into were user error.  However, I realized I was spending way too much time on something that is essentially a very simple problem.  Here&#8217;s what I needed out of an IoC tool:</p>
<p>1. Constructor Injection &#8211; I don&#8217;t really care about property or method injection, just constructors<br />
2. Cache the Output at Session Level<br />
3. Easy Registration with Constructor Args</p>
<p>So the following is what I came up with to solve the problem:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> DI <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">class</span> InjectionBinder <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> Type Abstract <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> Type Concrete <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> CtorArgs <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// A lightweight dependency injection tool that supports constructor injection and HttpContext.Session caching.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Injector <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> SessionBindingKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;__InjectorBindings__&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> SessionCacheKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;__InjectorCache__&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> List<span style="color: #008000;">&lt;</span>InjectionBinder<span style="color: #008000;">&gt;</span> _bindings <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionBindingKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionBindingKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>InjectionBinder<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>List<span style="color: #008000;">&lt;</span>InjectionBinder<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionBindingKey<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionBindingKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> List<span style="color: #008000;">&lt;</span>KeyValuePair<span style="color: #008000;">&lt;</span>Type, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;&gt;</span> _cache <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionCacheKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionCacheKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>KeyValuePair<span style="color: #008000;">&lt;</span>Type, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>List<span style="color: #008000;">&lt;</span>KeyValuePair<span style="color: #008000;">&lt;</span>Type, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#41;</span>HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionCacheKey<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionCacheKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Returns a concrete instance of the specified abstract type using the provided bindings and </span>
        <span style="color: #008080; font-style: italic;">/// attempts to satisfy that type's constructor arguments.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;abstractType&quot;&gt;The abstract type of the object to initialize.&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> GetInstance<span style="color: #000000;">&#40;</span>Type abstractType<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Return the item from the cache if it's in there</span>
            var cache <span style="color: #008000;">=</span> _cache.<span style="color: #0000FF;">ToDictionary</span><span style="color: #000000;">&#40;</span>o <span style="color: #008000;">=&gt;</span> o.<span style="color: #0000FF;">Key</span>, o <span style="color: #008000;">=&gt;</span> o.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cache.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>abstractType<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> cache<span style="color: #000000;">&#91;</span>abstractType<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// If the type is in the bindings, return the concrete</span>
            var foundBinding <span style="color: #008000;">=</span> _bindings.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>b <span style="color: #008000;">=&gt;</span> b.<span style="color: #0000FF;">Abstract</span> <span style="color: #008000;">==</span> abstractType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>foundBinding <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>foundBinding.<span style="color: #0000FF;">Concrete</span>, foundBinding.<span style="color: #0000FF;">CtorArgs</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">object</span> obj <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            var ctors <span style="color: #008000;">=</span> abstractType.<span style="color: #0000FF;">GetConstructors</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// If the type has no constructor, then just create one</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ctors.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>abstractType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// If there are constructors, find one which takes arguments of the types we have binders for</span>
            ctors.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
                var prms <span style="color: #008000;">=</span> c.<span style="color: #0000FF;">GetParameters</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                var args <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                var haveBindings <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                prms.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
                    var binding <span style="color: #008000;">=</span> _bindings.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>b <span style="color: #008000;">=&gt;</span> b.<span style="color: #0000FF;">Abstract</span> <span style="color: #008000;">==</span> p.<span style="color: #0000FF;">ParameterType</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>binding <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> haveBindings <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
                    <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span> args.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>binding.<span style="color: #0000FF;">Concrete</span>, binding.<span style="color: #0000FF;">CtorArgs</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>haveBindings<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> obj <span style="color: #008000;">=</span> Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>abstractType, args.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> obj<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Returns a concrete instance of the specified abstract type using the provided bindings and </span>
        <span style="color: #008080; font-style: italic;">/// attempts to satisfy that type's constructor arguments.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TAbstract&quot;&gt;The abstract type of the object to initialize.&lt;/typeparam&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TAbstract GetInstance<span style="color: #008000;">&lt;</span>TAbstract<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> where TAbstract <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> GetInstance<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>TAbstract<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> TAbstract<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Creates a binding rule between an abstract type and a concrete type.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TAbstract&quot;&gt;The abstract type for the binding.&lt;/typeparam&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TConcrete&quot;&gt;The concrete type for the binding&lt;/typeparam&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ctorArgs&quot;&gt;The list of constructor arguments to use when initializing the instance of the concrete type.&lt;/param&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Bind<span style="color: #008000;">&lt;</span>TAbstract, TConcrete<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ctorArgs<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            var binding <span style="color: #008000;">=</span> _bindings.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>b <span style="color: #008000;">=&gt;</span> b.<span style="color: #0000FF;">Abstract</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>TAbstract<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>binding <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                binding.<span style="color: #0000FF;">Concrete</span> <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>TConcrete<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                binding.<span style="color: #0000FF;">CtorArgs</span> <span style="color: #008000;">=</span> ctorArgs<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
                _bindings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> InjectionBinder <span style="color: #000000;">&#123;</span>
                    Abstract <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>TAbstract<span style="color: #000000;">&#41;</span>,
                    Concrete <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>TConcrete<span style="color: #000000;">&#41;</span>,
                    CtorArgs <span style="color: #008000;">=</span> ctorArgs
                <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Clears all Injector binding rules and empties the internal cache.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Kill<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionBindingKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span>SessionCacheKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And here&#8217;s how we register the bindings:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> BindInjectionContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    Injector.<span style="color: #0000FF;">Kill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Clears out previously created bindings and empties the cache</span>
&nbsp;
    var currentClient <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Client<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Doesn't matter...just an example</span>
    var currentUser <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var currentCulture <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;en-US&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    Injector.<span style="color: #0000FF;">Bind</span><span style="color: #008000;">&lt;</span>IClientRepository, ClientRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> currentClient, currentCulture <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Injector.<span style="color: #0000FF;">Bind</span><span style="color: #008000;">&lt;</span>IUserRepository, UserRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> currentClient, currentCulture, currentUser <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Injector.<span style="color: #0000FF;">Bind</span><span style="color: #008000;">&lt;</span>ISecurityRepository, SecurityRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And lastly, here&#8217;s how you request an object:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var secRepo <span style="color: #008000;">=</span> Injector.<span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>ISecurityRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>So, it may be ridiculous to roll-your-own on the IoC container but as you can see it was pretty simple to get what I needed.  It&#8217;s very solution specific but what&#8217;s wrong with that.  In this instance it&#8217;s pretty easy to swap out for something more feature rich if necessary but for now&#8230;I call YAGNI on all that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/04/24/ninject-vs-structuremap-vs-i-made-this/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
