Software Development

My First OpenRasta Project – Part 2 (Resource Templates)

To get started see part 1.

For this part the Invoice class has been expanded to include another property, you’ll see why in a moment. For now, it now looks like this:

public class Invoice
{
    public string Reference { get; set; }
    public DateTime Date { get; set; }
}

Up to this point we are just showing some simple XML based on one of the built in codecs that ship as part of OpenRasta. There is a single URI and it always returns the same thing. So far there is nothing much going on.

You can keep on adding resources to the ResourceSpace when you are configuring the site, but that is hardly a scalable solution when so many applications are based on dynamic data. You need a way to define a resource template.

The way this is done is by adding place holders into the URI. These placeholders are defined by a set of curly braces with a parameter name inside. This is a bit like the string.Format method, except you can use meaningful names instead of the ordinal position of the parameter.

The configuration of the ResourceSpace in the Configuration class is changed to:

ResourceSpace.Has.ResourcesOfType<Invoice>()
    .AtUri("/invoice/{reference}")
    .HandledBy<InvoiceHandler>()
    .AsXmlDataContract();

As you can see the only difference is that the parameter on the AtUri method is changed.

OpenRasta will then look in the handler for a method that matches the HTTP verb and the parameters defined in the template.

The InvoiceHandler now has a method that looks like this:

public Invoice Get(string reference)
{
    Invoice result = InvoiceRepository.GetInvoiceByReference(reference);
    return result;
}

Don’t worry about the InvoiceRepository. It simply exists to get an Invoice object from somewhere. It could be from a database, in memory, a file or wherever.

We can now go to the uri /invoice/123-ABC and get the output:

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://schemas.datacontract.org/2004/07/MyFirstOpenRastaProject.Resources">
  <Date>2009-09-30T00:00:00+01:00</Date>
  <Reference>123-ABC</Reference>
</Invoice>

However, that’s not the whole story. You can do some pretty neat things with resource templates.

For example, if the method parameter on the resource handler is a DateTime object you can build up the URI template using the property names in the DateTime object. The template parameters will then be mapped to the properties in the DateTime object.

First the configuration has to be updated:

ResourceSpace.Has.ResourcesOfType<Invoice>()
    .AtUri("/invoice/{reference}")
    .And.AtUri("/invoice/{day}/{month}/{year}")
    .HandledBy<InvoiceHandler>()
    .AsXmlDataContract();

There is only one additional line here and that is to add a URI with a template containing the day, month and year. It is still the same type of resource and the code hasn’t changed. All that is new is the URI template. If you were to attempt to create a brand new ResourceSpace for the Invoice resource you’d get an error that the resource type was already registered in the system.

The InvoiceHandler class will need an additional method to handle the new template. The new method looks like this:

public Invoice Get(DateTime date)
{
    Invoice result = InvoiceRepository.GetInvoiceByDate(date);
    return result;
}

The result for the URI /invoice/29/09/2009 looks like this

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://schemas.datacontract.org/2004/07/MyFirstOpenRastaProject.Resources">
  <Date>2009-09-29T00:00:00</Date>
  <Reference>3e5a4e9e-3b46-4b09-a8f8-45010411501b</Reference>
</Invoice>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s