<?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:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>AboutCode.net - c#</title>
    <link>http://www.aboutcode.net/</link>
    <description>Thoughts on Software by Andrew Davey</description>
    <language>en-us</language>
    <copyright>Andrew Davey</copyright>
    <lastBuildDate>Thu, 08 Oct 2009 14:55:37 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>andrew@equin.co.uk</managingEditor>
    <webMaster>andrew@equin.co.uk</webMaster>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=0f434d07-fe80-487b-b7c0-9fdb27dac4a8</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,0f434d07-fe80-487b-b7c0-9fdb27dac4a8.aspx</pingback:target>
      <dc:creator>Andrew Davey</dc:creator>
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,0f434d07-fe80-487b-b7c0-9fdb27dac4a8.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=0f434d07-fe80-487b-b7c0-9fdb27dac4a8</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Over the course of a couple of website developments I created a simple content management
system. I know there are loads our there already, but I wanted something simple and
easy for non-techie users. It runs on ASP.NET MVC, has an elegant extensibility model
and produces clean HTML 5. I very happy with it and plan to use it on all upcoming
simple websites.
</p>
        <p>
The system is file based, so no SQL database is needed. This makes it ideal for running
in shared hosting. It’s perfect for the ~10 page website. But there’s no reason it
can’t scale up to larger sites too.
</p>
        <p>
So I’m obviously enamoured with my own work :) but is it worth sharing? I would love
to get the code out there for people to poke at. However, is it really worth my time
doing so – setting up a project, documentation etc? My recent open sourced work seems
to have taken off like a lead balloon (Snooze, <a href="http://www.assembla.com/wiki/show/hasic">Hasic</a>,
etc)! Do you want to see my new CMS? Or shall I just keep it to myself?
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=0f434d07-fe80-487b-b7c0-9fdb27dac4a8" />
      </body>
      <title>Should I open source my new CMS?</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,0f434d07-fe80-487b-b7c0-9fdb27dac4a8.aspx</guid>
      <link>http://www.aboutcode.net/2009/10/08/ShouldIOpenSourceMyNewCMS.aspx</link>
      <pubDate>Thu, 08 Oct 2009 14:55:37 GMT</pubDate>
      <description>&lt;p&gt;
Over the course of a couple of website developments I created a simple content management
system. I know there are loads our there already, but I wanted something simple and
easy for non-techie users. It runs on ASP.NET MVC, has an elegant extensibility model
and produces clean HTML 5. I very happy with it and plan to use it on all upcoming
simple websites.
&lt;/p&gt;
&lt;p&gt;
The system is file based, so no SQL database is needed. This makes it ideal for running
in shared hosting. It’s perfect for the ~10 page website. But there’s no reason it
can’t scale up to larger sites too.
&lt;/p&gt;
&lt;p&gt;
So I’m obviously enamoured with my own work :) but is it worth sharing? I would love
to get the code out there for people to poke at. However, is it really worth my time
doing so – setting up a project, documentation etc? My recent open sourced work seems
to have taken off like a lead balloon (Snooze, &lt;a href="http://www.assembla.com/wiki/show/hasic"&gt;Hasic&lt;/a&gt;,
etc)! Do you want to see my new CMS? Or shall I just keep it to myself?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=0f434d07-fe80-487b-b7c0-9fdb27dac4a8" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,0f434d07-fe80-487b-b7c0-9fdb27dac4a8.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>cms</category>
      <category>mvc</category>
      <category>web</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=f40c1ace-f631-46f2-aaea-fc9efa16a44b</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,f40c1ace-f631-46f2-aaea-fc9efa16a44b.aspx</pingback:target>
      <dc:creator>Andrew Davey</dc:creator>
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,f40c1ace-f631-46f2-aaea-fc9efa16a44b.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=f40c1ace-f631-46f2-aaea-fc9efa16a44b</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
HTML forms seem to be general enough to warrant a better abstraction than manually
creating the HTML, or even using simple HTML helpers. Ideally the semantics of the
form should be enough to generate all the HTML content.
</p>
        <p>
I have been prototyping some ideas in this area. Here is some code that demonstrates
a simple (but incomplete) login form.
</p>
        <style type="text/css">
.cf { font-family: courier new; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
</style>
        <div class="cf">
          <pre class="cl">
            <span class="cb1">class</span>
            <span class="cb2">LoginData</span>
          </pre>
          <pre class="cl">{</pre>
          <pre class="cl">    <span class="cb1">public</span><span class="cb1">string</span> Username
{ <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre>
          <pre class="cl">    <span class="cb1">public</span><span class="cb1">string</span> Password
{ <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre>
          <pre class="cl">    <span class="cb1">public</span><span class="cb1">bool</span> Persist
{ <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre>
          <pre class="cl">}</pre>
          <pre class="cl"> </pre>
          <pre class="cl"> </pre>
          <pre class="cl">
            <span class="cb1">class</span>
            <span class="cb2">LoginForm</span> : <span class="cb2">Form</span>&lt;<span class="cb2">LoginData</span>&gt;</pre>
          <pre class="cl">{</pre>
          <pre class="cl">    <span class="cb1">public</span> LoginForm()</pre>
          <pre class="cl">    {</pre>
          <pre class="cl">        FieldContainer = <span class="cb1">new</span><span class="cb2">DivFieldContainer</span>();</pre>
          <pre class="cl"> </pre>
          <pre class="cl">        Add(d =&gt; d.Username);</pre>
          <pre class="cl">        Add(d =&gt; d.Password).AsPassword();</pre>
          <pre class="cl">        Add(d =&gt; d.Persist).WithLabel(<span class="cb3">"Stay
logged in on this computer"</span>);</pre>
          <pre class="cl">    }</pre>
          <pre class="cl">}</pre>
          <pre class="cl"> </pre>
          <pre class="cl">
            <span class="cb1">class</span>
            <span class="cb2">Program</span>
          </pre>
          <pre class="cl">{</pre>
          <pre class="cl">    <span class="cb1">static</span><span class="cb1">void</span> Main(<span class="cb1">string</span>[]
args)</pre>
          <pre class="cl">    {</pre>
          <pre class="cl">        <span class="cb1">var</span> data
= <span class="cb1">new</span><span class="cb2">LoginData</span>() { Username = <span class="cb3">"test"</span> };</pre>
          <pre class="cl">        <span class="cb1">var</span> form
= <span class="cb1">new</span><span class="cb2">LoginForm</span>();</pre>
          <pre class="cl"> </pre>
          <pre class="cl">        <span class="cb1">var</span> html
= form.Create(data);</pre>
          <pre class="cl">        <span class="cb2">Console</span>.WriteLine(html.ToString());</pre>
          <pre class="cl">    }</pre>
          <pre class="cl">}</pre>
        </div>
        <p>
This generates the following HTML. 
</p>
        <pre>&lt;form method="POST" action="http://localhost/"&gt;<br />
  &lt;div&gt;<br />
    &lt;label for="Username"&gt;Username&lt;/label&gt;<br />
    &lt;input id="Username" type="text" name="Username" value="test"
/&gt;<br />
  &lt;/div&gt;<br />
  &lt;div&gt;<br />
    &lt;label for="Password"&gt;Password&lt;/label&gt;<br />
    &lt;input id="Password" type="password" name="Password" /&gt;<br />
  &lt;/div&gt;<br />
  &lt;div&gt;<br />
    &lt;input id="Persist" type="checkbox" name="Persist" /&gt;<br />
    &lt;label for="Persist"&gt;Stay logged in on this computer&lt;/label&gt;<br />
  &lt;/div&gt;<br />
&lt;/form&gt;</pre>
        <p>
There is a clear separation of the data from the form meta-data. This lets me do interesting
things like applying conventions, for example, the generation of label text from property
names. I want to use mostly global conventions for a form, and then override a few
specific fields where needed. 
</p>
        <p>
I think validation can also fit nicely into this approach. Validation logic can be
put on the data model. The form model can then specify how errors are added to the
HTML.
</p>
        <p>
Has anyone else seen anything like this before? I don’t want the reinvent the wheel.
Or at least I can steal some good ideas ;)
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=f40c1ace-f631-46f2-aaea-fc9efa16a44b" />
      </body>
      <title>Automatic HTML Forms</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,f40c1ace-f631-46f2-aaea-fc9efa16a44b.aspx</guid>
      <link>http://www.aboutcode.net/2009/03/13/AutomaticHTMLForms.aspx</link>
      <pubDate>Fri, 13 Mar 2009 11:51:16 GMT</pubDate>
      <description>&lt;p&gt;
HTML forms seem to be general enough to warrant a better abstraction than manually
creating the HTML, or even using simple HTML helpers. Ideally the semantics of the
form should be enough to generate all the HTML content.
&lt;/p&gt;
&lt;p&gt;
I have been prototyping some ideas in this area. Here is some code that demonstrates
a simple (but incomplete) login form.
&lt;/p&gt;
&lt;style type="text/css"&gt;
.cf { font-family: courier new; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
&lt;/style&gt;
&lt;div class="cf"&gt;&lt;pre class="cl"&gt;&lt;span class="cb1"&gt;class&lt;/span&gt; &lt;span class="cb2"&gt;LoginData&lt;/span&gt;&lt;/pre&gt;&lt;pre class="cl"&gt;{&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;public&lt;/span&gt; &lt;span class="cb1"&gt;string&lt;/span&gt; Username
{ &lt;span class="cb1"&gt;get&lt;/span&gt;; &lt;span class="cb1"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;public&lt;/span&gt; &lt;span class="cb1"&gt;string&lt;/span&gt; Password
{ &lt;span class="cb1"&gt;get&lt;/span&gt;; &lt;span class="cb1"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;public&lt;/span&gt; &lt;span class="cb1"&gt;bool&lt;/span&gt; Persist
{ &lt;span class="cb1"&gt;get&lt;/span&gt;; &lt;span class="cb1"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre class="cl"&gt;}&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="cl"&gt;&lt;span class="cb1"&gt;class&lt;/span&gt; &lt;span class="cb2"&gt;LoginForm&lt;/span&gt; : &lt;span class="cb2"&gt;Form&lt;/span&gt;&amp;lt;&lt;span class="cb2"&gt;LoginData&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre class="cl"&gt;{&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;public&lt;/span&gt; LoginForm()&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FieldContainer = &lt;span class="cb1"&gt;new&lt;/span&gt; &lt;span class="cb2"&gt;DivFieldContainer&lt;/span&gt;();&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Add(d =&amp;gt; d.Username);&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Add(d =&amp;gt; d.Password).AsPassword();&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Add(d =&amp;gt; d.Persist).WithLabel(&lt;span class="cb3"&gt;"Stay
logged in on this computer"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre class="cl"&gt;}&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="cl"&gt;&lt;span class="cb1"&gt;class&lt;/span&gt; &lt;span class="cb2"&gt;Program&lt;/span&gt;&lt;/pre&gt;&lt;pre class="cl"&gt;{&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;static&lt;/span&gt; &lt;span class="cb1"&gt;void&lt;/span&gt; Main(&lt;span class="cb1"&gt;string&lt;/span&gt;[]
args)&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;var&lt;/span&gt; data
= &lt;span class="cb1"&gt;new&lt;/span&gt; &lt;span class="cb2"&gt;LoginData&lt;/span&gt;() { Username = &lt;span class="cb3"&gt;"test"&lt;/span&gt; };&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;var&lt;/span&gt; form
= &lt;span class="cb1"&gt;new&lt;/span&gt; &lt;span class="cb2"&gt;LoginForm&lt;/span&gt;();&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb1"&gt;var&lt;/span&gt; html
= form.Create(data);&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cb2"&gt;Console&lt;/span&gt;.WriteLine(html.ToString());&lt;/pre&gt;&lt;pre class="cl"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre class="cl"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This generates the following HTML. &lt;pre&gt;&amp;lt;form method="POST" action="http://localhost/"&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;div&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;label for="Username"&amp;gt;Username&amp;lt;/label&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;input id="Username" type="text" name="Username" value="test"
/&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;div&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;label for="Password"&amp;gt;Password&amp;lt;/label&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;input id="Password" type="password" name="Password" /&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;div&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;input id="Persist" type="checkbox" name="Persist" /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;label for="Persist"&amp;gt;Stay logged in on this computer&amp;lt;/label&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;/form&amp;gt;&lt;/pre&gt;
&lt;p&gt;
There is a clear separation of the data from the form meta-data. This lets me do interesting
things like applying conventions, for example, the generation of label text from property
names. I want to use mostly global conventions for a form, and then override a few
specific fields where needed. 
&lt;p&gt;
I think validation can also fit nicely into this approach. Validation logic can be
put on the data model. The form model can then specify how errors are added to the
HTML.
&lt;/p&gt;
&lt;p&gt;
Has anyone else seen anything like this before? I don’t want the reinvent the wheel.
Or at least I can steal some good ideas ;)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=f40c1ace-f631-46f2-aaea-fc9efa16a44b" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,f40c1ace-f631-46f2-aaea-fc9efa16a44b.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>html</category>
      <category>web</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=ed499724-c6b0-43d5-892b-34ee7bdbab6a</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,ed499724-c6b0-43d5-892b-34ee7bdbab6a.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,ed499724-c6b0-43d5-892b-34ee7bdbab6a.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=ed499724-c6b0-43d5-892b-34ee7bdbab6a</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Now that the ASP.NET MVC API has settled down I decided to bring some of my ideas
about REST and resource-oriented programming to it. 
</p>
        <p>
The following screencast shows my current prototype in action:<br /><a href="http://www.aboutcode.net/screencasts/snooze-aspnet-mvc.wmv">http://www.aboutcode.net/screencasts/snooze-aspnet-mvc.wmv</a></p>
        <p>
I'm keen for comments and feedback. 
</p>
        <p>
The source code is currently available via SVN here:<br /><a href="http://svn2.assembla.com/svn/snooze/branches/aspnet">http://svn2.assembla.com/svn/snooze/branches/aspnet</a></p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=ed499724-c6b0-43d5-892b-34ee7bdbab6a" />
      </body>
      <title>New Resource-Oriented Library for ASP.NET MVC</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,ed499724-c6b0-43d5-892b-34ee7bdbab6a.aspx</guid>
      <link>http://www.aboutcode.net/2008/11/26/NewResourceOrientedLibraryForASPNETMVC.aspx</link>
      <pubDate>Wed, 26 Nov 2008 03:23:02 GMT</pubDate>
      <description>&lt;p&gt;
Now that the ASP.NET MVC API has settled down I decided to bring some of my ideas
about REST and resource-oriented programming to it. 
&lt;p&gt;
The following screencast shows my current prototype in action:&lt;br&gt;
&lt;a href="http://www.aboutcode.net/screencasts/snooze-aspnet-mvc.wmv"&gt;http://www.aboutcode.net/screencasts/snooze-aspnet-mvc.wmv&lt;/a&gt; 
&lt;p&gt;
I'm keen for comments and feedback. 
&lt;p&gt;
The source code is currently available via SVN here:&lt;br&gt;
&lt;a href="http://svn2.assembla.com/svn/snooze/branches/aspnet"&gt;http://svn2.assembla.com/svn/snooze/branches/aspnet&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=ed499724-c6b0-43d5-892b-34ee7bdbab6a" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,ed499724-c6b0-43d5-892b-34ee7bdbab6a.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>mvc</category>
      <category>REST</category>
      <category>screencast</category>
      <category>web</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=9e9624bc-ae77-4aa6-8920-50a56e276005</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,9e9624bc-ae77-4aa6-8920-50a56e276005.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,9e9624bc-ae77-4aa6-8920-50a56e276005.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=9e9624bc-ae77-4aa6-8920-50a56e276005</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There are currently two approaches when using L2S. Either adding attributes to classes
to denote columns, or creating an external XML mapping definition. Neither of these
is great. Attributes mix non domain concerns, namely database schema, into domain
objects. XML mapping needs to be kept in sync with the classes it describes. During
the early stages of a new project, the structure of classes can vary and morph over
time. So manual mapping is annoying.
</p>
        <p>
My suggestion is to create a "convention-based" mapping. Where you can define POCOs
and then have reflection to build the mapping, based on some simple naming conventions.
This mapping can then be used by a DataContext and a database created from it. (Note
that I am only considering green field developments, where we are working "code-first".)
</p>
        <p>
The conventions I have thought about so far include:
</p>
        <ul>
          <li>
A property named "Id" will be the primary key.</li>
          <li>
If Id is an int then make it auto incrementing.</li>
          <li>
All primitive, string and DateTime properties are columns.</li>
          <li>
All properties having the type of another entity in the DataContext are associations.</li>
          <li>
All properties of type EntityRef&lt;T&gt; are associations.</li>
          <li>
There must be a foreign key Id property defined for associations.</li>
          <li>
Any property with subtype a of IList&lt;T&gt; is an association.</li>
        </ul>
        <p>
One-to-many associations need to be a bit smart with their names. Consider:
</p>
        <p>
class Customer {<br />
  public Employee ReferringEmployee { get; set; }<br />
}<br />
class Employee {<br />
  public List&lt;Customer&gt; ReferredCustomers { get; set; }<br />
}
</p>
        <p>
The mapping needs to understand the "ing" and "ed" relation. Also, we'll perhaps need
support for TechSupportEmployee &lt;--&gt; TechSupportedCustomers.
</p>
        <p>
The conventions should follow the usual rules of English grammar. I hope, therefore,
that they do not interfere with the universal language discovered by domain driven
design.
</p>
        <p>
As a project begins to settle down, there will be requirements that the convention
mapping doesn't support. Rather than having to switch to full manual XML mapping,
perhaps an augmentation layer could applied. This could describe specific cases of
the mapping that are different. For example, a certain primary key column using GUIDs
instead of integers.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=9e9624bc-ae77-4aa6-8920-50a56e276005" />
      </body>
      <title>LINQ-to-SQL Convention Mapping Source</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,9e9624bc-ae77-4aa6-8920-50a56e276005.aspx</guid>
      <link>http://www.aboutcode.net/2008/08/24/LINQtoSQLConventionMappingSource.aspx</link>
      <pubDate>Sun, 24 Aug 2008 10:09:43 GMT</pubDate>
      <description>&lt;p&gt;
There are currently two approaches when using L2S. Either adding attributes to classes
to denote columns, or creating an external XML mapping definition. Neither of these
is great. Attributes mix non domain concerns, namely database schema, into domain
objects. XML mapping needs to be kept in sync with the classes it describes. During
the early stages of a new project, the structure of classes can vary and morph over
time. So manual mapping is annoying.
&lt;/p&gt;
&lt;p&gt;
My suggestion is to create a "convention-based" mapping. Where you can define POCOs
and then have reflection to build the mapping, based on some simple naming conventions.
This mapping can then be used by a DataContext and a database created from it. (Note
that I am only considering green field developments, where we are working "code-first".)
&lt;/p&gt;
&lt;p&gt;
The conventions I have thought about so far include:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A property named "Id" will be the primary key.&lt;/li&gt;
&lt;li&gt;
If Id is an int then make it auto incrementing.&lt;/li&gt;
&lt;li&gt;
All primitive, string and DateTime properties are columns.&lt;/li&gt;
&lt;li&gt;
All properties having the type of another entity in the DataContext are associations.&lt;/li&gt;
&lt;li&gt;
All properties of type EntityRef&amp;lt;T&amp;gt; are associations.&lt;/li&gt;
&lt;li&gt;
There must be a foreign key Id property defined for associations.&lt;/li&gt;
&lt;li&gt;
Any property with subtype a of IList&amp;lt;T&amp;gt; is an association.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
One-to-many associations need to be a bit smart with their names. Consider:
&lt;/p&gt;
&lt;p&gt;
class Customer {&lt;br&gt;
&amp;nbsp; public Employee ReferringEmployee { get; set; }&lt;br&gt;
}&lt;br&gt;
class Employee {&lt;br&gt;
&amp;nbsp; public List&amp;lt;Customer&amp;gt; ReferredCustomers { get; set; }&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
The mapping needs to understand the "ing" and "ed" relation. Also, we'll perhaps need
support for TechSupportEmployee &amp;lt;--&amp;gt; TechSupportedCustomers.
&lt;/p&gt;
&lt;p&gt;
The conventions should follow the usual rules of English grammar. I hope, therefore,
that they do not interfere with the universal language discovered by domain driven
design.
&lt;/p&gt;
&lt;p&gt;
As a project begins to settle down, there will be requirements that the convention
mapping doesn't support. Rather than having to switch to full manual XML mapping,
perhaps an augmentation layer could applied. This could describe specific cases of
the mapping that are different. For example, a certain primary key column using GUIDs
instead of integers.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=9e9624bc-ae77-4aa6-8920-50a56e276005" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,9e9624bc-ae77-4aa6-8920-50a56e276005.aspx</comments>
      <category>c#</category>
      <category>linq</category>
      <category>linq-to-sql</category>
      <category>orm</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=804bfcdb-830d-4e0a-9117-3b0f1ac70b5f</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,804bfcdb-830d-4e0a-9117-3b0f1ac70b5f.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,804bfcdb-830d-4e0a-9117-3b0f1ac70b5f.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=804bfcdb-830d-4e0a-9117-3b0f1ac70b5f</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have started using <a href="http://ninject.org/">Ninject</a> a dependency injection
framework for .NET. I find it very easy to use and recommend that people take a look.
</p>
        <p>
Something in particular that I found useful is the ability to bind open generic types.
</p>
        <p>
          <font face="Courier New" size="2">class Program<br />
{<br />
    static void Main(string[] args)<br />
    {<br />
        var k = new StandardKernel(new MyModule()); </font>
        </p>
        <p>
          <font face="Courier New" size="2">        var di
= k.Get&lt;Data&lt;int&gt;&gt;();<br />
        di.Content = 14;<br />
        Console.WriteLine(di.Content); </font>
        </p>
        <p>
          <font face="Courier New" size="2">        var ds
= k.Get&lt;Data&lt;string&gt;&gt;();<br />
        ds.Content = "hello";<br />
        Console.WriteLine(ds.Content);<br />
    }<br />
} </font>
        </p>
        <p>
          <font face="Courier New" size="2">class MyModule : StandardModule<br />
{<br />
    public override void Load()<br />
    {<br />
        Bind(typeof(IData&lt;&gt;)).To(typeof(Data&lt;&gt;));<br />
    }<br />
} </font>
        </p>
        <p>
          <font face="Courier New" size="2">class Data&lt;T&gt; : IData&lt;T&gt;<br />
{<br />
    public T Content { get; set; }<br />
} </font>
        </p>
        <p>
          <font face="Courier New" size="2">interface IData&lt;T&gt;<br />
{<br />
    T Content { get; set; }<br />
}</font>
        </p>
        <p>
Very cool!
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=804bfcdb-830d-4e0a-9117-3b0f1ac70b5f" />
      </body>
      <title>Ninject Open Generic Types</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,804bfcdb-830d-4e0a-9117-3b0f1ac70b5f.aspx</guid>
      <link>http://www.aboutcode.net/2008/04/29/NinjectOpenGenericTypes.aspx</link>
      <pubDate>Tue, 29 Apr 2008 18:36:59 GMT</pubDate>
      <description>&lt;p&gt;
I have started using &lt;a href="http://ninject.org/"&gt;Ninject&lt;/a&gt; a dependency injection
framework for .NET. I find it very easy to use and recommend that people take a look.
&lt;/p&gt;
&lt;p&gt;
Something in particular that I found useful is the ability to bind open generic types.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;class Program&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Main(string[] args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var k = new StandardKernel(new MyModule()); &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var di
= k.Get&amp;lt;Data&amp;lt;int&amp;gt;&amp;gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; di.Content = 14;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(di.Content); &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var ds
= k.Get&amp;lt;Data&amp;lt;string&amp;gt;&amp;gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ds.Content = "hello";&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(ds.Content);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
} &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;class MyModule : StandardModule&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void Load()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Bind(typeof(IData&amp;lt;&amp;gt;)).To(typeof(Data&amp;lt;&amp;gt;));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
} &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;class Data&amp;lt;T&amp;gt; : IData&amp;lt;T&amp;gt;&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public T Content { get; set; }&lt;br&gt;
} &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;interface IData&amp;lt;T&amp;gt;&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T Content { get; set; }&lt;br&gt;
}&lt;/font&gt; 
&lt;p&gt;
Very cool!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=804bfcdb-830d-4e0a-9117-3b0f1ac70b5f" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,804bfcdb-830d-4e0a-9117-3b0f1ac70b5f.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>ninject</category>
      <category>programming</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=afe1d0c6-a4e5-451a-ad11-403a1e2126e4</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,afe1d0c6-a4e5-451a-ad11-403a1e2126e4.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,afe1d0c6-a4e5-451a-ad11-403a1e2126e4.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=afe1d0c6-a4e5-451a-ad11-403a1e2126e4</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have released an initial version of Snooze. <a title="http://www.assembla.com/wiki/show/snooze" href="http://www.assembla.com/wiki/show/snooze">http://www.assembla.com/wiki/show/snooze</a></p>
        <p>
Snooze is an REST framework for ASP.NET. It puts the emphasis back on the "R" of URI.
The Resource concept is a first-class citizen.
</p>
        <p>
In Snooze you create a resource class that defines the URI template and HTTP methods
it supports.
</p>
        <p>
          <font face="Courier New">[HttpResource("customer/{CustomerId}")] // Defines the URI
template for this resource<br />
public class CustomerResource : HttpResource // A useful base class that implements
IHttpResource<br />
{<br />
    // URI template variables are mapped to properties 
<br />
    public int CustomerId { get; set; }<br /><br />
    // Handles the HTTP GET method<br />
    public override void Get()<br />
    {<br /></font>
          <font face="Courier New">        RenderView("Customer",
Data.Customers[CustomerId]);<br />
    }<br /><br />
    public override void Put()<br />
    {<br /></font>
          <font face="Courier New">        // Read
data sent in Request body and update the customer...<br />
    }<br /></font>
          <font face="Courier New">
            <br />
    public override void Delete()<br />
    {<br />
        // Delete the customer...<br />
    }<br /><br />
    // HTTP POST can also be provided if it makes sense for the resource.<br />
}</font>
        </p>
        <p>
This will mean a URI like "http://yourserver.com/customer/42" will invoke the Get
method of an instance where CustomerId equals 42.
</p>
        <p>
What about creating a new Customer?
</p>
        <p>
Well you don't have an ID for the new customer since that is probably created in the
database. Therefore you don't know what the URI will be yet. The correct solution
here is to POST the new customer data to a CustomerList resource, with URI "/customers".
</p>
        <h2>Alternative GET Views
</h2>
        <p>
You can create extra GET methods:
</p>
        <p>
          <font face="Courier New">[HttpGetView("edit")]<br />
public void GetEdit()<br />
{<br />
    RenderView("CustomerEdit", Data.Customers[CustomerId]);<br />
}</font>
        </p>
        <p>
This results in URIs like "/customer/42?edit"
</p>
        <h2>File Extensions
</h2>
        <p>
To support different file formats, such as XML and JSON use:
</p>
        <font face="Courier New">[HttpGetFile(".xml")]<br />
public void GetEdit()<br />
{<br />
    SendXml(Data.Customers[CustomerId]);<br />
}</font>
        <p>
This will be mapped to by URIs like /customer/42.xml
</p>
        <h2>Sub-Resources
</h2>
        <p>
Resources can be naturally nested. For example, a Customer may have Orders.
</p>
        <p>
[HttpSubResource(typeof(CustomerResource), "order/{OrderId}", true)]<br />
public class OrderResource : HttpSubResource&lt;CustomerResource&gt;<br />
{<br />
    public int OrderId { get; set; }<br />
    // ... 
<br />
} 
</p>
        <p>
In this sub-resource, you can have access the parent CustomerResource, using the strongly-typed,
ParentResource property. 
</p>
        <p>
URIs for this sub-resource will look like: "/customer/42/order/123" 
</p>
        <p>
The "true" argument in the attribute, specifies that parent query string parameters
are added to this sub-resource URI as well. So a parent with URI template "customer/{CustomerId}?foo={Foo}"
will yield sub-resource URI template "/customer/{CustomerId}/order/{OrderId}?foo={Foo}". 
</p>
        <h2>Nullable Resource Properties
</h2>
        <p>
Imagine a blog posts resource. Its URI could be /posts/{Year}/{Month}/{Day} to get
all the posts on a given date. Now what if you want to naturally get the posts for
a whole month, year, or ever?
</p>
        <p>
By declaring the properties as nullable, e.g. "int?" in C#, Snooze will also map the
"reduced" URIs to your class. You can then test the properties for "null" and return
data accordingly.
</p>
        <h2>Reusing Resource URIs
</h2>
        <p>
When rendering an HTML view it is likely you will need to link to other resources.
Instead of manually typing the URI string, you can call the Uri property available
on HttpResource.
</p>
        <p>
          <font face="Courier New">new CustomerResource { CustomerId = 42 } .Uri</font>
        </p>
        <h2>Resource Areas
</h2>
        <p>
Snooze supports resource "areas". These let you map all the resources of a given assembly
into a given sub-path. If, for example, a third-party blogging package was built using
Snooze you could include it as follows:
</p>
        <p>
web.config:
</p>
        <p>
          <font face="Courier New">&lt;configuration&gt;<br />
  &lt;restAreas&gt;<br />
    &lt;area path="" assembly="MyWebsiteAssembly"/&gt;<br />
    &lt;area path="blog" assembly="SomeCompany.BlogEngine, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"/&gt;<br />
  &lt;/restAreas&gt;</font>
        </p>
        <p>
So the blog resource URIs would look something like: <a href="http://mysite.com/blog/posts/2007/01">http://mysite.com/blog/posts/2007/01</a></p>
        <h2>HTML Form Support
</h2>
        <p>
HTML forms only support GET and POST. To overcome this limitation you can include:
&lt;input type="hidden" name="__method" value="DELETE" /&gt; to override the name
of the HTTP method used by Snooze.
</p>
        <h2>Feedback
</h2>
        <p>
This project came mostly out of curiosity! I wanted to see if REST could be done better
under ASP.NET. The code is still very new (less than two days!) It is open source
(BSD license) and available via <a href="http://svn2.assembla.com/svn/snooze/trunk">SubVersion</a>.
A quick sample project is also provided.
</p>
        <p>
Let me know what you think. Everything is open to changes. Join the <a href="http://www.assembla.com/wiki/show/snooze">assembla</a> project
if you would like to contribute your help.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=afe1d0c6-a4e5-451a-ad11-403a1e2126e4" />
      </body>
      <title>Snooze - A REST Framework for .NET</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,afe1d0c6-a4e5-451a-ad11-403a1e2126e4.aspx</guid>
      <link>http://www.aboutcode.net/2008/01/10/SnoozeARESTFrameworkForNET.aspx</link>
      <pubDate>Thu, 10 Jan 2008 21:42:04 GMT</pubDate>
      <description>&lt;p&gt;
I have released an initial version of Snooze. &lt;a title="http://www.assembla.com/wiki/show/snooze" href="http://www.assembla.com/wiki/show/snooze"&gt;http://www.assembla.com/wiki/show/snooze&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Snooze is an REST framework for ASP.NET. It puts the emphasis back on the "R" of URI.
The Resource concept is a first-class citizen.
&lt;/p&gt;
&lt;p&gt;
In Snooze you create a resource class that defines the URI template and HTTP methods
it supports.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;[HttpResource("customer/{CustomerId}")] // Defines the URI
template for this resource&lt;br&gt;
public class CustomerResource : HttpResource // A useful base class that implements
IHttpResource&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // URI template variables are mapped to properties 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int CustomerId { get; set; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Handles the HTTP GET method&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void Get()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderView("Customer",
Data.Customers[CustomerId]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void Put()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Read
data sent in Request body and update the customer...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void Delete()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Delete the customer...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // HTTP POST can also be provided if it makes sense for the resource.&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
This will mean a URI like "http://yourserver.com/customer/42" will invoke the Get
method of an instance where CustomerId equals 42.
&lt;/p&gt;
&lt;p&gt;
What about creating a new Customer?
&lt;/p&gt;
&lt;p&gt;
Well you don't have an ID for the new customer since that is probably created in the
database. Therefore you don't know what the URI will be yet. The correct solution
here is to POST the new customer data to a CustomerList resource, with URI "/customers".
&lt;/p&gt;
&lt;h2&gt;Alternative GET Views
&lt;/h2&gt;
&lt;p&gt;
You can create extra GET methods:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;[HttpGetView("edit")]&lt;br&gt;
public void GetEdit()&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderView("CustomerEdit", Data.Customers[CustomerId]);&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
This results in URIs like "/customer/42?edit"
&lt;/p&gt;
&lt;h2&gt;File Extensions
&lt;/h2&gt;
&lt;p&gt;
To support different file formats, such as XML and JSON use:
&lt;/p&gt;
&lt;font face="Courier New"&gt;[HttpGetFile(".xml")]&lt;br&gt;
public void GetEdit()&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SendXml(Data.Customers[CustomerId]);&lt;br&gt;
}&lt;/font&gt; 
&lt;p&gt;
This will be mapped to by URIs like /customer/42.xml
&lt;/p&gt;
&lt;h2&gt;Sub-Resources
&lt;/h2&gt;
&lt;p&gt;
Resources can be naturally nested. For example, a Customer may have Orders.
&lt;/p&gt;
&lt;p&gt;
[HttpSubResource(typeof(CustomerResource), "order/{OrderId}", true)]&lt;br&gt;
public class OrderResource : HttpSubResource&amp;lt;CustomerResource&amp;gt;&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int OrderId { get; set; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // ... 
&lt;br&gt;
} 
&lt;p&gt;
In this sub-resource, you can have access the parent CustomerResource, using the strongly-typed,
ParentResource property. 
&lt;p&gt;
URIs for this sub-resource will look like: "/customer/42/order/123" 
&lt;p&gt;
The "true" argument in the attribute, specifies that parent query string parameters
are added to this sub-resource URI as well. So a parent with URI template "customer/{CustomerId}?foo={Foo}"
will yield sub-resource URI template "/customer/{CustomerId}/order/{OrderId}?foo={Foo}". 
&lt;h2&gt;Nullable Resource Properties
&lt;/h2&gt;
&lt;p&gt;
Imagine a blog posts resource. Its URI could be /posts/{Year}/{Month}/{Day} to get
all the posts on a given date. Now what if you want to naturally get the posts for
a whole month, year, or ever?
&lt;/p&gt;
&lt;p&gt;
By declaring the properties as nullable, e.g. "int?" in C#, Snooze will also map the
"reduced" URIs to your class. You can then test the properties for "null" and return
data accordingly.
&lt;/p&gt;
&lt;h2&gt;Reusing Resource URIs
&lt;/h2&gt;
&lt;p&gt;
When rendering an HTML view it is likely you will need to link to other resources.
Instead of manually typing the URI string, you can call the Uri property available
on HttpResource.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;new CustomerResource { CustomerId = 42 } .Uri&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;Resource Areas
&lt;/h2&gt;
&lt;p&gt;
Snooze supports resource "areas". These let you map all the resources of a given assembly
into a given sub-path. If, for example, a third-party blogging package was built using
Snooze you could include it as follows:
&lt;/p&gt;
&lt;p&gt;
web.config:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;lt;configuration&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;restAreas&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;area path="" assembly="MyWebsiteAssembly"/&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;area path="blog" assembly="SomeCompany.BlogEngine, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"/&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;/restAreas&amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
So the blog resource URIs would look something like: &lt;a href="http://mysite.com/blog/posts/2007/01"&gt;http://mysite.com/blog/posts/2007/01&lt;/a&gt;
&lt;/p&gt;
&lt;h2&gt;HTML Form Support
&lt;/h2&gt;
&lt;p&gt;
HTML forms only support GET and POST. To overcome this limitation you can include:
&amp;lt;input type="hidden" name="__method" value="DELETE" /&amp;gt; to override the name
of the HTTP method used by Snooze.
&lt;/p&gt;
&lt;h2&gt;Feedback
&lt;/h2&gt;
&lt;p&gt;
This project came mostly out of curiosity! I wanted to see if REST could be done better
under ASP.NET. The code is still very new (less than two days!) It is open source
(BSD license) and available via &lt;a href="http://svn2.assembla.com/svn/snooze/trunk"&gt;SubVersion&lt;/a&gt;.
A quick sample project is also provided.
&lt;/p&gt;
&lt;p&gt;
Let me know what you think. Everything is open to changes. Join the &lt;a href="http://www.assembla.com/wiki/show/snooze"&gt;assembla&lt;/a&gt; project
if you would like to contribute your help.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=afe1d0c6-a4e5-451a-ad11-403a1e2126e4" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,afe1d0c6-a4e5-451a-ad11-403a1e2126e4.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>programming</category>
      <category>REST</category>
      <category>web</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've made a custom Visual Studio tool that uses a C# parser<br />
(<a href="http://www.codeplex.com/csparser/">http://www.codeplex.com/csparser/</a>)
to convert C# code into an XML<br />
representation. So far only the declaration stuff, classes, method<br />
signatures etc.<br />
I then have a XSLT template engine that takes the XML and a template<br />
file to generate content.<br />
Templates look like:<br />
&lt;% for-each /namespace/class[attribute/@type='ServerModel'] %&gt;<br />
public class &lt;%@name%&gt;<br />
{<br />
}<br />
&lt;%/for-each%&gt;<br />
Easy stuff if you are happy with XSLT.<br />
Currently I use it to generate proxies to classes in my Mobile MVP<br />
library. There are lots of uses I imagine.<br />
Are there any other similar tools out there? If not what are the<br />
prefered places to release this? I took a look at CodePlex but the<br />
TFS source control made me shudder. I'm much more at home with SVN...
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b" />
      </body>
      <title>C# Code Transformation Tool</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b.aspx</guid>
      <link>http://www.aboutcode.net/2007/11/04/CCodeTransformationTool.aspx</link>
      <pubDate>Sun, 04 Nov 2007 23:02:13 GMT</pubDate>
      <description>&lt;p&gt;
I've made a custom Visual Studio tool that uses a C# parser&lt;br&gt;
(&lt;a href="http://www.codeplex.com/csparser/"&gt;http://www.codeplex.com/csparser/&lt;/a&gt;)
to convert C# code into an XML&lt;br&gt;
representation. So far only the declaration stuff, classes, method&lt;br&gt;
signatures etc.&lt;br&gt;
I then have a XSLT template engine that takes the XML and a template&lt;br&gt;
file to generate content.&lt;br&gt;
Templates look like:&lt;br&gt;
&amp;lt;% for-each /namespace/class[attribute/@type='ServerModel'] %&amp;gt;&lt;br&gt;
public class &amp;lt;%@name%&amp;gt;&lt;br&gt;
{&lt;br&gt;
}&lt;br&gt;
&amp;lt;%/for-each%&amp;gt;&lt;br&gt;
Easy stuff if you are happy with XSLT.&lt;br&gt;
Currently I use it to generate proxies to classes in my Mobile MVP&lt;br&gt;
library. There are lots of uses I imagine.&lt;br&gt;
Are there any other similar tools out there? If not what are the&lt;br&gt;
prefered places to release this? I took a look at CodePlex but the&lt;br&gt;
TFS source control made me shudder. I'm much more at home with SVN...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,ef59c8c0-9d08-47c7-9a09-bf70e0c28c9b.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>programming</category>
      <category>thinking</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=5965a970-eb7e-4282-b599-7a5a58777370</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,5965a970-eb7e-4282-b599-7a5a58777370.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,5965a970-eb7e-4282-b599-7a5a58777370.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=5965a970-eb7e-4282-b599-7a5a58777370</wfw:commentRss>
      <slash:comments>11</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
My latest screencast is online: <a title="Mobile Model in MVP web app" href="http://channel9.msdn.com/Showpost.aspx?postid=348722">Mobile
Model in MVP web app</a></p>
        <p>
This screencast continues from the work I demonstrated in a previous screencast: <a href="http://channel9.msdn.com/Showpost.aspx?postid=344711">http://channel9.msdn.com/Showpost.aspx?postid=344711</a><br />
When implemented a Model-View-Presenter pattern client-side in JavaScript there is
a need to get data from the server. Rather than make this divide explicit with HTTP
request, etc, I have used the idea of a model object that moves between the client
and server.<br />
So the programmer simply writes the client and server pieces of functionality and
lets my framework do the plumbing.<br />
I use code generation and <a href="http://projects.nikhilk.net/Projects/ScriptSharp.aspx">Script#</a> to
make writing a client-side web app much more seemless than the usual javascript/XML/JSON/web
service madness.<br />
I'm developing the ideas shown here in a project at the moment. If there is sufficient
interest I may extract a framework to give to the community.<br />
How do people feel about this approach to web development? What kinds of additions/modifications
should be made?<br />
p.s. I have a bit of a cold at the moment, so sorry for the croaky voice in the recording!
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=5965a970-eb7e-4282-b599-7a5a58777370" />
      </body>
      <title>Mobile Model Screencast</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,5965a970-eb7e-4282-b599-7a5a58777370.aspx</guid>
      <link>http://www.aboutcode.net/2007/10/17/MobileModelScreencast.aspx</link>
      <pubDate>Wed, 17 Oct 2007 20:32:19 GMT</pubDate>
      <description>&lt;p&gt;
My latest screencast is online: &lt;a title="Mobile Model in MVP web app" href="http://channel9.msdn.com/Showpost.aspx?postid=348722"&gt;Mobile
Model in MVP web app&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
This screencast continues from the work I demonstrated in a previous screencast: &lt;a href="http://channel9.msdn.com/Showpost.aspx?postid=344711"&gt;http://channel9.msdn.com/Showpost.aspx?postid=344711&lt;/a&gt;
&lt;br&gt;
When implemented a Model-View-Presenter pattern client-side in JavaScript there is
a need to get data from the server. Rather than make this divide explicit with HTTP
request, etc, I have used the idea of a model object that moves between the client
and server.&lt;br&gt;
So the programmer simply writes the client and server pieces of functionality and
lets my framework do the plumbing.&lt;br&gt;
I use code generation and&amp;nbsp;&lt;a href="http://projects.nikhilk.net/Projects/ScriptSharp.aspx"&gt;Script#&lt;/a&gt; to
make writing a client-side web app much more seemless than the usual javascript/XML/JSON/web
service&amp;nbsp;madness.&lt;br&gt;
I'm developing the ideas shown here in a project at the moment. If there is sufficient
interest I may extract a framework to give to the community.&lt;br&gt;
How do people feel about this approach to web development? What kinds of additions/modifications
should be made?&lt;br&gt;
p.s. I have a bit of a cold at the moment, so sorry for the croaky voice in the recording!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=5965a970-eb7e-4282-b599-7a5a58777370" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,5965a970-eb7e-4282-b599-7a5a58777370.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>mvp</category>
      <category>programming</category>
      <category>ria</category>
      <category>screencast</category>
      <category>tier_split</category>
      <category>web</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=900db2c5-53ea-417f-a093-71dae4db5622</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,900db2c5-53ea-417f-a093-71dae4db5622.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,900db2c5-53ea-417f-a093-71dae4db5622.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=900db2c5-53ea-417f-a093-71dae4db5622</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Has anyone looked into an extension method on the .NET 3.5 Expression class that returns
a JavaScript method? This could then be sent to the client web browser.
</p>
        <p>
So you could then write validation logic in C# and run it in the browser as JavaScript!
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=900db2c5-53ea-417f-a093-71dae4db5622" />
      </body>
      <title>ToJavaScript Extension Method</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,900db2c5-53ea-417f-a093-71dae4db5622.aspx</guid>
      <link>http://www.aboutcode.net/2007/10/02/ToJavaScriptExtensionMethod.aspx</link>
      <pubDate>Tue, 02 Oct 2007 08:39:56 GMT</pubDate>
      <description>&lt;p&gt;
Has anyone looked into an extension method on the .NET 3.5 Expression class that returns
a JavaScript method? This could then be sent to the client web browser.
&lt;/p&gt;
&lt;p&gt;
So you could then write validation logic in C# and run it in the browser as JavaScript!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=900db2c5-53ea-417f-a093-71dae4db5622" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,900db2c5-53ea-417f-a093-71dae4db5622.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>linq</category>
      <category>thinking</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=b3c24e16-a240-4eb4-b6cd-a5e420613f33</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,b3c24e16-a240-4eb4-b6cd-a5e420613f33.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,b3c24e16-a240-4eb4-b6cd-a5e420613f33.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=b3c24e16-a240-4eb4-b6cd-a5e420613f33</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Check out my latest <a href="http://channel9.msdn.com/Showpost.aspx?postid=344711" target="_blank">screencast</a> over
at Channel 9. It demonstrates how to implement a model-view-presenter architecture
that runs client-side in a web browser. I am using <a href="http://projects.nikhilk.net/Projects/ScriptSharp.aspx">Script#</a> to
compile a C# project into JavaScript.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=b3c24e16-a240-4eb4-b6cd-a5e420613f33" />
      </body>
      <title>Client-side MVP Screencast</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,b3c24e16-a240-4eb4-b6cd-a5e420613f33.aspx</guid>
      <link>http://www.aboutcode.net/2007/09/29/ClientsideMVPScreencast.aspx</link>
      <pubDate>Sat, 29 Sep 2007 16:08:52 GMT</pubDate>
      <description>&lt;p&gt;
Check out my latest &lt;a href="http://channel9.msdn.com/Showpost.aspx?postid=344711" target="_blank"&gt;screencast&lt;/a&gt; over
at Channel 9. It demonstrates how to implement a model-view-presenter architecture
that runs client-side in a web browser. I am using &lt;a href="http://projects.nikhilk.net/Projects/ScriptSharp.aspx"&gt;Script#&lt;/a&gt; to
compile a C# project into JavaScript.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=b3c24e16-a240-4eb4-b6cd-a5e420613f33" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,b3c24e16-a240-4eb4-b6cd-a5e420613f33.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>mvp</category>
      <category>programming</category>
      <category>screencast</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=e0c4d054-0e59-4ba1-8fd4-80e7467e0816</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,e0c4d054-0e59-4ba1-8fd4-80e7467e0816.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,e0c4d054-0e59-4ba1-8fd4-80e7467e0816.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e0c4d054-0e59-4ba1-8fd4-80e7467e0816</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Imagine we have a set of complex expressions. A subset of these need to be evaluated
at runtime, depending on some state external to the program (e.g. user input). If
these subsets are not distinct from each other then the code will likely become messy
and unstructured.
</p>
        <p>
What we would like to so is define all the expressions first and then pick and choose
those required. This would of course involve evaluating all of the them. This wasteful
of time if only a few are required.
</p>
        <p>
Enter lazy evaluation! From my rather limited exposure to Haskell, I hear that lazy
evaluation is all the rage. :)
</p>
        <p>
Using lambdas in C# 3.0 we can create code like this:
</p>
        <p>
long x = 42;<br />
var lx = Lazy.Eval(() =&gt; x * x * x * x * x * x);<br />
if (some_boolean_expression)<br />
{<br />
  UseNumber(lx);<br />
}
</p>
        <p>
where UseNumber is some function that takes a "long" as input.
</p>
        <p>
Now this example is over-simplified, but it shows the mechanics. We use a call to
Lazy.Eval to return a wrapper around the lambda. So at that point we have not calculated
the expensive expression. Later in the program the variable "lx" is used. lx is of
type <font face="Courier New">Lazy&lt;long&gt;</font> and there exists an implicit
cast from <font face="Courier New">Lazy&lt;long&gt;</font> to <font face="Courier New">long</font>.
At this point the original expression is evaluated and saved by the lazy wrapper.
So next time the value is required the cached value is returned.
</p>
        <p>
Here is the Lazy&lt;T&gt; class:
</p>
        <p>
          <font face="Courier New">public class Lazy&lt;T&gt;<br />
{       
<br />
    bool _gotValue;<br />
    T _value;<br />
    Func&lt;T&gt; _expr; </font>
        </p>
        <p>
          <font face="Courier New">    public Lazy(Func&lt;T&gt; expr)<br />
    {<br />
        _expr = expr;<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public T Value<br />
    {<br />
        get<br />
        {<br />
            if (!_gotValue)<br />
            {<br />
               
_value = _expr();<br />
               
_gotValue = true;<br />
            }<br />
            return _value;<br />
        }<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public static implicit operator T(Lazy&lt;T&gt;
l)<br />
    {<br />
        return l.Value;<br />
    }<br />
}</font>
        </p>
        <p>
          <font face="tre">And to allow the C# 3.0 compiler to infer types for us, we use a
separate Lazy class:</font>
        </p>
        <p>
          <font face="Courier New">public class Lazy<br />
{ 
<br />
    public static Lazy&lt;T&gt; Eval&lt;T&gt;(Func&lt;T&gt; expr)<br />
    {<br />
        return new Lazy&lt;T&gt;(expr);<br />
    }<br />
}</font>
        </p>
        <p>
          <font face="tr">Whilst lazy evaluation may not be useful in everyday programming,
this example does show some of what can be achieved with lambdas in C# 3.0.</font>
        </p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=e0c4d054-0e59-4ba1-8fd4-80e7467e0816" />
      </body>
      <title>Lazy Functions in LINQ</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,e0c4d054-0e59-4ba1-8fd4-80e7467e0816.aspx</guid>
      <link>http://www.aboutcode.net/2007/09/27/LazyFunctionsInLINQ.aspx</link>
      <pubDate>Thu, 27 Sep 2007 10:56:51 GMT</pubDate>
      <description>&lt;p&gt;
Imagine we have a set of complex expressions. A subset of these need to be evaluated
at runtime, depending on some state external to the program (e.g. user input). If
these subsets are not distinct from each other then the code will likely become messy
and unstructured.
&lt;/p&gt;
&lt;p&gt;
What we would like to so is define all the expressions first and then pick and choose
those required. This would of course involve evaluating all of the them. This wasteful
of time if only a few are required.
&lt;/p&gt;
&lt;p&gt;
Enter lazy evaluation! From my rather limited exposure to Haskell, I hear that lazy
evaluation is all the rage. :)
&lt;/p&gt;
&lt;p&gt;
Using lambdas in C# 3.0 we can create code like this:
&lt;/p&gt;
&lt;p&gt;
long x = 42;&lt;br&gt;
var lx = Lazy.Eval(() =&amp;gt; x * x * x * x * x * x);&lt;br&gt;
if (some_boolean_expression)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; UseNumber(lx);&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
where UseNumber is&amp;nbsp;some function that takes a "long" as input.
&lt;/p&gt;
&lt;p&gt;
Now this example is over-simplified, but it shows the mechanics. We use a call to
Lazy.Eval to return a wrapper around the lambda. So at that point we have not calculated
the expensive expression. Later in the program the variable "lx" is used. lx is of
type &lt;font face="Courier New"&gt;Lazy&amp;lt;long&amp;gt;&lt;/font&gt; and there exists&amp;nbsp;an implicit
cast from &lt;font face="Courier New"&gt;Lazy&amp;lt;long&amp;gt;&lt;/font&gt; to &lt;font face="Courier New"&gt;long&lt;/font&gt;.
At this point the original expression is evaluated and saved by the lazy wrapper.
So next time the value is required the cached value is returned.
&lt;/p&gt;
&lt;p&gt;
Here is the Lazy&amp;lt;T&amp;gt; class:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;public class Lazy&amp;lt;T&amp;gt;&lt;br&gt;
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; bool _gotValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T _value;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Func&amp;lt;T&amp;gt; _expr; &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Lazy(Func&amp;lt;T&amp;gt; expr)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _expr = expr;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public T Value&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; if (!_gotValue)&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; {&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;
_value = _expr();&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;
_gotValue = true;&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; }&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; return _value;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/font&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static implicit operator T(Lazy&amp;lt;T&amp;gt;
l)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return l.Value;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="tre"&gt;And to allow the C# 3.0 compiler to infer types for us, we use a
separate Lazy class:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;public class Lazy&lt;br&gt;
{ 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static Lazy&amp;lt;T&amp;gt; Eval&amp;lt;T&amp;gt;(Func&amp;lt;T&amp;gt; expr)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new Lazy&amp;lt;T&amp;gt;(expr);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="tr"&gt;Whilst lazy evaluation may not be useful in everyday programming,
this example does show some of what can be achieved with lambdas in C# 3.0.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=e0c4d054-0e59-4ba1-8fd4-80e7467e0816" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,e0c4d054-0e59-4ba1-8fd4-80e7467e0816.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>linq</category>
      <category>programming</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=6ce406ff-3ed9-4792-b0a3-7f12bb067353</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,6ce406ff-3ed9-4792-b0a3-7f12bb067353.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,6ce406ff-3ed9-4792-b0a3-7f12bb067353.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=6ce406ff-3ed9-4792-b0a3-7f12bb067353</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Have you seen <a href="http://www.nikhilk.net/ScriptSharpIntro.aspx">Script#</a>?
It is very impressive cross-compiler from C# to JavaScript. The libraries make it
simple to program against the HTML DOM and Silverlight among other technologies. I
have been using them recently to program Silverlight 1.0 without having to actually
touch JavaScript. Using high-level OOP concepts makes creating complex user interactions
much easier. 
</p>
        <p>
The Model-View-Presenter pattern has been very popular on the server-side when creating
HTML applications. However, it seems that not many people have considered using it
within the browser. With AJAX techniques becoming <strong>the</strong> way to make
interactive web applications, it seems a little odd that people are not using more
high-level concepts client-side. Of course, this mostly applies to the .NET space.
I am aware there are Java toolkits that generate JavaScript. 
</p>
        <p>
I am therefore going to look into using Script# to implement a client-side, MVP-style,
web application. The Model will contain local data and communicate with the server
(via XML HTTP requests). The View will be a simple object that can get and set values
from HTML/Silverlight elements and raise events when the user clicks on buttons, etc.
The Presenter object will then orchestrate the user interaction logic. All this will
be done within JavaScript by using Script# to convert C# classes, etc, into JavaScript
concepts. 
</p>
        <p>
By writing all the code C#, it should be possible to tier split the Model class into
server-side and client-side pieces. Whilst the JavaScript on the client-side is drastically
different from the C# server-side model, they will share a common set of data fields.
So it should be possible to serialize between the two worlds.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=6ce406ff-3ed9-4792-b0a3-7f12bb067353" />
      </body>
      <title>Client-side MVP with Script#</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,6ce406ff-3ed9-4792-b0a3-7f12bb067353.aspx</guid>
      <link>http://www.aboutcode.net/2007/09/17/ClientsideMVPWithScript.aspx</link>
      <pubDate>Mon, 17 Sep 2007 16:24:00 GMT</pubDate>
      <description>&lt;p&gt;
Have you seen &lt;a href="http://www.nikhilk.net/ScriptSharpIntro.aspx"&gt;Script#&lt;/a&gt;?
It is very impressive cross-compiler from C# to JavaScript. The libraries make it
simple to program against the HTML DOM and Silverlight among other technologies. I
have been using them recently to program Silverlight 1.0 without having to actually
touch JavaScript. Using high-level OOP concepts makes creating complex user interactions
much easier. 
&lt;/p&gt;
&lt;p&gt;
The Model-View-Presenter pattern has been very popular on the server-side when creating
HTML applications. However, it seems that not many people have considered using it
within the browser. With AJAX techniques becoming &lt;strong&gt;the&lt;/strong&gt; way to make
interactive web applications, it seems a little odd that people are not using more
high-level concepts client-side. Of course, this mostly applies to the .NET space.
I am aware there are Java toolkits that generate JavaScript. 
&lt;/p&gt;
&lt;p&gt;
I am therefore going to look into using Script# to implement a client-side, MVP-style,
web application. The Model will contain local data and communicate with the server
(via XML HTTP requests). The View will be a simple object that can get and set values
from HTML/Silverlight elements and raise events when the user clicks on buttons, etc.
The Presenter object will then orchestrate the user interaction logic. All this will
be done within JavaScript by using Script# to convert C# classes, etc, into JavaScript
concepts. 
&lt;/p&gt;
&lt;p&gt;
By writing all the code C#, it should be possible to tier split the Model class into
server-side and client-side pieces. Whilst the JavaScript on the client-side is drastically
different from the C# server-side model, they will share a common set of data fields.
So it should be possible to serialize between the two worlds.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=6ce406ff-3ed9-4792-b0a3-7f12bb067353" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,6ce406ff-3ed9-4792-b0a3-7f12bb067353.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>programming</category>
      <category>silverlight</category>
      <category>thinking</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=f5a854c2-f0d2-4f3b-8ba6-055e3546c816</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,f5a854c2-f0d2-4f3b-8ba6-055e3546c816.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,f5a854c2-f0d2-4f3b-8ba6-055e3546c816.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=f5a854c2-f0d2-4f3b-8ba6-055e3546c816</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When writing assertions it is annoying to write a string that basically mirrors
what the code your testing says. For example:
</p>
        <p>
Debug.Assert(input != null, "input != null");
</p>
        <p>
Similar statements appear when unit testing with tools like <a href="http://www.nunit.org/index.php?p=assertions&amp;r=2.2.7">NUnit</a>.
</p>
        <p>
With Linq it is now possible to avoid this by using expression trees. The basic idea
is to take a boolean assertion function as an expression tree so that we can call
ToString() to get the message for the assert.
</p>
        <p>
void Assert&lt;T&gt;(T obj, Expression&lt;Func&lt;T, bool&gt;&gt; test)<br />
{<br />
  System.Diagnostics.Debug.Assert(test.Compile().Invoke(obj), test.ToString());<br />
}
</p>
        <p>
This is then called like:
</p>
        <p>
Assert(input, i =&gt; i != null);
</p>
        <p>
Given this idea, we can play with the syntax a bit. Using an extension method:
</p>
        <p>
static class Exts<br />
{<br />
    public static void MustSatisfy&lt;T&gt;(this T obj, Expression&lt;Func&lt;T,
bool&gt;&gt; test)<br />
    {<br />
        Debug.Assert(test.Compile().Invoke(obj),
test.ToString());<br />
    }<br />
} 
</p>
        <p>
We then have: 
</p>
        <p>
input.MustSatisfy(i =&gt; i != null) 
</p>
        <p>
Another syntax option would be something like: 
</p>
        <p>
Assert.That(foo).Satisfies(<br />
  i =&gt; i &gt; 0,<br />
  i =&gt; i &lt; 100); 
</p>
        <p>
Where we are now passing an array of assertions (using a params arg in the Satisfies
method).
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=f5a854c2-f0d2-4f3b-8ba6-055e3546c816" />
      </body>
      <title>Assertions via Linq Expressions</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,f5a854c2-f0d2-4f3b-8ba6-055e3546c816.aspx</guid>
      <link>http://www.aboutcode.net/2007/09/05/AssertionsViaLinqExpressions.aspx</link>
      <pubDate>Wed, 05 Sep 2007 11:46:23 GMT</pubDate>
      <description>&lt;p&gt;
When writing assertions&amp;nbsp;it is annoying to write a string that basically mirrors
what the code your testing says. For example:
&lt;/p&gt;
&lt;p&gt;
Debug.Assert(input != null, "input != null");
&lt;/p&gt;
&lt;p&gt;
Similar statements appear when unit testing with tools like &lt;a href="http://www.nunit.org/index.php?p=assertions&amp;amp;r=2.2.7"&gt;NUnit&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
With Linq it is now possible to avoid this by using expression trees. The basic idea
is to take a boolean assertion function as an expression tree so that we can call
ToString() to get the message for the assert.
&lt;/p&gt;
&lt;p&gt;
void Assert&amp;lt;T&amp;gt;(T obj, Expression&amp;lt;Func&amp;lt;T, bool&amp;gt;&amp;gt; test)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; System.Diagnostics.Debug.Assert(test.Compile().Invoke(obj), test.ToString());&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
This is then called like:
&lt;/p&gt;
&lt;p&gt;
Assert(input, i =&amp;gt; i != null);
&lt;/p&gt;
&lt;p&gt;
Given this idea, we can play with the syntax a bit. Using an extension method:
&lt;/p&gt;
&lt;p&gt;
static class Exts&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void MustSatisfy&amp;lt;T&amp;gt;(this T obj, Expression&amp;lt;Func&amp;lt;T,
bool&amp;gt;&amp;gt; test)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(test.Compile().Invoke(obj),
test.ToString());&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
} 
&lt;p&gt;
We then have: 
&lt;p&gt;
input.MustSatisfy(i =&amp;gt; i != null) 
&lt;p&gt;
Another syntax option would be something like: 
&lt;p&gt;
Assert.That(foo).Satisfies(&lt;br&gt;
&amp;nbsp; i =&amp;gt; i &amp;gt; 0,&lt;br&gt;
&amp;nbsp; i =&amp;gt; i &amp;lt; 100); 
&lt;p&gt;
Where we are now passing an array of assertions (using a params arg in the Satisfies
method).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=f5a854c2-f0d2-4f3b-8ba6-055e3546c816" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,f5a854c2-f0d2-4f3b-8ba6-055e3546c816.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>linq</category>
      <category>programming</category>
      <category>syntax</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=d9660556-29e4-4b45-b420-d42ef678d63e</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,d9660556-29e4-4b45-b420-d42ef678d63e.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,d9660556-29e4-4b45-b420-d42ef678d63e.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=d9660556-29e4-4b45-b420-d42ef678d63e</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have so far managed to avoid needing to use any fancy ORM tools. However, this doesn't
mean I like writing all the standard ADO.NET code by hand. Using C# 2.0 anonymous
methods I am able to write code like the following. (I'm not sure all of this is truly
original work; if you have already done this then sweet! I just want to share
the ideas with everyone.)
</p>
        <p>
Customer c = With.Database&lt;Customer&gt;(delegate(Database db)<br />
{<br />
    return db.ExecuteReader&lt;Customer&gt;(<br />
        "select Id, FirstName, Surname from Customer
where Id = @id", // The SQL to execute<br />
        delegate(IDbCommand cmd) // This anonymous
function is called before executing so we can add params.<br />
        {<br />
            db.AddParameter(cmd,
"@id", DbType.Int32, id);<br />
        },<br />
        delegate(IDataReader reader) // The
IDataReader returned from ExecuteReader is passed here.<br />
        {<br />
            if (reader.Read())<br />
               
return new Customer(<br />
                   
reader.GetInt32(0),<br />
                   
reader.GetString(1),<br />
                   
reader.GetString(2));<br />
            else<br />
               
return null;<br />
        });<br />
}); 
</p>
        <p>
My Database class manages the creation of a connection and optionally a transaction.
It then exposes methods to invoke SQL commands (Reader, Scalar and NonQuery). A transaction
can be automatically provided by calling With.DatabaseInTransaction( ... ) instead.
This then wraps the inputted action in a try-catch-finally block, commit and rolling
back in the usual places.
</p>
        <p>
("With" is a static class that provides convenient access to the Database object) 
</p>
        <p>
public static T DatabaseInTransaction&lt;T&gt;(Function&lt;Database, T&gt; function)<br />
{<br />
    using (Database db = new Database())<br />
    {<br />
        db.BeginTransaction();<br />
        try<br />
        {<br />
            T result = function(db);<br />
            db.CommitTransaction();<br />
            return result;<br />
        }<br />
        catch<br />
        {<br />
            db.RollbackTransaction();<br />
            throw;<br />
        }<br />
    }<br />
} 
</p>
        <p>
There are generic and non-generic versions of the functions, depending on if we want
to return a value. 
</p>
        <p>
For example, here is the non-generic ExecuteReader method from Database: 
</p>
        <p>
public void ExecuteReader(string sql, Action&lt;IDbCommand&gt; addParameters, Action&lt;IDataReader&gt;
action)<br />
{<br />
    Debug.Assert(sql != null, "sql cannot be null.");<br />
    Debug.Assert(action != null, "action cannot be null."); 
</p>
        <p>
    using (IDbCommand cmd = CreateCommand(sql))<br />
    {<br />
        if (addParameters != null)<br />
        {<br />
            addParameters(cmd);<br />
        }<br />
        using (IDataReader reader = cmd.ExecuteReader())<br />
        {<br />
            action(reader);<br />
        }<br />
    }<br />
} 
</p>
        <p>
The IDbCommand is created using the following method. Notice that we also handle assigning
the transaction if we're in one.
</p>
        <p>
IDbCommand CreateCommand(string sql)<br />
{<br />
    Debug.Assert(_connection != null &amp;&amp; _connection.State ==
ConnectionState.Open); 
</p>
        <p>
    IDbCommand cmd = _connection.CreateCommand();<br />
    cmd.CommandText = sql;<br />
    cmd.CommandType = CommandType.Text;<br />
    if (_transaction != null)<br />
    {<br />
        cmd.Transaction = _transaction;<br />
    }<br />
    return cmd;<br />
} 
</p>
        <p>
The Database class implements IDisposable, thus allowing the "using" syntax in the
With class methods. In Dispose() I close the connection, if the transaction is still
open I rollback first.
</p>
        <p>
I anyone wants the full Database and With classes drop me a line. I'm looking forward
to C# 3.0 since the improved type inference and lambda syntax will slim down the amount
of keyboard time even more!
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=d9660556-29e4-4b45-b420-d42ef678d63e" />
      </body>
      <title>Data Access Syntax Using Anonymous Methods</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,d9660556-29e4-4b45-b420-d42ef678d63e.aspx</guid>
      <link>http://www.aboutcode.net/2007/08/27/DataAccessSyntaxUsingAnonymousMethods.aspx</link>
      <pubDate>Mon, 27 Aug 2007 09:41:09 GMT</pubDate>
      <description>&lt;p&gt;
I have so far managed to avoid needing to use any fancy ORM tools. However, this doesn't
mean I like writing all the standard ADO.NET code by hand. Using C# 2.0 anonymous
methods I am able to write code like the following. (I'm not sure all of this is truly
original work; if you have already done this&amp;nbsp;then sweet! I just want to share
the ideas with everyone.)
&lt;/p&gt;
&lt;p&gt;
Customer c = With.Database&amp;lt;Customer&amp;gt;(delegate(Database db)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return db.ExecuteReader&amp;lt;Customer&amp;gt;(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "select Id, FirstName, Surname from Customer
where Id = @id", // The SQL to execute&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delegate(IDbCommand cmd) // This anonymous
function is called before executing so we can add params.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; db.AddParameter(cmd,
"@id", DbType.Int32, id);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; },&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delegate(IDataReader reader) //&amp;nbsp;The
IDataReader&amp;nbsp;returned from&amp;nbsp;ExecuteReader is passed here.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; if (reader.Read())&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;
return new Customer(&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;
reader.GetInt32(0),&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;
reader.GetString(1),&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;
reader.GetString(2));&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; else&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;
return null;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br&gt;
}); 
&lt;p&gt;
My Database class manages the creation of a connection and optionally a transaction.
It then exposes methods to invoke SQL commands (Reader, Scalar and NonQuery). A transaction
can be automatically provided by calling With.DatabaseInTransaction( ... ) instead.
This then wraps the inputted action in a try-catch-finally block, commit and rolling
back in the usual places.
&lt;/p&gt;
&lt;p&gt;
("With" is a static class that provides convenient access to the Database object) 
&lt;p&gt;
public static T DatabaseInTransaction&amp;lt;T&amp;gt;(Function&amp;lt;Database, T&amp;gt; function)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; using (Database db = new Database())&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; db.BeginTransaction();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; T result = function(db);&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; db.CommitTransaction();&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; return result;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; db.RollbackTransaction();&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; throw;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
} 
&lt;p&gt;
There are generic and non-generic versions of the functions, depending on if we want
to return a value. 
&lt;p&gt;
For example, here is the non-generic ExecuteReader method from Database: 
&lt;p&gt;
public void ExecuteReader(string sql, Action&amp;lt;IDbCommand&amp;gt; addParameters, Action&amp;lt;IDataReader&amp;gt;
action)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(sql != null, "sql cannot be null.");&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(action != null, "action cannot be null."); 
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; using (IDbCommand cmd = CreateCommand(sql))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (addParameters != null)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; addParameters(cmd);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (IDataReader reader = cmd.ExecuteReader())&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; action(reader);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
} 
&lt;p&gt;
The IDbCommand is created using the following method. Notice that we also handle assigning
the transaction if we're in one.
&lt;/p&gt;
&lt;p&gt;
IDbCommand CreateCommand(string sql)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(_connection != null &amp;amp;&amp;amp; _connection.State ==
ConnectionState.Open); 
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; IDbCommand cmd = _connection.CreateCommand();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.CommandText = sql;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.CommandType = CommandType.Text;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (_transaction != null)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.Transaction = _transaction;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return cmd;&lt;br&gt;
} 
&lt;p&gt;
The Database class implements IDisposable, thus allowing the "using" syntax in the
With class methods. In Dispose() I close the connection, if the transaction is still
open&amp;nbsp;I rollback first.
&lt;/p&gt;
&lt;p&gt;
I anyone wants the full Database and With classes drop me a line. I'm looking forward
to C# 3.0 since the improved type inference and lambda syntax will slim down the amount
of keyboard time even more!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=d9660556-29e4-4b45-b420-d42ef678d63e" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,d9660556-29e4-4b45-b420-d42ef678d63e.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>data_access</category>
      <category>programming</category>
      <category>syntax</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=5b3ee36b-5ebd-4b47-83a9-071e593106d1</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,5b3ee36b-5ebd-4b47-83a9-071e593106d1.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,5b3ee36b-5ebd-4b47-83a9-071e593106d1.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=5b3ee36b-5ebd-4b47-83a9-071e593106d1</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recorded a screencast explaining my tier splitting tool for C#. It uses a Model-View-Presenter
style application to demonstrate the features of the tool.
</p>
        <p>
Check out the <a href="http://www.aboutcode.net/screencasts/tiersplit.wmv">video</a> (wmv
format, 14.8 MB)
</p>
        <p>
I'd love to get some feedback from people...
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=5b3ee36b-5ebd-4b47-83a9-071e593106d1" />
      </body>
      <title>Tier Splitting C# Screencast</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,5b3ee36b-5ebd-4b47-83a9-071e593106d1.aspx</guid>
      <link>http://www.aboutcode.net/2007/08/25/TierSplittingCScreencast.aspx</link>
      <pubDate>Sat, 25 Aug 2007 16:18:50 GMT</pubDate>
      <description>&lt;p&gt;
I recorded a screencast explaining my tier splitting&amp;nbsp;tool for C#. It uses a Model-View-Presenter
style application to demonstrate the features of the tool.
&lt;/p&gt;
&lt;p&gt;
Check out the &lt;a href="http://www.aboutcode.net/screencasts/tiersplit.wmv"&gt;video&lt;/a&gt; (wmv
format, 14.8 MB)
&lt;/p&gt;
&lt;p&gt;
I'd love to get some feedback from people...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=5b3ee36b-5ebd-4b47-83a9-071e593106d1" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,5b3ee36b-5ebd-4b47-83a9-071e593106d1.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>programming</category>
      <category>screencast</category>
      <category>tier_split</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=992199d9-96aa-42ea-a904-86fef64459f0</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,992199d9-96aa-42ea-a904-86fef64459f0.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,992199d9-96aa-42ea-a904-86fef64459f0.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=992199d9-96aa-42ea-a904-86fef64459f0</wfw:commentRss>
      <title>A (somewhat) new approach to application development</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,992199d9-96aa-42ea-a904-86fef64459f0.aspx</guid>
      <link>http://www.aboutcode.net/2007/08/14/ASomewhatNewApproachToApplicationDevelopment.aspx</link>
      <pubDate>Tue, 14 Aug 2007 13:02:43 GMT</pubDate>
      <description>&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;I am excited about Silverlight 1.1 – very excited! Whilst all
the graphics and animations will be a wonder for creating great user experiences,
I eagerly awaiting the chance to run managed code in the browser.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;In my mind, and partially in code, I have already begun to formulate
a new way to design applications. Of course, by &lt;i style="mso-bidi-font-style: normal"&gt;new&lt;/i&gt; I
really mean stealing a bunch of existing good ideas and gluing them together!&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The first key idea is putting the users first – always. These
auto-magic, drag-n-drop data binding tools are great for quick and dirty CRUD applications,
but I am yet to see one produce an application that has great usability. We need to
start by putting ourselves in the user’s position and thinking about why they are
using our software in the first place. It is all about finding the simplest, cleanest,
more beautiful way to enable the user to get their job done. Slapping another data
grid on a page probably is the worst approach!&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;I am a new convert to the Model-View-Presenter way of writing
client applications. Although, I think, the order of coding should be Presenter-View-Model
(it does not have quite the same ring to it however). In addition, by View I mean
the view implementation, not the UI design. I always sketch the view design on paper/white
board first. I suppose using a tool like Microsoft Expression Blend would be OK as
long as you are not precious about the XAML it creates. I am never afraid to throw
out code and start afresh, so why should UI mark-up be any different? Use whatever
tools you find easiest to knock up UI prototypes and shove them under the user’s nose.
The key here is to know roughly what buttons, textboxes, etc, the UI will contain
(we can leave all the fancy colouring in to some guy in a turtle neck). Once this
is the case, we can begin the most important part of the coding: The User Interaction
Logic (UIL).&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The UIL is the Presenter. The UIL says stuff like when the user
clicks this button, read this data from the UI, tell the underlying model to do something
and then tell the view to update. Whilst for some applications this level of control
may seem like overkill, I argue that having to slow down sometimes is OK. This approach
means you have to think about what the user will be doing in your application.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The best way to develop this code is by being test-driven. Create
unit tests that describe the behaviour of the Presenter in terms of how it interacts
with View and Model object-oriented interfaces. Each user story then becomes testable.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Key tools I plan to use here are:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Rhino Mocks – to create mock objects of Views and Models&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Boo + my Rhino Mocks DSL – to make the unit tests easy to read
and write&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;SharpDevelop – because Boo is not readily usable in Visual Studio
yet&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Visual Studio 2008 – to write the implementation of the Presenter
in C#&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;As awesome as the language Boo is, I just cannot stand SharpDevelop
for more than simple projects like the unit tests. Refactoring, intellisense and code-completion
are just way to well done in Visual Studio to ignore it. I have enough memory and
screen space to run VS and SD side-by-side.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;As the Presenter is written, the interfaces for the View and Model
are being fleshed out. The refactoring tool adding the methods automatically to the
interfaces usually does this.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The choice now is whether to develop the Model or the View next.
The View is a very dumb object that simply maps data to and from the UI and raises
events when the user does stuff (clicks, key presses, etc). Since Silverlight does
not have data binding yet, this may lead to somewhat tedious code. Bear in mind also
that the Presenter is meant to be doing tasks such as converting strings into numbers
and back. However, I reckon a tool can generate most of the standard mapping code.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The declarative nature of XAML means it should be easy to add
attributes to button elements that describe the event they should raise when clicked.
A tool could scan this data and write the code for me.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;If data binding was ever added to Silverlight, I would consider
creating pure “data” objects that the Presenter tells the View to bind too. No logic
would be contained within the data objects.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The Model is where things become more interesting from a software
architecture point of view. Silverlight runs in the client’s web browser. So the Presenter
is running client-side, as is the Model on which is depends. The Model is maintaining
state about the client application. For example, which check boxes are checked, which
customer is selected? However, the Model also will need to communicate with a database
running back on the server to read and write data.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;To solve this problem, I plan to use my tier splitting C# macro.
I will write the Model class as one coherent class. By coherent I mean all the data
is there and all the logic regarding the data is there. I do not believe manually
splitting the class and messing about with proxies and web services makes any business
sense. I am not developing some kind of SOA application that is to be called by numerous
different clients. I am trying to get my Model to save its valuable data on the database
that is only accessible from the web server from whence it came. This is a reasonable
tight coupling between the client and server versions of the Model. The coupling is
so tight; I argue that I do not even want to see it!&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;In the case where the data does come from a remote web service,
we &lt;i style="mso-bidi-font-style: normal"&gt;still&lt;/i&gt; have to return to the application’s
home server since Silverlight disallows cross-domain calls. It makes sense to put
any database calls/remote web services calls into the Model. The Model knows what
data it needs, so why bother moving that knowledge outside of the class only to marshal
all the data back in again.&lt;span style="FONT-SIZE: 14pt; mso-bidi-font-size: 12.0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;With LINQ being available we do not even have to pollute the Model
with database specific SQL.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The tier splitting macro will handle generating all the WCF services
and proxies. I just have to decorate the Model’s remote methods with [RunAtServer].&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Thorough unit testing of the Model is required. It should be possible
to create a mock D-LINQ implementation. I can then make assertions about how the Model
interacts with the data layer.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;In summary, I want to have a Silverlight UI that is but a marionette
to a Presenter running client-side. This presenter has complete unit testing to describe
the different user interactions. The Presenter uses a Model object to store all data.
This Model exists on both the client and server side. However, the split implementation
is effectively invisible to the developer.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The application of adequate tooling (both 3&lt;sup&gt;rd&lt;/sup&gt; party
and custom made) will make this an excellent way to approach writing applications.
As Silverlight 1.1 becomes more complete, I will continue to expand this topic.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;The result should be a happier developer and, more importantly,
a happier user.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=992199d9-96aa-42ea-a904-86fef64459f0" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,992199d9-96aa-42ea-a904-86fef64459f0.aspx</comments>
      <category>.net</category>
      <category>boo</category>
      <category>c#</category>
      <category>dsl</category>
      <category>programming</category>
      <category>rhino_mocks</category>
      <category>ria</category>
      <category>silverlight</category>
      <category>tier_split</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=1a9b2bf0-e792-4de1-9d79-505a57b5db2f</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,1a9b2bf0-e792-4de1-9d79-505a57b5db2f.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,1a9b2bf0-e792-4de1-9d79-505a57b5db2f.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=1a9b2bf0-e792-4de1-9d79-505a57b5db2f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When developing a Model for an MVP architecture it needs at some point to access the
database to read and write data. When this model is running on the client-side (in
Silverlight for example) it has no access to the database. I suppose the standard
solution here is to put the data access code in a separate class running on the server.
The client then invokes this service passing the data required.
</p>
        <p>
The approach obviously works, but I don't like it. The Model contains all the data
relevant to the business problem. It has the validation logic for the data. Why do
I need to suddenly create an entirely different service just to read and write to
the database? Now with LINQ there are no code-level ties to a particular database
technology.
</p>
        <p>
The Model should be in charge of telling the database what data it needs, reading
the data and storing into its fields. When saving data the Model should tell the database
what query to execute to save the data.
</p>
        <p>
The root of the problem here is that the Model needs to be split between the client
and server sides. However, I want this to be done in such a way that the developer
is not wading through plumbing and service layers. In essence, I want this:
</p>
        <p>
[DataContract]<br />
public class Model : IModel<br />
{<br />
  [DataMember] public string CustomerName { get; set; }<br />
  [DataMember] public decimal InvoiceAmount { get; set; }<br />
  [RunAtServer]<br />
  public void Load(int id)<br />
  {<br />
    // Access database here. Get data. Save into properties.<br />
  }<br />
}
</p>
        <p>
This needs to generate two Model classes, one for the server (which is basically
the same as above) and one for the client that replaces the [RunAtServer] methods
with calls to a service object.
</p>
        <p>
I have this working! :) I have used my C# macro tool to take the above code and generate
a new file that contains the two Models, a WCF service and client proxy. These
classes are surrounded by "#IF SERVER" and "#IF !SERVER". I then have a WCF service
project and Client project that both compile the generated file. The conditional preprocessor
directives mean each project only gets what it needs.
</p>
        <p>
This is all still prototype-level code, but it does work. I am waiting on Silverlight
1.1 to have better support for WCF, so for now I'm using XBAPs to test. However, there
is nothing stopping me using WinForms for the client as well. I see this as being
a great way to write rich internet applications. I can focus on modelling the
business problem and just decorate any methods that need to be remote with an attribute.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=1a9b2bf0-e792-4de1-9d79-505a57b5db2f" />
      </body>
      <title>Tier Splitting C# Classes</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,1a9b2bf0-e792-4de1-9d79-505a57b5db2f.aspx</guid>
      <link>http://www.aboutcode.net/2007/08/08/TierSplittingCClasses.aspx</link>
      <pubDate>Wed, 08 Aug 2007 12:17:48 GMT</pubDate>
      <description>&lt;p&gt;
When developing a Model for an MVP architecture it needs at some point to access the
database to read and write data. When this model is running on the client-side (in
Silverlight for example) it has no access to the database. I suppose the standard
solution here is to put the data access code in a separate class running on the server.
The client then invokes this service passing the data required.
&lt;/p&gt;
&lt;p&gt;
The approach obviously works, but I don't like it. The Model contains all the data
relevant to the business problem. It has the validation logic for the data. Why do
I need to suddenly create an entirely different service just to read and write to
the database? Now with LINQ there are no code-level ties to a particular database
technology.
&lt;/p&gt;
&lt;p&gt;
The Model should be in charge of telling the database what data it needs, reading
the data and storing into its fields. When saving data the Model should tell the database
what query to execute to save the data.
&lt;/p&gt;
&lt;p&gt;
The root of the problem here is that the Model needs to be split between the client
and server sides. However, I want this to be done in such a way that the developer
is not wading through plumbing and service layers. In essence, I want this:
&lt;/p&gt;
&lt;p&gt;
[DataContract]&lt;br&gt;
public class Model : IModel&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; [DataMember] public string CustomerName { get; set; }&lt;br&gt;
&amp;nbsp; [DataMember] public decimal InvoiceAmount { get; set; }&lt;br&gt;
&amp;nbsp; [RunAtServer]&lt;br&gt;
&amp;nbsp; public void Load(int id)&lt;br&gt;
&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Access database here. Get data. Save into properties.&lt;br&gt;
&amp;nbsp; }&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
This needs to generate two Model classes, one for the server&amp;nbsp;(which is basically
the same as above) and one for the client that replaces the [RunAtServer] methods
with calls to a service object.
&lt;/p&gt;
&lt;p&gt;
I have this working! :) I have used my C# macro tool to take the above code and generate
a new file that contains the two Models,&amp;nbsp;a WCF service and client proxy. These
classes are surrounded by "#IF SERVER" and "#IF !SERVER". I then have a WCF service
project and Client project that both compile the generated file. The conditional preprocessor
directives mean each project only gets what it needs.
&lt;/p&gt;
&lt;p&gt;
This is all still prototype-level code, but it does work. I am waiting on Silverlight
1.1 to have better support for WCF, so for now I'm using XBAPs to test. However, there
is nothing stopping me using WinForms for the client as well. I see this as being
a great way to write rich&amp;nbsp;internet applications. I can focus on modelling the
business problem and just decorate any methods that need to be remote with an attribute.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=1a9b2bf0-e792-4de1-9d79-505a57b5db2f" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,1a9b2bf0-e792-4de1-9d79-505a57b5db2f.aspx</comments>
      <category>.net</category>
      <category>c#</category>
      <category>programming</category>
      <category>tier_split</category>
      <category>ria</category>
    </item>
    <item>
      <trackback:ping>http://www.aboutcode.net/Trackback.aspx?guid=e9daab36-aecf-40c7-b531-90452f2ffe04</trackback:ping>
      <pingback:server>http://www.aboutcode.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.aboutcode.net/PermaLink,guid,e9daab36-aecf-40c7-b531-90452f2ffe04.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.aboutcode.net/CommentView,guid,e9daab36-aecf-40c7-b531-90452f2ffe04.aspx</wfw:comment>
      <wfw:commentRss>http://www.aboutcode.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e9daab36-aecf-40c7-b531-90452f2ffe04</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
After a late night hacking session, I finally have a working syntactic macro system
for C#! It is comprised of a VS custom tool that uses the <a href="http://www.codeplex.com/csparser/">CSparser</a>.
The end result allows me to do this:
</p>
        <p>
using System;<br />
[Macro(AutoClass)]<br />
public interface IModel<br />
{<br />
  string FirstName { get; set; }<br />
  string Surname { get; set; }<br />
  int ID { get; }<br />
  event EventHandler FirstNameChanged;<br />
  event EventHandler SurnameChanged;<br />
}
</p>
        <p>
The CustomTool property of the IModel.cs file is set to "MacroExpander" and the Build
action is set to None. This then generates a new file "under" the source file, called
for example "IModel.generated.cs". This generated file contains the original interface
(minus the bogus attribute) and a new class called "Model" that provides a stanard
implementation of IModel. It includes private fields, a constructor, properties, events
and event raising methods. The class is partial so you can still create a file called
"Model.cs" alongside the interface and implement any methods. In addition, if you
enter implementations of the interface properties/events in Model.cs then the generated
class will not contain them. So it is easy to generate the 80% of standard members
and custom write the others as required.
</p>
        <p>
The sharp-minded out there will note that "AutoClass" is a parameter passed to the
Macro attribute. The expansion process is able to load any IMacro implementation I
care to write. The macro implementation is handed the parsed source tree and allowed
to modify it as required. After all macros have expanded, the source tree is
converted back to CS source text and outputted from the VS custom tool. This generated
file is compiled by VS as usual.
</p>
        <p>
I'm going to start using this AutoClass macro to generate most of my Model code (in
the Model-View-Presenter architecture). Anywhere else I need code generation at this
level I can create more macros easily.
</p>
        <p>
If people are interested in seeing this product released in some way please get in
contact. I will perhaps get a screen cast made up to show it in action.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=e9daab36-aecf-40c7-b531-90452f2ffe04" />
      </body>
      <title>C# Syntactic Macros</title>
      <guid isPermaLink="false">http://www.aboutcode.net/PermaLink,guid,e9daab36-aecf-40c7-b531-90452f2ffe04.aspx</guid>
      <link>http://www.aboutcode.net/2007/07/31/CSyntacticMacros.aspx</link>
      <pubDate>Tue, 31 Jul 2007 12:04:41 GMT</pubDate>
      <description>&lt;p&gt;
After a late night hacking session, I finally have a working syntactic macro system
for C#! It is comprised of a VS custom tool that uses the &lt;a href="http://www.codeplex.com/csparser/"&gt;CSparser&lt;/a&gt;.
The end result allows me to do this:
&lt;/p&gt;
&lt;p&gt;
using System;&lt;br&gt;
[Macro(AutoClass)]&lt;br&gt;
public interface IModel&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; string FirstName { get; set; }&lt;br&gt;
&amp;nbsp; string Surname { get; set; }&lt;br&gt;
&amp;nbsp; int ID { get; }&lt;br&gt;
&amp;nbsp; event EventHandler FirstNameChanged;&lt;br&gt;
&amp;nbsp; event EventHandler SurnameChanged;&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
The CustomTool property of the IModel.cs file is set to "MacroExpander" and the Build
action is set to None. This then generates a new file "under" the source file, called
for example "IModel.generated.cs". This generated file contains the original interface
(minus the bogus attribute) and a new class called "Model" that provides a stanard
implementation of IModel. It includes private fields, a constructor, properties, events
and event raising methods. The class is partial so you can still create a file called
"Model.cs" alongside the interface and implement any methods. In addition, if you
enter implementations of the interface properties/events in Model.cs then the generated
class will not contain them. So it is easy to generate the 80% of standard members
and custom write the others as required.
&lt;/p&gt;
&lt;p&gt;
The sharp-minded out there will note that "AutoClass" is a parameter passed to the
Macro attribute. The expansion process is able to load any IMacro implementation I
care to write. The macro implementation is handed the parsed source tree and allowed
to modify it as required. After all macros have expanded, the&amp;nbsp;source tree is
converted back to CS source text and outputted from the VS custom tool. This generated
file is compiled by VS as usual.
&lt;/p&gt;
&lt;p&gt;
I'm going to start using this AutoClass macro to generate most of my Model code (in
the Model-View-Presenter architecture). Anywhere else I need code generation at this
level I can create more macros easily.
&lt;/p&gt;
&lt;p&gt;
If people are interested in seeing this product released in some way please get in
contact. I will perhaps get a screen cast made up to show it in action.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=e9daab36-aecf-40c7-b531-90452f2ffe04" /&gt;</description>
      <comments>http://www.aboutcode.net/CommentView,guid,e9daab36-aecf-40c7-b531-90452f2ffe04.aspx</comments>
      <category>.net</category>
      <category>programming</category>
      <category>c#</category>
    </item>
  </channel>
</rss>