<?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>stevescodingblog.co.uk</title>
	<atom:link href="http://stevescodingblog.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://stevescodingblog.co.uk</link>
	<description>Musings on software development, .Net platform and other stuff</description>
	<lastBuildDate>Thu, 10 Nov 2011 19:22:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Script#: Taking care of &#8216;this&#8217;</title>
		<link>http://stevescodingblog.co.uk/scriptshar-taking-care-of-this/</link>
		<comments>http://stevescodingblog.co.uk/scriptshar-taking-care-of-this/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 19:22:12 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[script#]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=514</guid>
		<description><![CDATA[Making sure &#8216;this&#8217; points to exactly what you think it does in Javascript is one of the challenges that a new programmer to the language has to overcome. [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Making sure &#8216;this&#8217; points to exactly what you think it does in Javascript is one of the challenges that a new programmer to the language has to overcome. <a title="ScriptSharp" href="http://projects.nikhilk.net/ScriptSharp" target="_blank">Script#</a> makes this somewhat easy for us because it tries to make sure it points to exactly what we&#8217;d expect it to if we were writing a normal C# app.</p>
<p>Take this snippet, for example:</p>
<pre class="brush: csharp; title: Code:; notranslate">
jQueryObject buttons = jQuery.Select(&quot;.homepageCreateButton&quot;);

buttons.MouseOver(delegate(jQueryEvent e)
{
	jQueryObject button = jQuery.This;
});
</pre>
<p>Here I&#8217;m selecting some buttons and applying a click handler. The generated code here is as you would expect, where jQuery. This clearly translates to $(this):</p>
<pre class="brush: jscript; title: Code:; notranslate">
var buttons = $('.homepageCreateButton');

buttons.mouseover(function(e) {
    var button = $(this);
});
</pre>
<p>What happens though when I start using the C# keyword &#8216;this&#8217; in my delegate?</p>
<pre class="brush: csharp; title: Code:; notranslate">
jQueryObject buttons = jQuery.Select(&quot;.homepageCreateButton&quot;);

buttons.MouseOver(delegate(jQueryEvent e)
{
	jQueryObject button = jQuery.This;

	object a = this;
});
</pre>
<p>This gets compiled to:</p>
<pre class="brush: jscript; title: Code:; notranslate">
var buttons = $('.homepageCreateButton');
buttons.mouseover(ss.Delegate.create(this, function(e) {
    var button = $(this);
    var a = this;
}));
</pre>
<p>.. which is not quite the same thing. As soon as the Script# compiler has detected that I am making use of the &#8216;this&#8217; pointer it wraps the delegate call in a method and changes the context of the code inside the delegate so that &#8216;this&#8217; points to the current instance of the class. This is of course the exact behaviour we would expect and love in C#, and for the most part this is also a good thing in Javascript. By the way, <strong>this transformation also happens if you implicitly access a class-level member without using the &#8216;this&#8217; keyword</strong>.</p>
<p><em>However,</em> look at what it has done to our jQuery selector; we expected &#8216;this&#8217; to be the current element that we are handling the &#8216;mouseover&#8217; event for, but now that has changed and we are effectively selecting the current instance of the class through a jQuery selector, which of course is nonsense.</p>
<p>To get around this and make sure that you&#8217;re accessing the actual object that you want inside the delegate, you should make use of the <a title="jQuery CurrentTarget" href="http://api.jquery.com/event.currentTarget/" target="_blank">CurrentTarget property on jQueryEvent</a>:</p>
<pre class="brush: csharp; light: true; title: Code:; notranslate">jQueryObject button = jQuery.FromElement(e.CurrentTarget)</pre>
<p>Just be careful when you intend to use &#8216;this&#8217; and &#8216;jQuery.This&#8217; together in a delegate, as they will not behave as you would expect in this case.</p>
<div class="shr-publisher-514"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fscriptshar-taking-care-of-this%2F' data-shr_title='Script%23%3A+Taking+care+of+%27this%27'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fscriptshar-taking-care-of-this%2F' data-shr_title='Script%23%3A+Taking+care+of+%27this%27'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fscriptshar-taking-care-of-this%2F' data-shr_title='Script%23%3A+Taking+care+of+%27this%27'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/scriptshar-taking-care-of-this/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.Net Image Processor v1.2 released!</title>
		<link>http://stevescodingblog.co.uk/net-image-processor-v1-2-released/</link>
		<comments>http://stevescodingblog.co.uk/net-image-processor-v1-2-released/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 11:32:48 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[gdi]]></category>
		<category><![CDATA[image processor]]></category>
		<category><![CDATA[imaging]]></category>
		<category><![CDATA[nuget]]></category>
		<category><![CDATA[resizing]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=503</guid>
		<description><![CDATA[I&#8217;ve just put another release out for .Net Image Processor, my image processing and resizing code library for .Net and GDI, which includes a number of new filters, [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I&#8217;ve just put another release out for <a href="http://dotnetimageprocessor.codeplex.com/" target="_blank">.Net Image Processor</a>, my image processing and resizing code library for .Net and GDI, which includes a number of new filters, including:</p>
<ul>
<li>Greyscale</li>
<li>Sepia</li>
<li>Invert</li>
<li>Image watermark</li>
<li>Text watermark</li>
</ul>
<p>The first three also introduce a small framework for applying any sort of single pixel-based filter (as opposed to kernal-based filters) where you can simply create an implementation of <strong>PixelFilter</strong> and supply your own algorithm for manipulating a single pixel (a blog post is forthcoming on this soon).</p>
<p>You can grab the <a href="http://dotnetimageprocessor.codeplex.com/releases/view/69759" target="_blank">latest version from the project homepage</a>, or you can update <a href="http://nuget.org/List/Packages/Simplicode.ImageProcessor" target="_blank">the Nuget package</a> from within Visual Studio. To see how to use all of the available filters, have a scan of <a href="http://dotnetimageprocessor.codeplex.com/documentation" target="_blank">the documentation</a>.</p>
<p>So what&#8217;s coming next?</p>
<p>I&#8217;m looking into using a couple of third-party libraries for a couple of prominent issues:</p>
<ul>
<li>PNG lossy compression isn&#8217;t supported very well by GDI+, so I&#8217;m looking into using <a href="http://www.libpng.org/pub/png/libpng.html" target="_blank">libpng </a>to help with this</li>
<li>An<a href="http://dotnetimageprocessor.codeplex.com/workitem/8976" target="_blank"> issue has come up with multi-page TIFF files</a>, but I think I can solve this one with another library</li>
<li>Batch processing</li>
</ul>
<p>Both of these issues will be looked at for the 1.3 release.</p>
<div class="shr-publisher-503"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fnet-image-processor-v1-2-released%2F' data-shr_title='.Net+Image+Processor+v1.2+released%21'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fnet-image-processor-v1-2-released%2F' data-shr_title='.Net+Image+Processor+v1.2+released%21'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fnet-image-processor-v1-2-released%2F' data-shr_title='.Net+Image+Processor+v1.2+released%21'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/net-image-processor-v1-2-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.Net Image Processing library now available</title>
		<link>http://stevescodingblog.co.uk/net-image-processing-library-now-available/</link>
		<comments>http://stevescodingblog.co.uk/net-image-processing-library-now-available/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 23:09:29 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 4.0]]></category>
		<category><![CDATA[imaging]]></category>
		<category><![CDATA[nuget]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=500</guid>
		<description><![CDATA[This is a pet project which I&#8217;ve had around for a while now, which started off as a simple wrapper for GDI+ which allows you to create thumbnails [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This is a pet project which I&#8217;ve had around for a while now, which started off as a simple wrapper for GDI+ which allows you to create thumbnails from images. It&#8217;s turning into something a bit more than that, with an extensible filter framework where filters can be chained together. Also supports image conversion to/from Jpeg, PNG, TIFF, Bmp and GIF.</p>
<p>The idea is that over time I will be developing more and more filters which can be plugged in, with support coming for batch processing and asynchronous processing. Finally, the entire project is all open-source and free!</p>
<p>All the documentation for how to use it is all contained within the project homepage (there is more technical documentation to come), so without further ado here are all the links you need:</p>
<ul>
<li><a href="http://dotnetimageprocessor.codeplex.com/" target="_blank">Project home page</a></li>
<li><a href="http://dotnetimageprocessor.codeplex.com/documentation" target="_blank">Usage / documentation</a></li>
<li><a href="http://dotnetimageprocessor.codeplex.com/SourceControl/list/changesets" target="_blank">Source explorer</a></li>
</ul>
<h2>Nuget Support</h2>
<p>What&#8217;s more, <a href="http://nuget.org/List/Packages/Simplicode.ImageProcessor" target="_blank">the project is also hosted on Nuget</a> &#8211; so if you run Visual Studio 2010 with the excellent <a href="http://nuget.org/" target="_blank">Nuget extension</a> you can just install the project from there. The package name is <strong>Simplicode.ImageProcessor</strong>.</p>
<p>I would love to hear any feedback or issues you have if you use the library &#8211; you can get in touch with me directly or post a review on the project site!</p>
<div class="shr-publisher-500"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fnet-image-processing-library-now-available%2F' data-shr_title='.Net+Image+Processing+library+now+available'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fnet-image-processing-library-now-available%2F' data-shr_title='.Net+Image+Processing+library+now+available'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fnet-image-processing-library-now-available%2F' data-shr_title='.Net+Image+Processing+library+now+available'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/net-image-processing-library-now-available/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Problems with ResourceManager and neutral culture in Asp.Net MVC</title>
		<link>http://stevescodingblog.co.uk/problems-with-resourcemanager-and-neutral-culture-in-asp-net-mvc/</link>
		<comments>http://stevescodingblog.co.uk/problems-with-resourcemanager-and-neutral-culture-in-asp-net-mvc/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 15:58:08 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[resource files]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=485</guid>
		<description><![CDATA[I&#8217;ve been struggling with this for a couple of hours now and found a solution that I would like to share. The problem is this: I added a [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I&#8217;ve been struggling with this for a couple of hours now and found a solution that I would like to share.</p>
<p>The problem is this: I added a resource file to my project as an Embedded Resource, configured the namespace as &#8220;Resources.Dialog&#8221; and set its Custom Tool property to &#8220;PublicResXFileCodeGenerator&#8221;. I then added some string resources to the file.</p>
<p>After inspection of the generated code file, everything looks normal. Next, I need to be able to access my resources dynamically, rather than simply using my Dialog resource class directly.</p>
<p>You would be tempted to do something like this:</p>
<pre class="brush: csharp; title: Code:; notranslate">ResourceManager dialogResources = new ResourceManager(typeof(Resources.Dialog));
string myString = dialogResources.GetString(&quot;MyString&quot;);</pre>
<p>This would work in theory, but instead (for me at least) it throws this nasty exception on the call to GetString():</p>
<blockquote><p>MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture</p></blockquote>
<p>I might be expecting this exception if I hadn&#8217;t provided a neutral culture resource file (which are basically your &#8216;fallback&#8217; resources if you&#8217;re not using a supported culture), or if I had defined the resource file in a different assembly, but I&#8217;m not. This is a simple neutral culture resource file, in the same assembly as where the resource is being used from.</p>
<p>I don&#8217;t know why its doing this, but I do know what I changed to finally fix it.</p>
<p>The class which is generated by the resource file exposes a static ResourceManager instance which is set up to manage itself &#8211; so the solution is to use this rather than create your own!</p>
<p>Therefore, the above code becomes:</p>
<pre class="brush: csharp; title: Code:; notranslate">ResourceManager dialogResources = Resources.Dialog.ResourceManager;
string myString = dialogResources.GetString(&quot;MyString&quot;);</pre>
<p>.. and the exception goes away.</p>
<div class="shr-publisher-485"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fproblems-with-resourcemanager-and-neutral-culture-in-asp-net-mvc%2F' data-shr_title='Problems+with+ResourceManager+and+neutral+culture+in+Asp.Net+MVC'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fproblems-with-resourcemanager-and-neutral-culture-in-asp-net-mvc%2F' data-shr_title='Problems+with+ResourceManager+and+neutral+culture+in+Asp.Net+MVC'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fproblems-with-resourcemanager-and-neutral-culture-in-asp-net-mvc%2F' data-shr_title='Problems+with+ResourceManager+and+neutral+culture+in+Asp.Net+MVC'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/problems-with-resourcemanager-and-neutral-culture-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding a new Jquery UI theme to the default Asp.Net MVC 3 Project</title>
		<link>http://stevescodingblog.co.uk/adding-a-new-jquery-ui-theme-to-the-default-asp-net-mvc-3-project/</link>
		<comments>http://stevescodingblog.co.uk/adding-a-new-jquery-ui-theme-to-the-default-asp-net-mvc-3-project/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 21:39:54 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery-ui]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[nuget]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=465</guid>
		<description><![CDATA[The default project template for Asp.Net is pretty functional out of the box, but an interesting feature is that new templates install jQuery and jQuery UI out of [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>The default project template for Asp.Net is pretty functional out of the box, but an interesting feature is that new templates install <a href="http://jquery.com/" target="_blank">jQuery</a> and <a href="http://jqueryui.com/" target="_blank">jQuery UI</a> out of the box! In fact, jQuery UI now comes pre-installed as a <a href="http://nuget.codeplex.com/" target="_blank">Nuget </a>package if you&#8217;ve got <a title="Read about the April 2011 MVC3 Tools update" href="http://haacked.com/archive/2011/04/12/introducing-asp-net-mvc-3-tools-update.aspx" target="_blank">the latest MVC3 Tools Update (April 2011)</a>. If you&#8217;re following this while sitting at a MVC3 project already in development, you can simply install the latest jQuery UI repository release using Nuget from within Visual Studio, and you will get the same thing.</p>
<p>In case you did not know, you can start working with jQuery UI pretty quickly and easily by just including the style sheet and script in your layout page like so (your actual file names may vary slightly):</p>
<pre class="brush: xml; title: Code:; notranslate">
&lt;link href=&quot;@Url.Content(&quot;~/Content/themes/base/jquery.ui.all.css&quot;)&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery-ui-1.8.11.min.js&quot;)&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</pre>
<p>This will get you working with a basic jQuery UI theme for all the components and controls which come with the framework. However, if you want to update the project with another jQuery theme to better suit your requirements, then you can do it like this.</p>
<p>First of all, find a theme using the <a href="http://jqueryui.com/themeroller/" target="_blank">jQuery UI theme roller</a> that takes your fancy. For this example, I&#8217;ve chosen &#8220;Redmond&#8221; as it fits in nicely with the default Asp.Net MVC 3 theme, but you can chose any theme or construct your own. When you click download, it will ask you which components you want to download themes for &#8211; just leave it at the default, which is <strong>all of them &#8211; </strong>and click &#8216;Download&#8217;. After .zip archive has been downloaded, extract it to somewhere temporary, and explore its contents.</p>
<p>You will get a file structure which looks like this:</p>
<p><img class="size-full wp-image-468 alignnone colorbox-465" title="jQuery UI Theme Contents 1" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/06/jqueryui-1.png" alt="" width="621" height="142" /></p>
<p>Everything you need is inside the &#8220;css&#8221; folder &#8211; if you open that now you will see a single folder which is named after the theme you just picked from the jQuery UI site. If you&#8217;ve created a custom theme, then it will be named &#8220;custom-theme&#8221; or something to that effect. You can also give it your own folder name if you change the options under &#8216;advanced theme options&#8217; when you download the theme.</p>
<p><img class="alignnone size-full wp-image-470 colorbox-465" title="jQuery UI Theme Folder 2" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/06/jqueryui-2.png" alt="" width="456" height="107" /></p>
<p><img class="size-full wp-image-471 alignright colorbox-465" title="jQuery UI Theme directory" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/06/jqueryui-3.png" alt="" width="176" height="230" /></p>
<p>Right-click and copy this folder. Now, browse to the /content/themes folder of your MVC 3 application and paste the folder there. Now you should have two folders in that location &#8211; one called &#8220;base&#8221; and your new theme folder (see right).</p>
<p>All we need to do now is include this folder in your Visual Studio project. The easiest way to do this is to turn on &#8220;Show All Files&#8221; by clicking the little button at the top of the Solution Explorer tool window, then right-click on the theme folder (which will appear with a white folder icon) and select &#8220;Include in Project&#8221;.</p>
<p><img class="alignnone size-full wp-image-473 colorbox-465" title="Including a folder into Visual Studio" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/06/jqueryui-4.png" alt="" width="229" height="246" /></p>
<p>Now, all that&#8217;s left is to include the theme&#8217;s style sheet into your layout, and you are done:</p>
<pre class="brush: xml; title: Code:; notranslate">&lt;link href=&quot;@Url.Content(&quot;~/Content/themes/redmond/jquery-ui-1.8.13.custom.css&quot;)&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; /&gt;</pre>
<p>Now your new theme will be automatically applied to any jQuery UI components you are using in your application. Here&#8217;s a shot of it working with <a href="http://jqueryui.com/demos/button/" target="_blank">jQuery UI Buttons</a>:</p>
<p><img class="alignnone size-full wp-image-476 colorbox-465" title="jQuery UI buttons" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/06/jqueryui-5.png" alt="" width="501" height="355" /></p>
<p>You can apply the same process to any additional themes you want to install, just line up all your themes in the same folder and include the relevant style sheet on your pages.</p>
<div class="shr-publisher-465"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fadding-a-new-jquery-ui-theme-to-the-default-asp-net-mvc-3-project%2F' data-shr_title='Adding+a+new+Jquery+UI+theme+to+the+default+Asp.Net+MVC+3+Project'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fadding-a-new-jquery-ui-theme-to-the-default-asp-net-mvc-3-project%2F' data-shr_title='Adding+a+new+Jquery+UI+theme+to+the+default+Asp.Net+MVC+3+Project'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fadding-a-new-jquery-ui-theme-to-the-default-asp-net-mvc-3-project%2F' data-shr_title='Adding+a+new+Jquery+UI+theme+to+the+default+Asp.Net+MVC+3+Project'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/adding-a-new-jquery-ui-theme-to-the-default-asp-net-mvc-3-project/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>UK Techdays 2011 in London</title>
		<link>http://stevescodingblog.co.uk/uk-techdays-2011-in-london/</link>
		<comments>http://stevescodingblog.co.uk/uk-techdays-2011-in-london/#comments</comments>
		<pubDate>Fri, 27 May 2011 13:23:58 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[techdays]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=451</guid>
		<description><![CDATA[I was able to get along to the UK Techdays event this year in Fulham (it&#8217;s a free event &#8211; if you can make the time, it&#8217;s worth [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://stevescodingblog.co.uk/wp-content/uploads/2011/05/blog-1.jpg"><img class="alignleft size-medium wp-image-456 colorbox-451" style="margin: 0 10px 10px 0;" title="Techdays 1" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/05/blog-1-300x225.jpg" alt="" width="300" height="225" /></a>I was able to get along to the <a href="http://uktechdays.cloudapp.net/home.aspx" target="_blank">UK Techdays</a> event this year in Fulham (it&#8217;s a free event &#8211; if you can make the time, it&#8217;s worth the trip) to hear about the latest developments in the Windows Azure platform and also Windows Phone 7.</p>
<p>The first day concerned Azure, and it was interesting to hear from the Microsoft techies and evangelists (<a href="http://blogs.msdn.com/b/david_gristwood/" target="_blank">David Gristwood</a>, <a href="http://blogs.msdn.com/b/ericnel/" target="_blank">Eric Nelson</a>, <a href="http://blogs.msdn.com/b/plankytronixx/" target="_blank">Steve Plank</a> and <a href="http://blogs.msdn.com/b/keithbu/" target="_blank">Keith Burns</a> amongst others) , giving us the low-down on features and also running through some brief tutorials which showed us how to accomplish specific tasks, like deploying new web roles and administering our platform instances via the control panel. Steve Plank took us through some more interesting aspects like connecting your local Active Directory to your virtual network in the Cloud, and also spoke a bit about AppFabric and the various components which it concerns.</p>
<p>It was also interesting to hear from external businesses which have based their products on Azure, or had at least implemented a solution based on the technology. The general theme is that, if you can move your infrastructure to the Cloud and get rid of your existing hardware, it really is a no-brainer. If anyone is thinking of implementing a high-volume scalable solution, then they can use this technology at a fraction of the price. One of the speakers mentioned that they were able to turn their six-figure annual costs into a three-figure monthly payment!</p>
<p>Keith Burns later gave a talk on the Sql Azure platform, and the differences between it and Sql Server. To be honest, the differences are fairly small (no full-text indexing or non-clustered indexes yet) but the sizes and costs seem a little prohibitive to me. There&#8217;s a maximum of 50Gb per database (which is a LOT of storage anyway, but you still have that limit whereas the Table Storage and Blob Storage technologies are vastly larger) and there&#8217;s a cost of £6.06 per Gb of data. There are of course data transfer costs, but these only apply when your data leaves the data centre &#8211; having an Sql Azure database which is used by a Web Role (for example) located within the same data centre will not occur any costs.</p>
<p>The second and third days were completed engulfed by Windows Phone 7 announcements, demos and more case studies from external developers (including <a href="http://cocktailflow.com/" target="_blank">Cocktail Flow</a> and <a href="http://www.verysoftware.co.uk/" target="_blank">Very Software</a>). They demonstrated how their applications were put together, design decisions they made and also presented demos of their applications running.</p>
<p><span id="more-451"></span></p>
<p><a href="http://brandonwatson.sys-con.com/" target="_blank">Brandon Watson</a> was there as the keynote speaker and gave some insight to the progress of the phone at the application repository which seems to be growing at some rate (1000+ new apps every month?). He also outlined some of the changes coming with the new &#8220;Mango&#8221; phone update and the new developer tools (which were released a couple of days ago).</p>
<p><a href="http://www.nachmore.com/" target="_blank">Oren Nachman</a> gave some great talks and demos surrounding performance on the phone, and highlighted the quite obvious issues with the standard list box and how slow it was with any decent size list. He walked through a simple broken application and attempted to fix it by using the proper debugging tools and applying the appropriate fixes. He also (on the second day) gave an account of the differences between Silverlight 4 on the desktop, and the forthcoming Silverlight 4 for the phone.</p>
<p><a href="http://stevescodingblog.co.uk/wp-content/uploads/2011/05/blog-2.jpg"><img class="alignright size-medium wp-image-457 colorbox-451" style="margin: 0 0 10px 10px;" title="Techdays 2" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/05/blog-2-300x225.jpg" alt="" width="300" height="225" /></a>Later, <a href="http://andywigley.sys-con.com/" target="_blank">Andy Wigley</a> (Appamundi) was on hand to give a complete demo of the new Sql CE for WP7, and set about converting an existing Xml-based phone app into one which makes use of Sql and Linq. By the looks of it, it&#8217;s almost exactly the same as the LinqToSql technology which is available on the desktop, except there is no designer. So all your entity classes and contexts must be created manually (including specifying all of the appropriate attributes and making sure you fire the usual PropertyChanging/PropertyChanged event pair for the change tracking to work. You could see during the demo that this is all very manual, and it will be a good day when some designer tools for this technology are released.</p>
<p>There was also a run-through of Expression Blend 4 by Brennon Williams, who showed us some design issues and how to create a functional prototype of an application using Sketchflow. No discredit to Brennon at all, because his talk and the presentation was excellent, but the topic missed the mark a little for me as a developer. I would have liked to have seen more about how to use Blend from the point of view of the developer &#8211; creating your application code in Visual Studio and then applying some gloss using Blend (which is what I understood the workflow to be). Nevertheless, I did enjoy it and hopefully such a topic will be part of next year&#8217;s event.</p>
<p>All in all, a good three days and would definitely attend next year. They do give out freebies and prizes and such, including geeky T-shirts, tokens for <a href="http://create.msdn.com/en-US/" target="_blank">AppHub </a>subscriptions (yours truly being the recipient of one such token!) and even phones!</p>
<div class="shr-publisher-451"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fuk-techdays-2011-in-london%2F' data-shr_title='UK+Techdays+2011+in+London'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fuk-techdays-2011-in-london%2F' data-shr_title='UK+Techdays+2011+in+London'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fuk-techdays-2011-in-london%2F' data-shr_title='UK+Techdays+2011+in+London'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/uk-techdays-2011-in-london/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AjaxOnly actions and controllers in Asp.Net MVC</title>
		<link>http://stevescodingblog.co.uk/ajaxonly-actions-and-controllers-in-asp-net-mvc/</link>
		<comments>http://stevescodingblog.co.uk/ajaxonly-actions-and-controllers-in-asp-net-mvc/#comments</comments>
		<pubDate>Tue, 17 May 2011 21:12:17 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[action filter]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=445</guid>
		<description><![CDATA[Currently as part of my work I find myself doing a lot of client-side work (using the excellent ScriptSharp I might add) and this does involve a lot [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Currently as part of my work I find myself doing a lot of client-side work (using the excellent ScriptSharp I might add) and this does involve a lot of calls to the server for bits and pieces via an AJAX call. I&#8217;ve decided to dedicate an entire controller to serve such requests.</p>
<p>However, I want to be able to prevent actions on this controller being run from normal requests through the browser. Normally I would just make sure that such requests done through AJAX are through a POST request, but there is a better way.</p>
<p>Inside an action, you can inspect the current HttpContext.Request object and use the handy IsAjaxRequest() extension method to test whether the current request is being executed from an AJAX request or not. You can then just throw an exception if the request isn&#8217;t AJAX based when you expect it to be. Performing this in every action would become tiresome though if you have a lot of them. So &#8211; you can wrap it up inside a handy Action Filter!</p>
<pre class="brush: csharp; title: Code:; notranslate">
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class AjaxOnlyAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    if (filterContext.HttpContext.Request.IsAjaxRequest())
    {
      base.OnActionExecuting(filterContext);
    }
    else
    {
      throw new InvalidOperationException(&quot;This operation can only be accessed via Ajax requests&quot;);
    }
  }
}
</pre>
<p>The code is very simple and really just wraps up code we would otherwise write in each action. Except now you can do this:</p>
<pre class="brush: csharp; title: Code:; notranslate">
public partial class HomeController : Controller
{
  [AjaxOnly]
  public ActionResult SomeAjaxAction()
  {
    return Content(&quot;Hello!&quot;);
  }
}</pre>
<p>Now, if you were to try accessing this action via the browser through the url <strong>/Home/SomeAjaxAction</strong>, you will hit the exception that is thrown from inside the filter. However, Ajax requests made through jQuery or some other client-side method will pass as normal. Notice how that it doesn&#8217;t break the normal GET/POST semantics; we can make Ajax actions which just return some data true Ajax GET requests without worrying about someone inadvertently linking to the action.</p>
<p>You can also mark the controller itself with this attribute so that all the contained actions inherit this behaviour if that is what is required.</p>
<div class="shr-publisher-445"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fajaxonly-actions-and-controllers-in-asp-net-mvc%2F' data-shr_title='AjaxOnly+actions+and+controllers+in+Asp.Net+MVC'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fajaxonly-actions-and-controllers-in-asp-net-mvc%2F' data-shr_title='AjaxOnly+actions+and+controllers+in+Asp.Net+MVC'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fajaxonly-actions-and-controllers-in-asp-net-mvc%2F' data-shr_title='AjaxOnly+actions+and+controllers+in+Asp.Net+MVC'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/ajaxonly-actions-and-controllers-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Top 3 jQuery Tools</title>
		<link>http://stevescodingblog.co.uk/top-3-jquery-tools/</link>
		<comments>http://stevescodingblog.co.uk/top-3-jquery-tools/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 12:14:48 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[links]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=439</guid>
		<description><![CDATA[While I wait for my shiny new download of Visual Studio 2010 SP1 to install, I just thought I&#8217;d share a couple of awesome frameworks and plugins I&#8217;ve [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>While I wait for my shiny new download of <a href="http://blogs.msdn.com/b/jasonz/archive/2010/12/07/announcing-visual-studio-2010-service-pack-1-beta.aspx" target="_blank">Visual Studio 2010 SP1</a> to install, I just thought I&#8217;d share a couple of awesome frameworks and plugins I&#8217;ve been working with over this past week or so. Some of them might not be news to the masses but I have recently found them to be extremely useful.</p>
<h2><strong>Plupload<br />
</strong><span style="font-size: 13px; font-weight: normal;"><a href="http://www.plupload.com/" target="_blank">Demo/Download</a></span></h2>
<p>This one blew me away. From the makers of the excellent<a href="http://tinymce.moxiecode.com/" target="_blank"> TinyMCE</a> rich-text editor comes Plupload &#8211; a free web-based multiple file upload utility that you can use within your sites to upload multiple files in an asynchronous fashion. The genius about it is that it detects the current browser capabilities and automatically chooses the best option for the user (based on the developer&#8217;s initial configuration). For example, it can be configured to use a Flash interface to do the upload in the first instance, but if Flash is not available then it will automatically fall back to another technology, including an Html5 or Google Gears implementation. The developer just has to configure the plugin with what technologies should be included based on their various capabilities and the rest is done for you.</p>
<p>What&#8217;s more it supports chunked transfer, so uploading massive files should be no problem.</p>
<p>I&#8217;m starting to use this in my home-brew projects now with great success and I will heartily recommend it over any licensed/paid products you might use! I plan to write a tutorial article in the near future showing how you can integrate this product into your Asp.Net MVC applications.</p>
<h2><strong>jQuery Context Menu plugin<br />
</strong><span style="font-size: 13px; font-weight: normal;"><a href="http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin/" target="_blank">Demo/Download</a></span></h2>
<p>This plugin is great for showing context menus within your interface. You basically set up an unordered Html list which describes your menu, and then bind it to those items via a jQuery selector which should activate the menu when right-clicked. You then get notification about which item was clicked, which menu entry was selected and even where the mouse cursor was when you clicked it.</p>
<p>I&#8217;m not writing just about this plugin though. This site has all sorts of little bits and pieces on it which come in extremely handy. Just looking through their blog posts (it&#8217;s a shame these individual plugins aren&#8217;t more visible from the home page) I found these cool little plugins which have made my life easier:</p>
<ul>
<li><a href="http://abeautifulsite.net/blog/2011/01/jquery-selectbox-plugin/" target="_blank">Select-box</a></li>
<li><a href="http://abeautifulsite.net/blog/2011/01/jquery-selectbox-plugin/" target="_blank"></a><a href="http://abeautifulsite.net/blog/2009/12/jquery-word-wrap-plugin/" target="_blank">Word Wrap</a></li>
</ul>
<p>And links to other plugins and utilities which I haven&#8217;t tried yet, including this gem: <a href="http://deepliquid.com/content/Jcrop.html" target="_blank">jCrop</a> (in-browser image cropping tool).</p>
<p>It&#8217;s a great resource and is now on my list of blogs to regularly check for cool stuff!</p>
<h2><strong>jQuery-UI Sortables<br />
</strong><span style="font-size: 13px; font-weight: normal;"><a href="http://jqueryui.com/demos/sortable/" target="_blank">Demo/Download</a></span></h2>
<p>jQuery-UI itself is not news (if you&#8217;re not using this yet, you should be), and to be honest, neither is the Sortable module within. But the discovery I made was that it&#8217;s not just for sorting ordered lists &#8211; it can be used to create interesting portlet-type interfaces where people can drag content around and lay out pages exactly how they want (think <a href="http://www.pageflakes.com" target="_blank">Pageflakes</a> or <a href="http://en.wikipedia.org/wiki/IGoogle" target="_blank">iGoogle</a>). This kind of power is extremely easy to achieve and works great!</p>
<div class="shr-publisher-439"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Ftop-3-jquery-tools%2F' data-shr_title='Top+3+jQuery+Tools'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Ftop-3-jquery-tools%2F' data-shr_title='Top+3+jQuery+Tools'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Ftop-3-jquery-tools%2F' data-shr_title='Top+3+jQuery+Tools'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/top-3-jquery-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ninject.MVC3 no longer on Nuget (or is it?)</title>
		<link>http://stevescodingblog.co.uk/ninject-mvc3-no-longer-on-nuget/</link>
		<comments>http://stevescodingblog.co.uk/ninject-mvc3-no-longer-on-nuget/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 16:25:49 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[ninject]]></category>
		<category><![CDATA[nuget]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=425</guid>
		<description><![CDATA[It seems that within hours of posting my article on Dependency Injection: A Beginner&#8217;s Guide, some things had changed with the Ninject packages available on Nuget. The article [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>It seems that within hours of posting my article on <a href="http://stevescodingblog.co.uk/dependency-injection-beginners-guide/" target="_self">Dependency Injection: A Beginner&#8217;s Guide</a>, some things had changed with the <a href="http://ninject.org/" target="_blank">Ninject </a>packages available on <a href="http://nuget.codeplex.com/" target="_blank">Nuget</a>.</p>
<p>The article uses the package &#8216;Ninject.MVC3&#8242; package, which also depends on <a href="https://bitbucket.org/davidebbo/webactivator/overview" target="_blank">Dave Ebbo&#8217;s WebActivator framework</a>, allowing access to some really early initialisation for things such as dependency injection.</p>
<p>Today it seems as if the package includes Ninject 2.2 and has changed to &#8216;Ninject.Web.Mvc3&#8242; (and I see they&#8217;ve also registered similar namespaces for MVC1 and MVC2), and this package no longer depends on WebActivator which means you don&#8217;t get the normal AppStart_NinjectMvc3.cs file in the root of your project. It also looks as though they&#8217;ve gotten rid of <strong>NinjectServiceLocator </strong>and now instead we have <strong>NinjectDependencyResolver</strong>.</p>
<p>However, WebActivator doesn&#8217;t seem strictly necessary. Taking these changes into account, I can just register all of my bindings in the Application_Start() method in global.asax.cs along with all my MVC routes and so on, and everything works as intended:</p>
<pre class="brush: csharp; title: Code:; notranslate">protected void Application_Start()
{
  IKernel kernel = new StandardKernel();
  kernel.Bind&lt;ILogger&gt;().To&lt;Logger&gt;();
  DependencyResolver.SetResolver(new Ninject.Web.Mvc.NinjectDependencyResolver(kernel));

  AreaRegistration.RegisterAllAreas();

  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}
</pre>
<h2>Update</h2>
<p>22nd Feb: And it looks as though everything is back to the way it was, with the Ninject.MCV3 package now available again. So pretty much everything in this post can be ignored but I&#8217;ll keep it up for historical archiving purposes!</p>
<div class="shr-publisher-425"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fninject-mvc3-no-longer-on-nuget%2F' data-shr_title='Ninject.MVC3+no+longer+on+Nuget+%28or+is+it%3F%29'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fninject-mvc3-no-longer-on-nuget%2F' data-shr_title='Ninject.MVC3+no+longer+on+Nuget+%28or+is+it%3F%29'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fninject-mvc3-no-longer-on-nuget%2F' data-shr_title='Ninject.MVC3+no+longer+on+Nuget+%28or+is+it%3F%29'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/ninject-mvc3-no-longer-on-nuget/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dependency Injection: A Beginner&#8217;s Guide</title>
		<link>http://stevescodingblog.co.uk/dependency-injection-beginners-guide/</link>
		<comments>http://stevescodingblog.co.uk/dependency-injection-beginners-guide/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 16:41:00 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[ninject]]></category>
		<category><![CDATA[nuget]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://stevescodingblog.co.uk/?p=379</guid>
		<description><![CDATA[This article intends to outline the concept of Dependency Injection and its uses within a software engineering project. From Wikipedia.org: “it is a design pattern that separates behaviour [..]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This article intends to outline the concept of Dependency Injection and its uses within a software engineering project. From Wikipedia.org:</p>
<blockquote><p>“it is a design pattern that separates behaviour from dependency resolution, thus decoupling highly dependent components”.</p></blockquote>
<p>Later, I demonstrate the use of Dependency Injection within the context of an Asp.Net MVC 3 application.</p>
<p>Dependency Injection (or DI) allows us to provide implementations and services to other classes for consumption in a very loosely-coupled way. The key tenet is that such implementations can be swapped out for other implementations by changing a minimal amount of code, as the implementation and the consumer are linked by <em>contract</em> only.</p>
<p>In C#, this means that your service implementations should adhere to an interface, and when creating consumers for your services you should program against the <em>interface</em> and not the <em>implementation</em>, and require that the implementation is provided for you, or <em>injected</em> rather than having to create instances yourself. Doing this allows your classes to not worry about how dependencies are create nor where they come from; all that matters is the contract.</p>
<p><span id="more-379"></span></p>
<h2>Dependency Injection by Example</h2>
<p>Let’s go through a simple example where DI could be useful. First, let’s create an interface (the <em>contract</em>) which will allow us to perform some task, say logging a message:</p>
<pre class="brush: csharp; title: Code:; notranslate">public interface ILogger
{
  void LogMessage(string message);
}
</pre>
<p>Notice that nothing about this interface describes how the message is logged and where it is logged to; it simply has the intention of recording a string to some repository. Next, lets create something which uses this interface. Say we create a class which watches a particular directory on disk, and logs a message whenever the directory is changed:</p>
<pre class="brush: csharp; title: Code:; notranslate">public class DirectoryWatcher
{
  private ILogger _logger;
  private FileSystemWatcher _watcher;

  public DirectoryWatcher(ILogger logger)
  {
    _logger = logger;
    _watcher = new FileSystemWatcher(@&quot;C:\Temp&quot;);
    _watcher.Changed += new FileSystemEventHandler(Directory_Changed);
  }

  void  Directory_Changed(object sender, FileSystemEventArgs e)
  {
    _logger.LogMessage(e.FullPath + &quot; was changed&quot;);
  }
}
</pre>
<p>The key thing to notice is that the constructor, we require that something which implements <em>ILogger</em> is given to us, but again notice that we don’t care about where the log goes or how it is created. We can just program against the interface and not worry about it.</p>
<p>This means that in order to create an instance of our <em>DirectoryWatcher</em> we must also have an implementation of <em>ILogger</em> ready. Let’s go ahead and create one which logs messages to a text file:</p>
<pre class="brush: csharp; title: Code:; notranslate">public class TextFileLogger : ILogger
{
  public void LogMessage(string message)
  {
    using (FileStream stream = new FileStream(&quot;log.txt&quot;, FileMode.Append))
    {
      StreamWriter writer = new StreamWriter(stream);
      writer.WriteLine(message);
      writer.Flush();
    }
  }
}
</pre>
<p>Let’s create another which logs messages to the Windows Event Log:</p>
<pre class="brush: csharp; title: Code:; notranslate">
public class EventFileLogger : ILogger
{
  private string _sourceName;

  public EventFileLogger(string sourceName)
  {
    _sourceName = sourceName;
  }

  public void LogMessage(string message)
  {
    if (!EventLog.SourceExists(_sourceName))
    {
      EventLog.CreateEventSource(_sourceName, &quot;Application&quot;);
    }
    EventLog.WriteEntry(_sourceName, message);
  }
}
</pre>
<p>Now we have two separate implementations which log messages in very different ways, but both implement <em>ILogger</em>, which means that either one can be used where an instance of <em>ILogger</em> is required. Now we can create an instance of <em>DirectoryWatcher</em> and have it use one of our loggers:</p>
<pre class="brush: csharp; title: Code:; notranslate">ILogger logger = new TextFileLogger();
DirectoryWatcher watcher = new DirectoryWatcher(logger);
</pre>
<p>Or, by just changing the right-hand side of the first line we can use our other implementation:</p>
<pre class="brush: csharp; title: Code:; notranslate">ILogger logger = new EventFileLogger();
DirectoryWatcher watcher = new DirectoryWatcher(logger);
</pre>
<p>This happens without any changes to the implementation of <em>DirectoryWatcher</em>, and this is the key concept. We are <em>injecting</em> our logger implementation into the consumer, so that the consumer doesn’t have to create this instance on its own. The example shown is trivial, but imagine using this in a large-scale project where you have several dependencies which need to be used by many times more consumers, and then suddenly a requirement comes along which means that the method of logging a message must change (say  the messages are required to be logged into Sql Server for auditing purposes). Without some form of dependency injection, you will have to carefully examine the code and change anything which actually creates an instance of a logger and then uses it. In a large project this can be painful and error prone. With DI, you would just have to change the dependency in one place, and the rest of your application will effectively absorb the change and immediately start using the new logging method.</p>
<p>Essentially, it solves the classic software problem of high-dependency and allows you to create a loosely-couple system which is extremely agile and easy to change.</p>
<h2>Dependency Injection Containers</h2>
<p>Many DI frameworks which you can download and use go a step further and employ the use of a Dependency Injection Container. This is essentially a class which stores a mapping of types and returns the registered implementation for that type. In our simple example we would be able to query the container for an instance of <em>ILogger</em> and it would return an instance of TextFileLogger, or whichever instance we had initialised the container with.</p>
<p>This has the advantage that we can register all of our type mappings in one place, usually in an “Application Start” event, and that gives us quick and clear visibility as to what dependencies we have in the system. Also, many professional frameworks allow us to configure the lifetime of such objects, either creating fresh instances every time we ask for one, or re-using instances across calls.</p>
<p>The container is usually created in such a way that we can get access to the ‘Resolver’ (the thing which allows us to query for instances) from anywhere in the project.</p>
<p>Finally, professional frameworks usually support the concept of “sub-dependencies”, where a dependency has itself one or more dependencies to other types also known to the container. In this case, the resolver can fulfil these dependencies too, giving you back a full chain of correctly created dependencies according to your type mappings.</p>
<p>Let’s create ourselves a very simple DI container to see how the concept works. This implementation doesn’t support nested dependencies, but does allow you to map an interface to an implementation and then query for that implementation later:</p>
<pre class="brush: csharp; title: Code:; notranslate">
public class SimpleDIContainer
{
  Dictionary&lt;Type, object&gt; _map;

  public SimpleDIContainer()
  {
    _map = new Dictionary&lt;Type, object&gt;();
  }

  /// &lt;summary&gt;
  /// Maps an interface type to an implementation of that interface, with optional arguments.
  /// &lt;/summary&gt;
  /// &lt;typeparam name=&quot;TIn&quot;&gt;The interface type&lt;/typeparam&gt;
  /// &lt;typeparam name=&quot;TOut&quot;&gt;The implementation type&lt;/typeparam&gt;
  /// &lt;param name=&quot;args&quot;&gt;Optional arguments for the creation of the implementation type.&lt;/param&gt;
  public void Map&lt;TIn, TOut&gt;(params object [] args)
  {
    if (!_map.ContainsKey(typeof(TIn)))
    {
      object instance = Activator.CreateInstance(typeof(TOut), args);
      _map[typeof(TIn)] = instance;
    }
  }

  /// &lt;summary&gt;
  /// Gets a service which implements T
  /// &lt;/summary&gt;
  /// &lt;typeparam name=&quot;T&quot;&gt;The interface type&lt;/typeparam&gt;
  public T GetService&lt;T&gt;() where T : class
  {
    if (_map.ContainsKey(typeof(T)))
      return _map[typeof(T)] as T;
    else
      throw new ApplicationException(&quot;The type &quot; + typeof(T).FullName + &quot; is not registered in the container&quot;);
  }
}
</pre>
<p>Then, we can construct a small program which creates a container, maps the types, and then queries for a service. Again a simple compact example but imagine what this would look like in a much larger application:</p>
<pre class="brush: csharp; title: Code:; notranslate">
class Program
{
  static SimpleDIContainer Container = new SimpleDIContainer();

  static void Main(string[] args)
  {
    // Map ILogger to TextFileLogger
    Container.Map&lt;ILogger, TextFileLogger&gt;();

    // Create a DirectoryWatcher using whatever implementation for ILogger contained in the DI container
    DirectoryWatcher watcher = new DirectoryWatcher(Container.GetService&lt;ILogger&gt;());
  }
}
</pre>
<h2>Dependency Injection with Asp.Net MVC 3 and Ninject</h2>
<p>Asp.Net MVC 3 is very well suited to dependency injection as it provides hooks and the framework for any DI vendor to create a suitable container. Whenever any controllers or views are created, they pass through the DI container for dependency resolution. It also provides a global way to retrieve dependencies from anywhere within the project.</p>
<p>Ninject (<a href="http://ninject.org/">http://ninject.org/</a>) is a DI container especially written for .Net and now has code to specifically make use of the new DI features in MVC 3. Coupled with the latest versions of MVC 3, VS2010 and the popular Nuget Package Manager, it is extremely easy to get up and running with DI using Ninject within an Asp.Net MVC 3 project.</p>
<p>Let’s run through an example of using Ninject with MVC 3 to implement our small logging framework. This tutorial requires a few bits and pieces which you should install if you haven’t done so already:</p>
<ul>
<li>VS2010 (Professional or Express editions)</li>
<li>Asp.Net MVC 3 (<a href="http://www.asp.net/mvc/mvc3">http://www.asp.net/mvc/mvc3</a>)</li>
<li>Nuget Package Manager for VS2010 (<a href="http://nuget.codeplex.com/">http://nuget.codeplex.com/</a>)</li>
</ul>
<p>If you wish, you can download the <a href="http://dl.dropbox.com/u/5229980/scb/code/MvcNinjectExample.zip">complete project source</a> to follow along with.</p>
<h2><span style="font-size: 13px; font-weight: normal;">Once the above has been installed, let’s begin:</span></h2>
<h2><span style="font-size: 13px; font-weight: normal;">1) Open up VS2010 and create a new MVC 3 Web Application (I’ve called mine MvcNinjectExample):</span></h2>
<p><a href="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial1.png"><img class="alignnone size-medium wp-image-390 colorbox-379" title="DI MVC 1" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial1-300x207.png" alt="" width="300" height="207" /></a></p>
<p>After this screen you will get a dialog asking you to choose an Empty application or an Internet Application – just choose “Empty”.</p>
<p>2) Once the project has loaded, right-click ‘References’ in solution explorer and choose “Add library package reference..” (this is Nuget in action):</p>
<p><img class="alignnone size-full wp-image-391 colorbox-379" title="tutorial2" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial2.png" alt="" width="401" height="219" /></p>
<p>3) On the left, choose “Online” then search for Ninject. In the search results, you should be able to see “Ninject.Mvc3”. Select this and click the ‘Install’ button:</p>
<p><a href="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial-3.png"><img class="alignnone size-medium wp-image-389 colorbox-379" title="DI MVC 3" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial-3-300x200.png" alt="" width="300" height="200" /></a></p>
<p>This will download Ninject and all the other things it needs in order to work, including the WebActivator library, which gives us a place to create our dependencies.</p>
<p>4) Once everything has installed, look for the ‘AppStart_NinjectMVC3.cs’ file which has now appeared in your solution and open it:</p>
<pre class="brush: csharp; title: Code:; notranslate">namespace MvcNinjectExample {
  public static class AppStart_NinjectMVC3 {

    public static void RegisterServices(IKernel kernel)
    {
      //kernel.Bind&lt;IThingRepository&gt;().To&lt;SqlThingRepository&gt;();
    }

    public static void Start() {
      // Create Ninject DI Kernel
      IKernel kernel = new StandardKernel();

      // Register services with our Ninject DI Container
      RegisterServices(kernel);

      // Tell ASP.NET MVC 3 to use our Ninject DI Container
      DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
    }
  }
}
</pre>
<p>You can see here how the Ninject Kernel is created, which is responsible for resolving our types. In RegisterServices() is where we would bind our contracts to our implementations. The DependencyResolver is used to register Ninject for type injection, which means that Ninject is used to resolve dependencies whenever they are needed by MVC.</p>
<p>Let’s have a quick look at how we can apply our logger to this and get our controllers using our logging framework.</p>
<p>5) Add a new folder called ‘Logging’ to the solution and create a new class called <em>ILogger</em>, and copy this code into it (overwrite the class which has been generated for you):</p>
<pre class="brush: csharp; title: Code:; notranslate">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcNinjectExample.Logging
{
  public interface ILogger
  {
    void LogMessage(string message);
  }
}
</pre>
<p>Create another class called TextFileLogger and copy this code into it, again overwriting the class which was generated for you:</p>
<pre class="brush: csharp; title: Code:; notranslate">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Hosting;

namespace MvcNinjectExample.Logging
{
  public class TextFileLogger : ILogger
  {
    public void LogMessage(string message)
    {
      string path = Path.Combine(HostingEnvironment.MapPath(&quot;~/app_data&quot;), &quot;log.txt&quot;);
      using (FileStream stream = new FileStream(path, FileMode.Append))
      {
        StreamWriter writer = new StreamWriter(stream);
        writer.WriteLine(message);
        writer.Flush();
      }
    }
  }
}
</pre>
<p>You will need to include a using statement for System.IO at the top of the file to get access to the FileStream class. Once all done, you should end up with your code and solution looking like this:</p>
<p><a href="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial5.png"><img class="alignnone size-medium wp-image-393 colorbox-379" title="DI MVC 5" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial5-300x236.png" alt="" width="300" height="236" /></a></p>
<p>Note that I have also created the App_Data directory (which is needed by the TextFileLogger) so that the log file has somewhere to go.</p>
<p>6) Now, head back to your “AppStart_NinjectMVC3.cs” file and set up the binding for this class:</p>
<pre class="brush: csharp; title: Code:; notranslate">public static void RegisterServices(IKernel kernel)
{
  kernel.Bind&lt;ILogger&gt;().To&lt;TextFileLogger&gt;();
}
</pre>
<p>7) Finally, lets create a controller which uses it. Right-click on the ‘Controllers’ folder in Solution Explorer, select ‘Add &gt;’ and choose the ‘Controller…’ option at the very top. Give it a name of “HomeController” and press enter.</p>
<p>Inside this controller, we create a constructor which takes a parameter of <em>MvcNinjectExample.Logging.ILogger</em>, and we’ll save that into a class-level private variable for later. Ninject is going to give us the actual implementation of that in accordance with the bindings we set up in the last step. I’ve also modified the index view so that is just returns a piece of content rather than looking for a view; this is just to prevent exceptions from being thrown as I’m not creating a view for this example. Finally, I’ve also called into the logger from the index view to log a message for us. After we run the page, we should get a log.txt file in the App_Data directory containing our message.</p>
<p>After my modifications, the controller now looks like this:</p>
<pre class="brush: csharp; title: Code:; notranslate">public class HomeController : Controller
{
  MvcNinjectExample.Logging.ILogger _logger;

  public HomeController(MvcNinjectExample.Logging.ILogger logger)
  {
    _logger = logger;
  }

  public ActionResult Index()
  {
    _logger.LogMessage(&quot;Running index page!&quot;);

    return Content(&quot;Message logged&quot;);
  }
}
</pre>
<p>This is the general pattern you will use with Dependency Injection in MVC; including your dependency contracts in your controller’s constructor, having Ninject fulfil the dependencies for you then consuming them from within the controller actions.</p>
<p>Let’s put a breakpoint in the constructor and see what we get when we run the project:</p>
<p><a href="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial6.png"><img class="alignnone size-medium wp-image-394 colorbox-379" title="DI MVC 6" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial6-300x25.png" alt="" width="300" height="25" /></a></p>
<p>As we intended, without doing any additional code than what I’ve presented here, we now have an instance of <em>ILogger</em> passed to us when the controller was created. This has happened because MVC has effectively asked Ninject to create this controller for us. During creation, Ninject has noticed that it requires an implementation of <em>ILogger</em> in order to create this controller. Since we registered a binding to <em>ILogger</em> in AppStart_NinjectMVC3, it knows how to create one of these and so it can go ahead and completely fulfil the dependency on this controller. Hence we have Dependency Injection.</p>
<p>If I let the solution run again, we should hit the Index action and the text file will be created with the log message inside it:</p>
<p><img class="alignnone size-full wp-image-395 colorbox-379" title="DI MVC 7" src="http://stevescodingblog.co.uk/wp-content/uploads/2011/02/tutorial7.png" alt="" width="531" height="345" /></p>
<p>If you were to change this to use our EventFileLogger implementation as in one of the earlier examples, you will see that all you would have to do is create the implementation, make it implement <em>ILogger</em> and then register it in your kernel bindings. No changes to any controllers which use it are necessary.</p>
<p>You should now follow this pattern when adding more dependencies to your project, and as your project scales in size you will see how easy it is to manage components in a loosely-coupled fashion even within a web project such as this. It gives you lots of flexibility and ultimately makes your projects a lot more maintainable and easy to change and adapt.</p>
<p><a href="http://dl.dropbox.com/u/5229980/scb/code/MvcNinjectExample.zip">Download project source code</a></p>
<div class="shr-publisher-379"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fdependency-injection-beginners-guide%2F' data-shr_title='Dependency+Injection%3A+A+Beginner%27s+Guide'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fdependency-injection-beginners-guide%2F' data-shr_title='Dependency+Injection%3A+A+Beginner%27s+Guide'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fstevescodingblog.co.uk%2Fdependency-injection-beginners-guide%2F' data-shr_title='Dependency+Injection%3A+A+Beginner%27s+Guide'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://stevescodingblog.co.uk/dependency-injection-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

