{"id":116,"date":"2010-05-16T20:39:25","date_gmt":"2010-05-16T20:39:25","guid":{"rendered":"http:\/\/blog.joefield.co.uk\/?p=116"},"modified":"2010-09-01T22:26:40","modified_gmt":"2010-09-01T22:26:40","slug":"digging-around-analysing-asp-net-mvc2-fasttrack-expressions","status":"publish","type":"post","link":"https:\/\/blog.joefield.co.uk\/?p=116","title":{"rendered":"Digging around: Analysing ASP.NET MVC2 FastTrack expressions"},"content":{"rendered":"<p>This is a third companion post to Scott Guthrie&#8217;s <a href=\"http:\/\/weblogs.asp.net\/scottgu\/archive\/2010\/01\/10\/asp-net-mvc-2-strongly-typed-html-helpers.aspx\" target=\"_blank\">ASP.NET MVC 2: Strongly Typed Html Helpers<\/a>.<\/p>\n<p>Today instead of reading code, I&#8217;d thought I&#8217;d write some. I threw together a class which does the analysis performed by the FastTrack class in ASP.NET MVC2. To recap &#8211; the FastTrack class: <\/p>\n<ul>\n<li>decides if an expression can be &#8220;fast tracked&#8221;<\/li>\n<li>if it can &#8211; builds, compiles and caches it<\/li>\n<li>returns the cached expression in future calls<\/li>\n<\/ul>\n<p>  This code demonstrates the bit that does the deciding, and gives us something to play around with.<\/p>\n<pre class=\"brush: csharp;\">\r\nusing System;\r\nusing System.Linq.Expressions;\r\n\r\nnamespace MvcHarness\r\n{\r\n  public class FastTrackAnalyser&lt;TModel&gt;\r\n  {\r\n    public void Analyse&lt;TValue&gt;(Expression&lt;Func&lt;TModel, TValue&gt;&gt; lambda)\r\n    {\r\n      ParameterExpression modelParameter = lambda.Parameters[0];\r\n      Expression body = lambda.Body;\r\n\r\n      if (modelParameter == body)\r\n      {\r\n        Console.WriteLine(&quot;{0} is a function returning its own argument&quot;, lambda);\r\n        return;\r\n      }\r\n\r\n      ConstantExpression constantExpression = body as ConstantExpression;\r\n      if (constantExpression != null)\r\n      {\r\n        Console.WriteLine(&quot;{0} is a function returning a constant value&quot;, lambda);\r\n        return;\r\n      }\r\n\r\n      MemberExpression memberExpression = body as MemberExpression;\r\n      if (memberExpression != null)\r\n      {\r\n        if (memberExpression.Expression == null)\r\n        {\r\n          Console.WriteLine(&quot;{0} is a function returning a static member of either the model or another type&quot;, lambda);\r\n          return;\r\n        }\r\n\r\n        if (memberExpression.Expression == modelParameter)\r\n        {\r\n          Console.WriteLine(&quot;{0} is a function returning a member of the model&quot;, lambda);\r\n          return;\r\n        }\r\n\r\n        ConstantExpression constantExpression2 = memberExpression.Expression as ConstantExpression;\r\n        if (constantExpression2 != null)\r\n        {\r\n          Console.WriteLine(&quot;{0} is a function representing a captured local variable&quot;, lambda);\r\n          return;\r\n        }\r\n      }\r\n      Console.WriteLine(&quot;{0} cannot be fast tracked&quot;, lambda);\r\n    }\r\n  }\r\n}\r\n\r\n\r\n<\/pre>\n<p>I called it like this (with output inline in the comments):<\/p>\n<pre class=\"brush: csharp;\">\r\nusing System;\r\n\r\nnamespace MvcHarness\r\n{\r\n  class Program\r\n  {\r\n    static void Main(string[] args)\r\n    {\r\n      FastTrackAnalyser&lt;string&gt; analyser = new FastTrackAnalyser&lt;string&gt;();\r\n\r\n      analyser.Analyse(x =&gt; x);\r\n      \/\/ outputs &quot;x =&gt; x is a function returning its own argument&quot;\r\n\r\n      analyser.Analyse(x =&gt; 999);\r\n      \/\/ outputs &quot;x =&gt; 999 is a function returning a constant value&quot;\r\n\r\n      analyser.Analyse(x =&gt; String.Empty);\r\n      \/\/ outputs &quot;x =&gt; String.Empty is a function returning a static member of either the model or another type&quot;\r\n\r\n      analyser.Analyse(x =&gt; x.Length);\r\n      \/\/ outputs &quot;x =&gt; x.Length is a function returning a member of the model&quot;\r\n\r\n      string name = &quot;Joe Field&quot;;\r\n      analyser.Analyse(x =&gt; name);\r\n      \/\/ outputs &quot;x =&gt; value(MvcHarness.Program+&lt;&gt;c__DisplayClass0).name is a function representing a captured local variable&quot;\r\n\r\n      analyser.Analyse(x =&gt; name.Length);\r\n      \/\/ outputs &quot;x =&gt; value(MvcHarness.Program+&lt;&gt;c__DisplayClass0).name.Length cannot be fast tracked&quot;\r\n    }\r\n  }\r\n}\r\n\r\n<\/pre>\n<p>The really interesting calls are the last two. They are analysing expressions which use captured local variables. The first one succeeds, but the second fails because it attempts to access a member of the variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a third companion post to Scott Guthrie&#8217;s ASP.NET MVC 2: Strongly Typed Html Helpers. Today instead of reading code, I&#8217;d thought I&#8217;d write some. I threw together a class which does the analysis performed by the FastTrack class in ASP.NET MVC2. To recap &#8211; the FastTrack class: decides if an expression can be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/116"}],"collection":[{"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":6,"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.joefield.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}