<?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>source code bean &#187; C#</title>
	<atom:link href="http://sourcecodebean.com/archives/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>giving you tricks and tips of good coding</description>
	<lastBuildDate>Sun, 06 Jun 2010 16:40:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>First steps toward EPiServer and ASP.NET MVC</title>
		<link>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595</link>
		<comments>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595#comments</comments>
		<pubDate>Mon, 19 Apr 2010 20:33:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=595</guid>
		<description><![CDATA[Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC,  [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC,  to try to get a running prototype up. </p>
<p>I did some research online to see what others had done. I found two great posts by <a href="http://joelabrahamsson.com/entry/episerver-and-mvc-retrieving-current-page-using-a-custom-model-binder">Joel Abrahamsson</a> and one from <a href="http://fbrz.wordpress.com/2009/10/10/episerver-and-mvc/">Fabio Fabrizio</a>, who based his solution on Joels experiments. </p>
<p>Joels first attempt was to create a base class from which all Controllers inherited. However, this made the controller do more than it should and made it very hard to test. His second attempt was to create a custom model binder and make every action receive currentPage as a parameter. This approach makes it much easier to create unit tests for the code, since it is now possible to moc the currentPage parameter. </p>
<p>Fabio took a different approach and decided to implement a custom MVC handler and a controller factory. This approach fits very well with MVC and felt like the right way to go, so I decided to create a prototype based on Fabios code. Fabios code was based on EPiServer CMS 5 and ASP.NET MVC 1.0, but I wanted to use EPiServer 6 and ASP.NET MVC 2.0, so I started porting the solution.</p>
<p>Porting the solution to EPiServer 6 and ASP.NET MVC 2.0 required some changes to both code and configuration, which not all of them was trivial. At the moment I have a prototype up running, and it works remarkably well! I will continue my experiments and post my results.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>EPiServer friendly URLs for paginated pages (and why the asp:LinkButton must die)</title>
		<link>http://sourcecodebean.com/archives/episerver-friendly-urls-for-paginated-pages-and-why-the-asplinkbutton-must-die/510</link>
		<comments>http://sourcecodebean.com/archives/episerver-friendly-urls-for-paginated-pages-and-why-the-asplinkbutton-must-die/510#comments</comments>
		<pubDate>Mon, 15 Mar 2010 06:05:37 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=510</guid>
		<description><![CDATA[Recently I got a request from a customer to perform some search engine optimizing for an old EPiServer site we are maintaining. One of the optimizations was to fix the paging on their product page. The products are fetched from an external data source and are not stored as pages in EPiServer, thus normal EPiServer [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I got a request from a customer to perform some search engine optimizing for an old EPiServer site we are maintaining. One of the optimizations was to fix the paging on their product page. The products are fetched from an external data source and are not stored as pages in EPiServer, thus normal EPiServer paging controls can not be used. Image a normal paging control like this:</p>
<p>Prev 1, 2 , 3, &#8230;, 99 Next</p>
<p>A fairly normal approach to this would have been to use a query parameter for handling the paging. Like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;a href=&quot;/products/?page=4&quot;&gt;4&lt;/a&gt;</div>
</li>
</ol>
</div>
<p>By using this approach each page would have a unique entry point, using query strings is however not optimal for SEO, but even worse is using asp:LinkButtons. Whoever created the site had decided to use asp:LinkButtons. LinkButtons are very convenient to work with in ASP.NET, but the HTML code they generate is not very SEO friendly. This is the HTML that was generated:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;div id=&quot;ctl00_MainContent_pnlPaging&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;a id=&quot;ctl00_MainContent_rptPages_ctl00_lbPage&quot;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$rptPages$ctl00$lbPage&#8217;,&#8221;)&quot;&gt;1&lt;/a&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;a id=&quot;ctl00_MainContent_rptPages_ctl01_lbPage&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$rptPages$ctl01$lbPage&#8217;,&#8221;)&quot;&gt;2&lt;/a&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &lt;a id=&quot;ctl00_MainContent_rptPages_ctl02_lbPage&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$rptPages$ctl02$lbPage&#8217;,&#8221;)&quot;&gt;3&lt;/a&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;a id=&quot;ctl00_MainContent_lbNext&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$lbNext&#8217;,&#8221;)&quot;&gt;Next&lt;/a&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;/div&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>We can see that:</p>
<ul>
<li>LinkButtons generates a javascript that is run when the link is clicked, instead of using a normal link</li>
<li>Paging is handled using a postback, the pages will no longer have unique entry points.</li>
</ul>
<p>This is <strong>disastrous </strong>from a SEO perspective.</p>
<p>So lets fix it. First step is to replace the LinkButtons with normal links (asp:Hyperlink) &#8211; and then we are going to do some URL rewrite magic to create SEO-friendly URLs. The code behind was changed to assign links like <code>/products/?page=4</code>.</p>
<p>Now, to get URLs like <code>/products/page/4</code>, we can create a custom url rewrite module in EPiServer. First we create a new class, Rewrite,  that inherits from <code>EPiServer.Web.FriendlyUrlRewriteProvider</code>. In this class we override three methods (don&#8217;t ask we why they have such confusing names, someone at EPiServer must have been under the influsene of something when nameing them):</p>
<ul>
<li><code>ConvertToInternalInternal </code>- Used to convert from <code>/product/page/4/</code> to an internal EPiServer page with the page id as a query parameter</li>
<li><code>ConvertToExternalInternal </code>- Used to convert from an internal EPiServer URL (<code>/PageType.aspx?lotsofqueryparameters=values</code>) to an external (<code>/products/page/4/</code>)</li>
<li><code>ConvertToInternal </code>- Needed to work around URL rewrite caching behaviour in EPiServer, will get into more detail on this later.</li>
</ul>
<p>This is the class:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">namespace Utils</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">public class Rewrite : FriendlyUrlRewriteProvider <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// The regexp to match a paged url</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw4">string</span> _regexpPaging = @<span class="st0">&quot;(.+)/page/([0-9]+)/$&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; protected override bool ConvertToInternalInternal<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; UrlBuilder url, ref object internalObject<span class="br0">&#41;</span><span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; protected override bool ConvertToExternalInternal<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; UrlBuilder url, object internalObject, Encoding toEncoding<span class="br0">&#41;</span><span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; public override bool ConvertToInternal<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; UrlBuilder url, out object internalObject<span class="br0">&#41;</span> <span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><br/></p>
<p>The first method we implement is the ConvertToInternalInternal which will convert the URL to an internal EPiServer URL.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected override bool ConvertToInternalInternal<span class="br0">&#40;</span>UrlBuilder url, ref object internalObject<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>url == <span class="kw2">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Regexp to match if the URL ends with /page/{Id}/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; Match match = Regex.<span class="me1">Match</span><span class="br0">&#40;</span>url.<span class="me1">Path</span>, _regexpPaging<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// If we have a match, remove the /page/{Id}/ from the end of the URL</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// and add a querystring to the internal URL that is dqcPagingId </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// (should have a unique name to not clash with some other querystring)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>match.<span class="me1">Length</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; url.<span class="me1">Path</span> = match.<span class="me1">Groups</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">Value</span> + <span class="st0">&quot;/&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; url.<span class="me1">QueryCollection</span><span class="br0">&#91;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#93;</span> = match.<span class="me1">Groups</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>.<span class="me1">Value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; base.<span class="me1">ConvertToInternalInternal</span><span class="br0">&#40;</span>url, ref internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Now when the /page/{Id}/ is removed from the URL, and the querystring is added, we can let EPiServer do its normal URL-rewriting.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> base.<span class="me1">ConvertToInternalInternal</span><span class="br0">&#40;</span>url, ref internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><br/></p>
<p>While not nessesary, we should override the <code>ConvertToExternalInternal </code>as well. This will let EPiServer automatically convert internal urls containing the <code>dqcPagingId </code>querystring to a external url ending with /page/{Id}/.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected override bool ConvertToExternalInternal<span class="br0">&#40;</span>UrlBuilder url, object internalObject, Encoding toEncoding<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// First let EPiServer convert the internal URL to an external. This will give us a URL like:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// /Products/?dqcPagingId=5 (if it is a paged page)</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;bool isRewritten = base.<span class="me1">ConvertToExternalInternal</span><span class="br0">&#40;</span>url, internalObject, toEncoding<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Check if the URLs query string contains dqcPagingId</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// If it does we add /page/{Id} to the URL and removes the query string</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>url.<span class="me1">Query</span>.<span class="me1">Contains</span><span class="br0">&#40;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; url.<span class="me1">Path</span> = <span class="kw4">string</span>.<span class="me1">Concat</span><span class="br0">&#40;</span>url.<span class="me1">Path</span>, <span class="st0">&quot;page/&quot;</span>, url.<span class="me1">QueryCollection</span><span class="br0">&#91;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#93;</span>, <span class="st0">&quot;/&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; url.<span class="me1">QueryCollection</span>.<span class="me1">Remove</span><span class="br0">&#40;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> isRewritten;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><br/></p>
<p>Now one can think we would be done, but there is one more method we need to implement. EPiServer uses a cache to cache URL rewrites from external URLs to internal URLs, but it will only cache the querystrings used by EPiServer, not the <code>dqcPagingId </code>we added. This gives a rather unexpected result. The first time the page is loaded everything loads fine, from code behind we can access the <code>dqcPagingId </code>and show the requested page. If you wait 10 senconds (default cache time) or more and reloads the page, it works fine. But if you reload the page sooner, the dqcPagingId will not be set when the page loads. This is because the querystring <code>dqcPagingId </code>is not cached by EPiServer. This took me quite some time to figure out.</p>
<p>The solution to the problem is to override ConvertToInternal to bypass the default caching:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">public override bool ConvertToInternal<span class="br0">&#40;</span>UrlBuilder url, out object internalObject<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// If the URL end on /page/{id}/, bypass cache by calling ConvertToInternalInternal</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// A more optimal solution would be to perform some kind of caching here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>Regex.<span class="me1">IsMatch</span><span class="br0">&#40;</span>url.<span class="me1">Path</span>, _regexpPaging<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; internalObject = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; ConvertToInternalInternal<span class="br0">&#40;</span>url, ref internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Else, it is ok to use the cached result</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> base.<span class="me1">ConvertToInternal</span><span class="br0">&#40;</span>url, out internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><br/></p>
<p>The last step is to add our Rewrite module to the web.config:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;providers&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &lt;add
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;name=&quot;MyUrlRewriter&quot;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp;enableSimpleAddress=&quot;true&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;friendlyUrlCacheAbsoluteExpiration=&quot;0:0:10&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;type=&quot;Utils.Rewrite, Utils&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;description=&quot;URL rewriter for paging&quot; /&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;/providers&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;/urlRewrite&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><br/></p>
<p>Now we should be all done! Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/episerver-friendly-urls-for-paginated-pages-and-why-the-asplinkbutton-must-die/510/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Book review: ASP.NET MVC 1.0 &#8211; Test Driven Development</title>
		<link>http://sourcecodebean.com/archives/book-review-asp-net-mvc-1-0-test-driven-development/354</link>
		<comments>http://sourcecodebean.com/archives/book-review-asp-net-mvc-1-0-test-driven-development/354#comments</comments>
		<pubDate>Wed, 09 Dec 2009 09:28:25 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=354</guid>
		<description><![CDATA[About a month ago I finished reading the book ASP.NET MVC 1.0 &#8211; Test Driven Development by Emad Ibrahim. The book weighs in only at about 300 pages, making it easily something you can read in a couple of nights. The book is written in tutorial fashion and is is probably best read with a [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago I finished reading the book ASP.NET MVC 1.0 &#8211; Test Driven Development by Emad Ibrahim. The book weighs in only at about 300 pages, making it easily something you can read in a couple of nights. The book is written in tutorial fashion and is is probably best read with a laptop running Visual Studio on your lap, so you can follow the examples in the book. The paradigm of the book is Problem, Design, Solution. Emand gudies the reader though the full process of creating a web application in a Test Driven manner. </p>
<p>Besides from only presenting MVC and TTD Emand presents several very useful libraries for Test Driven Development:</p>
<ul>
<li>Moq &#8211; A mocking library for .NET that uses the power of LINQ to create mocks.</li>
<li>Ninject &#8211; Dependency injection library for .NET</li>
<li>MBunit &#8211; An alternative to VSTest</li>
</ul>
<p>I think the book gives a good kickstart in ASP.NET MVC and TTD. My favorite part of the book was actually not reading about MVC itself, it was reading about testing it &#8211; Emad shows the strengths of MVC by showing how it makes testing easier. </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/book-review-asp-net-mvc-1-0-test-driven-development/354/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rename a Folder using EPiServer.Web.Hosting.UnifiedDirectory</title>
		<link>http://sourcecodebean.com/archives/rename-a-folder-using-episerver-web-hosting-unifieddirectory/304</link>
		<comments>http://sourcecodebean.com/archives/rename-a-folder-using-episerver-web-hosting-unifieddirectory/304#comments</comments>
		<pubDate>Mon, 05 Oct 2009 00:02:47 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=304</guid>
		<description><![CDATA[While reading the EPiServer 5 SDK documentation, i found this:
Rename a Folder
There is no Rename method on the EPiServer.Web.Hosting.UnifiedDirectory class. To rename a folder you need to call the MoveTo method as follows:



&#160;


protected void RenameFolder&#40;string path, string oldName, string name&#41;


&#123;


&#160; &#160; if &#40;IsFolder&#40;path&#41;&#41;


&#160; &#160; &#123;


&#160; &#160; &#160; &#160; UnifiedDirectory directory = 


&#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>While reading the EPiServer 5 SDK documentation, i found this:</p>
<blockquote><p><strong>Rename a Folder</strong></p>
<p>There is no Rename method on the EPiServer.Web.Hosting.UnifiedDirectory class. To rename a folder you need to call the MoveTo method as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">protected <span class="kw4">void</span> RenameFolder<span class="br0">&#40;</span><span class="kw4">string</span> path, <span class="kw4">string</span> oldName, <span class="kw4">string</span> name<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>IsFolder<span class="br0">&#40;</span>path<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; UnifiedDirectory directory = </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; System.<span class="me1">Web</span>.<span class="me1">Hosting</span>.<span class="me1">HostingEnvironment</span>.<span class="me1">VirtualPathProvider</span>.<span class="me1">GetDirectory</span><span class="br0">&#40;</span>path<span class="br0">&#41;</span> as UnifiedDirectory;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> e = <span class="nu0">-1</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>path.<span class="me1">IndexOf</span><span class="br0">&#40;</span>oldName, ++e<span class="br0">&#41;</span> &gt; <span class="nu0">-1</span><span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; sb.<span class="me1">Append</span><span class="br0">&#40;</span>path.<span class="me1">Substring</span><span class="br0">&#40;</span><span class="nu0">0</span>, e &#8211; <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; sb.<span class="me1">Append</span><span class="br0">&#40;</span>name<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; sb.<span class="me1">Append</span><span class="br0">&#40;</span><span class="st0">&quot;/&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; directory.<span class="me1">MoveTo</span><span class="br0">&#40;</span>sb.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
</blockquote>
<p>What a convenient way of renaming a folder <img src='http://sourcecodebean.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Good thing that you don&#8217;t have to do it too often.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/rename-a-folder-using-episerver-web-hosting-unifieddirectory/304/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functional programming in C# 3.0 and LINQ</title>
		<link>http://sourcecodebean.com/archives/lambda-expressions-in-c-30/199</link>
		<comments>http://sourcecodebean.com/archives/lambda-expressions-in-c-30/199#comments</comments>
		<pubDate>Mon, 25 May 2009 13:26:17 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Functional programming]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=199</guid>
		<description><![CDATA[I first learned about functional programming a few years ago when I took a class in Haskell programming at my university, Chalmers. While Haskell might not be for everyday use, many of the concepts are very compelling, such as higher order functions, pattern matching, lazy evaluation, list comprehensions, lambda functions and map/fold/filter. Over the years [...]]]></description>
			<content:encoded><![CDATA[<p>I first learned about functional programming a few years ago when I took a class in Haskell programming at my university, Chalmers. While Haskell might not be for everyday use, many of the concepts are very compelling, such as higher order functions, pattern matching, lazy evaluation, list comprehensions, lambda functions and map/fold/filter. Over the years many mainstream programming languages have adopted a subset of those features. With the release of C# 3.0 Microsoft has brought some of this functionality to C#. </p>
<p><strong>Lambda functions</strong><br />
A lambda function is simply an anonymous function that can contain expressions and statements. Lambda functions can be used to create delegates or expression trees. Lambda functions use the lambda operator => (read as “goes to”). On the left side the input parameters is defined and the right side holds the expressions or statements. A simple example would be:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> =&gt; x+y</div>
</li>
</ol>
</div>
<p>But wait, where is the type declaration? The types does not have to be defined, the compiler will declare the types by using type interference at compile time, so everything is still strongly typed. </p>
<p>The framework defines a number of parameterized delegate types (the first type is the return type):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;TR&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;T0, TR&gt;<span class="br0">&#40;</span>T0 a0<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;T0, T1, TR&gt;<span class="br0">&#40;</span>T0 a0, T1 a1<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">public delegate TR Func&lt;T0, T1, T2, TR&gt;<span class="br0">&#40;</span>T0 a0, T1 a1, T2 a2<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;T0, T1, T2, T3, TR&gt;<span class="br0">&#40;</span>T0 a0, T1 a1, T2 a2, T3 a3<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>So we could define our function as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Func&lt;int,<span class="kw4">int</span>,int&gt; fun = <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> =&gt; x + y; </div>
</li>
</ol>
</div>
<p></p>
<p><strong>Map/fold/filter</strong><br />
In functional programming it is very common to work with lists. Three very common computations are:</p>
<ol>1. Applying a function to every element of a list (map)</ol>
<ol>2. Computing a value based on all elements in the list. For example the total sum of a list containing numeric values (fold)</ol>
<ol>3. Selecting just a subset of the elements (filter)</ol>
<p>In C# the equivalents are <code>Select</code>, <code>Aggregate </code>and <code>Where</code>. Here is a simple example of how they can be used:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">var list = new <span class="kw4">int</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#123;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">5</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">list.<span class="me1">Aggregate</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="br0">&#40;</span>total, val<span class="br0">&#41;</span> =&gt; total += val<span class="br0">&#41;</span>; <span class="co1">// returns 15, 0 is the initial value. </span></div>
</li>
<li class="li1">
<div class="de1">list.<span class="me1">Select</span><span class="br0">&#40;</span>x =&gt; x + <span class="nu0">1</span><span class="br0">&#41;</span>; <span class="co1">// returns [2, 3, 4, 5, 6]</span></div>
</li>
<li class="li2">
<div class="de2">list.<span class="me1">Where</span><span class="br0">&#40;</span>x =&gt; x &gt; <span class="nu0">3</span><span class="br0">&#41;</span>; <span class="co1">// returns [4, 5]</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Using map/fold/filter instead of using regular loops makes it easier to understand and reason your code.  </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/lambda-expressions-in-c-30/199/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
