<?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>Melbourne Web Developer, Website Design &#38; Development &#187; ASP.NET MVC</title>
	<atom:link href="http://blog.koumei.net/category/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.koumei.net</link>
	<description>= fn(n){n xor 0xFFFF004B eq 0x0 ? n : fn(n - 0x1)}</description>
	<lastBuildDate>Fri, 03 Sep 2010 15:26:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET MVC Whole Site 301 Permanent Redirect</title>
		<link>http://blog.koumei.net/2010/08/12/asp-net-mvc-whole-site-301-permanent-redirect/</link>
		<comments>http://blog.koumei.net/2010/08/12/asp-net-mvc-whole-site-301-permanent-redirect/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 12:50:30 +0000</pubDate>
		<dc:creator>Koumei</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Interesting]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Principle]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Tech.]]></category>

		<guid isPermaLink="false">http://blog.koumei.net/?p=639</guid>
		<description><![CDATA[For some reasons, we need to permanently redirect the whole website to another website. It would be much easier that you are the owner of the server. But most of time you just host your website on the server out there. It wouldn&#8217;t be a big problem that your hosting company will support you to [...]]]></description>
			<content:encoded><![CDATA[<p>For some reasons, we need to permanently redirect the whole website to another website. It would be much easier that you are the owner of the server. But most of time you just host your website on the server out there. It wouldn&#8217;t be a big problem that your hosting company will support you to do some simple 301 redirect or may you have enough privilege to do it yourself. Is it possible to redirect permanently the website to other website by programming a little bit?</p>
<p>Sure you can. With MVC routing facilities, you can define your customized redirect logic: CREATE AN EMPTY MVC PROJECT, MODIFY GLOBAL.ASPX.CS FILE ONLY ON THIS EXAMPLE:</p>
<p>First of all, define two objects which implemented IRouteHandler and IHttpHandler, you can feel free to define those two objects on Global.aspx.cs file:</p>
<p>//@Implements IRouteHandler<br />
<em>class RedirectRouteHandler : IRouteHandler<br />
{<br />
private string newUrl;<br />
public RedirectRouteHandler(string newUrl) { this.newUrl = newUrl; }<br />
public IHttpHandler GetHttpHandler(RequestContext requestContext)<br />
{<br />
return new RedirectHandler(newUrl);<br />
}<br />
}</em></p>
<p>//@object implements IHttpHandler<br />
<em>class RedirectHandler : IHttpHandler<br />
{<br />
private string newUrl;</p>
<p>public RedirectHandler(string newUrl) { this.newUrl = newUrl; }</p>
<p>public bool IsReusable { get { return true; } }</p>
<p>public void ProcessRequest(HttpContext httpContext)<br />
{<br />
httpContext.Response.Status = &#8220;301 Moved Permanently&#8221;;<br />
httpContext.Response.StatusCode = 301;<br />
httpContext.Response.AppendHeader(&#8220;Location&#8221;, newUrl);<br />
return;<br />
}<br />
}</em></p>
<p>Then add one line on RegisterRoutes(RouteCollection routes) method (in Global.aspx.cs):</p>
<p><em>routes.Add(new Route(&#8220;{*pathInfo}&#8221;, new RedirectRouteHandler(@&#8221;http://koumei.net&#8221;));</em></p>
<p>Now all request to the website will redirect to &#8220;koumei.net&#8221; permanently. <img src='http://blog.koumei.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.koumei.net/2010/08/12/asp-net-mvc-whole-site-301-permanent-redirect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REMIX10 &#8211; Share the Web Love, Late Update</title>
		<link>http://blog.koumei.net/2010/06/19/remix10-share-the-web-love-late-update/</link>
		<comments>http://blog.koumei.net/2010/06/19/remix10-share-the-web-love-late-update/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 15:01:09 +0000</pubDate>
		<dc:creator>Koumei</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Interesting]]></category>
		<category><![CDATA[Living in Victoria]]></category>
		<category><![CDATA[Tech.]]></category>

		<guid isPermaLink="false">http://blog.koumei.net/?p=585</guid>
		<description><![CDATA[I almost forget the interesting 2-day event or you can say &#8220;meeting&#8221; in Melbourne. The idea of this conference is to show Microsoft developer the latest development kits and to let technical or IT people catch up each other. $300 for joining the meeting but I got it for free. Thanks to my boss to [...]]]></description>
			<content:encoded><![CDATA[<p>I almost forget the interesting 2-day event or you can say &#8220;meeting&#8221; in Melbourne. The idea of this conference is to show Microsoft developer the latest development kits and to let technical or IT people catch up each other. $300 for joining the meeting but I got it for free. Thanks to my boss to give me such great opportunity to join the great event.</p>
<p>I got 2 souvenirs, such as t-shirt, caps, but all are related to Microsoft stuff. You want to get the t-shirt? well, need to make some efforts. Everyone who attends the meeting will get a magic cube from the host, and need to fix the cube to a designated image. After all, once cubes are fixed, the host sorts them out and put them in a frame, to shape a IE icon:</p>
<div id="attachment_586" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.koumei.net/wp-content/uploads/2010/06/IMG00293-20100602-1056.jpg"><img class="size-medium wp-image-586" title="The Frame of Magic cubes" src="http://blog.koumei.net/wp-content/uploads/2010/06/IMG00293-20100602-1056-300x225.jpg" alt="The Frame of Magic cubes" width="300" height="225" /></a><p class="wp-caption-text">The Frame of Magic cubes,Everyone is involved</p></div>
<p>I think the most useful sessions are about how jQuery works peacefully with ASP.NET and VS.NET 2010 new features, and one interesting session talking about the future data representation, and introduce Pivot. Microsoft considers the cooperation with jQuery is a giant step forward, and shows its ambitions to open source community. Microsoft starts to contribute to open source community and make a lot of effort towards it, look forward to seeing its powerful tool released in months to come.</p>
<div id="attachment_587" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.koumei.net/wp-content/uploads/2010/06/IMG00294-20100602-1255.jpg"><img class="size-medium wp-image-587" title="Frankly Speaking - Talk Show in REMIX10" src="http://blog.koumei.net/wp-content/uploads/2010/06/IMG00294-20100602-1255-300x224.jpg" alt="Frankly Speaking - Talk Show in REMIX10" width="300" height="224" /></a><p class="wp-caption-text">Frankly Speaking - Talk Show in REMIX10</p></div>
<p>One thing somehow a little bit disappointed me is on this event, Microsoft doesn&#8217;t show any thing about HTML5 and CSS3, I think because Microsoft more focuses on sliverlight technology?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.koumei.net/2010/06/19/remix10-share-the-web-love-late-update/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Use of Lambda Expression to Create Complicated Query</title>
		<link>http://blog.koumei.net/2010/03/10/use-lambda-expression-to-create-complicated-query/</link>
		<comments>http://blog.koumei.net/2010/03/10/use-lambda-expression-to-create-complicated-query/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 07:12:31 +0000</pubDate>
		<dc:creator>Koumei</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Lab]]></category>
		<category><![CDATA[Tech.]]></category>

		<guid isPermaLink="false">http://blog.koumei.net/?p=465</guid>
		<description><![CDATA[.NET FX introduces a powerful weapon for creating the compact, breezy and elegant code in your project. The weapon&#8217;s name is as complicated as the mathematical stuff, called Lambda expression.
As the name implies, the great convenience comes great complication inside. Lambda expression is too important to understand, it is useful and I think it is [...]]]></description>
			<content:encoded><![CDATA[<p>.NET FX introduces a powerful weapon for creating the compact, breezy and elegant code in your project. The weapon&#8217;s name is as complicated as the mathematical stuff, called Lambda expression.</p>
<p>As the name implies, the great convenience comes great complication inside. Lambda expression is too important to understand, it is useful and I think it is good to know how it works. Well, it&#8217;s not as complicated as we think, just a little bit confusing when using it.</p>
<p>First of all, Lambda is used for 2 purposes. 1 is to simplify delegation, 2 is to create expression tree. In my case, I need to create lambda expression to create dynamic query.</p>
<p>Imagine the following usecase:</p>
<p>In your product table, there is a field called &#8220;Tags&#8221; and it&#8217;s used for storing the CSV (comma separated value) like &#8220;car, mazda, suv, luxury&#8221;, you have many records:</p>
<p>Product Table: (Table name: ProductTable)</p>
<p><span style="text-decoration: underline;"><strong>ID    ProductName    Tags</strong></span><br />
1    Tow bar        car,mazda,suv,luxury<br />
2    Cup holder    car,luxury<br />
3    Phone holder    car,mazda<br />
4    FM Transmitter    car,suv<br />
5    Queens bed    luxury<br />
6    DVD Burner    car,BMW,x5,luxury</p>
<p>When your searching criteria is<br />
&#8220;mazda&#8221; and &#8220;luxury&#8221;, the following records would be selected out:</p>
<p><span style="text-decoration: underline;"><strong>ID    ProductName    Tags</strong></span><br />
1    Tow bar        car,mazda,suv,luxury<br />
2    Cup holder    car,luxury<br />
3    Phone holder    car,mazda<br />
5    Queens bed    luxury<br />
6    DVD Burner    car,BMW,x5,luxury</p>
<p>In ancient SQL age, you would probably write a SQL like this:</p>
<address>string SQL = &#8220;SELECT * FORM ProductTable WHERE Tags LIKE&#8217;%mazda%&#8217; OR Tags LIKE &#8216;%luxury%&#8217;&#8221;;</address>
<p>It is IMpossible to create SQL like above except the project scope tells you the fixed keywords that would be used, the keywords should flexible. Then the code would be as much as:</p>
<address>string[] tags = new string[]{&#8220;mazda&#8221;, &#8220;luxury&#8221;};</address>
<address>string SQL = &#8220;SELECT * FROM ProductTable WHERE 1=1&#8243;;<br />
foreach(string tag in tags)<br />
{<br />
SQL += string.format(@&#8221; AND Tags like &#8216;%{0}%&#8217;&#8221;, tag);<br />
}</address>
<p>You must be very exciting that your code supports as many keywrods as it can. (I did before, I mean I would be very exciting when the code filled with logical stuff, LOL, of course, not now, that&#8217;s what geek did before~ )</p>
<p>Back to the reality, to the modern age, with the lambda expression, what you need to do is:</p>
<p>(pre-condition: you have created corresponding domain model which mapped to the database.)<br />
The domain object or entity would look like:</p>
<address>[Table("ProductTable")] Class ProductTable{<br />
[Column]public int Id{get; set;}<br />
[Column]public string ProductName{get; set;}<br />
[Column]public string Tags{get; set;}<br />
}</address>
<p>to fullfill the same kind of logic as the stone-aged people do, you just need to</p>
<address>DataContext db = new DataContext(&#8220;&lt;connection-string&gt;&#8221;);<br />
Table&lt;ProductTable&gt; table = db.GetTable&lt;ProductTable&gt;();</address>
<address>string tag1 = &#8220;mazda&#8221;;<br />
string tag2 = &#8220;luxury&#8221;;</address>
<address>var data = table.where(x=&gt;x.Tags.Contains(tag1)).where(x=&gt;x.Tags.Contains(tag2)) select new ProductTable;<br />
return data.AsQueryable();</address>
<p>Since so far, you still feel comfortable with this, but after minute, you would probably wanna go back stone age, how do we dynamically create the query in this case? Unfortunately, there is no short-cut for this, but many people provide lots of solutions on the Internet. We can either download some source codes about dynamically query on Internet or just grab the codes from the blogger&#8217;s website.( Most of time, you can&#8217;t use all of their codes posted on the blog, the codes are broken and bad structure, remember what Koumei always suggests, absorb the mind from the other coders, do not copy the code they provided. )</p>
<p>But in here, I would rather to use Expression Tree to implement the dynamic query.</p>
<p>First of all, we need to create an expression, because it would be applied to IQueryable.Where(Expression&lt;Func&lt;ProductTable, bool&gt;&gt; expression), so the expression should be defined as:</p>
<address>public static System.Linq.Expressions.Expression&lt;Func&lt;ProductTable, bool&gt;&gt; ApplyTags(IEnumerable&lt;string&gt; tags) //parameter is the collection of tags<br />
{</address>
<address>ParameterExpression c = Expression.Parameter(typeof(ProductTable), &#8220;c&#8221;); //Get the parameter from Func&lt;ProductTable, bool&gt;, remember? this is the advanced and lazy version of delegation, we need to craete ParameterExpression to hold this</address>
<address>var tagsProperty = Expression.Property(c, typeof(ProductTable).GetProperty(&#8220;Tags&#8221;)); //Here is the expression for get the Tags value from ProductTable object</address>
<address>Type[] ContainsTypes = new Type[1];</address>
<address>ContainsTypes[0] = typeof(string);</address>
<address>System.Reflection.MethodInfo myContainsInfo = typeof(string).GetMethod(&#8220;Contains&#8221;, ContainsTypes);</address>
<address>//Create the expression collection (based on the tags collection from parameter)<br />
List&lt;Expression&gt; myTagExpressions = new List&lt;Expression&gt;();</address>
<address>foreach (var t in tags)<br />
{<br />
myTagExpressions.Add(Expression.Call(Expression.Call(tagsProperty, &#8220;ToString&#8221;, null, null), myContainsInfo, Expression.Constant(t)));<br />
}</address>
<address>Expression OrExpression = null;</address>
<address>foreach (Expression myTagExpression in myTagExpressions)<br />
{<br />
if (OrExpression == null)<br />
{<br />
OrExpression = myTagExpression;<br />
}<br />
else<br />
{<br />
//Need to implement OR relationship among all the expressions<br />
OrExpression = Expression.Or(myTagExpression, OrExpression);<br />
}<br />
}</address>
<address>//This is how Expression would be extracted to expression tree later on.<br />
Expression&lt;Func&lt;ProductTable, bool&gt;&gt; predicate = Expression.Lambda&lt;Func&lt;ProductTable, bool&gt;&gt;(OrExpression, c);</address>
<address>return predicate;</address>
<address>}</address>
<p>Again, you can&#8217;t copy the code, since you would NOT have the same object like ProductTable and the same &#8220;Tags&#8221; property as I do. Luckily, you still can copy the code that I provide later, which is using the magic of template or generic programming. Before that, let us see how to use it:</p>
<address>IQueryable all = table.Where(ApplyTags(new List{&#8220;mazda&#8221;,&#8221;luxury&#8221;})).AsQueryable();</address>
<p>Since so far, I am 100% sure you can do it like refactoring, re-structuring to optimize your code. However, I would like to provide the code which can be general used:</p>
<address>public static System.Linq.Expressions.Expression&lt;Func&lt;T, bool&gt;&gt; ApplyFilter&lt;T&gt;(IEnumerable&lt;string&gt; filters, string property)<br />
{<br />
//Get parameter<br />
ParameterExpression c = Expression.Parameter(typeof(T), &#8220;c&#8221;);</address>
<address>//Get property from &lt;T&gt;<br />
var tagsProperty = Expression.Property(c, typeof(T).GetProperty(property));</address>
<address>Type[] ContainsTypes = new Type[1];<br />
ContainsTypes[0] = typeof(string);<br />
System.Reflection.MethodInfo myContainsInfo = typeof(string).GetMethod(&#8220;Contains&#8221;, ContainsTypes);<br />
List&lt;Expression&gt; myTagExpressions = new List&lt;Expression&gt;();<br />
foreach (var t in filters)<br />
{<br />
myTagExpressions.Add(Expression.Call(Expression.Call(tagsProperty, &#8220;ToString&#8221;, null, null), myContainsInfo, Expression.Constant(t)));<br />
}<br />
Expression OrExpression = null;<br />
foreach (Expression myTagExpression in myTagExpressions)<br />
{<br />
if (OrExpression == null)<br />
{<br />
OrExpression = myTagExpression;<br />
}<br />
else<br />
{<br />
OrExpression = Expression.Or(myTagExpression, OrExpression);<br />
}<br />
}<br />
Expression&lt;Func&lt;T, bool&gt;&gt; predicate = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(OrExpression, c);<br />
return predicate;<br />
}</address>
<p>The usage is similiar:</p>
<address>IQueryable all = table.Where(ApplyFilter&lt;ProductTable&gt;(new List{&#8220;mazda&#8221;,&#8221;luxury&#8221;}, &#8220;Tags&#8221;)).AsQueryable();</address>
<p>Not that hard, right?</p>
<p>Enjoy the powerful weapon, but don&#8217;t hurt yourself, bacause the deadline is ahead!</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 114px; width: 1px; height: 1px;">6    DVD Burner    car,BMW,x5,luxury</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.koumei.net/2010/03/10/use-lambda-expression-to-create-complicated-query/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
