Telligenti

Serving up fresh ideas every day, Telligent style
Welcome to Telligenti Sign in | Join | Help
in Search

Jayme Davis

I will think of something for here later

RestLess - A Simple REST Framework Part 2

Some cool changes have come along for RestLess. Instead of blabbering on, I'll dive right into the changes.

Small naming changes

Based on Dave Donaldson's recommendation, IRESTReadable, RESTProperty, etc have their case changed. They are now IRestReadable, RestProperty, etc. Just do a rename all on REST to Rest if you already have implemented the framework.

No More Default.aspx, pretty urls!

A problem with the first version, was default.aspx was specified in the Url. This is ugly and not very RESTful. There is a new key you can add to web.config appSettings:

<add key="RestLess::GeneratePlaceholderDirs" value="true" />

When this value is set to true, RestLess will generate placeholder folders with an empty default.aspx document. This way you can specify the url as http://mysite.com/api/products/ (notice no default.aspx). Your web application directory will look something like below:

IRestWritable

IRestReadable, in the first version, allowed you to send an xml document down to a user submitting a GET request at the resource url. IRestWritable exposes one method void GetRestPostedObject(object obj). When a POST is submitted to the url/resource, this method will be called. The best thing about GetPostedObject(object obj) is that it will populate the object from querystring for you based on your RestProperty's. If the attribute is specified as IOType.Input or IOType.InputOutput, RestLess will look in the url for you to see if these values were passed by the user, then send you a populated object.

For example (copied the example object from the first version for clarity, new additions in orange)

    // decorate the class as a RestResource, the first parameter is the url of the resource appended to
    // the root url (defined in web.config). In this case /api/products/
    // the second parameter specifies if this resource is discoverable (more on this later)
   
[RestResource("products", true)]
    public class Product : IRestReadable, IRestWritable
    {
        private string _name;
        private string _description;
        private double _amount;

        // render this property as xmlelement "productname"
        // IOType.InputOutput specifies that this property will be rendered
        // to the outputted xml, and can also be inputted via querystring
       
[RestProperty("productname", IOType.InputOutput)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        // render this property as xmlelement "productdescription"
        // IOType.Output specifies this property is rendered to the user,
        // but it is not allowed in querystring for input
       
[RestProperty("productdescription", IOType.Output)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        // render this property as xmlelement "amount" (output only again)
       
[RestProperty("amount", IOType.InputOutput)
        public object GetRESTObject()
        {

            // this is where you could go access your data access layer or whatever other method you choose.
            // this is also where you could request an API key via querystring for security etc
            // since productname is specified inputoutput, you should check for
            // Request.QueryString["productname"] to get any data the user sends

            // for the purposes of this demo, lets just make an object and pass it back
            Product p = new Product();
            p.Name = "Super Fly Ink Pen";
            p.Description = "This is the most super fly ink pen ever";
            p.Amount = 10.00;

            return p;
        }

        #endregion

        #region IRestWritable Members

        public void GetRestPostedObject(object obj)
        {
            Product product = obj as Product;

            // product will be populated here based on anything provided in QueryString, you do not
            // need to retrieve it manually. for example, if the POSTed url is
            // http://mysite.com/api/products/?productname=InkPen&amount=2.50

            product.Name // will equal InkPen
            product.Amount // will equal 2.50
        }
      
        #endregion

    }

Right now, IRestWritable only supports getting values from querystring. That will change later to support xml documents being posted, etc.

Grab the new dll here. (as of now, the plan is to open-source the framework when it's a little further along)

Enjoy!

Jayme

Read the complete post at http://feeds.feedburner.com/~r/JaymeDavis/~3/276620086/

Published Apr 23 2008, 11:20 PM by ndepth.net
Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems