<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>AboutCode.net</title>
  <link rel="alternate" type="text/html" href="http://www.aboutcode.net/" />
  <link rel="self" href="http://www.aboutcode.net/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2008-07-13T07:27:17.6168434-07:00</updated>
  <author>
    <name>Andrew Davey</name>
  </author>
  <subtitle>Thoughts on Software by Andrew Davey</subtitle>
  <id>http://www.aboutcode.net/</id>
  <generator uri="http://www.dasblog.net" version="1.9.6264.0">DasBlog</generator>
  <entry>
    <title>Strongly Typed View Data in NRest</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/07/13/Strongly+Typed+View+Data+In+NRest.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,cd7d5d9e-871d-424e-b24c-586501b2ed95.aspx</id>
    <published>2008-07-13T07:27:17.6168434-07:00</published>
    <updated>2008-07-13T07:27:17.6168434-07:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="dsl" label="dsl" scheme="http://www.aboutcode.net/CategoryView,category,dsl.aspx" />
    <category term="macro" label="macro" scheme="http://www.aboutcode.net/CategoryView,category,macro.aspx" />
    <category term="nemerle" label="nemerle" scheme="http://www.aboutcode.net/CategoryView,category,nemerle.aspx" />
    <category term="nrest" label="nrest" scheme="http://www.aboutcode.net/CategoryView,category,nrest.aspx" />
    <category term="REST" label="REST" scheme="http://www.aboutcode.net/CategoryView,category,REST.aspx" />
    <category term="web" label="web" scheme="http://www.aboutcode.net/CategoryView,category,web.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Sending data from a resource to a view in a strongly-typed, but syntactically lightweight
manner is not possible with C# or VB.NET.
</p>
        <p>
So check out this <a href="http://screencast.com/t/ByvBTDhJ">screencast</a> of the
new "render" macro in <a href="http://www.assembla.com/wiki/show/nrest">NRest</a>!
</p>
        <p>
In a nutshell, my resource looks like:
</p>
        <p>
          <font face="Courier New">[Resource("")]<br />
public class RootResource<br />
{<br />
    public Get() : void<br />
    {<br />
        def test = 42;<br />
        render (Foo = "Hello", Bar = "World", test);<br />
    }<br />
}</font>
        </p>
        <p>
My VB.NET view looks like: 
</p>
        <p>
          <font face="Courier New">Public Class RootPage<br />
    Inherits Page(Of RootPageContent) </font>
        </p>
        <p>
          <font face="Courier New">    Public Overrides Function GetHtml() As
XElement<br />
        Return _<br />
        &lt;html&gt;<br />
            &lt;body&gt;&lt;%=
Content.Foo %&gt;, &lt;%= Content.Bar %&gt;&lt;/body&gt;<br />
        &lt;/html&gt;<br />
    End Function 
<br />
End Class</font>
        </p>
        <p>
The RootPageContent DTO class is generated at compile time by the render macro. Everything
is strongly-typed. <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=cd7d5d9e-871d-424e-b24c-586501b2ed95" /></p>
      </div>
    </content>
  </entry>
  <entry>
    <title>Quick NRest Video</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/07/02/Quick+NRest+Video.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,18337f8d-e357-4017-835b-d5064c48d444.aspx</id>
    <published>2008-07-02T03:19:24.641048-07:00</published>
    <updated>2008-07-02T03:22:14.3140089-07:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="nemerle" label="nemerle" scheme="http://www.aboutcode.net/CategoryView,category,nemerle.aspx" />
    <category term="REST" label="REST" scheme="http://www.aboutcode.net/CategoryView,category,REST.aspx" />
    <category term="screencast" label="screencast" scheme="http://www.aboutcode.net/CategoryView,category,screencast.aspx" />
    <category term="vb.net" label="vb.net" scheme="http://www.aboutcode.net/CategoryView,category,vb.net.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is a very quick video showing the work I've been doing with <a href="http://nemerle.org/">Nemerle</a>,
ASP.NET routing and VB 9 XML literals.<br />
I'm trying to find the most efficient way to create RESTful web applications. I'm
willing to use all the tools in the box and even make my own to achieve this. 
</p>
        <p>
          <object id="NRest-001" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="695" width="1008" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
            <param name="_cx" value="14552" />
            <param name="_cy" value="10583" />
            <param name="FlashVars" value="" />
            <param name="Movie" value="http://www.aboutcode.net/screencasts/nrest-001.swf" />
            <param name="Src" value="http://www.aboutcode.net/screencasts/nrest-001.swf" />
            <param name="WMode" value="Window" />
            <param name="Play" value="0" />
            <param name="Loop" value="-1" />
            <param name="Quality" value="High" />
            <param name="SAlign" value="" />
            <param name="Menu" value="-1" />
            <param name="Base" value="" />
            <param name="AllowScriptAccess" value="" />
            <param name="Scale" value="NoScale" />
            <param name="DeviceFont" value="0" />
            <param name="EmbedMovie" value="0" />
            <param name="BGColor" value="FFFFFF" />
            <param name="SWRemote" value="" />
            <param name="MovieData" value="" />
            <param name="SeamlessTabbing" value="1" />
            <param name="Profile" value="0" />
            <param name="ProfileAddress" value="" />
            <param name="ProfilePort" value="0" />
            <param name="AllowNetworking" value="all" />
            <param name="AllowFullScreen" value="false" />
            <embed src="http://www.aboutcode.net/screencasts/nrest-001.swf" quality="high" bgcolor="#FFFFFF" width="1008" height="695" name="NRest-001" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
            </embed>
          </object>
        </p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=18337f8d-e357-4017-835b-d5064c48d444" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Inject app settings from config</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/06/24/Inject+App+Settings+From+Config.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,e148f446-c437-4d26-9b2a-172f27c7d497.aspx</id>
    <published>2008-06-24T04:13:21.511839-07:00</published>
    <updated>2008-06-24T06:34:24.605589-07:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
When unit testing a class that relies on app settings you don't want to be setting
up web.config to get it running. I realised today that app settings are a dependency
of a class. Therefore they must be injected by the IoC container. I'm using <a href="http://ninject.org/">Ninject</a>...
</p>
        <p>
I have this in my web.config:<br /><font face="Courier New">&lt;appSettings&gt; &lt;add key="bar" value="1234" /&gt;
&lt;/appSettings&gt;</font></p>
        <p>
          <font face="ver">I want to inject that value into a class constructor as below:</font>
        </p>
        <p>
class Foo {<br />
  public Foo(AppSetting bar) 
<br />
  { ... }<br />
}
</p>
        <p>
AppSetting is a simple wrapper around a string:<br /><font face="Courier New">class AppSetting {<br />
  public AppSetting(string value) { Value = value; }<br />
  public string Value { get; private set; }<br />
}</font></p>
        <p>
I then configure Ninject with:<br /><strong>Bind&lt;AppSetting&gt;().ToProvider&lt;AppSettingProvider&gt;();</strong></p>
        <p>
Where AppSettingProvider is:<br /><font face="Courier New">class AppSettingProvider : Ninject.Core.Creation.IProvider<br />
{<br />
    public object Create(Ninject.Core.Activation.IContext context)<br />
    {<br />
        return new AppSetting(WebConfigurationManager.AppSettings[context.Target.Name]);<br />
    } </font></p>
        <p>
          <font face="Courier New">    public Type GetImplementationType(Ninject.Core.Activation.IContext
context)<br />
    {<br />
        return typeof(AppSetting);<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public bool IsCompatibleWith(Ninject.Core.Activation.IContext
context)<br />
    {<br />
        return WebConfigurationManager.AppSettings[context.Target.Name]
!= null;<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public Type Prototype<br />
    {<br />
        get { return typeof(AppSetting); }<br />
    }<br />
}</font>
        </p>
        <p>
          <font face="verd">The end result is a class that can be trivially unit tested, since
it never calls the configuration API directly. Also, there was no need to create some
complex abstraction around the API just to enable unit testing. In the future I can
even change how and where app settings are stored (maybe using a database for example)
all I'd have to do is make a new provider.</font>
        </p>
        <p>
Ninject - in a word: awesome!
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=e148f446-c437-4d26-9b2a-172f27c7d497" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Hasic Demo</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/05/10/Hasic+Demo.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,77343809-58ef-46f7-b44b-2d002e72c4df.aspx</id>
    <published>2008-05-10T09:37:41.3736007-07:00</published>
    <updated>2008-05-10T09:37:41.3736007-07:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="vb.net" label="vb.net" scheme="http://www.aboutcode.net/CategoryView,category,vb.net.aspx" />
    <category term="web" label="web" scheme="http://www.aboutcode.net/CategoryView,category,web.aspx" />
    <category term="hasic" label="hasic" scheme="http://www.aboutcode.net/CategoryView,category,hasic.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
        <p>
A demo website using my Hasic view engine for ASP.NET MVC.
</p>
        <p>
          <a href="http://www.aboutcode.net/content/binary/HasicDemo.zip">HasicDemo.zip (275.95
KB)</a>
        </p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=77343809-58ef-46f7-b44b-2d002e72c4df" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Ninject Open Generic Types</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/04/29/Ninject+Open+Generic+Types.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,804bfcdb-830d-4e0a-9117-3b0f1ac70b5f.aspx</id>
    <published>2008-04-29T11:36:59.2795803-07:00</published>
    <updated>2008-04-29T11:36:59.2795803-07:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="c#" label="c#" scheme="http://www.aboutcode.net/CategoryView,category,c%23.aspx" />
    <category term="ninject" label="ninject" scheme="http://www.aboutcode.net/CategoryView,category,ninject.aspx" />
    <category term="programming" label="programming" scheme="http://www.aboutcode.net/CategoryView,category,programming.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Test-Friendly Snooze</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/03/24/TestFriendly+Snooze.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,70a89628-aa31-4ce8-815b-e5ff151a35f3.aspx</id>
    <published>2008-03-24T10:19:52.0163672-07:00</published>
    <updated>2008-03-24T10:19:52.0163672-07:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
After beginning to use Snooze on a real project I realised it needed to be more extensible
and more testable. So I re-wrote it!
</p>
        <p>
The new code is now in the trunk: <a title="Snooze SVN" href="http://svn2.assembla.com/svn/snooze/trunk">http://svn2.assembla.com/svn/snooze/trunk</a></p>
        <p>
A key extensibility point is the "ResourceContext" class. Before a resource is invoked,
it's ResourceContext is initialised with relevant data such as HttpContext, invoke
verb, GET view and file type. The base Resource class allows sub-classes to specify
a particular ResourceContext sub-class, by the use of a generic type parameter. So
when using Snooze in a web project it is easy to define extra context data that is
always available to any resources. Instead of adding properties to a Resource class,
the use of a context object allows data to be easily shared between a sub-resource
and it's parent resource chain.
</p>
        <p>
A Snooze resource also has a ResourceFactory object. The type of this is also generically
typed so that a web project can provide a customized factory.
</p>
        <p>
These changes make it easy to do things like test that a resource creates sub-resources
(through the factory).
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=70a89628-aa31-4ce8-815b-e5ff151a35f3" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Testing Controller RenderView</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/03/24/Testing+Controller+RenderView.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,6c1cb700-c584-43b3-9fb2-7ee0fe1ae3cd.aspx</id>
    <published>2008-03-24T09:08:16.0007422-07:00</published>
    <updated>2008-03-24T09:09:19.9226172-07:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="mvc" label="mvc" scheme="http://www.aboutcode.net/CategoryView,category,mvc.aspx" />
    <category term="thinking" label="thinking" scheme="http://www.aboutcode.net/CategoryView,category,thinking.aspx" />
    <category term="web" label="web" scheme="http://www.aboutcode.net/CategoryView,category,web.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've seen people testing that RenderView is called by a controller by inheriting into
a "testable" controller. Madness!
</p>
        <p>
RenderView is just a convenience method that calls to IViewEngine, so why not just
test expectations on that with a mock? We basically want to test that the correct
ViewContext is sent to the RenderView method of the view engine.
</p>
        <p>
This is my test:
</p>
        <p>
          <font face="Courier New">[TestFixture]<br />
public class HomeControllerTests : ControllerTestsBase&lt;HomeController&gt;<br />
{<br />
    public override HomeController CreateController()<br />
    {<br />
        return new HomeController();<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    [Test]<br />
    public void Renders_Index()<br />
    {<br />
        var render = ExpectRenderView(); </font>
        </p>
        <p>
          <font face="Courier New">        Controller.Index(); </font>
        </p>
        <p>
          <font face="Courier New">        Assert.That(render.Data.ViewName,
Is.EqualTo("Index"));<br />
    }<br />
}</font>
        </p>
        <p>
I don't think we can get more straight forward than that!
</p>
        <p>
This is my test base class:<br />
(I'm using Moq as the mock framework.)
</p>
        <p>
          <font face="Courier New">public abstract class ControllerTestsBase&lt;T&gt;<br />
    where T : Controller<br />
{<br />
    public T Controller;<br />
    public Mock&lt;IViewEngine&gt; ViewEngine; </font>
        </p>
        <p>
          <font face="Courier New">    [SetUp]<br />
    public virtual void SetUp()<br />
    {<br />
        Controller = CreateController();<br />
        ViewEngine = new Mock&lt;IViewEngine&gt;();<br />
        Controller.ViewEngine = ViewEngine.Object; </font>
        </p>
        <p>
          <font face="Courier New">        Controller.ControllerContext
= new Mock&lt;ControllerContext&gt;(new Mock&lt;HttpContextBase&gt;().Object, new
RouteData(), Controller).Object;<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public abstract T CreateController(); </font>
        </p>
        <p>
          <font face="Courier New">    public RenderCall&lt;ViewContext&gt; ExpectRenderView()<br />
    {<br />
        var render = new RenderCall&lt;ViewContext&gt;();<br />
        ViewEngine.Expect(v =&gt; v.RenderView(It.IsAny&lt;ViewContext&gt;()))<br />
            .Callback(new Action&lt;ViewContext&gt;(render.Set));<br />
        return render;<br />
    }<br />
}</font>
        </p>
        <p>
The RenderCall class provides a place to receive the view context that is passed. 
</p>
        <p>
public class RenderCall&lt;T&gt;<br />
{<br />
    bool _called;<br />
    T _data; 
</p>
        <p>
    public T Data<br />
    {<br />
        get<br />
        {<br />
            if (!_called) throw
new InvalidOperationException("View was not rendered.");<br />
            return _data;<br />
        }<br />
    } 
</p>
        <p>
    public void Set(T data)<br />
    {<br />
        _data = data;<br />
        _called = true;<br />
    }<br />
} <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=6c1cb700-c584-43b3-9fb2-7ee0fe1ae3cd" /></p>
      </div>
    </content>
  </entry>
  <entry>
    <title>VB.NET 9 as an HTML View Engine</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/02/19/VBNET+9+As+An+HTML+View+Engine.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,4e588971-16cd-4e75-b64e-cefaf759ff66.aspx</id>
    <published>2008-02-19T04:12:17.4389929-08:00</published>
    <updated>2008-02-19T04:12:17.4389929-08:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="html" label="html" scheme="http://www.aboutcode.net/CategoryView,category,html.aspx" />
    <category term="thinking" label="thinking" scheme="http://www.aboutcode.net/CategoryView,category,thinking.aspx" />
    <category term="vb.net" label="vb.net" scheme="http://www.aboutcode.net/CategoryView,category,vb.net.aspx" />
    <category term="web" label="web" scheme="http://www.aboutcode.net/CategoryView,category,web.aspx" />
    <category term="xml" label="xml" scheme="http://www.aboutcode.net/CategoryView,category,xml.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
VB.NET 9 introduces a clean syntax for expressing XML data. XHTML is XML so why not
use VB.NET to generate it, instead of ASPX pages? People are creating view engines
for the new ASP.NET MVC framework. How about a view engine that uses VB.NET XML literals?
</p>
        <p>
The benefits to this approach include full intellisense and access to a the full VB.NET
language when creating HTML.
</p>
        <p>
I see a view as a function from some data to an XML element (the &lt;html&gt; element):
</p>
        <p>
          <font face="Courier New">Function CustomersPage(ByVal title As String, ByVal customers
As IEnumerable(Of Customer)) As XElement<br />
    Return _<br />
        &lt;html&gt;<br />
            &lt;head&gt;<br />
               
&lt;title&gt;&lt;%= title %&gt;&lt;/title&gt;<br />
            &lt;/head&gt;<br />
            &lt;body&gt;<br />
            &lt;%= From customer
In customers Select _<br />
               
&lt;div id=&lt;%= customer.LastName %&gt;&gt;<br />
                   
&lt;h1&gt;&lt;%= customer.FirstName %&gt;&lt;/h1&gt;<br />
               
&lt;/div&gt; %&gt;<br />
            &lt;/body&gt;<br />
        &lt;/html&gt;<br />
End Function</font>
        </p>
        <p>
The important change from ASPX is that this is HTML in Code, rather than Code in HTML.
As a result less "automagical" behaviour is required; It's just a function! This means
AJAX features like "partial rendering", where a section of page needs to be updated,
can be expressed by just calling a function that returns a &lt;div&gt; element. That
same function can be used by the full HTML page function too.
</p>
        <p>
ASPX "usercontrols" become simply functions as well. ASPX Master Pages are functions
that have arguments for "placeholders" that get inserted into an template HTML page.
Instead of having to reinvent a bunch of programming language concepts inside ASPX,
we can just use a programming language that now support XML!
</p>
        <p>
The only down side to this approach is we lose the IDE visual designer support. However,
I find viewing an ASPX page that contains even simple conditional data rendering next
to useless. I'd rather keep IE open and just refresh the page to see changes.
</p>
        <p>
I am yet to embrace the MVC framework. I am waiting to see if the next release can
better support Snooze framework ideas. However, there's no reason I can't use this
VB XML magic in Snooze as it current is.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=4e588971-16cd-4e75-b64e-cefaf759ff66" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Async WebRequest using LINQ syntax</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/01/14/Async+WebRequest+Using+LINQ+Syntax.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,82b9402d-c3be-469f-9496-6fc50c8efef1.aspx</id>
    <published>2008-01-14T05:22:13.2543345-08:00</published>
    <updated>2008-01-14T06:20:20.9430245-08:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Wes Dyer's paper <a title="http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx" href="http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx">http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx</a> has
a great introduction to using Monads in C# and specifically how to create a continuation
Monad that leverages the LINQ syntax.
</p>
        <p>
After a lot of re-reading and coding, I managed to construct an asynchronous web request
program.
</p>
        <p>
          <em>UPDATE: Added Select&lt;T&gt; method for the monad so "let" syntax works.</em>
        </p>
        <p>
          <font face="Courier New">class Program<br />
{<br />
    static void Main(string[] args)<br />
    {<br />
        var requests = new[] 
<br />
        {<br />
            WebRequest.Create("</font>
          <a href="http://www.google.com/&quot;)">
            <font face="Courier New">http://www.google.com/")</font>
          </a>
          <font face="Courier New">,<br />
            WebRequest.Create("</font>
          <a href="http://www.yahoo.com/&quot;)">
            <font face="Courier New">http://www.yahoo.com/")</font>
          </a>
          <font face="Courier New">,<br />
            WebRequest.Create("</font>
          <a href="http://channel9.msdn.com/&quot;)">
            <font face="Courier New">http://channel9.msdn.com/")</font>
          </a>
          <br />
          <font face="Courier New">        };<br />
        var pages = from request in requests<br />
            select<br />
               
from response in request.GetResponseAsync()<br />
               
let stream = response.GetResponseStream()<br />
               
from html in stream.ReadToEndAsync()<br />
               
select new { html, response }; </font>
        </p>
        <p>
          <font face="Courier New">        foreach (var page
in pages)<br />
        {<br />
            page(d =&gt;<br />
            {<br />
               
Console.WriteLine(d.response.ResponseUri.ToString());<br />
               
Console.WriteLine(d.html.Substring(0, 40));<br />
               
Console.WriteLine();<br />
            });<br />
        } </font>
        </p>
        <p>
          <font face="Courier New">        Console.ReadKey();<br />
    }<br />
}</font>
        </p>
        <p>
The actual web requests are deferred until the for loop invokes each "page" method.
If you run the example a few times, you see that the results come back in a non-deterministic
order, as expected with concurrent asynchronous operations. In addition, the web response
stream is read asynchronously. So the main thread is never blocked - in fact that's
why we need the Console.ReadKey() at the end, to stop the process ending before the
results come back!
</p>
        <p>
I had to change the type of the continuations from what Wes used. Instead of Func&lt;T,
Answer&gt; I am just using Action&lt;T&gt;. The following code implements the continuation
monad and the additional WebRequest and Stream extension methods.
</p>
        <p>
          <font face="Courier New">delegate void K&lt;T&gt;(Action&lt;T&gt; c); </font>
        </p>
        <p>
          <font face="Courier New">static class Continuations<br />
{<br />
    public static K&lt;T&gt; ToContinuation&lt;T&gt;(this T value)<br />
    {<br />
        return c =&gt; c(value);<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public static K&lt;U&gt; SelectMany&lt;T,
U&gt;(this K&lt;T&gt; m, Func&lt;T, K&lt;U&gt;&gt; k)<br />
    {<br />
        return c =&gt; m(x =&gt; k(x)(c));<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public static K&lt;V&gt; SelectMany&lt;T,
U, V&gt;(this K&lt;T&gt; m, Func&lt;T, K&lt;U&gt;&gt; k, Func&lt;T, U, V&gt; s)<br />
    {<br />
        return m.SelectMany(x =&gt; k(x).SelectMany(y
=&gt; s(x, y).ToContinuation&lt;V&gt;()));<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public static K&lt;U&gt; Select&lt;U,
T&gt;(this K&lt;T&gt; m, Func&lt;T, U&gt; k)<br />
    {<br />
        return c =&gt; m(x =&gt; c(k(x)));<br />
    }</font>
        </p>
        <p>
          <font face="Courier New">    public static K&lt;WebResponse&gt; GetResponseAsync(this
WebRequest request)<br />
    {<br />
        return send =&gt; request.BeginGetResponse(result
=&gt;<br />
        {<br />
            var response =
request.EndGetResponse(result);<br />
            send(response);<br />
        }, null);<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">    public static K&lt;string&gt; ReadToEndAsync(this
Stream stream)<br />
    {<br />
        return send =&gt; 
<br />
        {<br />
            byte[] buffer =
new byte[1024];<br />
            StringBuilder sb
= new StringBuilder();<br />
            Func&lt;IAsyncResult&gt;
readChunk = null;<br />
            readChunk = ()
=&gt; stream.BeginRead(buffer, 0, 1024, result =&gt;<br />
            {<br />
               
int read = stream.EndRead(result);<br />
               
if (read &gt; 0)<br />
               
{<br />
                   
sb.Append(Encoding.UTF8.GetString(buffer, 0, read));<br />
                   
readChunk();<br />
               
}<br />
               
else<br />
               
{<br />
                   
stream.Dispose();<br />
                   
send(sb.ToString());<br />
               
}<br />
            }, null);<br />
            readChunk();<br />
        };<br />
    } </font>
        </p>
        <p>
          <font face="Courier New">}</font>
        </p>
        <p>
I imagine we will see something like this come out of the <a href="http://labs.live.com/volta">Volta</a> project
soon. Hopefully, it will form a generic asynchronous library that we can all use with
anything that implements the Begin/End Invoke pattern.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=82b9402d-c3be-469f-9496-6fc50c8efef1" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Snooze - A REST Framework for .NET</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2008/01/10/Snooze+A+REST+Framework+For+NET.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,afe1d0c6-a4e5-451a-ad11-403a1e2126e4.aspx</id>
    <published>2008-01-10T13:42:04.5911491-08:00</published>
    <updated>2008-01-10T13:42:04.5911491-08:00</updated>
    <category term=".net" label=".net" scheme="http://www.aboutcode.net/CategoryView,category,.net.aspx" />
    <category term="c#" label="c#" scheme="http://www.aboutcode.net/CategoryView,category,c%23.aspx" />
    <category term="programming" label="programming" scheme="http://www.aboutcode.net/CategoryView,category,programming.aspx" />
    <category term="REST" label="REST" scheme="http://www.aboutcode.net/CategoryView,category,REST.aspx" />
    <category term="web" label="web" scheme="http://www.aboutcode.net/CategoryView,category,web.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Nouns-Verbs, OOP-Functional, REST-Web Services</title>
    <link rel="alternate" type="text/html" href="http://www.aboutcode.net/2007/12/24/NounsVerbs+OOPFunctional+RESTWeb+Services.aspx" />
    <id>http://www.aboutcode.net/PermaLink,guid,ade094fa-17c5-4664-80da-fc0c9b3eda9e.aspx</id>
    <published>2007-12-24T01:44:48.2506433-08:00</published>
    <updated>2007-12-24T01:46:09.7193933-08:00</updated>
    <category term="programming" label="programming" scheme="http://www.aboutcode.net/CategoryView,category,programming.aspx" />
    <category term="REST" label="REST" scheme="http://www.aboutcode.net/CategoryView,category,REST.aspx" />
    <category term="thinking" label="thinking" scheme="http://www.aboutcode.net/CategoryView,category,thinking.aspx" />
    <category term="web" label="web" scheme="http://www.aboutcode.net/CategoryView,category,web.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is just a quick thought that came to me whilst making coffee this morning...
</p>
        <p>
REST places focus on nouns (resources) in a system. Whereas it seems web services
are more concerned with verbs. A service is a collection of operations. The data is
passed to and from operations as messages.
</p>
        <p>
Now both approaches have their merits. What strikes me as strange is our choice of
programming languages when attempting to implement the aforementioned architectural
styles. Object orientated programming is all about nouns (classes). Functional programming
is all about verbs (functions).
</p>
        <p>
I know that there is way more to web services than just a bunch of operations (because
that would be simply RPC right?). However, my point is that surely OOP is ideally
suited to describing a set of resources as required by a RESTful design.
</p>
        <img width="0" height="0" src="http://www.aboutcode.net/aggbug.ashx?id=ade094fa-17c5-4664-80da-fc0c9b3eda9e" />
      </div>
    </content>
  </entry>
</feed>