Caelum - Cursos de Java, Scrum, Ruby on Rails


Rest in C# with Restfulie

By admin em 12/07/10

Mauricio Aniche with contributions from Pedro Reys and Felipe Seixas have released the Restfulie’s C# Server 0.5 support on top of Asp.Net Mvc 2.

As new versions of Restfulie come up, it becomes more clear which extension points are important and should be easy to be used, the media type and http results are two of those aspects that Restfulie C# tries to help the developers by allowing them to write classes focused on one task only as we shall see here.

A simple C# Item model can be rendered in your controller as in ruby and java:

[ActAsRestfulie]
public class ItemsController : Controller
{
    private MemoryDatabase database;

    public ItemsController()
    {
        database = new MemoryDatabase();
    }

    public virtual ActionResult Index()
    {
        return new Ok(database.List());
    }

    public virtual ActionResult Get(int id)
    {
        var item = database.List().
            Where(i => i.Id == id).SingleOrDefault();

        if (item == null)
             return new NotFound();
        return new Ok(item);
    }
}

If you want to add some link relations to any resource, simply let your model implement IBehaveAsResource and add the SetRelations method:

public class Item : IBehaveAsResource
{
  public void SetRelations(Relations relations)
  {
    relations.Named("self").
              Uses().Get(Id);
    relations.Named("origin").
              At("http://www.some-fabric.com/");
  }
}

The http GET request to an item with application/json in the accept header will now return:

{"Id":1,"Name":"Pencil","Price":"1.50",
"links":[{"rel":"self","href":"http://localhost:1198/Items/1"},
{"rel":"origin","href":"http://www.some-fabric.com/"}]}

But we know relations work in different ways with different media types (and the Link header) so you might want to add your own media type marshaller/unmarshaller, which is capable of understanding a new media type. Restfulie C# comes with support for application/json, application/xml and application/atom+xml.

If you want to add your own result, simply implement a RestfulieResult using a series of decorators to your new result:

    public class Ok : RestfulieResult
    {
        public Ok() { }
        public Ok(object model) : base(model) { }

        public override ResultDecorator GetDecorators()
        {
            return new StatusCode((int)HttpStatusCode.OK,
                   new ContentType(MediaType.Synonyms.First(),
                   new Content(BuildContent())));
        }
    }

This release documentation is already out and the download can also be done at github. Another announcement was made at Mauricio’s blog.

Restfulie C# requires the dot net framework 3.5+, and uses castle in order to create dynamic proxy for setting up relations.

1 Comment »

  1. [...] Rest in C# with Restfulie – The Restfulie’s C# Server 0.5 support on top of Asp.Net Mvc 2 has been released. [...]

    Pingback by This Week in #REST – Volume 21 (Jun 28 2010 – Jul 12 2010) « This week in REST — July 14, 2010 @ 8:35 am

RSS feed for comments on this post. TrackBack URL

Leave a comment




Caelum
São Paulo: Rua Vergueiro, 3185, cj. 87, próximo ao Metrô Vila Mariana   |   Tel. +55 11 5571-2751
Rio de Janeiro: Rua Senador Dantas, 80, cj. 307/308 - Centro   |   Tel. +55 21 2220-4156 or +55 21 2297-0033
Brasília: SCS Qd. 8 Bl. B-50, Sala 521 - Ed. Venâncio 2000   |   Tel. +55 61 3039-4222