publish pattern example

2008-03-12 @ 03:39#

based on my 'enlightening moment' yesterday, i sat down to build a simple 'publish-pattern' example using exyus. i'll post it as an article later this weekend.

it was pretty simple. just implement the /articles/{id} resource as usual (using XmlSqlResource and then add a new resource to handle the publish actions - /articles/{id}/status. this second resource only accepts GET and PUT methods.

the exyus code (C#) is small, as usual:

using System;
using Exyus.Web;

namespace Exyus.Samples
{
    [UriPattern(@"/articles/(?<id>[0-9]*)\.xcs")]
    [MediaTypes("text/xml")]
    class Articles : XmlSqlResource
    {
        public Articles()
        {
            this.AllowCreateOnPut = false;
            this.AllowDelete = true;
            this.AllowPost = true;
            this.ConnectionString = "exyus_samples";
            this.ContentType="text/xml";
            this.DocumentsFolder = "~/documents/articles/";
            this.LocalMaxAge = 600;
            this.PostLocationUri = "/articles/";
            this.RedirectOnPost = true;
            this.RedirectOnPut = true;
            this.ImmediateCacheUriTemplates = new string[]
            {
                "/articles/.xcs",
                "/articles/{id}.xcs"
            };
        }
    }

    [UriPattern(@"/articles/(?<id>[0-9]*)/status\.xcs")]
    [MediaTypes("text/xml")]
    class ArticleStatus : XmlSqlResource
    {
        public ArticleStatus()
        {
            this.AllowCreateOnPut = false;
            this.AllowDelete = false;
            this.AllowPost = false;
            this.ConnectionString = "exyus_samples";
            this.ContentType = "text/xml";
            this.DocumentsFolder = "~/documents/articles/status/";
            this.LocalMaxAge = 600;
            this.RedirectOnPut = true;
            this.ImmediateCacheUriTemplates = new string[]
            {
                "/articles/{id}/status.xcs",
                "/articles/.xcs",
                "/articles/{id}.xcs"
            };
        }

        public override void Delete()
        {
            throw new System.Web.HttpException(405, "Unable to DELETE resource");
            //base.Delete();
        }

        public override void Post()
        {
            throw new System.Web.HttpException(405, "Unable to POST resource");
            //base.Post();
        }
    }
}

the XSLT was also simple (more on that in the article). the only other itemswas the SQL sproc work. since i'm using SQL Server 2005, i have some great XML support at the ready. for example, i use SQL Server's ability to return streaming XML to make it easy to handle datawithin the exyus engine. here's an example of getting a list of articles in the database:

CREATE PROCEDURE [dbo].[articles_list]
  @status nvarchar(50) = null
AS
BEGIN
  SET NOCOUNT ON;

  select id as '@id',
    [status] as 'status',
    datecreated as 'date-created',
    title,
    body
  from articles
  where
    @status is null or @status=[status]
  order by datecreated desc
  for xml path('article'), root('articles')
END

slick!

code