<?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>jhovgaard.net</title>
	<atom:link href="http://jhovgaard.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://jhovgaard.net</link>
	<description>Personal blog for professional web developer Jonas Hovgaard</description>
	<lastBuildDate>Wed, 20 Feb 2013 10:50:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Manage IIS from the browser</title>
		<link>http://jhovgaard.net/manage-iis-from-browser/</link>
		<comments>http://jhovgaard.net/manage-iis-from-browser/#comments</comments>
		<pubDate>Mon, 18 Feb 2013 20:36:31 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[servant]]></category>

		<guid isPermaLink="false">http://jhovgaard.net/?p=739</guid>
		<description><![CDATA[In this post I describe how you can use Servant for IIS to manage your IIS from any web browser.]]></description>
				<content:encoded><![CDATA[<p><a href="http://servant.io" target="_blank"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_logo_square-300x300.png" alt="servant_logo_square" width="300" height="300" class="alignleft size-medium wp-image-740" /></a> I&#8217;m very proud to introduce you to my own very first product: <a href="http://servant.io" target="_blank">Servant for IIS</a>. I daily administrate around 10 webservers. Each of these servers is forcing me to remote connect to the server or setup remote administration and install and configure both the client and server software. With different workstations I always end up hassling with VPN settings, client software etc. Personally I&#8217;ve never been a fan of &#8220;inetmgr&#8221;, so I decided to stop whining and start contributing to my favorite webserver.</p>
<p>Servant for IIS is a completely hassle-free way to administrate your IIS directly from your web browser.</p>
<p>I&#8217;ll start by showing you how to install it and then demonstrate its basic features.</p>
<div style="clear: both;">
</div>
<h1>Installation</h1>
<ul>
<li>Go to http://servant.io and click the big green button to download the zip file.</li>
<li>Login and copy the zip file to the server you want to install on.</li>
<li>Extract all files from the zip to a desired folder. I&#8217;ll go for <strong>C:\servant-1.0.1</strong>.</li>
<li>Jump into the installation folder and double click the file <strong>Install Servant Service.bat</strong>. Servant will now install it self as a Windows Service called <em>Servant for IIS</em>. If you want to uninstall it again, simply run the file <strong>Uninstall Servant Service.bat</strong>. If this gives you any problems, try running &#8220;Servant.Server.exe install&#8221; from a command prompt as Administrator.</li>
<li>A new browser window will automatically open. This is the installation wizard:<br /><a href="http://jhovgaard.net/wp-content/uploads/2013/02/servant_install_wizard.png"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_install_wizard-300x244.png" alt="servant_install_wizard" width="300" height="244" class="alignleft size-medium wp-image-747" /></a> <em>URL of Servant</em> is the URL you want Servant to be accessible from.<br /><em>Username</em> and <em>password</em> is the credentials you want to use to authenticate yourself. </li>
</ul>
<div style="clear: both;">
</div>
<ul>
<li>When all fields is entered click the green button. This will save your configuration.</li>
<li>The next screen leaves you only one button to click on: <em>Click here to login now</em></li>
<li>Servant will now ask for your credentials and log you in &#8211; That&#8217;s it! You can now log out of your server and access Servant from your desktop machine.</li>
</ul>
<h1>The home screen</h1>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/02/servant_home_screen.png"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_home_screen-300x224.png" alt="servant_home_screen" width="300" height="224" class="alignleft size-medium wp-image-754" /></a> The home screen of Servant gives you a quick overview of the latest unhandled exceptions thrown by any of your sites. Clicking on the exception will redirect you to the details/stack trace of the selected exception (we will get to exceptions later).</p>
<p>Notice that all of your sites is available by clicking the Sites item in the menu to the left. If you&#8217;re experiencing bugs, want new features or anything else there&#8217;s a feedback button to the right of the screen.</p>
<div style="clear: both;">
</div>
<h1>Create new site</h1>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/02/servant_create_new_site.png"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_create_new_site-300x188.png" alt="servant_create_new_site" width="300" height="188" class="alignleft size-medium wp-image-760" /></a> By clicking on <strong>Create new</strong> under the menu&#8217;s site section you&#8217;ll be able to create a new website. Simply fill out all of the fields like when you create sites in the IIS Manager, and Servant will create the site and redirect you to the site&#8217;s settings page. Simple as that!</p>
<div style="clear: both;">
</div>
<h1>Site settings</h1>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/02/servant_site_settings.png"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_site_settings-300x251.png" alt="servant_site_settings" width="300" height="251" class="alignleft size-medium wp-image-764" /></a> The site settings page give you direct access to the basic settings of a website.</p>
<p>You can change name, bindings, site path and application pool. Also you can <strong>start/stop a site</strong>, <strong>restart the site</strong> or <strong>recycle the application pool</strong> it&#8217;s running on. Of course you can also <strong>delete the site</strong>.</p>
<p>From this screen you can go to the errors screen from the top menu.</p>
<div style="clear: both;">
</div>
<h1>Site errors</h1>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/02/servant_site_errors.png"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_site_errors-300x197.png" alt="servant_site_errors" width="300" height="197" class="alignleft size-medium wp-image-767" /></a></p>
<p>The site errors screen is powerful tool that quickly shows you all unhandled exceptions thrown by the selected website.</p>
<p>The exception column is the message of the exception.<br />The timestamp column is the time since the exception thrown. By hovering over the text you&#8217;ll get the UTC timestamp.</p>
<p>By clicking on the exception message you&#8217;ll be redirected to the error details screen.</p>
<div style="clear: both;">
</div>
<h1>Error details</h1>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/02/servant_error_details.png"><img src="http://jhovgaard.net/wp-content/uploads/2013/02/servant_error_details-300x197.png" alt="servant_error_details" width="300" height="197" class="alignleft size-medium wp-image-771" /></a></p>
<p>The error details screen gives you all the details you need to fix your bug. You&#8217;ll be able to see both the stack trace and the thread information. There&#8217;s also a direct link to the page that threw the exception.</p>
<div style="clear: both;">
</div>
<p>That&#8217;s all for now. Servant is not released as open source. There&#8217;s a commercial edition on its way including awesome features like error monitoring, git publish and more.</p>
<p>I hope you like my product! I&#8217;ve extremely excited and would love to hear your feedback.</p>
<p><strong>So please tell me, will you try out Servant on your servers?</strong></p>
<p>Have a great day!</p>
<p>Jonas</p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/manage-iis-from-browser/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Deploying: Add Git support to your IIS server</title>
		<link>http://jhovgaard.net/deploying-git-support-iis-server/</link>
		<comments>http://jhovgaard.net/deploying-git-support-iis-server/#comments</comments>
		<pubDate>Thu, 12 Jul 2012 18:11:37 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[deploying]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[iis]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=412</guid>
		<description><![CDATA[Using Kudu you can enable .NET deploying via Git on your local IIS.]]></description>
				<content:encoded><![CDATA[<h1>Have you heard?</h1>
<p>Recently <a href="http://www.meetwindowsazure.com/">Microsoft bragged about the new and much more awesome Azure</a>.</p>
<p>One of the more cool things they announced is the ability to deploy your applications directly from your git repository (using <strong>git push</strong>) and straight to your Azure hosted website. It loads your master branch, compiles everything and upload the whole thing automatically. Pretty sick!</p>
<p>Now you may think <em>&#8220;Cool story bro, but I host my own IIS!&#8221;</em> &#8211; Well, no problem! <a href="http://weblogs.asp.net/davidfowler/">David Fowler</a> and <a href="http://blog.davidebbo.com/">David Ebbo</a> are the creators of Project Kudu &#8211; the engine behind automated deployment via Git on Azure. These guys did an awesome job by not limiting this project to Azure, making us able to use it on IIS. Now time to grok it!</p>
<h1>Clone the repo</h1>
<p>Point your browser to <a href="https://github.com/projectkudu/kudu">https://github.com/projectkudu/kudu</a> and <a href="https://help.github.com/articles/fork-a-repo">clone the master</a> down to your disk.</p>
<h1>Build n&#8217; start!</h1>
<blockquote>
<p>Below I&#8217;ll assume that you have IIS 7.x installed on your local machine. If you want to go live with Kudu, simply deploy the <strong>Kudu.Web</strong> project to a site on your production/online IIS as usual.</p>
</blockquote>
<p>Now open up the <strong>Kudu.sln</strong> in Visual Studio. Mark the <strong>Kudu.Web</strong> project as active (makes it bold) and hit CTRL+F5.</p>
<p>If everything went well you should now see the Kudu dashboard, looking like this:</p>
<p><a href="http://jhovgaard.net/deploying-add-git-support-to-your-iis-server/kudu-dashboard/" rel="attachment wp-att-532"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/kudu-dashboard.png" alt="kudu-dashboard" width="1073" height="913" class="alignnone size-full wp-image-532" /></a></p>
<h1>Create your first Git enabled site</h1>
<p>In your Kudu dashboard:</p>
<ul>
<li>Click the &#8220;Create application&#8221; link in the top menu.</li>
<li>Enter a preferred name for the IIS site (I.e. &#8220;jhovgaardnet&#8221;).</li>
<li>Click the blue &#8220;Create application&#8221; button.</li>
</ul>
<p>Kudu is now creating a brand new IIS site directly on your IIS (!!) and showing you some geeky stuff:</p>
<p><a href="http://jhovgaard.net/deploying-add-git-support-to-your-iis-server/kudu-created/" rel="attachment wp-att-522"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/kudu-created.png" alt="kudu-created" width="556" height="549" class="alignnone size-full wp-image-522" /></a></p>
<p>It tells the URL to the Git remote we can push apps to, the URL to the site itself and the service URL which actually is the Git remote from before.</p>
<p>Let&#8217;s take a quick look in IIS to see what happend:</p>
<p><a href="http://jhovgaard.net/deploying-add-git-support-to-your-iis-server/kudu-iis/" rel="attachment wp-att-552"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/kudu-iis.png" alt="kudu-iis" width="760" height="564" class="alignnone size-full wp-image-552" /></a></p>
<p>So Kudu is creating 2 sites with a kudu_ prefix. The first is the main site, the one hosting your application. The second is the &#8220;git remote site&#8221;/service site. Both sites is running on the same application pool.</p>
<p>If we go to the application URL we see a default screen:</p>
<p><a href="http://jhovgaard.net/deploying-add-git-support-to-your-iis-server/kudu-iis-default-site/" rel="attachment wp-att-562"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/kudu-iis-default-site.png" alt="kudu-iis-default-site" width="575" height="350" class="alignnone size-full wp-image-562" /></a></p>
<h1>Let&#8217;s push it!</h1>
<p>Alright, let&#8217;s assume that you want to push a FunnelWeb blog like mine to the newly created site. First clone your fork of FunnelWeb (or whatever application you like) to your disk. Next open up a git shell (if you&#8217;re using <a href="http://windows.github.com/">Github for Windows</a>, go to the repository, select <strong>tools</strong> then <strong>open a shell here</strong>). We now need to add a new remote, using the Git URL we got from Kudu. In my case I typed this:</p>
<pre><code>git remote add kudu http://localhost:63185/jhovgaardnet.git
</code></pre>
<p>Git now knows about the repository located on our local IIS, making us able to do a simple push of our <strong>master</strong> like this:</p>
<pre><code>git push kudu master
</code></pre>
<p>Awesome! If you didn&#8217;t mess it up, this is what you should see (not necessary the dependency installations of course):</p>
<p><img src="http://jhovgaard.net/wp-content/uploads/2013/01/kudu-deployed.png" alt="kudu-deployed" width="933" height="857" class="alignnone size-full wp-image-542" /></p>
<p>Now updating the application URL shows my fully deployed blog, just as expected. Yes, it&#8217;s freaking awesome!</p>
<h1>Conclusion</h1>
<p>The next thing for you will be to deploy Kudu on your production IIS, setting up sites as Kudu sites, setting correct bindings, etc.</p>
<p>I really think Kudu is swell and I hope to see some interesting forks of Kudu that moves it closer to a continuous integration system with <a href="https://github.com/projectkudu/kudu/tree/githubhook">Github hooks</a>, unit testing abilities, etc. TeamCity is cool, but it&#8217;s expensive and heavy. We need a slick open source .NET alternative and I hope Kudu is the beginning!</p>
<p>What do you think? Can you see the possibilities? Are you going to use Kudu?</p>
<p>Thanks for reading guys!</p>
<p>If you read so far, <a href="http://twitter.com/#!/jhovgaard">why don&#8217;t you follow me on Twitter? <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </a></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http://jhovgaard.net/deploying-git-support-iis-server" target="_blank"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjhovgaard.net%2fdeploying-git-support-iis-server&#038;bgcolor=0066FF" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/deploying-git-support-iis-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2012: Web Project is incompatible</title>
		<link>http://jhovgaard.net/visual-studio-2012-web-project-is-incompatible/</link>
		<comments>http://jhovgaard.net/visual-studio-2012-web-project-is-incompatible/#comments</comments>
		<pubDate>Tue, 12 Jun 2012 18:14:23 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aspnetmvc]]></category>
		<category><![CDATA[visualstudio2012]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=432</guid>
		<description><![CDATA[Loading a MVC2 web project for the first time in Visual Studio 2012 says "This project is incompatible with the current edition of Visual Studio".]]></description>
				<content:encoded><![CDATA[<h2>The problem</h2>
<p>So you just installed Visual Studio 2012 and hit your .sln file for the very first time. Visual Studio boots up and you feel excited. Then bang, you see something like this:</p>
<p><a href="http://jhovgaard.net/visual-studio-2012-web-project-is-incompatible/vs2012bug/" rel="attachment wp-att-512"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/vs2012bug.png" alt="vs2012bug" width="298" height="113" class="alignnone size-full wp-image-512" /></a></p>
<p>I&#8217;ve also seen errors like these:</p>
<ul>
<li>The application which this project type is based on was not found. </li>
<li>This project is incompatible with this version of visual studio 2012.</li>
<li>This project is incompatible with the current edition of Visual Studio.</li>
<li>Incompatible. The project will load in the background.</li>
<li>Load failed. The project requires user input.</li>
</ul>
<p>The reason is that Visual Studio 2012 doesn&#8217;t support ASP.NET MVC 1 or 2.</p>
<h2>The solution</h2>
<p>Download and run the <a href="http://aspnet.codeplex.com/releases/view/59008">ASP.NET MVC 3 Application Upgrader</a> on your solution. After doing that you should be able to open your solution.</p>
<p>If your project isn&#8217;t using MVC or you can&#8217;t get the upgrader to run, try <a href="http://weblogs.asp.net/leftslipper/archive/2009/01/20/opening-an-asp-net-mvc-project-without-having-asp-net-mvc-installed-the-project-type-is-not-supported-by-this-installation.aspx">following Eilon&#8217;s post</a>.</p>
<p><strong>Thanks to Eilon for pointing out the correct solution in comments.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/visual-studio-2012-web-project-is-incompatible/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How I stopped writing awesome code</title>
		<link>http://jhovgaard.net/how-i-stopped-writing-awesome-code/</link>
		<comments>http://jhovgaard.net/how-i-stopped-writing-awesome-code/#comments</comments>
		<pubDate>Sat, 09 Jun 2012 18:05:36 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best-practice]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=322</guid>
		<description><![CDATA[I don't write unit tests, interfaces, use IOC or heavy ORMs. Yet I'm not a brogrammer.]]></description>
				<content:encoded><![CDATA[<p>If writing awesome code is using all the best practices I can find, writing interfaces, unit tests and using top notch IoC containers to control my repositories and services all over my application&#8217;s different layers &#8211; Then I&#8217;m not writing awesome code at all!</p>
<p>I&#8217;ve been that guy, the one writing the awesome code, but I stopped. I&#8217;m not awesome any more. Instead, I&#8217;m productive, I&#8217;m so damn productive!</p>
<h2>I don&#8217;t write unit tests</h2>
<p><a href="http://www.urbandictionary.com/zoom.php?imageid=46380">ZOMG</a> you may think. You are right, I lose so much safety. I don&#8217;t know if I break other parts of my code if I change something.</p>
<p>I&#8217;ve spent so much time writing all of these tests, even before I ever wrote the code to test (that should be a &#8220;wtf&#8221;), and still my boss complained that functions suddenly didn&#8217;t worked any more.</p>
<p>To make my boss (or your customers) happy again I started investigating what I needed to make sure worked. The answer was the result in the browser. In my test scenario I found out that the important things for my boss was to be sure that these functions worked flawless:</p>
<ul>
<li>Creating a new profile</li>
<li>Logging into the new profile</li>
<li>Receive welcome message on first login (that was a visible div) </li>
<li>Log out </li>
<li>Login again </li>
<li>Delete profile </li>
<li>Not being able to login after deletion </li>
<li>And everything else you write unit tests for.</li>
</ul>
<p>My boss or your customers don&#8217;t care about the status of our unit tests. They look at the result in the browser and so should I.</p>
<p>To overcome this challenge, I downloaded <a href="http://seleniumhq.org/download/">the Selenium IDE for Firefox</a>. Selenium is neat tool that enables you to run a list of commands directly in a browser. It gives you a list of all failed and succeeded tests (yeah, like your current unit tests). The big ass difference here is that these results is based on the end result (the HTML returned), not something techy stuff going on even before the browser.</p>
<p>I don&#8217;t wanna teach how to use Selenium (spam me in comments if you want me to), I just want to point out that <strong>my tests tells me what will make my boss sad, and your test tells you what code that breaks</strong>. If you don&#8217;t agree, please comment &#8211; that&#8217;s the purpose of this post <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2>I don&#8217;t do interfaces or use IOC</h2>
<p>This started because of the lost connection between my concrete class and the place I ended when I hit F12 in Visual Studio (Go to declaration). This meant that not even Visual Studio understood what class my <strong>IUserService</strong> actually was. How could that be? What is the advantage? In my personal scenario (creating around 20-30 apps every year), I did it to be able to change the concrete class later on. The funny fact is that I never ever did this, except for one time.</p>
<p>I agree, it&#8217;s absolutely awesome that I can change the concrete type or lifecycle with ease, but for gods sake, I don&#8217;t even dare to count the hours I have spent creating these abstractions, making me able to do something <strong>I never freaking do</strong>.</p>
<p>Oh, I forgot something. I did spent a lot of time writing interfaces (yeah okay, Resharper did it, but was annoying when I created new methods). But also I spent hours trying to understand what Ninject or StructureMap did with my classes. When I finally understood, I handed over the application to the next developer who also was forced to spent time understanding some IOC.</p>
<p>How hard could it be to initialize every class like this:</p>
<pre><code>var userService = new UserService();
</code></pre>
<p>I could even create my classes as static, making them singletons or whatever to each individual class. My co-programmers could understand what was going on immediately, without finding and understanding my <strong>AwesomeIoc.Init()</strong> method.</p>
<p>Sure, again I lose some functionality and safety but seriously, programming is like Diablo 3 &#8211; You don&#8217;t need armor and life, if you kill in one hit!</p>
<p><em>For non gamers: I mean you don&#8217;t need all that functionality, if your productivity gets much higher without it.</em></p>
<h2>I don&#8217;t use regular ORMs</h2>
<p>One day I was struggling with NHibernate, trying to make it send the correct query to my SQL server (not MySQL, just mine). Funny thing was &#8211; I knew what it should sent. So one thing was obvious &#8211; I didn&#8217;t used NHibernate to help me write complex SQL queries &#8211; I&#8217;m no brogrammer, so I know how to write those magic strings!</p>
<p>So why did I use it? The answer was: to map the data from my database to my &#8220;domain&#8221; classes (I hate that word, it&#8217;s POCOs). Also I used it to track all changes to the POCO&#8217;s properties.</p>
<p>I started searching Github for better alternatives and I found <a href="https://github.com/markrendle/Simple.Data">Simple.Data</a> and <a href="http://code.google.com/p/dapper-dot-net/">Dapper</a>. This is extremely simple ORMs. Dapper is the most simple, letting me write true SQL directly in C#, and gives me the results as POCOs. Simple.Data is a bit more complex. You don&#8217;t write SQL directly, but use chained lamba, to instruct how to query.</p>
<p>Whether I use <a href="https://github.com/markrendle/Simple.Data">Simple.Data</a> or <a href="http://code.google.com/p/dapper-dot-net/">Dapper</a> I no more struggle with things I already know. I&#8217;m not afraid of relations and I don&#8217;t have to open a Profiler to find out what queries my ORM sends to the SQL server.</p>
<p>Again, with thoughts back to Diablo 3, I gained more damage, but lost some armor. However, my accumulated damage (meaning productivity) is now so high, I just kill it all instantly!</p>
<h2>I do everything to make things more simple</h2>
<p>I found out that simple code makes me smile. It makes me able to create new functions extremely fast, without hitting &#8220;walls&#8221;. So whenever I write code, I always try to keep it simple. Not <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>, but simple! If some code gets more complicated when <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRYing</a> it, I don&#8217;t care about copy pasting (however, most of the time I just rewrite the DRY code, making it simpler).</p>
<p>I don&#8217;t use Microsoft&#8217;s big mother MVC framework, but instead <a href="http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1">stick with minimalistic and simple frameworks like NancyFX</a>. Whatever I do, I just try to do it more simple.</p>
<h2>The result is the conclusion</h2>
<p>My personal result of doing all of this is productivity and better products. I can&#8217;t tell if I did it all wrong, and that&#8217;s why I&#8217;m writing better code now, but I truly believe that I&#8217;m not alone. In fact I think that most of us regular web developers, tend to do the same &#8220;mistakes&#8221; as I did.</p>
<p>Please, let me hear your thoughts. Do you think I&#8217;m just a noob who doesn&#8217;t understand shit? Is it all too &#8220;brogrammerish&#8221;, or do you actually agree with me? Whatever the reason, thanks for your comment!</p>
<p>If you read so far, <a href="http://twitter.com/#!/jhovgaard">why don&#8217;t you follow me on Twitter? <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </a></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http://jhovgaard.net/how-i-stopped-writing-awesome-code" target="_blank"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjhovgaard.net%2fhow-i-stopped-writing-awesome-code&#038;bgcolor=0066FF" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<h2>Update 13th June 2012</h2>
<p>Because of the big attention this post got, I wanna point out a few things:</p>
<p>I agree, the post was meant to provoke. However the post wasn&#8217;t written to flame unit tests. They are awesome and extremely important on large projects with a bigger lifespan. However, I believe they aren&#8217;t always necessary on smaller projects. The intention with this post is to make us all remember to be critical to our tools and remember that &#8220;best practices&#8221; can&#8217;t be best in all situations.</p>
<p>The post is targeted to the &#8220;average&#8221; developer (those we often forget), who&#8217;s responsible for developing the smaller applications, not necessary visible to the public, but extremely important for the right companies.</p>
<p>I do use TDD, I do use interfaces and IOCs, I do use complex ORMs and big frameworks, but only, and only when I really need it (I kinda try to reduce the noise in my apps). Like said in the comments: It&#8217;s a question about choosing the right tools for the job.</p>
<p>Oh btw, to avoid further flaming &#8211; Yes, you&#8217;re right, I&#8217;m a big noob who writes spaghetti code that isn&#8217;t maintainable at all.</sarcasm></p>
<h2>Update 12th July 2012</h2>
<p>A few people have responded via blog post. Check em&#8217; out here:</p>
<ul>
<li><a href="http://endlessobsession.com/blog/should-you-write-awesome-code">Should you write awesome code?</a></li>
<li><a href="http://www.codalicio.us/2012/06/why-would-i-write-unit-tests-when-i-get.html">Why Would I Write Unit Tests When I Get Paid by the Hour?</a></li>
<li><a href="http://engineersjourney.wordpress.com/2012/06/13/food-for-thought/">Food for thought</a></li>
</ul>
<p>Thanks for joining the discussion guys <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/how-i-stopped-writing-awesome-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>From ASP.NET MVC to Nancy &#8211; Part 3</title>
		<link>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-3/</link>
		<comments>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-3/#comments</comments>
		<pubDate>Thu, 23 Feb 2012 19:02:12 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aspnetmvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nancy]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=302</guid>
		<description><![CDATA[In this third part we take a look at dependency injection in Nancy using the built-in IoC container TinyIoC.]]></description>
				<content:encoded><![CDATA[<p><a href="http://jhovgaard.net/from-asp-net-mvc-to-nancy-part-3/nancylogo-transparent/" rel="attachment wp-att-572"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/nancylogo-transparent.png" alt="nancylogo-transparent" width="256" height="287" class="alignleft size-full wp-image-572" /></a></p>
<blockquote>
<p>This is a series of posts. If you just dropped into this article, you really should <a href="http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1">start by reading part 1</a>.</p>
</blockquote>
<p>Wauw! Nancy is really the thing at the time! Part 1 and 2 have only been online for 5 days and you guys already made 1500 pageviews. I&#8217;m impressed! Unfortunately I haven&#8217;t recieved much feedback. Please make a comment, <a href="http://twitter.com/jhovgaard">hit me on Twitter</a> or email (j at jhovgaard dot dk). I need that feedback to make better posts. Thanks! <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Today&#8217;s Nancy topic is a bit more complex. We&#8217;re going to work a bit with Dependency Injection. Whether you&#8217;re an experienced injector, or you think this Dependency Injection is just another buzzword really doesn&#8217;t matter. This stuff will hopefully be so easy to understand that everybody gets it.</p>
<h2>1. Installing TinyIoC on our Nancy project</h2>
<p>Congratulations! You&#8217;re successfully installed TinyIoC! Yes, it was already included in Nancy, but let&#8217;s celebrate anyway.</p>
<h2>2. Creating a service</h2>
<p>Alright, I&#8217;m not going to try explain what dependency injection is. If you don&#8217;t know, try to figure out what&#8217;s going on here, it&#8217;s really that simple!</p>
<p>In this example we&#8217;re creating an <strong>WebApplicationService.cs</strong>. No doubt it&#8217;s a bad example, but we need to keep things simple. Our new service will have these abilities:</p>
<ul>
<li>Return us a DateTime value for it&#8217;s creation time.</li>
<li>Tell us how many time we asked for it&#8217;s creation time <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </li>
</ul>
<p>Go ahead and create <strong>WebApplicationService.cs</strong> in our <strong>/Models</strong> folder. Next, paste these methods into your new service:</p>
<pre><code>public class WebApplicationService
{
    private readonly DateTime _created;
    private int _totalRequest;

    public WebApplicationService()
    {
        _created = DateTime.Now;
        _totalRequest = 0;
    }

    public DateTime GetCreationDate()
    {
        _totalRequest++;
        return _created;
    }

    public int TimesRequested()
    {
        return _totalRequest;
    }
}
</code></pre>
<h2>3. Time for the mandatory administration section</h2>
<p>Let&#8217;s create a ViewModel for our first action in the <em>admin</em> section, by creating a <strong>IndexModel.cs</strong> in /Views/Admin/Models/ (just create the folders). Drop in these properties in the <strong>IndexModel.cs</strong> file:</p>
<pre><code>using System;

namespace nancytest.Views.Admin.Models
{
    public class IndexModel
    {
        public DateTime CreationDate { get; set; }
        public int TotalRequests { get; set; }
    }
}
</code></pre>
<p>Next up, add our new action to <strong>AdminModule.cs</strong> and use depedency injection to serve the WebApplicationService to our Module:</p>
<pre><code>using Nancy;
using nancytest.Models;

namespace nancytest.Modules
{
    public class AdminModule : NancyModule
    {
        public AdminModule(WebApplicationService webApplicationService) : base("/admin")
        {
            Get["/"] = parameters =&gt; {
                var model = new Views.Admin.Models.IndexModel {
                    CreationDate = webApplicationService.GetCreationDate(),
                    TotalRequests = webApplicationService.TimesRequested()
                };

                return View["Index", model];
            };
        }
    }
}
</code></pre>
<p>Let&#8217;s take a deeper look at this code. First thing to notice is the expansion of the constructor **: base(&#8220;/admin&#8221;)**. This tells Nancy that every action&#8217;s URL inside this Module starts with /admin. The next big thing is the injection of our WebApplicationService. This is extremely simple, isn&#8217;t it? We just throw in the objects we want initialized in the parameter list of the constructor and Nancy together with TinyIoC takes care of everything. By default all injections on concrete/base classes will be made as multi-instance, meaning that every call (http request) creates a new instance of the object. If we had used an interface (could be IWebApplicationService) TinyIoC detects this and make the object behave like a singleton (meaning only a single instance of the object, shared over all http requests).</p>
<p>Before we can see all this in action we need (of course) to create the Index view. Create a new HTML page called <strong>Index.cshtml</strong> inside the <strong>/Views/Admin</strong> folder. Make it look like this:</p>
<pre><code>@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase&lt;nancytest.Views.Admin.Models.IndexModel&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Welcome to the admin page&lt;/h1&gt;
    &lt;table&gt;
        &lt;tr&gt;
            &lt;td&gt;Creation date for WebApplicationService:&lt;/td&gt;
            &lt;td&gt;@Model.CreationDate&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Total request on the service:&lt;/td&gt;
            &lt;td&gt;@Model.TotalRequests&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Save everything and power it up (CTRL+F5). Point your browser to /admin. If everything is as expected you should see something like this:</p>
<p><a href="http://jhovgaard.net/from-asp-net-mvc-to-nancy-part-3/part3-browser/" rel="attachment wp-att-502"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/part3-browser.png" alt="part3-browser" width="529" height="283" class="alignnone size-full wp-image-502" /></a></p>
<h2>4. Defining the lifecycle of WebApplicationService</h2>
<p>Okay, try to do some refreshes on our /admin page. You see? The creation date changes. That&#8217;s a bug combined with the intention of our service. To fix this, we need to tell Nancy that we don&#8217;t wanna use the default life cycle (which is multi-instance for concrete classes like ours), but rather wants to treat it as a singleton (meaning the object will only be created one time).</p>
<p>To do this we create a custom bootstrapper. This could seem a bit complex, but it really isn&#8217;t when you get the understanding (you didn&#8217;t figure that out, huh? Hehe). Create a <strong>MyBootstrapper.cs</strong> class in our <strong>/Models</strong> folder. Make it look like this:</p>
<pre><code>using Nancy.Bootstrapper;
using TinyIoC;

namespace nancytest.Models
{
    public class MyBootstrapper : Nancy.DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);
            container.Register&lt;WebApplicationService&gt;().AsSingleton();
        }
    }
}
</code></pre>
<p>I know. Freaking overrides and stuff. But actually most of this stuff is standard code. The only thing to notice here is this line:</p>
<pre><code>container.Register&lt;WebApplicationService&gt;().AsSingleton();
</code></pre>
<p>This tells Nancy that our WebApplicationService&#8230; Wait, you already get it, right? That&#8217;s how an awesome framework roll!</p>
<p>Again, save everything and hit CTRL+F5. Point your browser to /admin again and try refresh a couple of times. Do you see the difference? Our service is now only created once, making us able to count the requests and persist the <strong>CreationDate</strong>.</p>
<p>One last thing? Didn&#8217;t you expect we needed to tell Nancy about our custom bootstrapper? I did. But because Nancy is <span style="text-decoration:line-through">trying to be</span> the best micro MVC framework, it just detects our bootstrapper and uses that instead. No need for Global.asax, just plug and play.</p>
<h2>5. Conclusion</h2>
<p>In this post we learned how to use the integrated dependency injection of Nancy provided by TinyIoC. Also, we learned how to modify objects&#8217; lifecycle and last we learned how to create a base url for a NancyModule.</p>
<p>I&#8217;m not finished with Nancy yet and I believe there will be 2 or 3 additional parts. If you have some specific topic you want me to cover, let me know <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/01/nancytest-part3.zip">Click here to download the source for this post.</a></p>
<p><strong>Please, if you enjoyed reading this post &#8211; share and comment it!</strong></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-3" target="_blank"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjhovgaard.net%2ffrom-aspnet-mvc-to-nancy-part-3&#038;bgcolor=0066FF" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>From ASP.NET MVC to Nancy &#8211; Part 2</title>
		<link>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-2/</link>
		<comments>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-2/#comments</comments>
		<pubDate>Sun, 19 Feb 2012 18:58:32 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aspnetmvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nancy]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=282</guid>
		<description><![CDATA[This second part digs into the use of IntelliSense combined with Nancy by doing simple modelbinding on GET/POST actions.]]></description>
				<content:encoded><![CDATA[<p><a href="http://jhovgaard.net/from-asp-net-mvc-to-nancy-part-3/nancylogo-transparent/" rel="attachment wp-att-572"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/nancylogo-transparent.png" alt="nancylogo-transparent" width="256" height="287" class="alignleft size-full wp-image-572" /></a></p>
<blockquote>
<p>This is a series of posts. If you just dropped into this article, you really should <a href="http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1">start by reading part 1</a>.</p>
</blockquote>
<p>Alright. Note to my self: Never say &#8220;tomorrow I will&#8230;&#8221; in a blog post unless the stuff to do already is done <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  Sorry for the delay but here we go again.</p>
<h1>Nancy Modelbinding &#8211; moving data from our module to the view</h1>
<p>Today we&#8217;re going to move an object from our module/controller to the view. Next we&#8217;re going the other way, posting a new object from the view and back to the module.</p>
<h2>Creating a <strong>BlogPost</strong> object</h2>
<p>First, let&#8217;s create an actual object to return to our View. We make a classic <strong>BlogPost</strong> object in a folder called Objects:</p>
<pre><code>using System.Collections.Generic;
namespace nancytest.Objects
{
    public class BlogPost
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public List&lt;string&gt; Tags { get; set; }

        public BlogPost()
        {
            Tags = new List&lt;string&gt;();
        }
    }
}
</code></pre>
<h2>Return the new <strong>BlogPost</strong> object to our view</h2>
<p>Start by extending our &#8220;/&#8221; action to create a new <strong>BlogPost</strong> object and return it to our Index view:</p>
<pre><code>using Nancy;
using nancytest.Objects;

namespace nancytest.Modules
{
    public class HomeModule : NancyModule
    {
        public HomeModule()
        {
        Get["/"] = parameters =&gt; {
            var blogPost = new BlogPost {
                Id = 1,
                Title = "From ASP.NET MVC to Nancy - Part 2",
                Content = "Lorem ipsum...",
                Tags = {"c#", "aspnetmvc", "nancy"}
            };

            return View["Index", blogPost];
        };
        }
    }
}
</code></pre>
<p>Notice that I simply pass in my object as the second parameter when I return my view.</p>
<h2>Preparing the view for <strong>BlogPost</strong> and enabling IntelliSense</h2>
<p>Open up <strong>Views/Home/Index.cshtml</strong>. We need to tell Razor what model it should use. In ASP.NET MVC we would do it like this:</p>
<pre><code>@model nancytest.Objects.BlogPost
</code></pre>
<p>Unfortunately <a href="http://groups.google.com/group/nancy-web-framework/browse_thread/thread/ffe653b84d6be3f6">Microsoft made some bad decisions designing Razor</a>. Because of that we&#8217;re not able to do this using Nancy. Instead insert this line of code in the very top of the page:</p>
<pre><code>@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase&lt;nancytest.Objects.BlogPost&gt;
</code></pre>
<p>This tells Razor about the model &#8211; Nancy Style!</p>
<p>Take it for a spin by adding some @Model usage on <strong>Views/Home/Index.cshtml</strong>:</p>
<pre><code>@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase&lt;nancytest.Objects.BlogPost&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
    &lt;body&gt;
        &lt;table&gt;
            &lt;tr&gt;
                &lt;td&gt;Id:&lt;/td&gt;
                &lt;td&gt;@Model.Id&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Title:&lt;/td&gt;
                &lt;td&gt;@Model.Title&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Content:&lt;/td&gt;
                &lt;td&gt;@Model.Content&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Tags:&lt;/td&gt;
                &lt;td&gt;@string.Join(", ", Model.Tags)&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Did you just freaking notice how natural that felt for IntelliSense? Or do you instead wondering why I&#8217;m so psyched about an old feature in Visual Studio? Well, the reason for that is in the early versions of Nancy, IntelliSense wasn&#8217;t available and Visual Studio didn&#8217;t recognize the .cshtml as Razor view pages, so even simple HTML made VS throw errors. These days are over and Nancy works, as you can see, like a magical unicorn in VS.</p>
<p>Do some CTRL+SHIFT+B and CTRL+F5 magic and you should see something like this:</p>
<p><a href="http://jhovgaard.net/from-asp-net-mvc-to-nancy-part-2/part2-view/" rel="attachment wp-att-492"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/part2-view.png" alt="part2-view" width="529" height="283" class="alignnone size-full wp-image-492" /></a></p>
<h2>Time for the post action. Let&#8217;s pretend we create a <strong>BlogPost</strong></h2>
<p>Jump right back into <strong>HomeModule</strong> and add a new action mapped to /new/ (GET) like this:</p>
<pre><code>Get["/new/"] = parameters =&gt; {
    return View["New"];
};
</code></pre>
<p>Next create a <strong>New.cshtml</strong> in the folder <strong>Views/Home</strong>. Again we declare it to use our BlogPost object as model:</p>
<pre><code>@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase&lt;nancytest.Objects.BlogPost&gt;
</code></pre>
<p>Create a simple form, posting to /new/:</p>
<pre><code>@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase&lt;nancytest.Objects.BlogPost&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
    &lt;body&gt;
        &lt;form action="/new/" method="post"&gt;
            &lt;table&gt;
                &lt;tr&gt;
                    &lt;td&gt;Id:&lt;/td&gt;
                    &lt;td&gt;&lt;input type="text" name="Id" value="@Model.Id" /&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Title:&lt;/td&gt;
                    &lt;td&gt;&lt;input type="text" name="Title" value="@Model.Title" /&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Content:&lt;/td&gt;
                    &lt;td&gt;&lt;input type="text" name="Content" value="@Model.Content"/&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Tags:&lt;/td&gt;
                    &lt;td&gt;&lt;input type="text" name="Tags" value="@string.Join(",", Model.Tags)"/&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td colspan="2"&gt;&lt;input type="submit" value="Add new"/&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Next up is creating the action handling the POST coming from this form. Jump back into our <strong>HomeModule</strong> and add a new action:</p>
<pre><code>Post["/new/"] = paramters =&gt; {
    var blogPost = this.Bind&lt;BlogPost&gt;();
    // Redirects the user to our Index action with a "status" value as a querystring.
    return Response.AsRedirect("/?status=added&amp;title=" + blogPost.Title);
};
</code></pre>
<p>Also, don&#8217;t forget to import the <strong>Nancy.ModelBinding</strong> namespace in the top of our class like this:</p>
<pre><code>using Nancy.ModelBinding;
</code></pre>
<p>Awesome! Here we throw the user back to our Index page (root) including some stuff in the querystring for our convenience.</p>
<p>Nancy is just getting more and more fantastic. Did you notice the extremely fast build times and the native feeling when you just do raw web development?</p>
<p>That&#8217;s all folks! In post 3 I&#8217;ll start by looking into the IoC container <a href="https://github.com/grumpydev/TinyIoC">TinyIoC</a> which is by default integrated in Nancy.</p>
<h3><a href="http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-3">Still psyched about Nancy? Click here to go directly to part 3! </a></h3>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/01/nancytest-part2.zip">Click here to download the source for this post.</a></p>
<p><strong>Please, if you enjoyed reading this post &#8211; share and comment it!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>From ASP.NET MVC to Nancy &#8211; Part 1</title>
		<link>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1/</link>
		<comments>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 18:04:10 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aspnetmvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nancy]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=162</guid>
		<description><![CDATA[This post is the first in a series of post on getting started with Nancy (NancyFX) as an experienced ASP.NET MVC developer.]]></description>
				<content:encoded><![CDATA[<p><a href="http://jhovgaard.net/from-asp-net-mvc-to-nancy-part-3/nancylogo-transparent/" rel="attachment wp-att-572"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/nancylogo-transparent.png" alt="nancylogo-transparent" width="256" height="287" class="alignleft size-full wp-image-572" /></a>If you have ever been in the world of Ruby you would know about this little framework called Sinatra. In it&#8217;s core concept it&#8217;s a MVC framework, just extremely simple and fast.</p>
<p>Nancy is a Micro .NET framework inspired by Sinatra. If you ever felt that ASP.NET MVC is too heavy, too clumsy and stands in your way, Nancy is definitely made for you.</p>
<p>I&#8217;m an average .NET developer. I work for middle-sized company where we build a lot of applications in ASP.NET MVC 3 and because of that, this series of posts will be designed exactly for that purpose: moving from ASP.NET MVC 3 to Nancy.</p>
<h2>Creating the application</h2>
<p>Grap a new instance of Visual Studio 2010 and create a new <strong>Empty ASP.NET Application</strong>. Yes that is what we want. Personally this was my first time using this template.</p>
<p>Next we&#8217;re going to install Nancy using Nuget. Right click <em>References</em> in the solution explorer and click <em>Manage Nuget Packages&#8230;</em>. Search for Nuget and install <strong>Nancy.Hosting.Aspnet</strong> and <strong>Nancy.Viewengines.Razor</strong>:</p>
<p><a href="http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1/part1-nuget/" rel="attachment wp-att-482"><img src="http://jhovgaard.net/wp-content/uploads/2013/01/part1-nuget.png" alt="part1-nuget" width="800" height="450" class="alignnone size-full wp-image-482" /></a></p>
<p>This will install Nancy including Razor support. Also it will add Razor as a BuildProvider in your web.config, enabling Visual Studio to support IntelliSense in our views.</p>
<h2>Time to get nasty &#8211; the good way&#8230;</h2>
<p>Yay! We&#8217;re ready! You remember controllers in MVC3 right? In Nancy we got something like that, it&#8217;s just called Modules, but seen from our eyes it&#8217;s the same thing.</p>
<ul>
<li>Create a new folder called <strong>Modules</strong></li>
<li>Create a new class called <strong>HomeModule.cs</strong></li>
</ul>
<p>Again, in MVC3, inside our controller, we had a massive amounts of &#8220;Actions&#8221; defined as methods. This is not nearly as complex in Nancy. Let me create an Index-action for you:</p>
<pre><code>public class HomeModule : NancyModule
{
    public HomeModule()
    {
        Get["/"] = parameters =&gt; {
            return View["Index"];
        };
    }
}
</code></pre>
<p>zOMG! See what&#8217;s going on here? First I create a &#8220;parameterless constructor&#8221;. Fancy word, but this is actually just the <strong>public HomeModule()</strong> part. So inside our constructor (if you&#8217;re in for seh winz you call this a &#8220;ctor&#8221;) I both define the route (the ones we know from Global.asax) and the action it self. At last I return a view called Index.</p>
<h2>Creating the &#8220;Index&#8221; view</h2>
<p>Next up is our Razor Index view. Create a new folder called <strong>Views</strong>. In here create another folder called <strong>Home</strong>. Now right click the folder and create a HTML page called <strong>Index.cshtml</strong>. Put in some motivating text and hit CTRL+F5.</p>
<p>Now two things can happen. You&#8217;ll see a scary YSOD page telling you that the page &#8220;/Views/Home/Index.cshtml couldn&#8217;t be served, or you will see your motivating text, served directly from Nancy. If you got the scary one, point your browser to the root of the application. If the last I would like to congratulate you with your first Nancy application <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h3><a href="http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-2/">This was part 1. Wanna know something about Nancy&#8217;s ModelBinding, ViewModels and so on? Go for part 2 <img src='http://jhovgaard.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </a></h3>
<p><a href="http://jhovgaard.net/wp-content/uploads/2013/01/nancytest-part1.zip">Click here to download the source for this post.</a></p>
<p><strong>Please, if you enjoyed reading this post &#8211; share it!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/from-aspnet-mvc-to-nancy-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Generating new priority/index order</title>
		<link>http://jhovgaard.net/c-generating-new-priorityindex-order/</link>
		<comments>http://jhovgaard.net/c-generating-new-priorityindex-order/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 18:01:01 +0000</pubDate>
		<dc:creator>jhovgaard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://jhovgaardnet.azurewebsites.net/?p=72</guid>
		<description><![CDATA[I just came by one of those scenarios where you need to generate new priority indexes because the user wants to reorganize some list.]]></description>
				<content:encoded><![CDATA[<p>Sup!</p>
<p>I just came by one of those scenarios where you need to generate new priority indexes because the user wants to reorganize some list.</p>
<p>Normally I iterate through my list of objects and then calculate the new priority/order number on each loop (by doing difference stuff whether the new index is positive nor negative).</p>
<p>But this time I freestyled a little with NHibernate and the List generic. The user gets a list of “pages” that can be reordered.</p>
<p>I came out with this awesome method:</p>
<pre><code>public void SetNewPagePriority(int pageId, int newPriority)
{
    newPriority--; //Abstract the user from zero-index
    var page = _pageService.GetById(pageId);
    if(page == null) return;

    var pagesInNewOrder = page.Test.Pages.OrderBy(x =&amp;gt; x.Priority).ToList();
    pagesInNewOrder.RemoveAt(pagesInNewOrder.IndexOf(page));
    pagesInNewOrder.Insert(newPriority, page);

    //Sets the new priority in the database using the current index of the loop.
    for(var i = 0; i &amp;lt; pagesIsNewOrder.Count; i++)
        pagesInNewOrder[i].Priority = i;
}
</code></pre>
<p>The code simply removes the object that needs to be reorganized from the list (NHibernate is clever enough to not delete the object from the database at this time), and then pushes it into the new location.</p>
<p>After that I simply iterate the newly reorganized list and sets the current index. NHibernate will then commit the changes for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://jhovgaard.net/c-generating-new-priorityindex-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
