<?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>blog.cleverswine.net &#187; Uncategorized</title>
	<atom:link href="http://blog.cleverswine.net/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cleverswine.net</link>
	<description>Kevin Noone</description>
	<lastBuildDate>Mon, 26 Sep 2011 23:13:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>WCF Client and the &#8220;using&#8221; Statement</title>
		<link>http://blog.cleverswine.net/2011/09/26/wcf-client-and-the-using-statement/</link>
		<comments>http://blog.cleverswine.net/2011/09/26/wcf-client-and-the-using-statement/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 22:55:56 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=735</guid>
		<description><![CDATA[As I was researching patterns and practices for a WCF client implementation, I came across a bug in the .Net implementation of ClientBase&#60;T&#62; (whether it's actually a bug is arguable). It's well documented on the web, but it could cause major headaches if you happened to miss it. The core of the issue is that [...]]]></description>
			<content:encoded><![CDATA[<p>As I was researching patterns and practices for a WCF client implementation, I came across a bug in the .Net implementation of ClientBase&lt;T&gt; (whether it's actually a bug is arguable). It's well documented on the web, but it could cause major headaches if you happened to miss it.</p>
<p>The core of the issue is that ClientBase&lt;T&gt; implements IDisposable. With this knowledge, just about any programmer would naturally wrap its usage in a using{} block. Digging deeper, you'll find that the ClientBase&lt;T&gt; Dispose() method simply calls Close() on the underlying connection. This is problematic because in certain connection states (such as "Faulted"), the Close() will fail.  As I said, this is well documented elsewhere, for example: <a title="Avoiding Problems with the Using Statement" href="http://msdn.microsoft.com/en-us/library/aa355056.aspx" target="_blank">on MSDN</a>, <a href="http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue" title="What is the best workaround for the WCF client `using` block issue?" target="_blank">Stack Overflow</a>, and <a href="http://blogs.msdn.com/b/jjameson/archive/2010/03/18/avoiding-problems-with-the-using-statement-and-wcf-service-proxies.aspx" title="Avoiding Problems with the Using Statement and WCF Service Proxies" target="_blank">here</a>.</p>
<p>There are a few different solutions to this issue. I went with a simple Disposable wrapper...</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Get a disposable MyClientWrapper object</span>
<span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #008000;">&#40;</span>var clientWrapper <span style="color: #008000;">=</span> ClientFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMyClientWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    clientWrapper<span style="color: #008000;">.</span><span style="color: #0000FF;">Client</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DoSomething</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyClientWrapper <span style="color: #008000;">:</span> IDisposable
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> MyClient _client<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> MyClientWrapper<span style="color: #008000;">&#40;</span>Binding binding, Uri endpoint<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// creates an instance of MyClient, which is derived from ClientBase&lt;T&gt;</span>
        _client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyClient<span style="color: #008000;">&#40;</span>binding, <span style="color: #008000;">new</span> EndpointAddress<span style="color: #008000;">&#40;</span>endpoint<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> MyClient Client
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _client<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// safe dispose with extension method (below)</span>
        _client<span style="color: #008000;">.</span><span style="color: #0000FF;">CloseProxy</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// extension method for safe Close() on ClientBase&lt;T&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> CloseProxy<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> ClientBase<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> proxy<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>proxy<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">!=</span> CommunicationState<span style="color: #008000;">.</span><span style="color: #0000FF;">Closed</span> 
                <span style="color: #008000;">&amp;&amp;</span> proxy<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">!=</span> CommunicationState<span style="color: #008000;">.</span><span style="color: #0000FF;">Faulted</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            proxy<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// may throw exception while closing</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            proxy<span style="color: #008000;">.</span><span style="color: #0000FF;">Abort</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>CommunicationException<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        proxy<span style="color: #008000;">.</span><span style="color: #0000FF;">Abort</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">throw</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/09/26/wcf-client-and-the-using-statement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Find/Kill a Linux Process by Name</title>
		<link>http://blog.cleverswine.net/2011/09/09/findkill-a-linux-process-by-name/</link>
		<comments>http://blog.cleverswine.net/2011/09/09/findkill-a-linux-process-by-name/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 22:19:40 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[MiscTech]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=730</guid>
		<description><![CDATA[I was doing some research to figure out how to easily kill a Linux process given part of it's name. It's fairly simple to do it manually, but if you didn't already know, programmers are lazy. The first thing I was doing... > ps aux &#124; grep Foo find the process ID (PID) with my [...]]]></description>
			<content:encoded><![CDATA[<p>I was doing some research to figure out how to easily kill a Linux process given part of it's name.  It's fairly simple to do it manually, but if you didn't already know, programmers are lazy.</p>
<p>The first thing I was doing...<br />
> ps aux | grep Foo<br />
find the process ID (PID) with my eyes, and then kill it...<br />
> kill -9 123456</p>
<p>Next, I started finding complex piped commands to find and extract the PID.  For instance...<br />
> pid=`ps -eo pid,args | grep Foo | grep -v grep | cut -c1-6`</p>
<p>and slightly simpler...<br />
> ps aux | grep Foo | grep -v grep | awk '{print $2}'</p>
<p>Finally, I discovered <a href="http://www.linuxmanpages.com/man1/pgrep.1.php" title="pgrep" target="_blank">pgrep and pkill</a>.  There are quite a few options for each command, but simply put:</p>
<p>To get the PID for a process with a name containing 'Foo'...<br />
> pgrep Foo</p>
<p>To get the PID for a process with a command line containing 'Bar' (as in > Foo -n Bar)...<br />
> pgrep -f Bar</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/09/09/findkill-a-linux-process-by-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canning Fever</title>
		<link>http://blog.cleverswine.net/2011/08/31/canning-fever/</link>
		<comments>http://blog.cleverswine.net/2011/08/31/canning-fever/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 23:01:24 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Food]]></category>
		<category><![CDATA[Gardening]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/2011/08/31/canning-fever/</guid>
		<description><![CDATA[Pasta Sauce and Blueberry Jam Originally uploaded by cleverswine Bren has canning fever. It all started with a canning book that she received as a Christmas present. Now, she has canned (or jarred as I like to call it) Salsa, Pasta Sauce, Blueberry Jam, and Sweet Pickles. The nice thing is that we'll be well [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/cleverswine/6087280290/" title="photo sharing"><img src="http://farm7.static.flickr.com/6089/6087280290_71f4107933_m.jpg" alt="" style="border: solid 2px #000000;" /></a><br />
<br />
<span style="font-size: 0.9em; margin-top: 0px;"><a href="http://www.flickr.com/photos/cleverswine/6087280290/">Pasta Sauce and Blueberry Jam</a><br />
<br />
Originally uploaded by <a href="http://www.flickr.com/photos/cleverswine/">cleverswine</a><br />
</span><br />
<br clear="all" /></p>
<p>Bren has canning fever.  It all started with a canning book that she received as a Christmas present.  Now, she has canned (or jarred as I like to call it) Salsa, Pasta Sauce, Blueberry Jam, and Sweet Pickles.  The nice thing is that we'll be well stocked with food that we know is locally grown and chemical free.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/08/31/canning-fever/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slow Gardening</title>
		<link>http://blog.cleverswine.net/2011/07/18/slow-gardening/</link>
		<comments>http://blog.cleverswine.net/2011/07/18/slow-gardening/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 23:15:54 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Gardening]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[Weather]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/2011/07/18/slow-gardening/</guid>
		<description><![CDATA[Bellpepper Originally uploaded by cleverswine This summer is getting off to a slow start, as are our vegetables. We planted tomatoes, cucumbers, zucchini, various peppers, and carrots. So far we haven't picked a thing. We've barely reached the 80s in temperature, and we've had more rain than usual. However, despite the lack of veggies, I'm [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/cleverswine/5943862099/" title="photo sharing"><img src="http://farm7.static.flickr.com/6141/5943862099_ab977a0817_m.jpg" alt="" style="border: solid 2px #000000;" /></a><br />
<br />
<span style="font-size: 0.9em; margin-top: 0px;"><a href="http://www.flickr.com/photos/cleverswine/5943862099/">Bellpepper</a><br />
<br />
Originally uploaded by <a href="http://www.flickr.com/photos/cleverswine/">cleverswine</a><br />
</span><br />
<br clear="all" /></p>
<p>This summer is getting off to a slow start, as are our vegetables.  We planted tomatoes, cucumbers, zucchini, various peppers, and carrots.  So far we haven't picked a thing.  We've barely reached the 80s in temperature, and we've had more rain than usual.  However, despite the lack of veggies, I'm personally enjoying the cool summer.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/07/18/slow-gardening/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Moq</title>
		<link>http://blog.cleverswine.net/2011/04/20/simple-moq/</link>
		<comments>http://blog.cleverswine.net/2011/04/20/simple-moq/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 22:40:32 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=690</guid>
		<description><![CDATA[I recently worked on a project in which 90% of its tests were integration tests because the core purpose of the project was to interact with external entities. In an effort create more unit tests, I employed Moq (and lots of refactoring).  This is an example of basic Moq usage. Let's say we have a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently worked on a project in which 90% of its tests were integration tests because the core purpose of the project was to interact with external entities. In an effort create more unit tests, I employed <a title="Moq" href="http://code.google.com/p/moq/" target="_blank">Moq</a> (and lots of refactoring).  This is an example of basic Moq usage.</p>
<p>Let's say we have a repository that gets documents from a database.  The assumption is that a repository interface, IDocumentRepository, is being implemented.</p>
<p>In my case, I found it easier to deserialize results from file, rather than building the results by hand for each mocked response.  I used System.Xml.Serialization to serialize real responses to file.</p>
<p>Let's Moq it...</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var mockRepository <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mock<span style="color: #008000;">&lt;</span>IDocumentRepository<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// if the query is for text/plain documents only, mock a response for that</span>
mockRepository<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">FindDocuments</span><span style="color: #008000;">&#40;</span>
	It<span style="color: #008000;">.</span><span style="color: #008000;">Is</span><span style="color: #008000;">&lt;</span>DocumentQueryParams<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>q <span style="color: #008000;">=&gt;</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">MimeType</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;text/plain&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span>DeSerializeResults<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span>Document<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;TextDocuments.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// use It.IsAny to return the same result regardless of the parameters</span>
mockRepository<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">FindDocuments</span><span style="color: #008000;">&#40;</span>
	It<span style="color: #008000;">.</span><span style="color: #0000FF;">IsAny</span><span style="color: #008000;">&lt;</span>DocumentQueryParams<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span>DeSerializeResults<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span>Document<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;AllDocuments.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/04/20/simple-moq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Favorite Albums (Right Now)</title>
		<link>http://blog.cleverswine.net/2011/04/19/my-favorite-albums-right-now-3/</link>
		<comments>http://blog.cleverswine.net/2011/04/19/my-favorite-albums-right-now-3/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 18:53:08 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Music]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=685</guid>
		<description><![CDATA[This is what I'm listening to... The Record by FEAR This is old school punk from the early 80s, when punk rock was a means of expression and rebellion.  This particular album is humorous and offensive, as punk should be. Gothic Kabbalah by Therion This isn't my favorite Therion album, but I've been listening to [...]]]></description>
			<content:encoded><![CDATA[<p>This is what I'm listening to...</p>
<p><strong><a title="The Record" href="http://www.last.fm/music/Fear/The+Record" target="_blank">The Record</a> by FEAR</strong></p>
<p>This is old school punk from the early 80s, when punk rock was a means of expression and rebellion.  This particular album is humorous and offensive, as punk should be.</p>
<p><strong><a title="Gothic Kabbalah" href="http://www.last.fm/music/Therion/Gothic+Kabbalah" target="_blank">Gothic Kabbalah</a> by Therion</strong></p>
<p>This isn't my favorite Therion album, but I've been listening to it a lot lately because of some catchy songs.  I find it good to write code to because it takes you to another world.</p>
<p><strong><a title="Soviet Kitsch" href="http://www.last.fm/music/Regina+Spektor/Soviet+Kitsch" target="_blank">Soviet Kitsch</a> by Regina Spektor</strong></p>
<p>This is something that's generally outside of my musical tastes, but I like her quirky voice and odd lyrics.  She reminds me of a disturbed version of Tori Amos.</p>
<p><strong><a title="The Other Side of Time" href="http://www.last.fm/music/Mary+Fahl/The+Other+Side+of+Time" target="_blank">The Other Side of Time</a> by Mary Fahl</strong></p>
<p>I  once claimed that Tarja has the greatest female voice, but I actually  meant Mary Fahl.  This album is completely not my style of music  (folk/new age/soft rock), but her voice is irresistible and addictive.</p>
<p><a href="http://www.last.fm/music/Mary+Fahl"><img class="aligncenter size-full wp-image-686" title="Mary Fahl" src="http://blog.cleverswine.net/wp-content/uploads/2011/04/31686605.jpg" alt="Mary Fahl" width="174" height="174" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/04/19/my-favorite-albums-right-now-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Complex XSDs as Embedded Resources</title>
		<link>http://blog.cleverswine.net/2011/03/17/using-complex-xsds-as-embedded-resources/</link>
		<comments>http://blog.cleverswine.net/2011/03/17/using-complex-xsds-as-embedded-resources/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 23:44:43 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=662</guid>
		<description><![CDATA[I was recently tasked with validating XML files against a very complex set of XSD schema files.  This is easily accomplished if your XSD files live on the filesystem, because the .Net xml resolver can find referenced schemas via a relative or absolute Uri.  In my case, the schema files were compiled as embedded resources [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently tasked with validating XML files against a very complex set of XSD schema files.  This is easily accomplished if your XSD files live on the filesystem, because the .Net xml resolver can find referenced schemas via a relative or absolute Uri.  In my case, the schema files were compiled as embedded resources in my project.  As expected, the XML schema loader didn't know how to find referenced schemas - it was likely searching for them  in the path of the running application.  To solve this, I had to help out the schema loader by implementing a custom resolver that knew how to pull schema from the embedded resources.  The code...</p>
<p>Implement a custom XmlUrlResolver to find schema references as embedded resources.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ManifestResourceResolver <span style="color: #008000;">:</span> XmlUrlResolver
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> Assembly _resourceAssembly<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span> _baseNamespaceForReferences<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> ManifestResourceResolver<span style="color: #008000;">&#40;</span>Assembly resourceAssembly, <span style="color: #6666cc; font-weight: bold;">string</span> baseNamespaceForReferences<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        _resourceAssembly <span style="color: #008000;">=</span> resourceAssembly<span style="color: #008000;">;</span>
        _baseNamespaceForReferences <span style="color: #008000;">=</span> baseNamespaceForReferences<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> baseNamespaceForReferences <span style="color: #008000;">:</span> baseNamespaceForReferences <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetEntity<span style="color: #008000;">&#40;</span>Uri absoluteUri, <span style="color: #6666cc; font-weight: bold;">string</span> role, Type ofObjectToReturn<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>absoluteUri<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFile</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var file <span style="color: #008000;">=</span> Path<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFileName</span><span style="color: #008000;">&#40;</span>absoluteUri<span style="color: #008000;">.</span><span style="color: #0000FF;">AbsolutePath</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            var stream <span style="color: #008000;">=</span> _resourceAssembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetManifestResourceStream</span><span style="color: #008000;">&#40;</span>_baseNamespaceForReferences <span style="color: #008000;">+</span> file<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> stream<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Create a XmlSchemaSet, and set the XmlResolver property to the custom resolver implemented above.  Then, load up the main XSD schema.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var xmlSchemaSet <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlSchemaSet<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
xmlSchemaSet<span style="color: #008000;">.</span><span style="color: #0000FF;">XmlResolver</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManifestResourceResolver<span style="color: #008000;">&#40;</span>Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;MyApp.Schemas&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var stream <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCallingAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetManifestResourceStream</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyApp.Schemas.MainSchema.xsd&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var xmlSchema <span style="color: #008000;">=</span> XmlSchema<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>stream, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    xmlSchemaSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>xmlSchema<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Create a XmlReaderSettings object and set the Schemas property to the schema set loaded above.  Lastly, load up an XML file with an XmlReader and read it.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">XmlReaderSettings settings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlReaderSettings<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
settings<span style="color: #008000;">.</span><span style="color: #0000FF;">ValidationType</span> <span style="color: #008000;">=</span> ValidationType<span style="color: #008000;">.</span><span style="color: #0000FF;">Schema</span><span style="color: #008000;">;</span>
settings<span style="color: #008000;">.</span><span style="color: #0000FF;">Schemas</span> <span style="color: #008000;">=</span> xmlSchemaSet<span style="color: #008000;">;</span>
settings<span style="color: #008000;">.</span><span style="color: #0000FF;">ValidationEventHandler</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> ValidationEventHandler <span style="color: #008000;">&#40;</span>ValidationCallBack<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
XmlReader reader <span style="color: #008000;">=</span> XmlReader<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SomeXml.xml&quot;</span>, settings<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The validation callback function will be invoked on each validation error while reading the file with the XmlReader.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ValidationCallBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ValidationEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Validation Error: {0}&quot;</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>References:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/as3tta56%28v=VS.90%29.aspx">Validation Using the XmlSchemaSet</a></li>
<li><a href="http://www.hanselman.com/blog/LoadingXmlSchemaFilesOutOfAssemblyResources.aspx">Loading XmlSchema files out of Assembly Resources</a><br/> (contains some obsoleted code, but it was still helpful)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/03/17/using-complex-xsds-as-embedded-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Local Groceries</title>
		<link>http://blog.cleverswine.net/2011/03/13/local-groceries/</link>
		<comments>http://blog.cleverswine.net/2011/03/13/local-groceries/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 18:32:57 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Food]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=659</guid>
		<description><![CDATA[We did this week's grocery shopping at Food Front in the Hillsdale neighborhood.  They carry many local and organic food products.  And, it may be my imagination, but local eggs fresh from the chicken taste so much better than the mass produced commercial eggs.]]></description>
			<content:encoded><![CDATA[<p>We did this week's grocery shopping at <a title="Food Front" href="http://www.foodfront.coop/" target="_blank">Food Front</a> in the Hillsdale neighborhood.  They carry many local and organic food products.  And, it may be my imagination, but local eggs fresh from the chicken taste so much better than the mass produced commercial eggs.</p>
<p><a href="http://www.flickr.com/photos/cleverswine/5522923837/sizes/o/in/photostream/"><img class="alignnone" title="Eggs" src="http://farm6.static.flickr.com/5296/5522923837_982a77b925_d.jpg" alt="" width="500" height="373" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/03/13/local-groceries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC3 + MongoDB + Autofac</title>
		<link>http://blog.cleverswine.net/2011/03/08/mvc3-mongodb-autofac/</link>
		<comments>http://blog.cleverswine.net/2011/03/08/mvc3-mongodb-autofac/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 03:31:08 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=646</guid>
		<description><![CDATA[I recently posted a brief summary of creating a recipe database.  I decided to expand on that and go into the implementation details of using ASP.NET MVC3 with MongoDB and Autofac.  This code depends on the NoRM driver for MongoDB, Autofac, and MVC3 (and it's dependencies). This is by no means the only (or best) [...]]]></description>
			<content:encoded><![CDATA[<p>I recently posted a <a href="http://blog.cleverswine.net/2011/03/08/a-diet-and-an-application/">brief summary</a> of creating a recipe database.  I decided to expand on that and go into the implementation details of using ASP.NET MVC3 with MongoDB and Autofac.  This code depends on the <a href="https://github.com/atheken/NoRM">NoRM</a> driver for <a href="http://www.mongodb.org/">MongoDB</a>, <a href="http://code.google.com/p/autofac/">Autofac</a>, and <a href="http://www.asp.net/mvc">MVC3</a> (and it's dependencies).  This is by no means the only (or best) way to do things, but this is how I chose to do it.  I've simplified the repository to one method for brevity, but you'd obviously want to add all of the required CRUD operations.</p>
<p>First, we'll want to create an interface for our database session.  The implementation of this interface can be swapped out depending on the backing store.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> ISession <span style="color: #008000;">:</span> IDisposable
<span style="color: #008000;">&#123;</span>
    <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Linq</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IQueryable</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> All<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>, <span style="color: #008000;">new</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Add a MongoDB implementation of ISession.  This implementation will eventually be used in our repository.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MongoSession <span style="color: #008000;">:</span> ISession
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> IMongo _provider<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> MongoSession<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//this looks for a connection string in your Web.config</span>
        <span style="color: #008080; font-style: italic;">//&lt;connectionStrings&gt;</span>
        <span style="color: #008080; font-style: italic;">//  &lt;add name=&quot;db&quot; connectionString=&quot;mongodb://localhost/testdb?strict=true&quot;/&gt;</span>
        <span style="color: #008080; font-style: italic;">//&lt;/connectionStrings&gt;</span>
        _provider <span style="color: #008000;">=</span> Mongo<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;db&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> IQueryable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> All<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>, <span style="color: #008000;">new</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> _provider<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCollection</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsQueryable</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        _provider<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Create an interface for the repository.  This is what the controller will expect to be injected via it's constructor.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IRecipeRepository
<span style="color: #008000;">&#123;</span>
    IEnumerable<span style="color: #008000;">&lt;</span>RecipeCard<span style="color: #008000;">&gt;</span> GetAll<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> page <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, <span style="color: #6666cc; font-weight: bold;">int</span> pageSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Add the IRecipeRepository implementation, which is created with a new()-able type of ISession.  Why didn't I just inject an ISession into the repository?  It's because Mongo sessions are expected to be disposed of after use, as connections are pooled and reused.  Therefore we have to create and dispose of our sessions when using them.  This might require some workarounds if you really did switch repositories (do nothing on Dispose(), etc).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RecipeRepository<span style="color: #008000;">&lt;</span>SessionT<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IRecipeRepository <span style="color: #0600FF; font-weight: bold;">where</span> SessionT <span style="color: #008000;">:</span> ISession, <span style="color: #008000;">new</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>RecipeCard<span style="color: #008000;">&gt;</span> GetAll<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> page <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, <span style="color: #6666cc; font-weight: bold;">int</span> pageSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var session <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SessionT<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">All</span><span style="color: #008000;">&lt;</span>RecipeCard<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OrderByDescending</span><span style="color: #008000;">&#40;</span>r <span style="color: #008000;">=&gt;</span> r<span style="color: #008000;">.</span><span style="color: #0000FF;">UpdatedDate</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Skip</span><span style="color: #008000;">&#40;</span>pageSize <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>page <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span>pageSize<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now it's time set up our IoC container.  We'll do this in the Global.asax.cs.  Here we are registering a recipe repository of type MongoSession such that any controller that expects an IRecipeRepository in the constructor will get this.  We're letting Autofac handle controller creation and dependency injection.  See the <a href="http://code.google.com/p/autofac/wiki/Mvc3Integration">Autofac MVC3 integration page</a> for more details.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span>r <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> RecipeRepository<span style="color: #008000;">&lt;</span>MongoSession<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">As</span><span style="color: #008000;">&lt;</span>IRecipeRepository<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InstancePerLifetimeScope</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    builder<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterControllers</span><span style="color: #008000;">&#40;</span>Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var container <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Build</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    DependencyResolver<span style="color: #008000;">.</span><span style="color: #0000FF;">SetResolver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AutofacDependencyResolver<span style="color: #008000;">&#40;</span>container<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    AreaRegistration<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAllAreas</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    RegisterGlobalFilters<span style="color: #008000;">&#40;</span>GlobalFilters<span style="color: #008000;">.</span><span style="color: #0000FF;">Filters</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    RegisterRoutes<span style="color: #008000;">&#40;</span>RouteTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Routes</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Finally, here is our controller.  Notice we never have to create a repository object - one was injected for us.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> IRecipeRepository recipeRepository<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> RecipesController<span style="color: #008000;">&#40;</span>IRecipeRepository recipeRepository<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">recipeRepository</span> <span style="color: #008000;">=</span> recipeRepository<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> ViewResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span>recipeRepository<span style="color: #008000;">.</span><span style="color: #0000FF;">GetAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>That's it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/03/08/mvc3-mongodb-autofac/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Diet and an Application</title>
		<link>http://blog.cleverswine.net/2011/03/08/a-diet-and-an-application/</link>
		<comments>http://blog.cleverswine.net/2011/03/08/a-diet-and-an-application/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 01:13:27 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=639</guid>
		<description><![CDATA[I've started on the Paleo Diet, which, of course, means I have to create a software application to go along with it. I decided this was a good time to give MVC3 and MongoDB a test drive.  After diving in, I realized that I could benefit from some dependency injection via MVC3's DependencyResolver.  I've been [...]]]></description>
			<content:encoded><![CDATA[<p>I've started on the <a title="Paleo Lifestyle" href="http://paleodietlifestyle.com/" target="_blank">Paleo Diet</a>, which, of course, means I have to create a software application to go along with it.</p>
<p>I decided this was a good time to give <a title="Asp.net MVC" href="http://www.asp.net/mvc" target="_blank">MVC3</a> and <a title="MongoDB" href="http://www.mongodb.org/" target="_blank">MongoDB</a> a test drive.  After diving in, I realized that I could benefit from some dependency injection via MVC3's DependencyResolver.  I've been wanting to learn <a title="Autofac" href="http://code.google.com/p/autofac/" target="_blank">Autofac</a>, so I threw that in as the container.  This was sweet, as it injected my repository implementations into my controllers with very little setup.</p>
<p>At this point, I've learned all about the Razor view engine, DI with Autofac and DependencyResolver, MongoDB with the NoRM driver, jQuery, jQuery-ui, and some other MVC3 goodness.</p>
<p>For various reasons, including the fact that my web hosting provider is a Linux/non-ASP.NET provider, I ported the whole thing to Rails/MySql.  I'm glad I went through the MVC3 process, because I learned a lot, but in the end, Rails is just simpler.  As for MongoDB, and NoSQL in general, I think it's best served as an edge case storage medium for fast read operations.  Data is relational.</p>
<p>Alas, here is a screenshot of the recipe database.  I'll be adding lots of features, including menus and weight management.  You can't see it here, but it has a cool tag cloud feature and theme switching.</p>
<p><a href="http://blog.cleverswine.net/wp-content/uploads/2011/03/recipes.png"><img class="alignnone size-medium wp-image-640" title="recipes" src="http://blog.cleverswine.net/wp-content/uploads/2011/03/recipes-300x139.png" alt="Recipes" width="300" height="139" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2011/03/08/a-diet-and-an-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>My Favorite Albums (Right Now)</title>
		<link>http://blog.cleverswine.net/2010/12/10/my-favorite-albums-right-now-2/</link>
		<comments>http://blog.cleverswine.net/2010/12/10/my-favorite-albums-right-now-2/#comments</comments>
		<pubDate>Sat, 11 Dec 2010 00:00:13 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=635</guid>
		<description><![CDATA[These are my favorite albums, in no particular order... My Winter Storm by Tarja Tarja used to sing with Nightwish, but has recently gone solo.  She has one of the greatest female voices that I've ever heard. Beautiful Death by Bella Morte This is still one of the best albums I've heard in the past [...]]]></description>
			<content:encoded><![CDATA[<p>These are my favorite albums, in no particular order...</p>
<p><strong><a href="http://www.last.fm/music/Tarja/My+Winter+Storm" target="_blank">My Winter Storm</a> by Tarja</strong></p>
<p>Tarja used to sing with <a href="http://www.last.fm/music/Nightwish" target="_blank">Nightwish</a>, but has recently gone solo.  She has one of the greatest female voices that I've ever heard.</p>
<p><strong><a href="http://www.last.fm/music/Bella+Morte/Beautiful+Death" target="_blank">Beautiful Death</a> by Bella Morte</strong></p>
<p>This is still one of the best albums I've heard in the past 10 years or so.</p>
<p><strong><a href="http://www.last.fm/music/My+Chemical+Romance/Danger+Days%3A+The+True+Lives+of+the+Fabulous+Killjoys" target="_blank">Danger Days: The True Lives of the Fabulous Killjoys</a> by My Chemical Romace</strong></p>
<p>I was pleasantly surprised by this new album from MCR.  I didn't think it would be possible to live up to <em>The Black Parade</em>, but they did.</p>
<p><strong><a href="http://www.last.fm/music/Palast+Orchester+mit+Max+Raabe/Kein+Schwein+Ruft+Mich+An" target="_blank">Kein Schwein Ruft Mich An</a> by Palast Orchester mit Max Raabe</strong></p>
<p>This is some great 20s/30s era German music.  It's very upbeat and probably humorous (I don't understand German).</p>
<p><strong><a href="http://www.last.fm/music/Faith+No+More/King+for+a+Day%2C+Fool+for+a+Lifetime" target="_self">King for a Day, Fool for a Lifetime</a> by Faith No More</strong></p>
<p>This is one of the most underrated, overlooked albums of the 90s.  This is Faith No More at their best, in my opinion.</p>
<p><strong><a href="http://www.last.fm/music/Ego+Likeness/The+Order+of+the+Reptile" target="_blank">The Order of the Reptile</a> by Ego Likeness</strong></p>
<p>I saw Ego Likeness when they opened up for <a href="http://www.last.fm/music/The+Cr%C3%BCxshadows" target="_blank">The Cruxshadows</a>.  It's some good, hard-hitting electronic/industrial/dance music.</p>
<p style="text-align: center;"><a href="http://blog.cleverswine.net/wp-content/uploads/2010/12/33155303.jpg"><img class="size-full wp-image-636 aligncenter" title="Ego Likeness" src="http://blog.cleverswine.net/wp-content/uploads/2010/12/33155303.jpg" alt="Ego Likeness" width="174" height="174" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2010/12/10/my-favorite-albums-right-now-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fall is (almost) here</title>
		<link>http://blog.cleverswine.net/2010/10/07/fall-is-almost-here/</link>
		<comments>http://blog.cleverswine.net/2010/10/07/fall-is-almost-here/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 21:07:02 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Weather]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=632</guid>
		<description><![CDATA[Summer came and went.  It was a mild summer, but I'm happy to feel cooler weather.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/cleverswine/4096314903/"><img class="alignnone" title="Fall 2009" src="http://farm3.static.flickr.com/2724/4096314903_1d0620f6d4_m_d.jpg" alt="Fall 2009" width="240" height="161" /></a></p>
<p>Summer came and went.  It was a mild summer, but I'm happy to feel cooler weather.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2010/10/07/fall-is-almost-here/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring is (almost) here</title>
		<link>http://blog.cleverswine.net/2010/05/17/spring-is-almost-here/</link>
		<comments>http://blog.cleverswine.net/2010/05/17/spring-is-almost-here/#comments</comments>
		<pubDate>Mon, 17 May 2010 22:06:30 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Nature]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Weather]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/2010/05/17/spring-is-almost-here/</guid>
		<description><![CDATA[Spring was here for a few days, but the cool, wet weather is returning this week. We planted some veggies in the few spots where we get sunlight. Hopefully we'll get something out of them.]]></description>
			<content:encoded><![CDATA[<p><a title="photo sharing" href="http://www.flickr.com/photos/cleverswine/4610283460/"><img style="border: solid 2px #000000;" src="http://farm4.static.flickr.com/3407/4610283460_c09f2d3ef5_m.jpg" alt="" /></a></p>
<p>Spring was here for a few days, but the cool, wet weather is returning this week.  We planted some veggies in the few spots where we get sunlight.  Hopefully we'll get something out of them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2010/05/17/spring-is-almost-here/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Favorite Albums (Right Now)</title>
		<link>http://blog.cleverswine.net/2010/03/17/my-favorite-albums-right-now/</link>
		<comments>http://blog.cleverswine.net/2010/03/17/my-favorite-albums-right-now/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 17:09:56 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[lastfm]]></category>
		<category><![CDATA[Music]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=621</guid>
		<description><![CDATA[These are the albums that I've been enjoying the most lately. Beautiful Death by Bella Morte Years of Refusal by Morrissey Walking with Strangers by The Birthday Massacre My Winter Storm by Tarja Oceanborn by Nightwish Banned in D.C. by Bad Brains]]></description>
			<content:encoded><![CDATA[<p>These are the albums that I've been enjoying the most lately.</p>
<ol>
<li><a title="Beautiful Death" href="http://www.last.fm/music/Bella+Morte/Beautiful+Death" target="_blank">Beautiful Death</a> by <strong><a title="Bella Morte" href="http://www.last.fm/music/Bella+Morte" target="_blank">Bella Morte</a></strong></li>
<li><a title="Years of Refusal" href="http://www.last.fm/music/Morrissey/Years+Of+Refusal" target="_blank">Years of Refusal</a> by <strong><a title="Morrissey" href="http://www.last.fm/music/Morrissey" target="_blank">Morrissey</a></strong></li>
<li><a title="Walking with Strangers" href="http://www.last.fm/music/The+Birthday+Massacre/Walking+With+Strangers" target="_blank">Walking with Strangers</a> by <strong><a title="The Birthday Massacre" href="http://www.last.fm/music/The+Birthday+Massacre" target="_blank">The Birthday M</a><a title="The Birthday Massacre" href="http://www.last.fm/music/The+Birthday+Massacre" target="_blank">assacre</a></strong></li>
<li><a title="My Winter Storm" href="http://www.last.fm/music/Tarja/My+Winter+Storm" target="_blank">My Winter Storm</a> by <strong><a title="Tarja" href="http://www.last.fm/music/Tarja" target="_blank">Tarja</a></strong></li>
<li><a title="Oceanborn" href="http://www.last.fm/music/Nightwish/Oceanborn" target="_blank">Oceanborn</a> by <strong><a title="Nightwish" href="http://www.last.fm/music/Nightwish" target="_blank">Nightwish</a></strong></li>
<li><a title="Banned in D.C." href="http://www.last.fm/music/Bad+Brains/Banned+in+D.C.%3A+Bad+Brains+Greatest+Riffs" target="_blank">Banned in D.C.</a> by <strong><a title="Bad Brains" href="http://www.last.fm/music/Bad+Brains" target="_blank">Bad Brains</a></strong></li>
</ol>
<p style="text-align: center;"><a href="http://www.last.fm/music/Bella+Morte/Beautiful+Death"><img class="aligncenter" title="Bella Morte - Beautiful Death" src="http://userserve-ak.last.fm/serve/174s/14788965.gif" alt="" width="174" height="174" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2010/03/17/my-favorite-albums-right-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Home Office</title>
		<link>http://blog.cleverswine.net/2010/01/27/new-home-office/</link>
		<comments>http://blog.cleverswine.net/2010/01/27/new-home-office/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 02:08:09 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[furniture]]></category>
		<category><![CDATA[House]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[shopping]]></category>

		<guid isPermaLink="false">http://blog.cleverswine.net/?p=619</guid>
		<description><![CDATA[After a few weeks of shopping in and around Portland, we finally landed some nice office furniture at a reasonable price.  City Liquidators turns out to be the place for furniture (and other things ranging from tampons to sofas).  Next up, we'll be painting the room.]]></description>
			<content:encoded><![CDATA[<p>After a few weeks of shopping in and around Portland, we finally landed some nice office furniture at a reasonable price.  <a title="City Liquidators" href="http://cityliquidators.com/" target="_blank">City Liquidators</a> turns out to be the place for furniture (and other things ranging from tampons to sofas).  Next up, we'll be painting the room.</p>
<p><img class="alignnone" title="furniture" src="http://farm5.static.flickr.com/4015/4306216721_d9de598e7b_m_d.jpg" alt="" width="240" height="179" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cleverswine.net/2010/01/27/new-home-office/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

