Siesta: Painless REST via ASP.NET MVC
Zen uses quite a few open source libraries, and I feel a sense of responsibility to contribute back where I can. To that end, I’m releasing the basic infrastructure that powers the Zen API under the name Siesta.
Siesta is a simple and flexible REST system driven by ASP.NET MVC. While you can take the Siesta bits and use them directly in your app, it’s intended more as an example of how to implement your own system.
The basic idea behind Siesta is to communicate only using models within your application, and allow the infrastructure to handle the serialization and de-serialization, similar to how a view engine manipulates ViewModels or ViewData. Models are just POCOs, and serialization is controlled the [DataContract] and [DataMember] attributes in System.Runtime.Serialization (so yes, this requires .NET 3.5).
Siesta also uses the (sweet-with-extra-awesomesauce) JSON.NET library from James Newton-King. There’s a DataContractJsonSerializer in the BCL now, but as far as I could tell, it doesn’t support indentation. JSON.NET supports both, so it’s a great fit. I’ve also been using it forever, so I’m just used to it – if you deal with JSON formatting, it’s definitely worth a look.
The wire (serialization) format is based on MIME types, and Siesta comes with XML and JSON built-in. However, it’s also extensible by implementing a new IModelSerializer. Speaking of which, if someone wants to implement one for YAML, it would be greatly appreciated.
As described in the Zen API documentation, the wire format is determined by the Content-Type and Accept headers. Content-Type is preferred for incoming data, and Accept is preferred for outgoing data. If a header is missing, the system will fail over to the other one.
Here’s an example of an entity that can be serialized via REST:
[DataContract(Name = "user", Namespace = "")]
public class User : IEntity
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "username")]
public string UserName { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "email")]
public string Email { get; set; }
}
And its matching controller:
public class UserController : SiestaControllerBase
{
public IRepository<User> UserRepository { get; private set; }
public UserController(IRepository<User> userRepository)
{
UserRepository = userRepository;
}
public ModelResult<User> Get(GetRequest request)
{
User user = UserRepository.Get(request.Id);
if (user == null)
throw NotFound();
return Model(user, request.Formatting);
}
//...
}
Don’t worry, SiestaControllerBase is just a simple base controller which provides the Model() method shortcut. This (and the IEntity interface you might have noticed on User) are entirely optional, and are actually part of the Siesta.Example assembly rather than the main Siesta assembly. As long as the types you want to communicate with are serializable, they’ll work with Siesta.
In contrast to the Siesta example, in Zen, we actually have separate models (which act as DTOs, and are serialized to/from a wire format) and entities (which are persisted to the database). The example doesn’t show this because it requires a whole separate mapping step, which can get tricky. I would highly recommend that if you use this in a real-world app, you create a separate type hierarchy and map between them.
Anyhow, you can grab the Siesta bits on GitHub, including the full version of this (simple) example. If you have feedback, by all means let me know! There’s actually quite a bit of stuff in Zen that I’d like to release as OSS – it’s just a matter of getting the time to split the code off the product and wrap it up.
Beginning ASP.NET MVC 1.0
Earlier this week, the UPS delivery guy surprised me with a copy of Beginning ASP.NET MVC 1.0 by Simone Chiaretta and Keyvan Nayyeri. I haven’t had a chance to read much more than just a brief skim of it so far, but I had an opportunity to review some of the content before it was published. What I’ve seen looks great, and I don’t just say that because it talks about Ninject.
I plan to dig into it further when I can scrounge a few minutes.
Congratulations to Simone and Keyvan for finishing the book and getting it published! It looks like a great resource for aspiring ASP.NET MVC developers.
If you want to learn more, you can check out Simone and Keyvan’s posts.
The Media Blitz Continues
In addition to working hard to improve Zen, I’ve also been talking about it to anyone that will listen.
I’ve been fortunate enough to be featured on a few podcasts over the past few weeks to discuss lean, kanban, and Zen:
First, Scott Hanselman had me on Hanselminutes in mid-July. This conversation was a quick introduction about lean and kanban, with a few tidbits about Zen sprinkled in. I already mentioned this before, but the post was a 30-second-hurry-up-and-publish-and-then-get-back-to-grinding-out-code, so I figured I’d do it a bit more justice.
Then, the guys at Herding Code had me on to talk about the technology behind Zen, and the process of launching a company. This turned into a great conversation, although I wasn’t quite lucid by the end. (I’m not sure you can tell by listening, but I’d put in 16 hours that day and by the end of recording, it was 1:30AM EST!)
Last, but certainly not least, I was featured on .NET Rocks, where I talked with Carl and Richard about the idea behind Zen and my best understanding of lean and kanban. This was the first time I was on .NET Rocks, and I think it turned into a great conversation.
That’s all the podcasts that I’ve recorded so far, and without something new to talk about, I’m not sure I want to do too many more. I’m sure the audiences of the podcasts overlap, and by now I must be starting to annoy people. If so, sorry about that!
Anyhow, a huge thanks to the hosts of the podcasts, and everyone that listened. I’m obviously excited about this stuff, and I hope after listening to me ramble, others get excited about it too.
Hanselminutes
In case you missed it yesterday, Scott Hanselman was nice enough to have me on Hanselminutes to talk about lean, kanban, and Zen. I think it turned into a pretty good conversation, so go have a listen!
Zen is Live!
It’s been about a year since the ideas for Zen started to gel in my mind. After more long nights and working weekends than I can count, I’m proud to announce that Zen is open to the public! I’d like to thank everyone for their support, especially those that tinkered with the product during the beta process – your feedback was absolutely invaluable.
I’d also like to extend a special thanks to James Avery, Mike Eaton, Jayme Davis, and Sean Chambers. They not only used the application since the days of the first alpha, but also through their constant heckling, convinced me to work on it in the first place, and helped me through the dip to get the product to launch.
Finally, I’d like to thank my beautiful and brilliant wife, Nicole, for signing up to ride this rollercoaster with me. I couldn’t do it without you.
So what are you waiting for? Go sign up for Zen!
Microsoft Finally Realizes I’m Awesome
July 1st came with a great surprise, as I was named a Microsoft C# MVP for 2009! If you’re not familiar with the award, here’s a blurb from their website:
Microsoft Most Valuable Professionals (MVPs) are exceptional technical community leaders from around the world who are awarded for voluntarily sharing their high quality, real world expertise in offline and online technical communities. Microsoft MVPs are a highly select group of experts that represents the technical community’s best and brightest, and they share a deep commitment to community and a willingness to help others.
Although it’s overshadowed a bit by my excitement about the imminent launch of Zen, I’m thrilled to receive the award. Thank you to all the existing MVPs that nominated me, and congratulations to those who were renewed and other freshman MVPs, including Mike Eaton, John Stockton, and Sarah Dutkiewicz.
(And yes, the title is a joke. Totally kidding.)
