<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>i have a framework... : Orcas</title><link>http://weblogs.asp.net/freedomdumlao/archive/tags/Orcas/default.aspx</link><description>Tags: Orcas</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>C# Dynamic Function Factory</title><link>http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/c-dynamic-function-factory.aspx</link><pubDate>Mon, 23 Feb 2009 06:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6923976</guid><dc:creator>fdumlao</dc:creator><slash:comments>2</slash:comments><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/freedomdumlao/commentapi.aspx?PostID=6923976</wfw:comment><comments>http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/c-dynamic-function-factory.aspx#comments</comments><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;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6923976" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/WCF/default.aspx">WCF</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Troubleshooting ASP.NET MVC Routing</title><link>http://weblogs.asp.net/freedomdumlao/archive/2008/09/09/troubleshooting-asp-net-mvc-routing.aspx</link><pubDate>Wed, 10 Sep 2008 02:33:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6614981</guid><dc:creator>fdumlao</dc:creator><slash:comments>5</slash:comments><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/freedomdumlao/commentapi.aspx?PostID=6614981</wfw:comment><comments>http://weblogs.asp.net/freedomdumlao/archive/2008/09/09/troubleshooting-asp-net-mvc-routing.aspx#comments</comments><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;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6614981" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>