<?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>Chris van de Steeg &#187; .net</title>
	<atom:link href="http://www.chrisvandesteeg.nl/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisvandesteeg.nl</link>
	<description>What&#039;s practical is logical</description>
	<lastBuildDate>Thu, 06 May 2010 07:42:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fluent NHibernate&#8217;s AutoPersistenceModel: I Love it!</title>
		<link>http://www.chrisvandesteeg.nl/2008/12/02/fluent-nhibernates-autopersistencemodel-i-love-it/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/12/02/fluent-nhibernates-autopersistencemodel-i-love-it/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 20:52:04 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/12/02/fluent-nhibernates-autopersistencemodel-i-love-it/</guid>
		<description><![CDATA[I checked out the latest version of Fluent NHibernate today and noticed the AutoPersistenceModel. I looked at the unit-tests to see what it’s supposed to do and I loved what I saw… Let’s say you have a 2 simple classes like this: Now, in the global.asax’s init (assuming a web application), we can configure NHibernate [...]]]></description>
			<content:encoded><![CDATA[<p>I checked out the latest version of <a href="http://code.google.com/p/fluent-nhibernate/">Fluent NHibernate</a> today and noticed the AutoPersistenceModel. I looked at the unit-tests to see what it’s supposed to do and I loved what I saw…</p>
<p>Let’s say you have a 2 simple classes like this:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="259" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image.png" width="344" border="0" /> </p>
<p>Now, in the global.asax’s init (assuming a web application), we can configure NHibernate using Fluent NHibernate like this:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="185" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image1.png" width="577" border="0" /> </p>
<p>It uses convention over configuration in lots of places, eg the Id property is always used as identity-column if a property with that name exists.   <br />But you could also easily add your own conventions. If you want to have the Id colums to be ArtistId and AlbumId (and any other className + Id), you’d do:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="56" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image2.png" width="465" border="0" /> </p>
<p>Want to restrict the namespace as well? No problem!</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="68" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image3.png" width="463" border="0" /> </p>
<p>Fantastic! Now, to see what mapping file this code automagically creates for our Artist class, we can run the following code:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="119" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image4.png" width="300" border="0" /> </p>
<p>which returns an XmlDocument. In this case, the InnerXml of that document looks like this:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="312" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image5.png" width="552" border="0" /> </p>
<p>Again: fantastic! See how it even generates the one-to-many relation with the correct end-class, because we defined a property IList&lt;Album&gt;!</p>
<p>Running the same code for the Album class gives:</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="246" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/12/image6.png" width="520" border="0" /> </p>
<p>If you want to use Fluent NHibernate the ‘old’ way, you can still add ClassMap&lt;Artist&gt; to your assembly. Hence, you can even mix it with the AutoPersistenceModel! If you have a classmap file, it’ll use that for your class, if it isn’t there it’ll automatically create a mapping for you!</p>
<p>I’m sold! This makes using NHibernate a breeze. You can get up and running with your NHibernate project so much faster now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/12/02/fluent-nhibernates-autopersistencemodel-i-love-it/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Howto use jQuery with asp.net mvc preview 4&#8242;s AjaxHelper</title>
		<link>http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 20:56:11 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/</guid>
		<description><![CDATA[Working on jQuery4mvc preview 4, one of the first things I ofcourse wanted to do was to replace the ASP.NET Ajax library that&#8217;s used by the AjaxHelper. Unfortunately the AjaxHelper itself is sealed, so atm we can&#8217;t (easily) change how the AjaxHelper generates its links. So, as an alternative, I created a javascript library that [...]]]></description>
			<content:encoded><![CDATA[<p>Working on jQuery4mvc preview 4, one of the first things I ofcourse wanted to do was to replace the ASP.NET Ajax library that&#8217;s used by the AjaxHelper.</p>
<p>Unfortunately the AjaxHelper itself is sealed, so atm we can&#8217;t (easily) change how the AjaxHelper generates its links. So, as an alternative, I created a javascript library that uses the same methodnames as the MicrosoftMvcAjax version.</p>
<p>Though at first I was upset that AjaxHelper wasn&#8217;t opened up, this javascript-only solution makes it very easy to use. All you have to do is </p>
<ul>
<li>place the <a href="http://jquery.com/">latest jQuery version</a> in your Content folder; </li>
<li>place <a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=15521">my javascript file</a> in your Content folder; </li>
<li>add references in your views (or masterpage ofcourse) to these 2 files instead of referencing the MicrosoftMvcAjax.js and MicrosoftAjax.js file.</li>
</ul>
<p>If you haven&#8217;t played with the AjaxHelper yet, take a look at <a href="http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx">Scott Hanselman&#8217;s post on Using Ajax and Ajax.Form</a></p>
<p>This jQuery version also has a little extra: browser-history support for your ajax requests</p>
<p>Now, go <a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=15521">download</a> the js file and play!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>jQuery for Asp.net MVC preview 3</title>
		<link>http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 13:13:08 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/</guid>
		<description><![CDATA[So, I finally got a chance to sit down and finish up the jQuery for Asp.net Mvc preview 3. If you haven&#8217;t already read Scott&#8217;s post about preview 3,&#160; you should definitely do so before reading this one. So, quite some things have changed in preview 3, which made me decide to change a lot [...]]]></description>
			<content:encoded><![CDATA[<p>So, I finally got a chance to sit down and finish up the jQuery for Asp.net Mvc preview 3. If you haven&#8217;t already read <a href="http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-preview-3-release.aspx">Scott&#8217;s post about preview 3</a>,&#160; you should definitely do so before reading this one.</p>
<p>So, quite some things have changed in preview 3, which made me decide to change a lot in the jQuery for Asp.net mvc (<em>jqmvc</em>) too. To start with most radical one: I ditched the ajax.master. This little &#8216;framework&#8217; does not rely on a special masterpage in your website from now on. Since things have radically changed, I&#8217;ll start all over again on how to use this project. </p>
<p>In this post, I&#8217;ll explain how the project works globally. I won&#8217;t go into too much details about everything, but I will just explain the things you&#8217;ll directly touch in your application. In the next post, I&#8217;ll explain how to <em>use</em> the project in your website (until that time you could take a look at the included sample website). That post will explain the Ajax- and HtmlHelpers in this project and it will explain how to do an ajax request using jQuery.</p>
<p>Before my story starts, let&#8217;s start with the links and downloads for all those who want to read code rather than stories :</p>
<ul>
<li><a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=14383">Downloads</a>&#160;</li>
<li><a href="http://code.google.com/p/jquerymvc/source/checkout">SVN source Repository</a></li>
<li><a href="http://code.google.com/p/jquerymvc/downloads/detail?name=preview%203%20bin.zip&amp;can=2&amp;q="></a><a href="http://www.chrisvandesteeg.nl/demo/jquerymvc3/default.aspx">The online demo</a> </li>
<li><a href="http://www.codeplex.com/jquery4mvc/">Codeplex homepage</a></li>
</ul>
<h2>Application and routes</h2>
<p>To have a quick go for your application, it&#8217;s recommended that you inherit your application from JqueryMvc.JqueryMvcApplication. You can ofcourse easily do this in your global.asax like so:    <br /><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="22" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image3.png" width="531" border="0" />     <br />This application sets up the Unity dependency container and will set up the default routes. If you wish to setup a different (or no) dependency container, you should override InitializeContainer() and ConfigureContainer(). JQueryMvc itself is no longer dependent on a dependency container.</p>
<p>I found it hard to set up the default routes in preview 3 if your server does not support wildcard redirects (like visual studio&#8217;s webdevserver doesn&#8217;t). In that case you need to catch existing files (default.aspx needs to exist, but shouldn&#8217;t do anything), but you need to skip the Content folder. I came up with the following solution:    <br /><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="648" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image6.png" width="536" border="0" />     <br />So if there&#8217;s a default.aspx in the root of your application, it set&#8217;s routeExistingFiles to true. If the url matches the RegEx ^Content\/.* the routing will be stopped using the new IgnoreRoute method. Also, your controllers will then be extended with .aspx to be sure the requests passes the asp.net cycle. If you have the option to catch all incoming requests, all you have to do is remove the default.aspx from the root of your website.</p>
<h1></h1>
<h2>Controllers</h2>
<p>As you&#8217;ve seen in Scott&#8217;s post, controllers should now return an ActionResult. The normal way would be to have your controller action return the result of the View() method. With <em>jqmvc</em> you should not call the View() method at the end of your action. Ofcourse, if you&#8217;re sure you want to return the normal view, you could still do so. In <em>jqmvc</em> there is a ExtController which you should inherit your controllers from. This baseclass contains a method called Action() which accepts the same attributes as View() but does some magic to decide what type of request the current request is, and will try to return the correct ActionResult for that type of request. This could be either an Ajax-request, a Json request or a normal page-request. Your typical controller with some actions would look like this:</p>
<p>&#160;&#160; <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="399" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image7.png" width="537" border="0" /></p>
<h2>Views</h2>
<p>If <em>jqmvc</em> encountered the current request as a normal page request, it will create the default ViewResult, nothing special there.</p>
<p>If <em>jqmvc </em>decided Json should be returned, a JsonResult is created and no further actions will be taken. The rendered view will be the ViewData serialized to Json. The ViewEngine of the controller is also changed to a JsonViewEngine but this is only called upon in case of an Exception.</p>
<p>If <em>jqmvc</em> decided that the request is an Ajax request, it will create a normal ViewResult, but it will change the ViewLocator of the controller.     <br />(Offtopic: I think it&#8217;s a design-error to have the viewlocator on the controller. It should be on the ViewResult object, that&#8217;s the only object that uses it. Perhaps this will be changed in the next preview&#8230;)     <br />This AjaxViewLocator will first look for .ascx files instead of .aspx files. If it can&#8217;t find the .ascx it will load .aspx afterall.&#160; You can also create a target-specific .ascx file: if you do an ajax-request through our javascript function jQuery.mvc.request() (or one of the htmlhelper functions), you have to specify a target for the loaded content. Let&#8217;s say we want to load /Home/Index into a &lt;div id=&quot;dynamiccontent&quot;&gt;&lt;/div&gt;     <br />The AjaxViewLocator will then look for :</p>
<ol>
<li>/Views/Home/Index.dynamiccontent.ascx </li>
<li>/Views/Home/Index.acx </li>
<li>/Views/Home/dynamiccontent.ascx </li>
<li>/Views/Home/Index.aspx </li>
<li>/Views/Shared/Index.dynamiccontent.ascx </li>
<li>/Views/Shared/Index.ascx </li>
<li>/Views/Shared/dynamiccontent.ascx </li>
<li>/Views/Shared/Index.aspx </li>
</ol>
<p>To have the page /Home/index available both as an ajax request as well as normal request, you could create an /Views/Home/Index.aspx which loads the index.ascx like so:<img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="25" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image1.png" width="454" border="0" /> </p>
<h2>Things to know</h2>
<h4></h4>
<ul>
<li>
<p>We have some HelperClasses to simplify the use of <em>jqmvc</em></p>
</li>
<li>
<p>The ViewData keys &quot;Messages&quot; and &quot;Errors&quot; are treated specially by <em>jqmvc.</em> </p>
</li>
<li>
<p>Your page should always contain:</p>
<ul>
<li>
<p>&lt;%= Html.RegisterJqueryMvc(Page.ClientScript) %&gt; To register the required javascript files</p>
</li>
<li>
<p>&lt;div id=&quot;loading&quot;&gt;&lt;/div&gt;; or something else with id=&quot;loading&quot;. This will be made visible during ajax-requests</p>
</li>
<li>
<p>&lt;div id=&quot;errors&quot;&gt;&lt;/div&gt; to display errors</p>
</li>
<li>
<p>&lt;div id=&quot;messages&quot;&gt;&lt;/div&gt; to display messages (informational)</p>
</li>
</ul>
</li>
<li>
<p>We have a special contentplaceholder in JqueryMvc.UI.WebControls.ContentPlaceHolder. This will use the AjaxViewLocator to load some default content. Eg, if used like this in your masterpage, and the current request is /Home/Index:        <br />&lt;jq:ContentPlaceHolder ID=&quot;maincontent&quot; runat=&quot;server&quot; /&gt;         <br />It will look for for content in:</p>
<ol>       </ol>
<ol>
<li>/Views/Home/Index.maincontent.ascx </li>
<li>/Views/Home/Index.acx </li>
<li>/Views/Home/maincontent.ascx </li>
<li>/Views/Home/Index.aspx </li>
<li>/Views/Shared/Index.maincontent.ascx </li>
<li>/Views/Shared/Index.ascx </li>
<li>/Views/Shared/maincontent.ascx </li>
<li>/Views/Shared/Index.aspx
<p>This way, your index.aspx could just be an empty page that points to MasterPage with this default-content-loading contentplaceholer.<img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="27" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image2.png" width="454" border="0" /> </li>
</ol>
</li>
<li>Your ascx files should inherit from JqueryMvc.Mvc.ExtViewUserControl to have access to the AjaxHelper (bug in asp.net mvc!) and to have the messages &amp; errors parsed by <em>jqmvc</em>. </li>
<li>In case of an error the default controller will look for a rescue page in /Views/[ControllerName]/Rescues/error.aspx or /Views/Shared/Rescues/error.aspx (or .ascx in case of an ajax request). You can specify your own rescues for different types of errors by using the RescueAttribute of </li>
<li>You should remove default.aspx if your application-server supports wildcard mappings. </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>jQuery for ASP.NET MVC preview 2</title>
		<link>http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 19:37:22 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/</guid>
		<description><![CDATA[I updated the jQueryMvc project so that it works with ASP.NET MVC preview 2. I also switched the IoC container from Objectbuilder to Unity. I submitted the Unity container to the MvcContrib project but it has not yet been accepted in their repository, so you&#8217;ll have to do with the unofficial version I include in [...]]]></description>
			<content:encoded><![CDATA[<p>I updated the jQueryMvc project so that it works with <a href="http://www.google.nl/url?sa=t&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.microsoft.com%2Fdownloads%2Fdetails.aspx%3FFamilyID%3D38cc4cf1-773a-47e1-8125-ba3369bf54a3&amp;ei=t57qR77FIJacxAHfgu31Cg&amp;usg=AFQjCNH5Y3w0Qt8KMrr3vLRHHQH1GwEJpQ&amp;sig2=_BtORUlMGY88ZK2Qqjz46g">ASP.NET MVC preview 2</a>. I also switched the IoC container from Objectbuilder to <a href="http://www.codeplex.com/unity">Unity</a>. I submitted the Unity container to the <a href="http://www.mvccontrib.org">MvcContrib</a> project but it has not yet been accepted in their repository, so you&#8217;ll have to do with the unofficial version I include in the download. Unity is, to put it very baldly, Objectbuilder version 2. So it seemed logical to make the switch.</p>
<p>I also made up a nice(r) way to inject the ViewEngine into the BaseController. It&#8217;s now injected through a public method using a MethodInjection attribute:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="137" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/03/image.png" width="450" border="0"/></p>
<p>Thanks to this method, we no longer have pre-register all the Controllers with our container (in the previous version of jQueryMvc I scanned all dll&#8217;s on ApplicationStart to register the Controller and assign the ServiceViewFactory as the default ViewFactory)</p>
<p>Furthermore, since the ASP.NET MVC team decided to change the way the Views are rendered (now through a ViewEngine, which is nice I think), it was no longer needed to use the code from <a href="http://www.aaronlerch.com/blog/2008/01/01/unifying-web-sites-and-web-services-with-the-aspnet-mvc-framework/">Aaron Lerch</a>. Though still inspired by his code ofcourse, I removed almost all of his code now. We now have a single &#8216;<a href="http://jquerymvc.googlecode.com/svn/trunk/jQueryMvc/ServiceViewEngine.cs">ServiceViewEngine</a>&#8216; inheriting from the ASP.NET MVC&#8217;s default WebFormViewEngine. All code deciding what view to render, is now located in this class.</p>
<p>What I did not like about preview 2, is that the name of the master-template cannot be set from within the ViewEngine. It&#8217;s now stored into the ViewContext, and the setter is private <img src='http://www.chrisvandesteeg.nl/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> . So now the mastertemplate is set in the BaseController, causing the BaseController to have to know what type of request the current one is (which is bad):</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="191" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/03/image1.png" width="577" border="0"/> </p>
<p>As usual, you can download the hottest source from the Subversion repository at <a title="http://code.google.com/p/jquerymvc/source/checkout" href="http://code.google.com/p/jquerymvc/source/checkout">http://code.google.com/p/jquerymvc/source/checkout</a> or you can <a href="/download/jquerymvc/jquerymvc-0.3.zip">download a zipped package of the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery for ASP.NET Mvc &#8211; part 2</title>
		<link>http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 20:35:59 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/</guid>
		<description><![CDATA[[go to downloads] A view days after I created my sample project that added some jQuery magic to the ASP.NET Mvc Framework, Aaron Lerch wrote an interesting article. He used a ViewFactory to do the json-serialization and added XML serialization as well. I liked this approach better then using masterpages. I took his code, altered [...]]]></description>
			<content:encoded><![CDATA[<p><a href="#downloads">[go to downloads]</a></p>
<p>A view days after I created my sample project that added some jQuery magic to the ASP.NET Mvc Framework, <a href="http://www.aaronlerch.com/">Aaron Lerch</a> wrote an <a href="http://www.aaronlerch.com/blog/2008/01/01/unifying-web-sites-and-web-services-with-the-aspnet-mvc-framework/">interesting article</a>.</p>
<p>He used a ViewFactory to do the json-serialization and added XML serialization as well. I liked this approach better then using masterpages. I took his code, altered some bits and created a library called jQueryMvc with it. This library also utilizes the <a href="http://www.codeplex.com/MVCContrib">MvcContrib</a> project. It&#8217;s now even easier to make your ASP.NET Mvc project ajax-enabled with jQuery, it just requires 4 steps:</p>
<ol>
<li>Add a reference to jQueryMvc.dll </li>
<li>Make your global.asax inherit from JqueryMvcApplication </li>
<li>Add &lt;%= Ajax.RegisterJqueryMvc(Page.ClientScript) %&gt; to the &lt;head&gt; section of your (master)pages. </li>
<li>Let your controllers inherit from BaseController </li>
</ol>
<p>That&#8217;s it, your all set. You should take a look at the SampleSite that&#8217;s included in the download on how to use the utility. Using it works almost the same as the previous version, so if you haven&#8217;t done so, you should read my <a href="http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/">previous article</a>. The only difference is the exception handling. Since I decided to embrace the MvcContrib project, the exception handling for ajax &amp; html requests have changed. Exceptions are now handled with a [Rescue] attribute. The BaseController has a default Rescue attribute attached which leads all exceptions to the &quot;error.aspx&quot; rescue handler. So your project should at least have /Views/Shared/Rescues/error.aspx that outputs the exceptions for your site. If you want to catch specific exception for yourself, you can add a rescue attribute to your site that catches that error type. E.g. if you want to catch a SecurityException, you could add<a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="44" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image-thumb.png" width="509" border="0" /></a> </p>
<p>If an exception of type SecurityException is thrown during a controller action execution in your HomeController, MvcContrib will look for /Views/Home/Rescues/security.aspx and if that doesn&#8217;t exist, it will look for /Views/Shared/Rescues/security.aspx. The viewdata for those pages will be the exception itself. The rescue attribute can also be used on method level.</p>
<p>Another nice thing is that the MvcContrib is build to use Inversion of Control (IoC). <strong>BUT!</strong>: if you don&#8217;t want to do anything with IoC for now, you can leave it where it is! jQueryMvc just uses it for attaching the ViewFactory to your controllers, that&#8217;s all you need to know. You can also stop reading just here, the rest is about the dependency container that I used.</p>
<p>For jQueryMvc I choose the <a href="http://www.codeplex.com/ObjectBuilder">Microsoft Objectbuilder</a> framework for this, although, with MvcContrib, you&#8217;re free to choose an other framework. I choose the ObjectBuilder, since I&#8217;m the one that committed that to MvcContrib <img src='http://www.chrisvandesteeg.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In JqueryMvcApplication, when the application get&#8217;s initialized, we&#8217;re looking for all controllers in the currently running project. <a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="292" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image-thumb1.png" width="756" border="0" /></a></p>
<p>All these controllers are registered with the Dependency container and our custom ViewFactory is also registered with this container. The ObjectBulderControllerFactory makes sure the controllers are fetched through that very container, and when that happens, the ViewFactory is attached to them.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="108" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image-thumb2.png" width="723" border="0" /></a></p>
<p>There are also some&#160; nice helper functions in JqueryMvcApplication, for making the registration of your own services/helpers/etc into the container a breeze.</p>
<p>I&#8217;ll talk about using the container in some other article sometime.</p>
<p>For now, have fun with jQueryMvc.</p>
<p> <a name="downloads"></a>
<p><strong>Downloads</strong></p>
<ul>
<li><a href="http://code.google.com/p/jquerymvc/">Project at GoogleCode</a> </li>
<li><a href="/download/jquerymvc/jquerymvc2.zip">Download source, binaries &amp; sample project</a> </li>
<li><a href="/demo/jquerymvc2/">View online demo of the sample site</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Ajax and json for ASP.NET MVC with jQuery</title>
		<link>http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/</link>
		<comments>http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 17:28:39 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/</guid>
		<description><![CDATA[I&#8217;ve created an example project that uses the jQuery library to create an ajax enabled ASP.NET MVC website. Generally what this project does: Load views through ajax (with back-button support) Do ajax-style form-posts and retrieve only the messages, errors and updated content Do json form-posts and retrieve the errors and messages as a json object [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve created an example project that uses the <a href="http://jquery.com">jQuery</a> library to create an ajax enabled <a href="http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx">ASP.NET MVC</a> website. Generally what this project does:</p>
<ul>
<li>Load views through ajax (with back-button support) </li>
<li>Do ajax-style form-posts and retrieve only the messages, errors and updated content </li>
<li>Do json form-posts and retrieve the errors and messages as a json object </li>
</ul>
<p>Here are the quicklinks for all you people who don&#8217;t want to read the story.</p>
<ul>
<li><a href="/demo/jquerymvc/">View online demo</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.zip">Download webproject</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.vsi">Download Visual Studio template</a> </li>
</ul>
<p>For all you people who want read about it before playing around, here&#8217;s a little intro to the code&#8230;</p>
<p>The project contains a BaseController, which should be inherited by all your controllers. This controller (ofcourse) overrides the System.Web.Mvc.Controller and handles the detection of Ajax requests and/or Json requests. It does that by overriding the Execute method and reading some http-headers. Unfortunately reading http-headers is the only (evil) way I know to detect this.    <br />Fortunately, while I was playing with this project, I saw that <a href="http://weblogs.asp.net/mschwarz/archive/2007/12/19/mvc-web-application-and-json.aspx">Michael Schwarz</a> posted exactly (well, 99%) the same solution as I figured out, so for now I&#8217;ll take this as the best solution until someone provides me a better one.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image3.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="748" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb2.png" width="536" border="0" /></a></p>
<p>As you can see, there&#8217;s also some error handling in here. If there is an exception thrown during the controller execution, it&#8217;s wrapped in a simple class called SimpleException. This class is easily serializable, so it can be passed on as a javascript object to the browser, in case of a json request.&#160; In case of a non-json request (either normal or ajax), the default error handler called &quot;error.aspx&quot; in views/shared/ is rendered.</p>
<p>The BaseController also overrides the RenderView. In the RenderView, we do some special stuff if the request is a json- or ajax-request.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="536" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb3.png" width="537" border="0" /></a></p>
<p>If the current request is a json-request, the template &quot;json.aspx&quot; is called upon. The default &quot;json.aspx&quot; is located at /views/shared/json.aspx and the only thing it does is serializing the current ViewData.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image5.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="110" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb4.png" width="434" border="0" /></a></p>
<p>One special note is that in the codebehind, I had &#8216;strong type&#8217; the ViewData to an &#8216;object&#8217;, since the ViewData class doesn&#8217;t allow us to serialize the real object.</p>
<p>Then there are the 2 masterpages: ajax.master and site.master. Both of them contain 4 ContentPlaceHolders:</p>
<ul>
<li>Errors </li>
<li>Messages </li>
<li>maincontent </li>
<li>dynamiccontent </li>
</ul>
<p>Errors and messages should ofcourse output the errors and informational messages for the current request. I created a handy extension method on the HtmlHelper to quickly output these:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="171" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb5.png" width="494" border="0" /></a></p>
<p>The maincontent should contain the entire page without the usual MasterPage stuff (eg navigation). The dynamiccontent should only contain the content that can change during the same request (eg. the &#8216;hello {name}&#8217; part in the <a href="/demo/jquerymvc/">demo</a>).</p>
<p>In case of an ajax request, the javascript adds a querystring to the request telling what content is requested (either maincontent or dynamiccontent) and the ajax.master decides what to output for that request based on this querystring:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image7.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="314" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb6.png" width="536" border="0" /></a></p>
<p>Ajax links are very easily created, also through an extension method:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image8.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="58" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb7.png" width="536" border="0" /></a></p>
<p>The first parameter is the text of the link, the second one is the action on the controller and the third one is the id of the container-div.</p>
<p>The other client functions are all form related, you an see them in use in the /views/home/index.aspx template:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image9.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="203" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb8.png" width="534" border="0" /></a></p>
<p>You can use any of these methods to submit your forms, just use them as you like it. The methods can be found in /Content/mvc.js</p>
<p>Well that should be enough talking about code, just go ahead and play to find out the rest by yourself.</p>
<li><a href="/demo/jquerymvc/">View online demo</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.zip">Download webproject</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.vsi">Download Visual Studio template</a> </li>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>asp.net mvc -&gt; the rise of the messy controllers</title>
		<link>http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/</link>
		<comments>http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 08:40:24 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/</guid>
		<description><![CDATA[All the buzz these days goes to asp.net mvc and as many others, I&#8217;m loving it! Messing around with it, and looking at samples of others, I see the controllers tend to get quite messy. Where normally the codebehinds got messy, it&#8217;s now moved to the controllers. Let&#8217;s say we&#8217;re using Subsonic as our data-access [...]]]></description>
			<content:encoded><![CDATA[<p>All the buzz these days goes to <a href="http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx">asp.net mvc</a> and as many others, I&#8217;m loving it!</p>
<p>Messing around with it, and looking at samples of others, I see the controllers tend to get quite messy. Where normally the codebehinds got messy, it&#8217;s now moved to the controllers.</p>
<p>Let&#8217;s say we&#8217;re using <a href="http://www.subsonicproject.com">Subsonic<br />
</a> as our data-access layer and we have created this controlleraction:</p>
<pre name="code" class="c-sharp">
[ControllerAction]
public void List(string foldername)
{
    ICollection folders = GetFolders(userId);
    bool hasInbox = false, hasDrafts = false, hasSent = false, hasBin = false;
    foreach (PrivateMessageFolder folder in folders)
    {
        hasInbox = hasInbox || folder.FolderType == PrivateMessageFolderType.Inbox;
        hasDrafts = hasDrafts || folder.FolderType == PrivateMessageFolderType.Drafts;
        hasSent =hasSent || folder.FolderType == PrivateMessageFolderType.Sent;
        hasBin = hasBin|| folder.FolderType == PrivateMessageFolderType.Bin;
        if (hasInbox &#038;&#038; hasDrafts &#038;&#038; hasSent &#038;&#038; hasBin
            ) break;
    }
    if (!hasInbox)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Inbox, userId));
    }
    if (!hasDrafts)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Drafts, userId));
    }
    if (!hasSent)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Sent, userId));
    }
    if (!hasBin)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Bin, userId));
    }
    PrivateMessageCollection messages = new PrivateMessageCollection();
    messages.LoadAndCloseReader(SPs.GetMessages(foldername, UserId).GetReader());
    PMData data = new PMData
    {
        Folders = folders,
        PrivateMessages = messages
    };
    RenderView("List", data);
}
</pre>
<p>Now, while this works perfectly fine, it&#8217;s messy. It&#8217;s messy because now my controller is very dependant on how subsonic works. So if subsonic changes in the future, or I choose to pick another dataaccess layer since subsonic support has stopped(or whatever reason), I have to rewrite all my controllers! Not nice, not nice at all.<br />
It&#8217;s also messy because I can&#8217;t see at one glance what my controller is passing to my view. I have to actually understand the code to see what it&#8217;s passing on.</p>
<p>Now look at this peice of code:</p>
<pre name="code" class="c-sharp">
Daos.PrivateMessageDao privateMessageDao = new Forums.Daos.PrivateMessageDao();

[ControllerAction]
public void List(string foldername)
{
    //load the collection
    PMData data = new PMData
    {
        Folders = privateMessageDao.GetAndEnsureFolders(UserId),
        PrivateMessages = privateMessageDao.GetMessages(foldername, UserId)
    };
    RenderView("List", data);
}
</pre>
<p>All code that has been removed, is now in the privatemessagedao</p>
<p>To me, its quite clear what this is passing on, it&#8217;s very clean code. I moved all the data-access parts to the Dao. So the dao (or service, or what ever you want to name it) contains the logic that&#8217;s needed to retreive, update, delete, etc. The controller shouldn&#8217;t even care how that dao does that, it just needs the <a href="http://en.wikipedia.org/wiki/POCO">POCO</a>&#8216;s. </p>
<p>In our example, we also have folders to be created by default. This is by no means the responsibility of the controller, the controller should just tell something else that it needs those folders, that&#8217;s it. The other thingy should create them.</p>
<p>Now we have seperation of concerns, so we have the possibility of using Dependency Injection for accessing the dao (and others), but I&#8217;ll write about that some other time</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AppDomain recycle different from real restart</title>
		<link>http://www.chrisvandesteeg.nl/2006/06/15/appdomain-recycle-different-from-real-restart/</link>
		<comments>http://www.chrisvandesteeg.nl/2006/06/15/appdomain-recycle-different-from-real-restart/#comments</comments>
		<pubDate>Thu, 15 Jun 2006 10:14:40 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>

		<guid isPermaLink="false">http://blog.ilikeu2.nl/2006/06/15/appdomain-recycle-different-from-real-restart/</guid>
		<description><![CDATA[In iis6 there is this feature to recycle your appdomain at certain tresholds, great. But I recently ran into strange problems when my appdomain was recycled. When my application starts, it loops through all the assemblies it gets from System.AppDomain.CurrentDomain.GetAssemblies(). This works great when the application starts for the first time, I really do get [...]]]></description>
			<content:encoded><![CDATA[<p>In iis6 there is this feature to recycle your appdomain at certain tresholds, great.</p>
<p>But I recently ran into strange problems when my appdomain was recycled. When my application starts, it loops through all the assemblies it gets from System.AppDomain.CurrentDomain.GetAssemblies(). This works great when the application starts for the first time, I really do get all assemblies that are in my bin-folder. However, when my worker-process gets killed, the next application start will <em>NOT</em> return all the assemblies in the bin folder. It will only return the assemblies that are used up untill that moment. However, changing the bin folder, somehow triggers a different recycle: this one <em>DOES</em> load all assemblies from my bin folder.</p>
<p>Conclusion: there are 2 types of recycles</p>
<ul>
<li>one that does load all assemblies from &#8216;bin&#8217; into the appdomain as the application starts. And</li>
<li>one that only loads the assemblies from &#8216;bin&#8217; into the appdomain as soon as they&#8217;re needed.</li>
</ul>
<p>I didn&#8217;t exactly figure out what causes this, and why it is done so. Only thing I could think of (and did) is to check if only my basic assemblies where returned by System.AppDomain.CurrentDomain.GetAssemblies(). If so, I loop through the bin folder&#8217;s files and load them all into the appdomain manually. This works and I do have all assemblies again on application-start, but it still is weird imo.</p>
<p><strong> Update: </strong>Hmm, more things seem to have <a href="http://weblogs.asp.net/owscott/archive/2006/02/21/438678.aspx">changed</a> regarding recycling</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2006/06/15/appdomain-recycle-different-from-real-restart/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
