Use with AttributeRouting

Dec 2, 2011 at 10:53 PM
Edited Dec 2, 2011 at 11:20 PM

First of all, I just wanted to say thanks for creating this. I've been looking at other similar frameworks such as WCF (too complex), ServiceStack (promising, but I disagree with the base philosophy of "everything is a CRUD operation on a request DTO"), and OpenRasta (too disorganized - no website, no docs, no binaries, and their "stable" version doesn't even build). I finally found this and was delighted at the simplicity and thus flexibility and power. 

One thing I just thought I'd pass along (and this is also partially a way of getting the community talking around the project) is my use with AttributeRouting (avail via nuget). It lets me build services like this:

 

public MyService.Service svc;   // set via dependency injection

[GET("users")]
public ActionResult Index() 
{
   return View(svc.LoadAllUsers());
}

[GET("users/{userid}")]
public ActionResult Details(Guid userid) 
{
   return View(svc.LoadUser(userid));
}

[GET("users/{userid}/comments")]
public ActionResult Comment(Guid userid) 
{
   return View(svc.LoadCommentsByUser(userid));
}

[POST("users/{userid}/comments")]
public ActionResult CreateComment(Guid userid, Models.Comment newcomment) 
{
   newcomment.UserId = userid;
   return View(svc.CreateUserComment(newcomment));
}

All you need to do is install the AttributeRouting NuGet package, and then put the attributes on. If you already have ROM working, it works right away.

It's a very nice complement to ROM, as it makes it easy and natural to configure nice REST-style URLs. The default ROM mapping works, don't get me wrong, but this is a bit easier and more natural.

While by convention can be good, when I showed the plain ROM controller code to a co-worker, he did not at all get that the controller code:

 

[HttpPut]
public ActionResult Detail(Guid id, Models.User user)

 

would be invoked when you did a POST to the url /users/739e9195-225d-475a-b705-051098c9e4d0, and honestly, it's not obvious. 

By changing the [HttpPut] attribute out for [Put("users/{id}"] it's now self-documenting, obvious to anyone who doesn't already know ROM, and of course a lot more flexible.

 

 

Coordinator
Dec 3, 2011 at 10:58 PM

That does look like a nice way of defining the routes. I'll have a look into it.