<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>i have a framework...</title><link>https://weblogs.asp.net:443/freedomdumlao/</link><description>Freedom Dumlao's Blog</description><item><title>Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications</title><link>https://weblogs.asp.net:443/freedomdumlao/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications</link><description>&lt;h3&gt;&lt;a href="http://www.ihaveaframework.com/2010/05/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications/"&gt;Read the original article here...&lt;/a&gt;&lt;/h3&gt;

As a long time Ruby lover and deep rooted .NET supporter, I was probably more psyched than anyone I knew when IronRuby 1.0 was finally released. I immediately grabbed and started building some apps with it to see where the boundaries were going to lie between IronRuby and ruby.exe, and so far I've been pleasantly surprised by how many things just work as I'd expect.

I then started to try out some of my favorite libs that I was sure would not work with IronRuby, and I wasn't surprised at all when &lt;a href="http://wiki.github.com/shoes/shoes/okay-well-shoes"&gt;_why's amazing Shoes library&lt;/a&gt; didn't work. Being somewhat familiar with Shoes (it's a great DSL for building simple UIs in Ruby) I felt it wouldn't be that difficult to port it over and as it turned out, &lt;a href="http://iqueryable.com/2008/05/20/CreatingCrossPlatformGUIsWithIronRuby.aspx"&gt;someone else had already started the work&lt;/a&gt;.

As cool as this was, I was never quite satisfied with good 'ol shoes. While it was quite complete, it lacked simple extensibility points, and although easy, it wasn't quite "kid friendly". At the same time on the .NET side of the fence, IronRuby could easily compile XAML to create WPF and Silverlight UIs, but trying to do it declaratively in plain Ruby was no fun at all. And so, the Shoes-inspired, WPF/Silverlight GUI DSL was born. (and it lives here: &lt;a href="http://bitbucket.org/fdumlao/kiddo/src"&gt;http://bitbucket.org/fdumlao/kiddo/src&lt;/a&gt;)
&lt;h1&gt;Introducing Kiddo&lt;/h1&gt;
Tell you what. Let's start with a quick code example first. We'll build a useful app that we can use to quickly reverse strings whenever we need it.

&lt;h3&gt;&lt;a href="http://www.ihaveaframework.com/2010/05/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications/"&gt;Read the complete article here...&lt;/a&gt;&lt;/h3&gt;</description><pubDate>Thu, 27 May 2010 20:08:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications</guid><category>.NET</category><category>IronRuby</category><category>Ruby</category><category>Silverlight</category><category>WPF</category></item><item><title>Building Functions vs. Building Expressions: Performance Comparison</title><link>https://weblogs.asp.net:443/freedomdumlao/building-functions-vs-building-expressions-performance-comparison</link><description>&lt;p&gt;Yesterday I posted an entry describing a method for &lt;a title="C# Dynamic Function Factory" href="http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/c-dynamic-function-factory.aspx" target="_blank"&gt;building functions as opposed to building expressions&lt;/a&gt; to get a more succinct syntax, and in general a more functional approach. There was a touch of reflection in there though, so I wasn’t too sure what the performance would be like.&lt;/p&gt;  &lt;p&gt;Even though the post was specifically about an alternative to building expressions, the commenter suggested that it was more efficient, based on his own benchmarks. I was shocked.&lt;/p&gt;  &lt;p&gt;Not being the type to believe something without seeing it, I found myself compelled to write my own simple benchmark and the results were shocking.&lt;/p&gt;  &lt;p&gt;Building functions is FASTER than building expressions. WAY faster. Let’s take a look:&lt;/p&gt;  &lt;h3&gt;The Code:&lt;/h3&gt;  &lt;p&gt;The function building method I created was this (again sorry for the formatting – I’m trying to make it fit):&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; BuildEqFuncFor&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    (&lt;span class="kwrd"&gt;string&lt;/span&gt; prop, &lt;span class="kwrd"&gt;object&lt;/span&gt; val)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; t =&amp;gt; t.GetType()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        .InvokeMember(prop, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            BindingFlags.GetProperty, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            t, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            &lt;span class="kwrd"&gt;null&lt;/span&gt;) == val;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;And the commenter suggested I use the approach where you build up an expression, and then compile it:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; ExBuildEqFuncFor&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    (&lt;span class="kwrd"&gt;string&lt;/span&gt; prop, &lt;span class="kwrd"&gt;object&lt;/span&gt; val)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    var o = Expression.Parameter(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T), &lt;span class="str"&gt;&amp;quot;t&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; expression = &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        Expression.Lambda&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt;(&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            Expression.Equal(&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                Expression.PropertyOrField(o, prop), &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;                Expression.Constant(val)), o);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; expression.Compile();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;h3&gt;The Results:&lt;/h3&gt;

&lt;p&gt;I ran each method 1, 100, and 10,000 times and here were the results (in seconds):&lt;/p&gt;

&lt;p&gt;&lt;img title="buildfuncVSbuildexpBenchmarks" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="559" alt="buildfuncVSbuildexpBenchmarks" src="https://aspblogs.blob.core.windows.net/media/freedomdumlao/Media/buildfuncVSbuildexpBenchmarks_250B1A14.png" width="373" border="0" /&gt; &lt;/p&gt;

&lt;p&gt;It’s unbelievable how much faster the function building method ran, compared to creating an expression tree and compiling it. In fact, within 100 runs there was almost no change at all!&lt;/p&gt;

&lt;p&gt;I looked into what was making the expression building function so long to create, and as I suspected it was the “Compile” method call that is used to turn the whole tree into a function. I changed the method to just return the expression and not compile it and re-ran the tests. &lt;strong&gt;&lt;em&gt;It still took twice as long just to build the expression as it did to build an actual function.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you think about it though, it makes sense. Compiling on the fly was never supposed to be considered a performant (yes I said “performant”) method of metaprogramming in C# and if you are going to use it, it would be wise to implement some caching to prevent the need to constantly build the same functions again and again.&lt;/p&gt;

&lt;p&gt;Or, you can just build the functions directly.&lt;/p&gt;</description><pubDate>Tue, 24 Feb 2009 04:47:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/building-functions-vs-building-expressions-performance-comparison</guid><category>.NET</category><category>ASP.NET</category><category>ASP.NET MVC</category><category>C#</category><category>LINQ</category><category>LINQ to SQL</category><category>Silverlight</category><category>WCF</category><category>Web Services</category></item><item><title>C# Dynamic Function Factory</title><link>https://weblogs.asp.net:443/freedomdumlao/c-dynamic-function-factory</link><description>&lt;p&gt;Building functions &lt;em&gt;instead&lt;/em&gt; of expression trees.&lt;/p&gt;  &lt;p&gt;The functional programming features of the latest versions of C# allow developers to harness a very expressive programming paradigm to solve challenges in new, elegant ways. Two of the very interesting new features are the “Func&amp;lt;&amp;gt;” delegate and the Lamda expression.&lt;/p&gt;  &lt;p&gt;A Lamda expression is a simple expression that is interpreted by the compiler as either a delegate or an expression tree, depending on the Type called for in the given context. You see them used often in LINQ. For example:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var bostonPeople = people.Where( p =&amp;gt; p.City == &lt;span class="str"&gt;&amp;quot;Boston&amp;quot;&lt;/span&gt; );&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Some time has been devoted in explaining how a LINQ query like the above can be created dynamically by building expression trees (see the excellent article: &lt;a title="Creating Dynamic Queries in LINQ" href="http://srtsolutions.com/blogs/billwagner/archive/2007/11/20/creating-dynamic-queries-in-linq.aspx" target="_blank"&gt;Creating Dynamic Queries in LINQ&lt;/a&gt; by Bill Wagner). While this approach to creating a Lambda expression dynamically is very powerful, some may find that it is far more than they need and instead could get by with a simpler implementation. Others may find it difficult to read or understand and therefore provides a challenge in maintenance.&lt;/p&gt;

&lt;p&gt;One possible solution is to use the powerful new Func&amp;lt;&amp;gt; and a lambda expression to easily create a function on the fly. This solution is not quite as powerful as building an expression tree but in situations where it’s possible it will save quite a bit of code.&lt;/p&gt;

&lt;p&gt;To demonstrate, how about a completely contrived example. Let’s say you have this object in your domain:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Person&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; City { get; set; }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; State { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;and then let’s assume you need to have a way to return back a subset of a collection of “people” based on some criteria that won’t be known until runtime. We can create a factory that builds a function on the fly that we can pass into a LINQ method. Essentially what the “BuildEqFuncFor&amp;lt;T&amp;gt;” method does is build a Func&amp;lt;T, bool&amp;gt; that compares a given value against a named property of T. The signature of the built function is compatible with most LINQ query parameter expectations. (Please excuse the code formatting and naming conventions – I’m trying to fit it into this post.) &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; FunctionFactory&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; BuildEqFuncFor&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        (&lt;span class="kwrd"&gt;string&lt;/span&gt; prop, &lt;span class="kwrd"&gt;object&lt;/span&gt; val)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; t =&amp;gt; t.GetType()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            .InvokeMember(prop, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;                BindingFlags.GetProperty, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;                t, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;) == val;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Now this factory only supports building functions that test for equivalency between some property of some object and a value, however more advanced functions could be constructed using the same concept.&lt;/p&gt;

&lt;p&gt;We can now use this FunctionFactory to create a function on the fly and use it in a LINQ method to query our people collection. Let’s see what that looks like:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var personIsFromBoston = &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    FunctionFactory.BuildEqFuncFor&amp;lt;Person&amp;gt;(&lt;span class="str"&gt;&amp;quot;City&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Boston&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;var bostonPeople = people.Where( personIsFromBoston );&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (bostonPeople.Any())&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    bostonPeople.ToList().ForEach(p =&amp;gt; Console.WriteLine(p.Name));&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;





.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Obviously this is a simple example but it could easily be expanded upon (and optimized) to create a more advanced function factory that could generate functions to solve problems beyond just creating dynamic LINQ queries. An issue you should take into account, is there is no compile time type checking so you should take care to handle cases where the type of the property doesn’t match the type of the value, or that the property even exists on the type provided at all.&lt;/p&gt;

&lt;p&gt;As an interesting aside… this could be made even a bit more flexible by adding another function layer:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, Func&amp;lt;Person, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; personIsFrom =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    s =&amp;gt; FunctionFactory.BuildEqFuncFor&amp;lt;Person&amp;gt;(&lt;span class="str"&gt;&amp;quot;City&amp;quot;&lt;/span&gt;, s);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;var bostonPeople = people.Where( personIsFrom(&lt;span class="str"&gt;&amp;quot;Boston&amp;quot;&lt;/span&gt;) );&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I hope you found this interesting, I look forward to your comments.&lt;/p&gt;</description><pubDate>Mon, 23 Feb 2009 06:36:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/c-dynamic-function-factory</guid><category>.NET</category><category>ASP.NET</category><category>ASP.NET MVC</category><category>C#</category><category>LINQ</category><category>LINQ to SQL</category><category>Orcas</category><category>Silverlight</category><category>WCF</category><category>Web Services</category></item><item><title>Silverlight Adaptive Streaming: How it works</title><link>https://weblogs.asp.net:443/freedomdumlao/silverlight-adaptive-streaming-how-it-works</link><description>&lt;p&gt;Recently an &lt;a href="http://www.akamai.com/html/about/press/releases/2008/press_102808.html" target="_blank"&gt;announcement was made&lt;/a&gt; by Akamai that it was partnering with Microsoft to provide an Adaptive Streaming solution for Silverlight and IIS 7.0. Since I work in the online video industry I found the announcement very interesting, especially considering &lt;a href="http://www.movenetworks.com/news-releases/move-networks-to-enter-into-strategic-relationship-with-microsoft" target="_blank"&gt;Move Network's previous announcement&lt;/a&gt; that it had formed a &amp;quot;Strategic Releationship&amp;quot; with Microsoft to provide this exact functionality.&lt;/p&gt;  &lt;p&gt;I'm not sure if this represents the fruits of this relationship or not, but I can say that the two technologies are extremely similar in how they provide an almost &amp;quot;instant-on&amp;quot;, high-definition stream of video to the client. Which brings me to the &amp;quot;meat&amp;quot; of this article - how the heck to they do that?&lt;/p&gt;  &lt;p&gt;Specifically, you can see an example of what I'm talking about at &lt;a href="http://www.smoothHD.com"&gt;www.smoothHD.com&lt;/a&gt;. You'll need Silverlight of course, but once you do you'll notice that the time it takes the video to begin playing is almost negligible and the quality is outstanding... unless you have a crappy connection... in which case you'll notice that it starts up quickly and has so-so to poor quality; which is by design. This is because Adaptive Streaming &amp;quot;adapts&amp;quot; to your bandwidth so that you get the best experience available to you. The thought is that it would be better for a client with slower access to get &amp;quot;something&amp;quot; rather than &amp;quot;buffering&amp;quot;. &lt;/p&gt;  &lt;h4&gt;Step By Step - What makes this thing tick?&lt;/h4&gt;  &lt;p&gt;Let me start with the caveat that this is a high level overview of the technology - it's not perfect, but should provide you the jist of it. Now that I've said that, let's get started.&lt;/p&gt;  &lt;p&gt;It all starts with encoding. In order to provide users with a stream appropriate for their bandwidth, the video needs to be encoded at various bitrates from low to very high quality. The more versions created the better the stream will be able to adapt.&lt;/p&gt;  &lt;p&gt;Besides just creating several versions, the video needs to be &amp;quot;cut up&amp;quot; into many pieces. Most likely, this is done by cutting at particular times or frames in the video instead of cutting at particular sizes. So for example, if my video was encoded at 3 different bitrates (by the way 3 is far to few but makes my example easier) I might slice each of them at 5 second intervals ( I would probably make the interval shorter than that but again, this is an example). This way, the first slice of the video encoded using bitrate A would be 2kb, the first slice of the one using bitrate B would be 5kb, and the first slice of bitrate C would be 8kb. They are all different sizes but contain the exact same portion of video as their peers.&lt;/p&gt;  &lt;p&gt;This is where Akamai comes in (although you could likely use any modern CDN). You need to ensure that access to these video &amp;quot;pieces&amp;quot; is fast, reliable, etc... So using a CDN like Akamai puts the files closer to the video consumer making delivery problems less likely. Once the video is on CDN, it's all up to the player.&lt;/p&gt;  &lt;p&gt;The player bears the brunt of the work of making sure that the video stays smooth regardless of network conditions. The first thing that happens when the player starts up, is it grabs a manifest file containing information about all of those video pieces. It then makes a quick determination of the user's network bandwidth and grabs the first piece of video that is appropriate for the client's connection and begins playing. As that first piece begins playing, it looks closer at the bandwidth of the client and then adjusts the video quality when it grabs the next piece of video. It will continue to monitor the client's performance as it plays, and adjusting which piece of video to grab based on the information it has most recently gathered.&lt;/p&gt;  &lt;p&gt;If you look at what it's doing here, you'll see that we aren't really talking about a &amp;quot;stream&amp;quot; but instead the player is playing small video files in a seamless sequence so that there is no interruption to the client. Because the video files are very small they load quickly and don't require waiting for &amp;quot;buffering&amp;quot;. The video can begin playing when the first &amp;quot;piece&amp;quot; is downloaded (even before if the codec is one that allows for partial file playback). This also allows a client to &amp;quot;jump&amp;quot; to another spot in the video without having to wait for the player to re-buffer, because it just has to grab the small video file at the point you want to jump to.&lt;/p&gt;  &lt;p&gt;One cool thing to note here; if you think about how the Internet works, you'll realize that there are many many more significant benefits to this than just smooth video for clients. As more and more clients watch a particular video, proxy caches all across the Internet will cache these small &amp;quot;pieces&amp;quot; of video files (as they tend to do with smaller files that are requested frequently). In essence these proxies become an extension of your CDN, except that it's free; because the proxies will serve up these small cached files without ever talking to Akamai or whatever CDN you are working with to deliver your video. &lt;/p&gt;  &lt;p&gt;Hopefully this has provided some insight for those of you who wondered about this technology - I am excited to see more of this coming soon.&lt;/p&gt;</description><pubDate>Mon, 03 Nov 2008 16:11:12 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/silverlight-adaptive-streaming-how-it-works</guid><category>.NET</category><category>Akamai</category><category>ASP.NET</category><category>CDN</category><category>IIS</category><category>Silverlight</category><category>Streaming</category><category>Video</category><category>Web Services</category></item><item><title>LINQ to Concurrency Problems</title><link>https://weblogs.asp.net:443/freedomdumlao/linq-to-concurrency-problems</link><description>&lt;p&gt;LINQ to SQL is fascinating - the more I work with it, the more I hate it, and then love it again all at the same time. Recently we had an issue while using it in a slightly older version of our N-Tier WCF application which uses LINQ to SQL as it's primary ORM solution (yes, I know).&lt;/p&gt;  &lt;p&gt;I'll try to briefly familiarize you with the part of the system that became interesting here. In our system we have a domain object called &amp;quot;Asset&amp;quot;. Asset has a property called &amp;quot;AssetType&amp;quot; that is an &amp;quot;AssetType&amp;quot; type. Here's a super simplified version:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/freedomdumlao/WindowsLiveWriter/LINQtoConcurrencyProblems_AD37/image%255B14%255D.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="152" alt="image[14]" src="https://aspblogs.blob.core.windows.net/media/freedomdumlao/WindowsLiveWriter/LINQtoConcurrencyProblems_AD37/image%255B14%255D_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The interesting thing about our system is that it is strongly tied to a legacy system which keeps all of it's data in XML files organized using a folder structure. The legacy system can't go away and we can't yet migrate the data to our new system's database so we actually pull our asset objects from either the database &lt;em&gt;or&lt;/em&gt; the XML files. Once we have the asset built based on the XML data, we need to assign the AssetType, which is only stored in the database. The first solution was to use LINQ to SQL to select an appropriate AssetType and attach it to the asset created from XML. The problem here, is that getting an asset from XML (which happens more often than from the db) took about twice as long as we wanted it to.&lt;/p&gt;  &lt;h3&gt;Solution ? Cache Baby, Cache!&lt;/h3&gt;  &lt;p&gt;We started by creating a very basic cache. We decided to just create a static class called &amp;quot;DataCacheHelper&amp;quot; with a single static property that was a static collection of AssetType. By selecting all of the AssetTypes from the database the first time we needed them, and storing them in the static collection, we could quickly grab the one we wanted from any OperationContext when we needed it, without having to go to the database. It was fast and it worked &lt;strong&gt;great&lt;/strong&gt; in when we unit tested it. We deployed it to our test environment, went through the usual workflows and test cases, and everything seemed to check out, so we were cleared to go to production.&lt;/p&gt;  &lt;p&gt;That's when it all got... interesting.&lt;/p&gt;  &lt;p&gt;Everything was going fantastic. More than fantastic. The system was 3 times faster for some calls, and just more performant all around. We were all sitting here, hitting the system from various vectors, gleefully proving to ourselves how great we all are, never imagining that we had overlooked something critically important in our design. Then:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Person 1: &amp;quot;Uh... I have a failure.&amp;quot;&lt;/p&gt;    &lt;p&gt;Me: &amp;quot;Which method?&amp;quot;&lt;/p&gt;    &lt;p&gt;Person 2: &amp;quot;Testcase 3.&amp;quot;&lt;/p&gt;    &lt;p&gt;Person 3: &amp;quot;It passes for me.&amp;quot;&lt;/p&gt;    &lt;p&gt;Me: &amp;quot;Yeah me too... wait... it just failed for me now.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And then they all failed. The whole thing kept on failing and we hurriedly rolled back the production deployment. But what happened?&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;System.ArgumentException: Could not modify EntitySet.      &lt;br /&gt;&amp;#160;&amp;#160; at System.Data.Linq.EntitySet`1.CheckModify()       &lt;br /&gt;&amp;#160;&amp;#160; at System.Data.Linq.EntitySet`1.Add(TEntity entity)       &lt;br /&gt;&amp;#160;&amp;#160; at Domain.Entities.Asset.set_AssetType(AssetType value) in DomainDataEntities.designer.cs:line 2933&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;So this was unexpected, but at least I had a place to start. The generated code for setting the AssetType property for Asset was throwing an exception for some reason. I looked at the code and that's when the first problem dawned on me. LINQ to SQL will automatically create the reverse reference for you by default. So my AssetType had a property called &amp;quot;Assets&amp;quot; that was a collection of Asset objects. Specifically, it was an EntitySet&amp;lt;Asset&amp;gt;. I realized instantly that EntitySet is probably not thread safe, and even if it were, it certainly wasn't OperationContext safe (as you can have more than one operation per thread). So the design had failed because LINQ to SQL was doing it's job and making assumptions about how we wanted to work with our data, and we weren't doing our job and customizing what LINQ to SQL had created to fit the specific way we wanted to work with our domain objects. Fortunately this was REALLY easy to fix.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:a31fb3d3-00d6-4caf-9d82-16f71943c7a3" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;div&gt;&lt;object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="628" height="596"&gt; &lt;param name="movie" value="http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/bootstrap.swf"&gt;&lt;/param&gt; &lt;param name="quality" value="high"&gt;&lt;/param&gt; &lt;param name="bgcolor" value="#FFFFFF"&gt;&lt;/param&gt; &lt;param name="flashVars" value="thumb=http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/FirstFrame.jpg&amp;amp;content=http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/2008-10-28_1315.swf&amp;amp;width=628&amp;amp;height=596"&gt;&lt;/param&gt; &lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt; &lt;param name="scale" value="showall"&gt;&lt;/param&gt; &lt;param name="allowScriptAccess" value="always"&gt;&lt;/param&gt; &lt;param name="base" value="http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/"&gt;&lt;/param&gt;  &lt;embed src="http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/bootstrap.swf" quality="high" bgcolor="#FFFFFF" width="628" height="596" type="application/x-shockwave-flash" allowScriptAccess="always" flashVars="thumb=http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/FirstFrame.jpg&amp;content=http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/2008-10-28_1315.swf&amp;width=628&amp;height=596" allowFullScreen="true" base="http://content.screencast.com/users/FreedomDumlao/folders/Jing/media/6f4ef711-a596-41aa-8294-6f01f9095bc0/" scale="showall"&gt;&lt;/embed&gt; &lt;/object&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If you look at the properties for the relationship you'll se a property named &amp;quot;Child Property&amp;quot;. All you have to do is set that to false and voila! The AssetType object no longer has an &amp;quot;Assets&amp;quot; property. We rebuilt, retested, and are now happy with a system that is both more performant AND it has the added benefit of actually working.&lt;/p&gt;  &lt;p&gt;As a side note, while researching this issue, I found absolutely 0 results for a Google search on &amp;quot;EntitySet.CheckModify()&amp;quot;. Unfortunately you can't get source code for System.Linq.Data yet to see what that method is checking before it throws, but I used Reflector (search for it) and was able to see exactly how it worked and why it was throwing. I'd love to post more about that but I think MS frowns on posting decompiled source code, so I'll have to come up with a better way.&lt;/p&gt;</description><pubDate>Tue, 28 Oct 2008 17:19:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/linq-to-concurrency-problems</guid><category>.NET</category><category>ASP.NET</category><category>C#</category><category>LINQ</category><category>LINQ to SQL</category><category>WCF</category></item><item><title>Troubleshooting ASP.NET MVC Routing</title><link>https://weblogs.asp.net:443/freedomdumlao/troubleshooting-asp-net-mvc-routing</link><description>&lt;P&gt;One thing I absolutely love about the ASP.NET MVC framework is that there isn't too much "magic". Magic can be nice if you are able to adhere to a framework's "Golden Path", but as soon as some customization becomes necessary you find that the magic can get in the way (DataContractSerializer anyone?). &lt;/P&gt;
&lt;P&gt;One of the places in the ASP.NET MVC framework (and now part of .NET SP1) where magic&amp;nbsp;&lt;EM&gt;is&lt;/EM&gt; happening is routing. Routing takes a url, looks for the best fit from the pre-defined list of routes, breaks it up into parameters and feeds the whole thing to a handler. Pretty sweet, but if you have done something wrong it'll be tough to tell since all that is happening behind the scenes. &lt;/P&gt;
&lt;P&gt;I've created a personal checklist to follow when I start to have routing troubles, if you have any additional steps you've found useful please post them in the comments and I will try to keep this checklist up to date. This article covers troubleshooting routes when using the ASP.NET MVC framework, however some of the steps mentioned will be applicable no matter how you are using routing. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;1. Verify your controllers and actions.&lt;/STRONG&gt; &lt;/P&gt;
&lt;P&gt;I'm a contract first developer. I'll typically define my routes and views before I develop the controllers to use them. It's happened more than once that I started testing before I had written the method in my controller that I'm trying to access. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2. Look for duplicate routes.&lt;/STRONG&gt; &lt;/P&gt;
&lt;P&gt;This may not be as obvious as it seems. It's easy to create duplicate routes that read different but are hard for the routing system to distinguish. Remember that every parameter in your route is basically an empty slot where some data can be, so if you have 2 routes with the same number of slots in the same locations, they will be indistinguishable by the routing system. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;3. Make sure your most specific routes are first.&lt;/STRONG&gt; &lt;/P&gt;
&lt;P&gt;Routes are tested against the URL in the order that they were added. If there is a less specific route that your URL can "fit" into before the one you actually wanted to execute, the routing system will choose that one and never get to your more specific route. &lt;/P&gt;
&lt;P&gt;These 3 steps have helped me to successfully solve every routing issue I've come across so far, but if you have any other tips, I'd love to hear them. &lt;/P&gt;</description><pubDate>Wed, 10 Sep 2008 02:33:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/troubleshooting-asp-net-mvc-routing</guid><category>.NET</category><category>ASP.NET</category><category>ASP.NET MVC</category><category>Orcas</category></item><item><title>Thinking In Rest - Part 2</title><link>https://weblogs.asp.net:443/freedomdumlao/thinking-in-rest-part-2</link><description>&lt;p&gt;So last time I covered the concept of resources (if you haven't read it yet, you should check that out &lt;a href="http://weblogs.asp.net/freedomdumlao/archive/2008/08/06/thinking-in-rest.aspx"&gt;here&lt;/a&gt; first) and the idea that when browsing the web you aren't looking at &amp;quot;pages&amp;quot; but instead you are looking at resources that happen to be formatted as pages. As it turns out, an HTML page is a pretty convenient format for a resource when a human is accessing it. Unfortunately, HTML is pretty poor for machine consumption - so how do we provide resources to client programs?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Representations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As is talked about in the very excellent book &lt;a title="RESTful Web Services" href="http://oreilly.com/catalog/9780596529260/" target="_blank"&gt;RESTful Web Services (OReilly)&lt;/a&gt; your resources can have more than one representation. A representation is basically a different way of delivering the same information - kind of like Tylenol. If you think about Tylenol you can get it in many forms: Gelcaps, Pills, Liquid, and Chewables. No matter what you are still getting Tylenol, it's just up to you to select the representation that is easiest for you to consume.&lt;/p&gt;  &lt;p&gt;Obviously the most typical representation on the web is HTML. RESTful services however are most commonly found using XML, JSON, or both. The thing to understand is that it really doesn't matter. You could use any representation you like and it would still be REST. Just be sure to choose representations that your clients are likely going to be able to consume (imagine how well a new Tylenol delivered in convenient 30 Ton block form would go over).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Give Them What They Want&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Many of us web surfers take certain parts of the web experience for granted. Such as getting back some HTML when we ask for a web page. It just seems natural that this would happen automatically; but it doesn't. Behind the scenes&amp;#160; your web browser is &lt;em&gt;telling&lt;/em&gt; the server what it wants and the server is paying attention. Here's an example of what I'm talking about. When I visit my weblog, I instruct my browser to go to: &lt;a href="http://weblogs.asp.net/FreedomDumlao"&gt;http://weblogs.asp.net/FreedomDumlao&lt;/a&gt;. My browser responds by doing the following:     &lt;br /&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;pre&gt;GET /freedomdumlao/ HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml
Accept-Language: en-us&lt;/pre&gt;

&lt;p&gt;As you can see - the browser actually &lt;em&gt;tells &lt;/em&gt;the server that it will accept HTML, XHTML, or XML. It even tells the server what language it will accept content in! It is then up to you, the service provider to attempt to comply with the request. Likewise, if you can't do what the client wants, you should tell them so.&lt;/p&gt;

&lt;p&gt;Using the Accept header is a perfectly reasonable way of having your clients tell you what they want - but not always practical. Another common method is for the client to specify what they want by appending an extension onto the end of the URI. I first saw this practice used by the folks over in the Ruby on Rails camp, and it is also mentioned as a best practice in &lt;a title="RESTful Web Services" href="http://oreilly.com/catalog/9780596529260/" target="_blank"&gt;RESTful Web Services (OReilly)&lt;/a&gt;. Using this method, the client would request one of the following URLs:&lt;/p&gt;

&lt;pre&gt;http://fakehost.com/posts.html
http://fakehost.com/posts.xml
http://fakehost.com/posts.json&lt;/pre&gt;

&lt;p&gt;No matter which one they call, they'll get the same data, with the representation that is implied by the extension. if the extension is not practical, you might consider adding the desired representation to the url path instead:&lt;/p&gt;

&lt;pre&gt;http://fakehost.com/html/posts
http://fakehost.com/xml/posts
http://fakehost.com/json/posts&lt;/pre&gt;

&lt;p&gt;You might think that this is inconsequential for the service you plan on building - because you your service will only have 1 representation. This is fine - however your client should still indicate in some way what it expects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Making Things Happen&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So we understand now that REST is about resources and resources can have any representation that we want them to. In the next article I'll discuss how we can create, update, and delete our RESTful resources.&lt;/p&gt;</description><pubDate>Thu, 14 Aug 2008 17:43:14 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/thinking-in-rest-part-2</guid><category>ASP.NET</category><category>WCF</category><category>Web Services</category></item><item><title>Thinking in REST</title><link>https://weblogs.asp.net:443/freedomdumlao/thinking-in-rest</link><description>&lt;p&gt;I've been reading blogs around the .NET community lately and there seems to be some serious confusion about what exactly REST is. Is it a SOAP replacement? Is it a definition of how to prepare data for consumption by AJAX clients? And what does it have to do with all this new MVC stuff we've been hearing about? What &lt;em&gt;is&lt;/em&gt; it exactly? &lt;/p&gt;  &lt;p&gt;In the .NET and Microsoft world - we have been conditioned to believe in SOAP as &lt;em&gt;the&lt;/em&gt; way to do machine conversations over the web (although SOAP of course is capable of much more than just web based services). In fact if you ask any .NET engineer what a web service is I'd bet that you'd here him/her talk about SOAP and probably ASMX or WCF as well. It's just as well too because honestly SOAP is a very well defined and robust way to do that job, and because it is so well defined we as .NET engineers often don't even have to think about the details, instead having them be handled automatically for us by the framework and tools we all love so much. &lt;/p&gt;  &lt;p&gt;So then why are we even talking about this? Well, it just so happens that the entire world of technology doesn't actually speak SOAP. If you are a Java or .NET engineer you'll have no problem finding tools to parse out the WSDL and spin up clients for you automatically, but if you are using PHP or ActionScript you are likely going to have problems (&lt;em&gt;especially&lt;/em&gt; if the service is using SOAP 1.2). But almost all technologies today have do have one thing in common - they can make web requests. Almost every technology has the ability to make POST and GET request, and most of those support the extended set of HTTP verbs as well (HEAD, PUT, DELETE, etc...). The nature of REST is to use those verbs, those verbs that are as old as the HTTP spec itself, to represent actions and to use that information to allow a service to make decisions about how it processes requests. &lt;/p&gt;  &lt;h3&gt;Now then - let's start thinking in REST.&lt;/h3&gt;  &lt;p&gt;So what is REST? Perhaps if we expand the acronym that will help us understand it better: REST = Representational State Transfer. Hmmmmm. I don't think that adds any clarity to the situation. So instead let's talk about what really sets it apart from the web services technologies/concepts you are already familiar with. The first think you need to be able to understand that REST is all about &lt;strong&gt;resources&lt;/strong&gt;. &amp;quot;That's nice.&amp;quot; you say, &amp;quot;But what exactly do you mean by '&lt;em&gt;resources&lt;/em&gt;'?&amp;quot; Well, resources are the things that make your application what it is. In a blog you have resources; posts, comments, pictures, etc. In an ecommerce site you have resources; products, user reviews, users, orders, and so on. REST is all about thinking of your application in terms of those resources instead of t&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:540ae1b6-9958-4eb2-9b48-5b51e3029590" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/WCF" rel="tag"&gt;WCF&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Web%20Services" rel="tag"&gt;Web Services&lt;/a&gt;&lt;/div&gt;  &lt;p&gt;hinking about how you will provide interaction to those resources to your clients. Let's look closer at the blog resources I mentioned. &lt;/p&gt;  &lt;p&gt;If I wanted to provide a SOAP API to my weblog I'd probably start by writing 2 methods: GetPost and GetRecentPosts right? But wait - I already HAVE a way to tell the server that I want to get a post... &lt;code&gt;&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;http://mycoolbutfictionalblog.com/posts/my_cool_post &lt;/code&gt;&lt;/p&gt;  &lt;p&gt;See? If that were a real weblog, I bet I could follow that to a post called &amp;quot;My Cool Post&amp;quot; or something along those lines right? So let's take a look at what the browser is doing when we follow that link.&lt;/p&gt;  &lt;p&gt;&lt;code&gt;GET /posts/my_cool_post HTTP/1.1     &lt;br /&gt;Host: mycoolbutfictionalblog.com      &lt;br /&gt;(more stuff we'll talk about later...) &lt;/code&gt;&lt;/p&gt;  &lt;p&gt;Look at that! The browser is doing EXACTLY what I want to do with my web service! It's GET-ing the post. But what happens if I want to get a list of all the posts? I guess I would go to: &lt;/p&gt; &lt;code&gt;http://mycoolbutfictionalblog.com/posts &lt;/code&gt;  &lt;p&gt;And the browser would do:&lt;/p&gt; &lt;code&gt;GET /posts HTTP/1.1   &lt;br /&gt;Host: mycoolbutfictionalblog.com    &lt;br /&gt;(more stuff we'll talk about later...) &lt;/code&gt;  &lt;p&gt;So all by itself our blog has managed to support 2 methods that I wanted to create in my web service. I already have &amp;quot;GetPost&amp;quot; because of the &lt;code&gt;GET /posts/my_cool_post&lt;/code&gt; and I have &amp;quot;GetPosts&amp;quot; by means of &lt;code&gt;GET /posts&lt;/code&gt;. The only problem of course is that these posts are returning in HTML format right now. Not ideal for using in a web service. &lt;/p&gt;  &lt;p&gt;The truth is, at this point, we can truly claim that we have a RESTful web service to retrieve a Post or a collection of Posts. REST doesn't define what format the resource is in - and that is where a lot of the confusion comes in. Until learning about REST, you probably thought about that blog post as a web page that had a blog post on it. Now that you are beginning to understand REST you should see that it is a blog post formatted as a web page. The post is what we are asking for, it just happens that the current delivery of the post is using an HTML container. &lt;/p&gt;  &lt;p&gt;Once that logic is in place then, we can see how we could do the exact same thing but instead of formatting the post as an HTML web page, we could choose to format it as XML. We could choose to format it as JSON, or AMF, or any other way we might want to return that resource to the client. &lt;/p&gt;  &lt;p&gt;There is quite a bit more to be written on this subject, and I'll pick it up again in a later post. For now though, remember, if you aren't talking about resources - you aren't talking about REST. &lt;/p&gt;</description><pubDate>Wed, 06 Aug 2008 20:31:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/freedomdumlao/thinking-in-rest</guid><category>ASP.NET</category><category>WCF</category><category>Web Services</category></item></channel></rss>