<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>[Giagnocavo]Michael::Write() - ASP.NET</title>
    <link>http://www.atrevido.net/blog/</link>
    <description>Something about .NET.</description>
    <copyright>Michael Giagnocavo</copyright>
    <lastBuildDate>Thu, 16 Jul 2009 09:35:53 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mggUNSPAM@telefinity.com</managingEditor>
    <webMaster>mggUNSPAM@telefinity.com</webMaster>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=d5591bdc-add1-4fbc-b1d9-0c25359433a6</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d5591bdc-add1-4fbc-b1d9-0c25359433a6</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I came across a <a href="http://cs.hubfs.net/forums/thread/11076.aspx">thread on hubFS
about deserialising F# records with JSON</a>. After <a href="http://lorgonblog.spaces.live.com/">Mr.
McNamara</a> pointed out that DataContract serialization would work with F# records,
I realised we can do the same for other serialisation systems, such as ASP.NET MVC. 
</p>
        <p>
ASP.NET MVC binding doesn't work with F# records for a few reasons. First, it requires
a default constructor, and record types don't have one. Second, it needs settable
properties, and records have read-only properties. Fortunately, the backing field
for a record's property is a mutable field. The name is mangled (@ is appended), but
otherwise we're ok to set that field.
</p>
        <p>
With this, we can subclass the default model binder and add in code to construct records
as well as set their fields directly. Unlike DataContract serializers, I didn't use
FormatterServices.GetUninitializedObject to create the object, I use the F# reflection
function MakeRecord. This is because I want to attempt to initialise all fields on
the record type, to try to keep out nulls. This goes against how the rest of MVC's
null handling goes, so perhaps it's not a great idea. 
</p>
        <p>
At any rate, here's the quite short code. A lot of things probably don't work, such
as F# lists. Perhaps there should be a community project that collects F#-specific
type helpers for different frameworks to make serialization, binding, etc. easier.
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> System
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> System.Web.Mvc
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> Microsoft.FSharp.Reflection
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">type</span> RecordDefaultModelBinder() = 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">inherit</span> DefaultModelBinder()
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span> isrec = FSharpType.IsRecord
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: green">/// Makes a record, trying to provide
initialised values for each field    </span></p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span><span style="COLOR: blue">rec</span> makeDefaultRecord
ty = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> defval
ty = 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">if</span> isrec
ty <span style="COLOR: blue">then</span> makeDefaultRecord ty 
</p>
          <p style="MARGIN: 0px">
                   
    <span style="COLOR: blue">else</span><span style="COLOR: blue">match</span> ty.GetConstructor(Type.EmptyTypes) <span style="COLOR: blue">with</span><span style="COLOR: blue">null</span><span style="COLOR: blue">-&gt;</span><span style="COLOR: blue">null</span> |
c <span style="COLOR: blue">-&gt;</span> c.Invoke <span style="COLOR: blue">null</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> vals =
FSharpType.GetRecordFields ty |&gt; Array.map (<span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> defval
x.PropertyType)
</p>
          <p style="MARGIN: 0px">
        FSharpValue.MakeRecord(ty, vals)
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> this.CreateModel(cc,
bc, ty) = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: green">// We have to avoid
them calling Activator.CreateInstance on records</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> isrec ty <span style="COLOR: blue">then</span> makeDefaultRecord(ty) <span style="COLOR: blue">else</span><span style="COLOR: blue">base</span>.CreateModel(cc,
bc, ty)
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> this.GetModelProperties(cc,
bc) = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: green">// Default one filters
out read-only, but we own the field</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> isrec bc.ModelType <span style="COLOR: blue">then</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">let</span> props
= ComponentModel.TypeDescriptor.GetProperties(bc.ModelType)
</p>
          <p style="MARGIN: 0px">
                   
    |&gt; Seq.cast&lt;ComponentModel.PropertyDescriptor&gt; <span style="COLOR: green">//
BCLFail</span></p>
          <p style="MARGIN: 0px">
                   
    |&gt; Seq.filter(<span style="COLOR: blue">fun</span> p <span style="COLOR: blue">-&gt;</span> bc.PropertyFilter.Invoke(p.Name))
</p>
          <p style="MARGIN: 0px">
            ComponentModel.PropertyDescriptorCollection(Seq.to_array
props)
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">else</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">base</span>.GetModelProperties(cc,
bc) 
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> this.SetProperty(cc,
bc, propDesc, value) = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: green">// To set a record
property, set the mangled field</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> isrec bc.ModelType <span style="COLOR: blue">then</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">let</span> field
= bc.ModelType.GetField(propDesc.Name + <span style="COLOR: maroon">"@"</span>, Reflection.BindingFlags.Instance
||| Reflection.BindingFlags.NonPublic)
</p>
          <p style="MARGIN: 0px">
            field.SetValue(bc.Model,
value)
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">else</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">base</span>.SetProperty(cc,
bc, propDesc, value)
</p>
        </div>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d5591bdc-add1-4fbc-b1d9-0c25359433a6" />
      </body>
      <title>F# Records and ASP.NET MVC Binding</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</guid>
      <link>http://www.atrevido.net/blog/2009/07/16/F+Records+And+ASPNET+MVC+Binding.aspx</link>
      <pubDate>Thu, 16 Jul 2009 09:35:53 GMT</pubDate>
      <description>&lt;p&gt;
I came across a &lt;a href="http://cs.hubfs.net/forums/thread/11076.aspx"&gt;thread on hubFS
about deserialising F# records with JSON&lt;/a&gt;. After&amp;nbsp;&lt;a href="http://lorgonblog.spaces.live.com/"&gt;Mr.
McNamara&lt;/a&gt;&amp;nbsp;pointed out that DataContract serialization would work with F# records,
I realised we can do the same for other serialisation systems, such as ASP.NET MVC. 
&lt;/p&gt;
&lt;p&gt;
ASP.NET MVC binding doesn't work with F# records for a few reasons. First, it requires
a default constructor, and record types don't have one. Second, it needs settable
properties, and records have read-only properties. Fortunately, the backing field
for a record's property is a mutable field. The name is mangled (@ is appended), but
otherwise we're ok to set that field.
&lt;/p&gt;
&lt;p&gt;
With this, we can subclass the default model binder and add in code to construct records
as well as set their fields directly. Unlike DataContract serializers, I didn't use
FormatterServices.GetUninitializedObject to create the object, I use the F# reflection
function MakeRecord. This is because I want to attempt to initialise all fields on
the record type, to try to keep out nulls. This goes against how the rest of MVC's
null handling goes, so perhaps it's not a great idea. 
&lt;/p&gt;
&lt;p&gt;
At any rate, here's the quite short code. A lot of things probably don't work, such
as F# lists. Perhaps there should be a community project that collects F#-specific
type helpers for different frameworks to make serialization, binding, etc. easier.
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Web.Mvc
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; Microsoft.FSharp.Reflection
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;type&lt;/span&gt; RecordDefaultModelBinder() = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;inherit&lt;/span&gt; DefaultModelBinder()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; isrec = FSharpType.IsRecord
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;/// Makes a record, trying to provide
initialised values for each field&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; &lt;span style="COLOR: blue"&gt;rec&lt;/span&gt; makeDefaultRecord
ty = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; defval
ty = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec
ty &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; makeDefaultRecord ty 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; &lt;span style="COLOR: blue"&gt;match&lt;/span&gt; ty.GetConstructor(Type.EmptyTypes) &lt;span style="COLOR: blue"&gt;with&lt;/span&gt; &lt;span style="COLOR: blue"&gt;null&lt;/span&gt; &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="COLOR: blue"&gt;null&lt;/span&gt; |
c &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; c.Invoke &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; vals =
FSharpType.GetRecordFields ty |&amp;gt; Array.map (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; defval
x.PropertyType)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FSharpValue.MakeRecord(ty, vals)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; this.CreateModel(cc,
bc, ty) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// We have to avoid
them calling Activator.CreateInstance on records&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec ty &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; makeDefaultRecord(ty) &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.CreateModel(cc,
bc, ty)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; this.GetModelProperties(cc,
bc) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// Default one filters
out read-only, but we own the field&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec bc.ModelType &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; props
= ComponentModel.TypeDescriptor.GetProperties(bc.ModelType)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.cast&amp;lt;ComponentModel.PropertyDescriptor&amp;gt; &lt;span style="COLOR: green"&gt;//
BCLFail&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.filter(&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; p &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; bc.PropertyFilter.Invoke(p.Name))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ComponentModel.PropertyDescriptorCollection(Seq.to_array
props)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.GetModelProperties(cc,
bc) 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; this.SetProperty(cc,
bc, propDesc, value) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// To set a&amp;nbsp;record
property, set the mangled field&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec bc.ModelType &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; field
= bc.ModelType.GetField(propDesc.Name + &lt;span style="COLOR: maroon"&gt;"@"&lt;/span&gt;, Reflection.BindingFlags.Instance
||| Reflection.BindingFlags.NonPublic)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; field.SetValue(bc.Model,
value)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.SetProperty(cc,
bc, propDesc, value)
&lt;/p&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d5591bdc-add1-4fbc-b1d9-0c25359433a6" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</comments>
      <category>ASP.NET</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=cc0a8d5c-102f-452b-927c-59b2ca2017ca</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=cc0a8d5c-102f-452b-927c-59b2ca2017ca</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you use ASP.NET MVC with F# CTP (Monday brings Beta 1 and probably many changes),
you might run into an issue of how to scope certain things. For example, an IDisposable
is used to create blocks, for things like an HTML form. Example in C#:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
          <p style="MARGIN: 0px">
    <span style="BACKGROUND: #ffee62">&lt;%</span><span style="COLOR: blue">using</span> (Html.BeginForm())
{<span style="BACKGROUND: #ffee62">%&gt;</span></p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">&lt;</span><span style="COLOR: #a31515">fieldset</span><span style="COLOR: blue">&gt;</span><span style="COLOR: blue">&lt;</span><span style="COLOR: #a31515">legend</span><span style="COLOR: blue">&gt;</span>Fields<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">legend</span><span style="COLOR: blue">&gt;...</span><span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">fieldset</span><span style="COLOR: blue">&gt;</span></p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="BACKGROUND: #ffee62">&lt;%</span> } <span style="BACKGROUND: #ffee62">%&gt;</span></p>
          <p style="MARGIN: 0px">
            <span style="BACKGROUND: #ffee62">
            </span> 
</p>
        </div>
        <p>
          <!--EndFragment-->This is needed because VB and C# didn't have any easy function/block
syntax (VB 10 should fix this), and many C# developers are still wary of higher order
functions. How does this play out in F#? First off, whitespace is important. This
can get really messy with the current F# ASP.NET integration. Basically, always open
the script blocks on a separate line, and indent from the first column. Example:
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span> let i = 0
</p>
            <p style="MARGIN: 0px">
   this.Response.Write(sprintf "i = %d" i) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
          </div>
        </blockquote>
        <!--EndFragment-->
        <p>
(Note that the 'this' variable is bound to the current page.) This fails:
</p>
        <p>
          <strong>Compiler Error Message: </strong>
          <font face="Arial">FS0010: Unexpected keyword
'let' or 'use' in expression. Expected incomplete structured construct at or before
this point or other token<br /><br /></font>
          <b>Source Error:</b>
          <br />
          <br />
          <table width="100%" bgcolor="#ffffcc">
            <tbody>
              <tr>
                <td>
                </td>
              </tr>
              <tr>
                <td>
                  <code>
                    <pre>Line 111:                let mutable parameterContainer = parameterContainer
Line 112:                __w.Write("\r\n") |&gt; ignore
<font color="red">Line
113: let i = 0 </font>Line 114: this.Response.Write(sprintf "i = %d" i) Line 115:
__w.Write("\r\n &lt;h2&gt;Create&lt;/h2&gt;\r\n\r\n ") |&gt; ignore</pre>
                  </code>
                </td>
              </tr>
            </tbody>
          </table>
        </p>
        <p>
Note how the &lt;% is counted as space, so the let starts off indented 3 spaces. Instead,
we need to write it so:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
          <p style="MARGIN: 0px">
            <span style="BACKGROUND: #ffee62">&lt;%</span>
          </p>
          <p style="MARGIN: 0px">
let i = 0
</p>
          <p style="MARGIN: 0px">
this.Response.Write(sprintf "i = %d" i) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
        </div>
        <!--EndFragment-->
        <p>
This works fine. You can also put the <span style="BACKGROUND: #ffee62">%&gt;</span> on
the next line if you like. Now, on to ASP.NET MVC's IDisposable usage. A straightforward
use of the F# using function won't work:
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Demo<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span>
            </p>
            <p style="MARGIN: 0px">
using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&gt; <span style="BACKGROUND: #ffee62">%&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
    <span style="COLOR: blue">&lt;</span><span style="COLOR: #a31515">p</span><span style="COLOR: blue">&gt;</span>Inside
a form<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">p</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span> ) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
          </div>
        </blockquote>
        <!--EndFragment-->
        <p>
          <strong>Compiler Error Message: </strong>
          <font face="Arial">FS0191: The mutable variable
'__w' is used in an invalid way. Mutable variables may not be captured by closures.
Consider eliminating this use of mutation or using a heap-allocated mutable reference
cell via 'ref' and '!'.<br /><br /></font>
          <b>Source Error:</b>
          <br />
          <br />
        </p>
        <p>
          <table width="100%" bgcolor="#ffffcc">
            <tbody>
              <tr>
                <td>
                </td>
              </tr>
              <tr>
                <td>
                  <code>
                    <pre>Line 114:                __w.Write("\r\n\r\n&lt;b&gt;Demo&lt;/b&gt;\r\n\r\n") |&gt; ignore
Line 115:                  
<font color="red">Line
116: using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&gt; </font>Line
117: __w.Write("\r\n\r\n &lt;p&gt;Inside a form&lt;/p&gt;\r\n\r\n") |&gt; ignore Line
118: ) </pre>
                  </code>
                </td>
              </tr>
            </tbody>
          </table>
        </p>
        <p>
As the error says, this is because the __w variable is mutable, so we can't play with
it inside a lambda. I'm not sure if this will be worked around -- they'd have to change
the codegen quite a bit, I'd think. As a side note, the F# CTP does not support C#
extension methods (hence the verbose calling of BeginForm), but F# will eventually
-- maybe in the Beta.
</p>
        <p>
The way we must scope is with a use binding. There's no way I see to accomplish this
with whitespace alone. Due to the ASPX translation process, this would probably be
very error prone. Instead, we can simply put the use binding inside a do expression:
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Demo<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span>
            </p>
            <p style="MARGIN: 0px">
do (use form = Mvc.Html.FormExtensions.BeginForm this.Html <span style="BACKGROUND: #ffee62">%&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Inside
the form<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span> ) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Outside
of the form<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
          </div>
        </blockquote>
        <!--EndFragment-->
        <p>
The parentheses setup the scope exactly how we want it.
</p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=cc0a8d5c-102f-452b-927c-59b2ca2017ca" />
      </body>
      <title>F# CTP and ASP.NET MVC ASPX: Scoping disposables</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</guid>
      <link>http://www.atrevido.net/blog/2009/05/15/F+CTP+And+ASPNET+MVC+ASPX+Scoping+Disposables.aspx</link>
      <pubDate>Fri, 15 May 2009 20:46:39 GMT</pubDate>
      <description>&lt;p&gt;
If you use ASP.NET MVC with F# CTP (Monday brings Beta 1 and probably many changes),
you might run into an issue of how to scope certain things. For example, an IDisposable
is used to create blocks, for things like an HTML form. Example in C#:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (Html.BeginForm())
{&lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;legend&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Fields&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;legend&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;...&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;!--EndFragment--&gt;This is needed because VB and C# didn't have any easy function/block
syntax (VB 10 should fix this), and many C# developers are still wary of higher order
functions. How does this play out in F#? First off, whitespace is important. This
can get really messy with the current F# ASP.NET integration. Basically, always open
the script blocks on a separate line, and indent from the first column. Example:
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; let i = 0
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp; this.Response.Write(sprintf "i = %d" i) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
(Note that the 'this' variable is bound to the current page.)&amp;nbsp;This fails:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Compiler Error Message: &lt;/strong&gt;&lt;font face=Arial&gt;FS0010: Unexpected keyword
'let' or 'use' in expression. Expected incomplete structured construct at or before
this point or other token&lt;br&gt;
&lt;br&gt;
&lt;/font&gt;&lt;b&gt;Source Error:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;table width="100%" bgcolor=#ffffcc&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;&lt;pre&gt;Line 111:                let mutable parameterContainer = parameterContainer
Line 112:                __w.Write("\r\n") |&amp;gt; ignore
&lt;font color=red&gt;Line
113: let i = 0 &lt;/font&gt;Line 114: this.Response.Write(sprintf "i = %d" i) Line 115:
__w.Write("\r\n &amp;lt;h2&amp;gt;Create&amp;lt;/h2&amp;gt;\r\n\r\n ") |&amp;gt; ignore&lt;/pre&gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
Note how the &amp;lt;% is counted as space, so the let starts off indented&amp;nbsp;3 spaces.&amp;nbsp;Instead,
we need to write it so:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
let i = 0
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
this.Response.Write(sprintf "i = %d" i) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
This works fine. You can also put the &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt; on
the next line if you like.&amp;nbsp;Now, on to ASP.NET MVC's IDisposable usage. A straightforward
use of the F# using function won't work:
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Demo&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&amp;gt; &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;p&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Inside
a form&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;p&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; ) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
&lt;strong&gt;Compiler Error Message: &lt;/strong&gt;&lt;font face=Arial&gt;FS0191: The mutable variable
'__w' is used in an invalid way. Mutable variables may not be captured by closures.
Consider eliminating this use of mutation or using a heap-allocated mutable reference
cell via 'ref' and '!'.&lt;br&gt;
&lt;br&gt;
&lt;/font&gt;&lt;b&gt;Source Error:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;table width="100%" bgcolor=#ffffcc&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;&lt;pre&gt;Line 114:                __w.Write("\r\n\r\n&amp;lt;b&amp;gt;Demo&amp;lt;/b&amp;gt;\r\n\r\n") |&amp;gt; ignore
Line 115:                  
&lt;font color=red&gt;Line
116: using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&amp;gt; &lt;/font&gt;Line
117: __w.Write("\r\n\r\n &amp;lt;p&amp;gt;Inside a form&amp;lt;/p&amp;gt;\r\n\r\n") |&amp;gt; ignore Line
118: ) &lt;/pre&gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
As the error says, this is because the __w variable is mutable, so we can't play with
it inside a lambda. I'm not sure if this will be worked around -- they'd have to change
the codegen quite a bit, I'd think. As a side note, the F# CTP does not support C#
extension methods (hence the verbose calling of BeginForm), but F# will eventually
-- maybe in the Beta.
&lt;/p&gt;
&lt;p&gt;
The way we must scope is with a use binding. There's no way I see to accomplish this
with whitespace alone. Due to the ASPX translation process, this would probably be
very error prone. Instead, we can simply put the use binding inside a do expression:
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Demo&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
do (use form = Mvc.Html.FormExtensions.BeginForm this.Html &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Inside
the form&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; ) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Outside
of the form&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
The parentheses setup the scope exactly how we want it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=cc0a8d5c-102f-452b-927c-59b2ca2017ca" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</comments>
      <category>ASP.NET</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=d7828ef8-8c3f-4ac5-82c7-84b292ce1595</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,d7828ef8-8c3f-4ac5-82c7-84b292ce1595.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,d7828ef8-8c3f-4ac5-82c7-84b292ce1595.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d7828ef8-8c3f-4ac5-82c7-84b292ce1595</wfw:commentRss>
      <slash:comments>10</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <style>
          <!--
 /* Font Definitions */
 @font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin-top:0in;
	margin-right:0in;
	margin-bottom:10.0pt;
	margin-left:0in;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
.MsoPapDefault
	{margin-bottom:10.0pt;
	line-height:115%;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
	{page:Section1;}
-->
        </style>
        <p class="MsoNormal">
[Yea, I’m not a web dev, and actively avoid it as much as possible, so I’m late to
this party.] 
</p>
        <p class="MsoNormal">
The interesting thing about ASP.NET MVC is that it takes an opposite approach to ASP.NET
in general. ASP.NET concepts try to “build up”, so as to shelter us from the evolved
idiocy that is HTML, as well as clean up the inherently stateless nature of HTTP.
ASP.NET MVC makes no attempt and forces you to deal with reality. Considering ASP.NET’s
abstraction isn’t really perfect (example: databinding sucks), MVC’s approach is unfortunately
refreshing.
</p>
        <p class="MsoNormal">
Because of its “raw” nature, you’ll be writing a lot more HTML than you’d do with
ASP.NET, and this HTML must line up with code on your server. To make this less of
a pain, there are some HTML helper functions. Rob “<a href="http://blog.wekeroad.com/2007/08/24/linq-understanding-linq-vars-and-lambdas/"> type
inference”</a> Conery has an overview <a href="http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/">here</a>.
</p>
        <p class="MsoNormal">
Here’s the signature for one of the functions:
</p>
        <p class="MsoNormal">
          <font face="Courier New">    public static string CheckBox(this HtmlHelper
helper, string htmlName, string text, string value, bool isChecked, <b>object htmlAttributes</b>);</font>
        </p>
        <p class="MsoNormal">
The last parameter confused me. Why would it be an object? Am I supposed to pass in
an IDictionary&lt;string,string&gt;? Just a long string? To make it more confusing,
other helpers had two overloads:
</p>
        <p class="MsoNormal">
          <font face="Courier New">    public static string TextArea(this HtmlHelper
helper, string htmlName, object value, <b>IDictionary&lt;string, object&gt; htmlAttributes</b>);</font>
        </p>
        <p class="MsoNormal">
          <font face="Courier New">    public static string TextArea(this HtmlHelper
helper, string htmlName, object value, <b>object htmlAttributes</b>);</font>
        </p>
        <p class="MsoNormal">
OK, so they explicitly called out the IDictionary there – THEN WHO WAS OBJECT HTMLATTRIBUTES?
</p>
        <p class="MsoNormal">
Rob covers in his overview. The idea is that you’re supposed to use anonymous types
to hack around the lack of tuples. 
</p>
        <p class="MsoNormal" style="margin-left: 0.5in; text-indent: 0.5in;">
          <font face="Courier New">&lt;%=Html.Whatever(arg1, bla, …,<br />
                            
new { @class=”cssx”, style=”x:f” …}) %&gt;</font>
        </p>
        <p class="MsoNormal">
What a great case of not-having-built-in-tuples-is-lame. It’s so lame, Microsoft’s
own developer teams have to resort to weird (but quite creative!) hacks like this
so that their syntax won’t completely suck*. Damn.
</p>
        <p class="MsoNormal">
          <b>And now, a duck</b>
        </p>
        <p class="MsoNormal">
For bonus points, there is another C# compiler feature that the MVC team could have
[ab]used, and it would arguably have made more sense (although the syntax isn’t as
tight). C# supports duck typing on collection initializers! So, they could create
a class like this: 
<br /></p>
        <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
          <span style="font-family: Consolas;">    <span style="color: blue;">public</span><span style="color: blue;">class</span><span style="color: rgb(43, 145, 175);">HtmlAttributes</span> :
System.Collections.<span style="color: rgb(43, 145, 175);">IEnumerable</span></span>
        </p>
        <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
          <span style="font-family: Consolas;">    {</span>
        </p>
        <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
          <span style="font-family: Consolas;">        <span style="color: blue;">public</span> System.Collections.<span style="color: rgb(43, 145, 175);">IEnumerator</span> GetEnumerator()
{ ... }</span>
        </p>
        <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
          <span style="font-family: Consolas;">        <span style="color: blue;">public</span><span style="color: blue;">void</span> Add(<span style="color: blue;">string</span> name, <span style="color: blue;">object</span> val)
{ ... }</span>
        </p>
        <p class="MsoNormal">
          <span style="font-family: Consolas;">    }</span>
        </p>
        <p class="MsoNormal">
And then they can write this:
</p>
        <p class="MsoNormal">
          <span style="font-family: Consolas; color: blue;">new</span>
          <span style="font-family: Consolas;">
            <span style="color: rgb(43, 145, 175);">HtmlAttributes</span> {
{ <span style="color: rgb(163, 21, 21);">"A"</span>, 123} , {<span style="color: rgb(163, 21, 21);">"B"</span>, <span style="color: rgb(163, 21, 21);">"test"</span>}
}</span>
        </p>
        <p class="MsoNormal">
No, it’s not as tight as the anonymous type syntax, but it does make a lot more sense. 
And tuples still make much more sense than either approach, and have benefits for
the rest of the language to boot (death to out parameters!). 
<br /></p>
        <p class="MsoNormal">
 *The only benefit I see in anonymous types is that, at compile time, you'll 
know there are no key conflicts - but that is totally trivial in the way they use
them, since all the keys are declared right there.<br /></p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d7828ef8-8c3f-4ac5-82c7-84b292ce1595" />
      </body>
      <title>ASP.NET MVC Begs for Tuples</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,d7828ef8-8c3f-4ac5-82c7-84b292ce1595.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/27/ASPNET+MVC+Begs+For+Tuples.aspx</link>
      <pubDate>Wed, 27 Aug 2008 01:12:13 GMT</pubDate>
      <description>&lt;style&gt;
&lt;!--
 /* Font Definitions */
 @font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin-top:0in;
	margin-right:0in;
	margin-bottom:10.0pt;
	margin-left:0in;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
.MsoPapDefault
	{margin-bottom:10.0pt;
	line-height:115%;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
	{page:Section1;}
--&gt;
&lt;/style&gt;
&lt;p class="MsoNormal"&gt;
[Yea, I’m not a web dev, and actively avoid it as much as possible, so I’m late to
this party.] 
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
The interesting thing about ASP.NET MVC is that it takes an opposite approach to ASP.NET
in general. ASP.NET concepts try to “build up”, so as to shelter us from the evolved
idiocy that is HTML, as well as clean up the inherently stateless nature of HTTP.
ASP.NET MVC makes no attempt and forces you to deal with reality. Considering ASP.NET’s
abstraction isn’t really perfect (example: databinding sucks), MVC’s approach is unfortunately
refreshing.
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
Because of its “raw” nature, you’ll be writing a lot more HTML than you’d do with
ASP.NET, and this HTML must line up with code on your server. To make this less of
a pain, there are some HTML helper functions. Rob “&lt;a href="http://blog.wekeroad.com/2007/08/24/linq-understanding-linq-vars-and-lambdas/"&gt; type
inference”&lt;/a&gt; Conery has an overview &lt;a href="http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/"&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
Here’s the signature for one of the functions:
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static string CheckBox(this HtmlHelper
helper, string htmlName, string text, string value, bool isChecked, &lt;b&gt;object htmlAttributes&lt;/b&gt;);&lt;/font&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
The last parameter confused me. Why would it be an object? Am I supposed to pass in
an IDictionary&amp;lt;string,string&amp;gt;? Just a long string? To make it more confusing,
other helpers had two overloads:
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static string TextArea(this HtmlHelper
helper, string htmlName, object value, &lt;b&gt;IDictionary&amp;lt;string, object&amp;gt; htmlAttributes&lt;/b&gt;);&lt;/font&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static string TextArea(this HtmlHelper
helper, string htmlName, object value, &lt;b&gt;object htmlAttributes&lt;/b&gt;);&lt;/font&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
OK, so they explicitly called out the IDictionary there – THEN WHO WAS OBJECT HTMLATTRIBUTES?
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
Rob covers in his overview. The idea is that you’re supposed to use anonymous types
to hack around the lack of tuples. 
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: 0.5in;"&gt;
&lt;font face="Courier New"&gt;&amp;lt;%=Html.Whatever(arg1, bla, …,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
new { @class=”cssx”, style=”x:f” …}) %&amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
What a great case of not-having-built-in-tuples-is-lame. It’s so lame, Microsoft’s
own developer teams have to resort to weird (but quite creative!) hacks like this
so that their syntax won’t completely suck*. Damn.
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;b&gt;And now, a duck&lt;/b&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
For bonus points, there is another C# compiler feature that the MVC team could have
[ab]used, and it would arguably have made more sense (although the syntax isn’t as
tight). C# supports duck typing on collection initializers! So, they could create
a class like this: 
&lt;br&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;HtmlAttributes&lt;/span&gt; :
System.Collections.&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; System.Collections.&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerator&lt;/span&gt; GetEnumerator()
{ ... }&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(&lt;span style="color: blue;"&gt;string&lt;/span&gt; name, &lt;span style="color: blue;"&gt;object&lt;/span&gt; val)
{ ... }&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;span style="font-family: Consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
And then they can write this:
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;span style="font-family: Consolas; color: blue;"&gt;new&lt;/span&gt;&lt;span style="font-family: Consolas;"&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;HtmlAttributes&lt;/span&gt; {
{ &lt;span style="color: rgb(163, 21, 21);"&gt;"A"&lt;/span&gt;, 123} , {&lt;span style="color: rgb(163, 21, 21);"&gt;"B"&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"test"&lt;/span&gt;}
}&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
No, it’s not as tight as the anonymous type syntax, but it does make a lot more sense.&amp;nbsp;
And tuples still make much more sense than either approach, and have benefits for
the rest of the language to boot (death to out parameters!). 
&lt;br&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&amp;nbsp;*The only benefit I see in anonymous types is that, at compile time, you'll&amp;nbsp;
know there are no key conflicts - but that is totally trivial in the way they use
them, since all the keys are declared right there.&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d7828ef8-8c3f-4ac5-82c7-84b292ce1595" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,d7828ef8-8c3f-4ac5-82c7-84b292ce1595.aspx</comments>
      <category>ASP.NET</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=41902041-dd54-4407-ac4b-46ae041a3e57</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,41902041-dd54-4407-ac4b-46ae041a3e57.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,41902041-dd54-4407-ac4b-46ae041a3e57.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=41902041-dd54-4407-ac4b-46ae041a3e57</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">Someone on our team started using ASP.NET
MVC for a new web interface we're doing. I must say I'm impressed with the level that
the MVC team [ab]uses the C# compiler, mostly in a good way. On the plus side, they
end up with a bit more compiler time checking than would be possible otherwise (we
can only hope WPF will follow suit some day). 
<br /><br />
But one thing struck me odd was how their helper method for generating an HTML form
works. The goal is to generate an HTML form tag with the right action, and they use
lambdas as symbolic references to figure out the action. The next problem is making
sure that the &lt;form&gt; gets closed with a &lt;/form&gt;. The straightforward answer
to this is "create a function that takes a function". The C# signature would be: void
Form&lt;A&gt;(Expression&lt;Action&lt;A&gt;&gt;, Action). Then your ASPX code would
be:<br /><br />
&lt;% Html.Form&lt;FooController&gt;(x =&gt; x.Edit(someVar.FieldX), () =&gt; { %&gt;<br />
    Some Html &lt;% SomeCode%&gt;<br />
&lt;% }); %&gt;<br /><br />
The code is nicely bracketed and works fine. But ASP.NET MVC doesn't actually do that.
Instead, the Form method returns an IDisposable! The code to use it is:<br /><br />
&lt;% using (Html.Form&lt;FooController&gt;(x =&gt; x.Edit(someVar.FieldX)) { %&gt;<br />
    Some Html &lt;% SomeCode%&gt;<br />
&lt;% } %&gt;<br /><br />
Why do they use an IDisposable? The rest of the MVC framework seems to assumes people
are somewhat familiar with lambdas, closures and what not. The only thing I can think
of is that <a href="http://www.atrevido.net/blog/2008/03/31/The+Reason+VBNET+Is+Truly+A+Second+Class+Citizen.aspx">VB
doesn't support anonymous methods</a>. So in order to make it VB friendly, they come
up with quite a strange use of IDisposable to abuse language support for it. Overall,
I'm not sure if this is dumb or cute. 
<br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=41902041-dd54-4407-ac4b-46ae041a3e57" /></body>
      <title>ASP.NET MVC - Abusing Using (At VB's request?)</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,41902041-dd54-4407-ac4b-46ae041a3e57.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/26/ASPNET+MVC+Abusing+Using+At+VBs+Request.aspx</link>
      <pubDate>Tue, 26 Aug 2008 21:19:59 GMT</pubDate>
      <description>Someone on our team started using ASP.NET MVC for a new web interface we're doing. I must say I'm impressed with the level that the MVC team [ab]uses the C# compiler, mostly in a good way. On the plus side, they end up with a bit more compiler time checking than would be possible otherwise (we can only hope WPF will follow suit some day). &lt;br&gt;
&lt;br&gt;
But one thing struck me odd was how their helper method for generating an HTML form
works. The goal is to generate an HTML form tag with the right action, and they use
lambdas as symbolic references to figure out the action. The next problem is making
sure that the &amp;lt;form&amp;gt; gets closed with a &amp;lt;/form&amp;gt;. The straightforward answer
to this is "create a function that takes a function". The C# signature would be: void
Form&amp;lt;A&amp;gt;(Expression&amp;lt;Action&amp;lt;A&amp;gt;&amp;gt;, Action). Then your ASPX code would
be:&lt;br&gt;
&lt;br&gt;
&amp;lt;% Html.Form&amp;lt;FooController&amp;gt;(x =&amp;gt; x.Edit(someVar.FieldX), () =&amp;gt; { %&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Some Html &amp;lt;% SomeCode%&amp;gt;&lt;br&gt;
&amp;lt;% }); %&amp;gt;&lt;br&gt;
&lt;br&gt;
The code is nicely bracketed and works fine. But ASP.NET MVC doesn't actually do that.
Instead, the Form method returns an IDisposable! The code to use it is:&lt;br&gt;
&lt;br&gt;
&amp;lt;% using (Html.Form&amp;lt;FooController&amp;gt;(x =&amp;gt; x.Edit(someVar.FieldX)) { %&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Some Html &amp;lt;% SomeCode%&amp;gt;&lt;br&gt;
&amp;lt;% } %&amp;gt;&lt;br&gt;
&lt;br&gt;
Why do they use an IDisposable? The rest of the MVC framework seems to assumes people
are somewhat familiar with lambdas, closures and what not. The only thing I can think
of is that &lt;a href="http://www.atrevido.net/blog/2008/03/31/The+Reason+VBNET+Is+Truly+A+Second+Class+Citizen.aspx"&gt;VB
doesn't support anonymous methods&lt;/a&gt;. So in order to make it VB friendly, they come
up with quite a strange use of IDisposable to abuse language support for it. Overall,
I'm not sure if this is dumb or cute. 
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=41902041-dd54-4407-ac4b-46ae041a3e57" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,41902041-dd54-4407-ac4b-46ae041a3e57.aspx</comments>
      <category>Code</category>
      <category>ASP.NET</category>
    </item>
  </channel>
</rss>