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.
Restfulie C# requires the dot net framework 3.5+, and uses castle in order to create dynamic proxy for setting up relations.
Siga-me no twitter!
[...] This release documentation is already out and the download can also be done at github. Another announcement was made at Mauricio’s blog. [...]
Muito legal o trabalho Maurício.
Estou acompanhando o Restfulie por cima, mas gosto muito da idéia, e acredito que o mindset por trás dessa tecnologia tem muito espaço pra conquistar.
Parabéns!
[...] This post was mentioned on Twitter by Matt Hinze, Pedro Reys. Pedro Reys said: RT @mhinze: congrats to the restfulie team on a new release http://bit.ly/9aNpRT // (fwd: @mauricioaniche) [...]