<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="/rss.xsl"?><rss version="2.0"><channel><title>restbucks Wiki Rss Feed</title><link>http://restbucks.codeplex.com/</link><description>restbucks Wiki Rss Description</description><item><title>Updated Wiki: RestBucks on .Net; the barista-bot</title><link>http://restbucks.codeplex.com/wikipage?title=RestBucks on .Net; the barista-bot&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;&lt;em&gt;Previous posts&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://joseoncode.com/2011/05/31/introducing-restbucks-on-net/"&gt;&lt;em&gt;Introducing RestBucks on .Net&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/01/restbucks-on-net-ordering-coffee/"&gt;&lt;em&gt;RestBucks on .Net; ordering coffee&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&lt;/em&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/03/restbucks-on-net-is-online/"&gt;&lt;em&gt;RestBucks on .Net is onilne!&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://joseoncode.com/2011/06/07/restbucks-on-net-querying-the-order/"&gt;RestBucks on .Net; querying the order&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://joseoncode.com/2011/06/09/restbucks-on-net-canceling-an-order/"&gt;RestBucks on .Net; canceling the order&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://joseoncode.com/2011/06/15/restbucks-on-net-paying-the-order/"&gt;RestBucks on .Net; paying the order&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;&lt;img align="left" src="" style="margin:0px 14px 18px 0px; display:inline; float:left"&gt;&lt;/p&gt;
&lt;p&gt;Imagine now that you have another system running in parallel with RestBucks on .Net using the same object domain. This other system is used by the Barista;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A &lt;b&gt;barista&lt;/b&gt; (from the Italian for &amp;quot;bartender&amp;quot;) is a person, usually a coffee-house employee, who prepares and serves
&lt;a href="http://en.wikipedia.org/wiki/Espresso"&gt;espresso&lt;/a&gt;-based &lt;a href="http://en.wikipedia.org/wiki/Coffee"&gt;
coffee&lt;/a&gt; drinks.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What I did in RestBucks was to simulate this behavior; our BaristaBot will pick paid orders each 45 seconds and prepare it. For this purpose I have used some ReactiveExtensions magic:&lt;/p&gt;
&lt;pre&gt;public class Barista
{
    private readonly ISessionFactory sessionFactory;
    private readonly IDisposable subscription;

    public Barista(ISessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
        subscription = Observable.Interval(TimeSpan.FromSeconds(45))
            .Subscribe(PrepareNextOrder);
    }

    public void PrepareNextOrder(long i)
    {
        using(var session = sessionFactory.OpenSession())
        using(var tx = session.BeginTransaction())
        {
            var order = session.Query&amp;lt;Order&amp;gt;()
                            .Where(o =&amp;gt; o.Status == OrderStatus.Paid)
                            .OrderBy(o =&amp;gt; o.Payment.DateTime)
                            .FirstOrDefault();
            if (order == null) return;
            
            order.Finish();
            
            tx.Commit();
        }
    }

    public void Dispose()
    {
        subscription.Dispose();
    }
}&lt;/pre&gt;
&lt;p&gt;I didn't use the repository or other abstractions here to keep things simples. This solution depends on NHibernate and Rx.&lt;/p&gt;
&lt;p&gt;You can test the behavior in our live instance at &lt;a href="http://restbuckson.net"&gt;
http://restbuckson.net&lt;/a&gt;. &lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 15 Jun 2011 18:16:34 GMT</pubDate><guid isPermaLink="false">Updated Wiki: RestBucks on .Net; the barista-bot 20110615061634P</guid></item><item><title>Updated Wiki: Documentation</title><link>http://restbucks.codeplex.com/documentation?version=6</link><description>&lt;div class="wikidoc"&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Overview"&gt;Overview&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Preparing%20the%20environment"&gt;Preparing the environment&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Running%20the%20Application"&gt;Running the Application&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;Implementation:
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3a%20Ordering%20Coffee"&gt;Ordering Coffee&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3b%20querying%20the%20order"&gt;Querying the order&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3b%20canceling%20an%20order"&gt;Canceling the order&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3b%20paying%20the%20order"&gt;Paying the order&lt;/a&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 15 Jun 2011 17:23:26 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Documentation 20110615052326P</guid></item><item><title>Updated Wiki: Documentation</title><link>http://restbucks.codeplex.com/documentation?version=5</link><description>&lt;div class="wikidoc"&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Overview"&gt;Overview&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Preparing%20the%20environment"&gt;Preparing the environment&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Running%20the%20Application"&gt;Running the Application&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;Implementation:
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3a%20Ordering%20Coffee"&gt;Ordering Coffee&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3b%20querying%20the%20order"&gt;Querying the order&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3b%20canceling%20an%20order"&gt;Canceling the order&lt;/a&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 15 Jun 2011 17:22:45 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Documentation 20110615052245P</guid></item><item><title>Updated Wiki: RestBucks on .Net; canceling an order</title><link>http://restbucks.codeplex.com/wikipage?title=RestBucks on .Net; canceling an order&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;&lt;em&gt;Previous posts&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://joseoncode.com/2011/05/31/introducing-restbucks-on-net/"&gt;&lt;em&gt;Introducing RestBucks on .Net&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/01/restbucks-on-net-ordering-coffee/"&gt;&lt;em&gt;RestBucks on .Net; ordering coffee&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&lt;/em&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/03/restbucks-on-net-is-online/"&gt;&lt;em&gt;RestBucks on .Net is onilne!&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://joseoncode.com/2011/06/07/restbucks-on-net-querying-the-order/"&gt;RestBucks on .Net; querying the order&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;In this post we are going to implement a new use case; the customer cancels an order.&lt;/p&gt;
&lt;p&gt;Before we go to the REST operation itself it is important to introduce some modifications to our domain class:&lt;/p&gt;
&lt;pre&gt;public class Order : Entity
{
    //more properties and methods....
    
    public OrderStatus Status { get; private set; }
    public string CancelReason { get; private set; }

    public virtual void Cancel(string cancelReason)
    {
        if(Status != OrderStatus.Unpaid)
        {
            throw new InvalidOrderOperationException(string.Format(&amp;quot;The order can not be canceled because it is {0}.&amp;quot;,
                Status.ToString().ToLower()));
        }
        CancelReason = cancelReason;
        Status = OrderStatus.Canceled;
    }
}&lt;/pre&gt;
&lt;p&gt;The method is simple, before changing the state of the order to Canceled we have to validate if the order is at the Unpaid state (&lt;a href="http://joseoncode.com/wp-content/uploads/2011/06/2011-06-07_1150_thumb.png"&gt;remember this graph&lt;/a&gt;?), once the order
 has been Paid or it is at any other stage of the process; the Cancel method will throw an exception.&lt;/p&gt;
&lt;p&gt;I’ve tested the happy path as follows:&lt;/p&gt;
&lt;pre&gt;[TestFixture]
public class GivenAnUnpaidOrder
{
    private Order order;

    [SetUp]
    public void SetUp()
    {
        order = new Order();
    }

    [Test]
    public void CancelShouldWork()
    {
        order.Cancel(&amp;quot;error&amp;quot;);
        order.Status.Should().Be.EqualTo(OrderStatus.Canceled);
    }
}&lt;/pre&gt;
&lt;p&gt;And then the not so happy path:&lt;/p&gt;
&lt;pre&gt;[TestFixture]
public class GivenAPayedOrder
{
    private Order order;

    [SetUp]
    public void SetUp()
    {
        order = new Order();
        order.Pay(&amp;quot;123&amp;quot;, &amp;quot;jose&amp;quot;);
    }

    [Test]
    public void CancelShouldThrow()
    {
        order.Executing(o =&amp;gt; o.Cancel(&amp;quot;error&amp;quot;))
            .Throws&amp;lt;InvalidOrderOperationException&amp;gt;()
            .And
            .Exception.Message.Should().Be.EqualTo(&amp;quot;The order can not be canceled because it is paid.&amp;quot;);
    }
}&lt;/pre&gt;
&lt;p&gt;I choose to use different classes much like as per-context fixtures, because I needed to test several other things when the order is Unpaid/Paid.&lt;/p&gt;
&lt;p&gt;The next thing is to handle the REST stuff in the handler:&lt;/p&gt;
&lt;pre&gt;[WebInvoke(UriTemplate = &amp;quot;{orderId}&amp;quot;, Method = &amp;quot;DELETE&amp;quot;)]
public HttpResponseMessage Cancel(int orderId)
{
    var order = orderRepository.GetById(orderId);
    if(order == null) return Responses.NotFound();
    order.Cancel(&amp;quot;canceled from the rest interface&amp;quot;);
    return Responses.NoContent();
}&lt;/pre&gt;
&lt;p&gt;This handler validate first if the order exists, if it doesn't exists; return 404. If the order exists it calls the Cancel operation and returns 204. You can see the tests
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/a964b0a6a660#RestBucks.Tests%2fResources%2fWhenUserCancelAnOrder.cs"&gt;
here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Since we are doing something like &amp;quot;soft deletes&amp;quot; the next question is what will happen if somebody makes a GET requests to order/id with the id of a canceled order. In my mind the above Cancel method moves the resource from one collection to another
 one (the trash) so we can modify our GET to work as follows:&lt;/p&gt;
&lt;pre&gt;[Test]
public void ACallToGet_ShouldReturnMovedPermanentlyAndNewLocation()
{
    var order = new Order { Id = 123 };
    var handler = new OrderResourceHandler(new RepositoryStub&amp;lt;Order&amp;gt;(order), resourceLinker);
    handler.Cancel(123);

    
    var responseToGet = handler.Get(123, null);

    var expected = &amp;quot;http://restbuckson.net/trash/order/123&amp;quot;;
    responseToGet.Satisfy(r =&amp;gt; r.StatusCode == HttpStatusCode.MovedPermanently
                            &amp;amp;&amp;amp; r.Headers.Location.ToString() == expected);
}&lt;/pre&gt;
&lt;p&gt;The implementation:&lt;/p&gt;
&lt;pre&gt;[Test]
public void ACallToGet_ShouldReturnMovedPermanentlyAndNewLocation()
{
    var order = new Order { Id = 123 };
    var handler = new OrderResourceHandler(new RepositoryStub&amp;lt;Order&amp;gt;(order), resourceLinker);
    handler.Cancel(123);

    
    var responseToGet = handler.Get(123, null);

    var expected = &amp;quot;http://restbuckson.net/trash/order/123&amp;quot;;
    responseToGet.Satisfy(r =&amp;gt; r.StatusCode == HttpStatusCode.MovedPermanently
                            &amp;amp;&amp;amp; r.Headers.Location.ToString() == expected);
}&lt;/pre&gt;
&lt;p&gt;And finally we need a TrashHandler (nice name!):&lt;/p&gt;
&lt;pre&gt;[ServiceContract, WithUriPrefix(&amp;quot;trash&amp;quot;)]
public class TrashHandler
{
    private readonly IRepository&amp;lt;Order&amp;gt; orderRepository;

    public TrashHandler(IRepository&amp;lt;Order&amp;gt; orderRepository)
    {
        this.orderRepository = orderRepository;
    }
    
    [WebGet(UriTemplate = &amp;quot;/order/{orderId}&amp;quot;)]
    public HttpResponseMessage GetCanceled(int orderId)
    {
        var order = orderRepository.Retrieve(o =&amp;gt; o.Id == orderId &amp;amp;&amp;amp; o.Status == OrderStatus.Canceled)
                                   .FirstOrDefault();

        return order == null ? Responses.NotFound()
            : new HttpResponseMessage&amp;lt;OrderRepresentation&amp;gt;(OrderRepresentationMapper.Map(order));
    }
}&lt;/pre&gt;
&lt;p&gt;This is all for now! I will talk about the Pay operation in my next article, thanks for reading!&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 15 Jun 2011 12:33:38 GMT</pubDate><guid isPermaLink="false">Updated Wiki: RestBucks on .Net; canceling an order 20110615123338P</guid></item><item><title>Updated Wiki: RestBucks on .Net; paying the order</title><link>http://restbucks.codeplex.com/wikipage?title=RestBucks on .Net; paying the order&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;&lt;em&gt;Previous posts&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://joseoncode.com/2011/05/31/introducing-restbucks-on-net/"&gt;&lt;em&gt;Introducing RestBucks on .Net&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/01/restbucks-on-net-ordering-coffee/"&gt;&lt;em&gt;RestBucks on .Net; ordering coffee&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&lt;/em&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/03/restbucks-on-net-is-online/"&gt;&lt;em&gt;RestBucks on .Net is onilne!&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://joseoncode.com/2011/06/07/restbucks-on-net-querying-the-order/"&gt;RestBucks on .Net; querying the order&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://joseoncode.com/2011/06/09/restbucks-on-net-canceling-an-order/"&gt;RestBucks on .Net; canceling the order&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This time I am going to show how did I implement the use case for payment.&lt;/p&gt;
&lt;p&gt;As I shown in my previous post about &lt;a href="http://joseoncode.com/2011/06/09/restbucks-on-net-canceling-an-order/"&gt;
canceling the order&lt;/a&gt;, the first thing to modify is our domain class Order;&lt;/p&gt;
&lt;pre&gt;public class Order
{
    //more properties and methods


    public virtual Payment     Payment { get; private set; }
    public virtual OrderStatus Status  { get; private set; }

    public virtual void Pay(string cardNumber, string cardOwner)
    {
        if (Status != OrderStatus.Unpaid)
        {
            string message = string.Format(&amp;quot;The order can not be paid because it is {0}.&amp;quot;,
                                           Status.ToString().ToLower());
            throw new InvalidOrderOperationException(message);
        }
        Status = OrderStatus.Paid;
        Payment = new Payment
                  {
                      CardOwner = cardOwner, 
                      CreditCardNumber = cardNumber
                  };
    }
}&lt;/pre&gt;
&lt;p&gt;And the next step is to add another operation to our REST end point:&lt;/p&gt;
&lt;pre&gt;[WebInvoke(UriTemplate = &amp;quot;{orderId}/payment&amp;quot;, Method = &amp;quot;POST&amp;quot;)]
public HttpResponseMessage Pay(int orderId, PaymentRepresentation paymentArgs)
{
    var order = orderRepository.GetById(orderId);
    if (order == null) return Responses.NotFound();
    order.Pay(paymentArgs.CardNumber, paymentArgs.CardOwner);
    return Responses.Ok();
}&lt;/pre&gt;
&lt;p&gt;It is a POST to &lt;a href="http://restbuckson.net/order/{id}/payment"&gt;http://restbuckson.net/order/{id}/payment&lt;/a&gt;. As usual you can see the tests
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/c318a36da791#RestBucks.Tests%2fResources%2fWhenUserPayAnOrder.cs"&gt;
here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After you paid an order the order will remain in the “Paid” status for few minutes until the barista prepares it. In my next post I will show how I wrote my barista-bot ;).&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 15 Jun 2011 12:31:53 GMT</pubDate><guid isPermaLink="false">Updated Wiki: RestBucks on .Net; paying the order 20110615123153P</guid></item><item><title>Updated Wiki: Documentation</title><link>http://restbucks.codeplex.com/documentation?version=4</link><description>&lt;div class="wikidoc"&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Overview"&gt;Overview&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Preparing%20the%20environment"&gt;Preparing the environment&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Running%20the%20Application"&gt;Running the Application&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;Implementation:
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3a%20Ordering%20Coffee"&gt;Ordering Coffee&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3b%20querying%20the%20order"&gt;Querying the order&lt;/a&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Thu, 09 Jun 2011 17:44:42 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Documentation 20110609054442P</guid></item><item><title>Updated Wiki: RestBucks on .Net; querying the order</title><link>http://restbucks.codeplex.com/wikipage?title=RestBucks on .Net; querying the order&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;&lt;em&gt;Previous posts&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://joseoncode.com/2011/05/31/introducing-restbucks-on-net/"&gt;&lt;em&gt;Introducing RestBucks on .Net&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/01/restbucks-on-net-ordering-coffee/"&gt;&lt;em&gt;RestBucks on .Net; ordering coffee&lt;/em&gt;&lt;/a&gt;&lt;em&gt;
&lt;/em&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://joseoncode.com/2011/06/03/restbucks-on-net-is-online/"&gt;&lt;em&gt;RestBucks on .Net is onilne!&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;In my previous post; &lt;a href="http://joseoncode.com/2011/06/01/restbucks-on-net-ordering-coffee/"&gt;
ordering coffee&lt;/a&gt; we end up with a response from the server with the &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30"&gt;
Location&lt;/a&gt; of the new resource.&lt;/p&gt;
&lt;p&gt;Then, a REST client is supposed to do a GET of this URL in order to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get some additional information, like the total cost of the order which is calculated in the backend.
&lt;/li&gt;&lt;li&gt;Get the current state of the order. &lt;/li&gt;&lt;li&gt;Get the links for the next actions. &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This must sound familiar if you’ve ever shop online. Even if we don’t have a graphical interface; the workflow is just like any online purchase.&lt;/p&gt;
&lt;p&gt;In RESTBucks the order process follows this workflow:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=248467"&gt;&lt;img title="2011-06-07_1150" border="0" alt="2011-06-07_1150" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=248468" width="490" height="485" style="border-right-width:0px; margin:0px auto 18px; padding-left:0px; padding-right:0px; display:block; float:none; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then, if we do a GET of http://restbuckson.net/order/{id} we will expect something like this:&lt;/p&gt;
&lt;pre&gt;&amp;lt;order&amp;gt;
    &amp;lt;links&amp;gt;
        &amp;lt;link uri=&amp;quot;http://restbuckson.net/order/557056&amp;quot; 
            rel=&amp;quot;http://restbuckson.net/docs/order-get.htm&amp;quot; 
            mediaType=&amp;quot;application/vnd.restbucks&amp;#43;xml&amp;quot;/&amp;gt;

        &amp;lt;link uri=&amp;quot;http://restbuckson.net/order/557056&amp;quot; 
            rel=&amp;quot;http://restbuckson.net/docs/order-update.htm&amp;quot; 
            mediaType=&amp;quot;application/vnd.restbucks&amp;#43;xml&amp;quot;/&amp;gt;

        &amp;lt;link uri=&amp;quot;http://restbuckson.net/order/557056&amp;quot; 
            rel=&amp;quot;http://restbuckson.net/docs/order-cancel.htm&amp;quot; 
            mediaType=&amp;quot;application/vnd.restbucks&amp;#43;xml&amp;quot;/&amp;gt;

        &amp;lt;link uri=&amp;quot;http://restbuckson.net/order/557056/payment&amp;quot; 
            rel=&amp;quot;http://restbuckson.net/docs/order-pay.htm&amp;quot; 
            mediaType=&amp;quot;application/vnd.restbucks&amp;#43;xml&amp;quot;/&amp;gt;
    &amp;lt;/links&amp;gt;
    &amp;lt;location&amp;gt;inShop&amp;lt;/location&amp;gt;
    &amp;lt;cost&amp;gt;7.60000&amp;lt;/cost&amp;gt;
    &amp;lt;items&amp;gt;
        &amp;lt;item&amp;gt;
            &amp;lt;name&amp;gt;Latte&amp;lt;/name&amp;gt;
            &amp;lt;quantity&amp;gt;1&amp;lt;/quantity&amp;gt;
            &amp;lt;milk&amp;gt;skim&amp;lt;/milk&amp;gt;
            &amp;lt;size&amp;gt;large&amp;lt;/size&amp;gt;
        &amp;lt;/item&amp;gt;
    &amp;lt;/items&amp;gt;
    &amp;lt;status&amp;gt;unpaid&amp;lt;/status&amp;gt;
&amp;lt;/order&amp;gt;&lt;/pre&gt;
&lt;p&gt;This is documented &lt;a href="http://restbuckson.net/docs/order-get.htm"&gt;here&lt;/a&gt; ;) . The purpose of the &amp;lt;links /&amp;gt; section is that the consumer of this API should not know much about the implementation or the location of each step. You can think this
 as if you were buying something in amazon, you don’t know where is the payment step and it is unlikely that some user will put an URL in the browser like “http://amazon.com/pay”.&lt;/p&gt;
&lt;h1&gt;Implementing the GET method&lt;/h1&gt;
&lt;p&gt;The implementation of the get method is this:&lt;/p&gt;
&lt;pre&gt;[WebGet(UriTemplate = &amp;quot;{orderId}&amp;quot;)]
public HttpResponseMessage Get(int orderId)
{
    var order = orderRepository.GetById(orderId);

    if (order == null) return Responses.NotFound();
    
    var representation = OrderRepresentationMapper.Map(order);
    var response = new HttpResponseMessage&amp;lt;OrderRepresentation&amp;gt;(representation);
    
    return response;
}&lt;/pre&gt;
&lt;p&gt;As I said in my previous post, I am using a OrderRepresentation to -represent- the resource and serialize it.&lt;/p&gt;
&lt;p&gt;The OrderRepresentationMapper.Map method converts an Order to an OrderRepresentation as follows:&lt;/p&gt;
&lt;pre&gt;public static class OrderRepresentationMapper
{
    public static OrderRepresentation Map(Order order)
    {
        return new OrderRepresentation
                   {
                       Cost = order.Total,
                       Status = order.Status,
                       Location = order.Location,
                       Items = order.Items.Select(i =&amp;gt; new OrderItemRepresentation
                                    {
                                        Name = i.Product.Name,
                                        Preferences = i.Preferences.ToDictionary(p =&amp;gt; p.Key, p =&amp;gt; p.Value),
                                        Quantity = i.Quantity
                                    }).ToList(),
                       Links = GetLinks(order).ToList()   
                   };
        
    }

    public static IEnumerable&amp;lt;Link&amp;gt; GetLinks(Order order)
    {
        var baseUri = new UriSegment(ConfigurationManager.AppSettings[&amp;quot;baseUri&amp;quot;]);
        var linker = new ResourceLinker(baseUri.Segment);

        var get = new Link(linker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(r =&amp;gt; r.Get(0, null), new { orderId = order.Id }), 
                            baseUri &amp;#43; &amp;quot;docs/order-get.htm&amp;quot;, 
                            MediaTypes.Default);

        var update = new Link(linker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(r =&amp;gt; r.Update(0, null), new { orderId = order.Id }),
                            baseUri &amp;#43; &amp;quot;docs/order-update.htm&amp;quot;,
                            MediaTypes.Default);

        var cancel = new Link(linker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(r =&amp;gt; r.Cancel(0), new { orderId = order.Id }),
                            baseUri &amp;#43; &amp;quot;docs/order-cancel.htm&amp;quot;,
                            MediaTypes.Default);

        var pay = new Link(linker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(r =&amp;gt; r.Pay(0, null), new { orderId = order.Id }),
                            baseUri &amp;#43; &amp;quot;docs/order-pay.htm&amp;quot;,
                            MediaTypes.Default);

        var receipt = new Link(linker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(r =&amp;gt; r.Receipt(0), new { orderId = order.Id }),
                            baseUri &amp;#43; &amp;quot;docs/receipt-coffee.htm&amp;quot;,
                            MediaTypes.Default);

        switch (order.Status)
        {
            case OrderStatus.Unpaid:
                yield return get;
                yield return update;
                yield return cancel;
                yield return pay;
                break;
            case OrderStatus.Paid:
            case OrderStatus.Delivered:
                yield return get;
                break;
            case OrderStatus.Ready:
                yield return receipt;
                break;
            case OrderStatus.Canceled:
                yield break;
            default:
                yield break;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;The GetLinks method builds an enumeration of the possible next steps given the status of an order. The ResourceLinker is just a fancy way to get a link to another action/resource (we didn't implement those actions yet).&lt;/p&gt;
&lt;p&gt;If you want to explore how this work (or how to test it) you can have a look to
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/a964b0a6a660#RestBucks.Tests%2fDomain%2fGivenAnUnpaidOrder.cs"&gt;
these&lt;/a&gt; &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/a964b0a6a660#RestBucks.Tests%2fResources%2fWhenUserGETsAnOrder.cs"&gt;
tests&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the next article I am going to show how I did the Cancel operation, stay tuned!&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Thu, 09 Jun 2011 17:42:44 GMT</pubDate><guid isPermaLink="false">Updated Wiki: RestBucks on .Net; querying the order 20110609054244P</guid></item><item><title>Updated Wiki: Home</title><link>http://restbucks.codeplex.com/wikipage?version=6</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;This a REST application inspired in the excellent example of the book &lt;a href="http://restinpractice.com"&gt;
Rest in Practice&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I started this application as an exploratory example of REST and the state of the art of the web frameworks in .Net.&lt;/p&gt;
&lt;p&gt;This is the result of a collaborative work. Even if I wrote almost all the code; I did it with the guidance of the amazing group of experts working at
&lt;a href="http://www.tellago.com"&gt;Tellago&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The example has served me to learn the REST theory as well as the new &lt;a href="http://wcf.codeplex.com"&gt;
Wcf Web API&lt;/a&gt;. It has also served to the team to discuss various different approaches and to explore different technologies.&lt;/p&gt;
&lt;p&gt;The purpose of the example is not to show &amp;ldquo;the&amp;rdquo; way of doing things but to have something where we can clone, change, discuss and so. Consider this only as one way of doing REST on the .Net platform.&lt;/p&gt;
&lt;p&gt;My plan is to keep growing this example as well as write an entire series of posts about how I implemented each use case.&lt;/p&gt;
&lt;p&gt;Try online!&lt;/p&gt;
&lt;p&gt;Do you want to try the application online without installing anything? &lt;a href="http://restbuckson.net"&gt;
http://restbuckson.net&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.amazon.com/gp/reader/0596805829/ref=sib_dp_pt#reader-link"&gt;&lt;img src="http://ecx.images-amazon.com/images/I/51xSVWRSEFL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg" alt="" width="300" height="300"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Mon, 06 Jun 2011 20:20:10 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20110606082010P</guid></item><item><title>Updated Wiki: Home</title><link>http://restbucks.codeplex.com/wikipage?version=5</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;This a REST application inspired in the excellent example of the book &lt;a href="http://restinpractice.com"&gt;
Rest in Practice&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I started this application as an exploratory example of REST and the state of the art of the web frameworks in .Net.&lt;/p&gt;
&lt;p&gt;This is the result of a collaborative work. Even if I wrote almost all the code; I did it with the guidance of the amazing group of experts working at
&lt;a href="http://www.tellago.com"&gt;Tellago&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The example has served me to learn the REST theory as well as the new &lt;a href="http://wcf.codeplex.com"&gt;
Wcf Web API&lt;/a&gt;. It has also served to the team to discuss various different approaches and to explore different technologies.&lt;/p&gt;
&lt;p&gt;The purpose of the example is not to show &amp;ldquo;the&amp;rdquo; way of doing things but to have something where we can clone, change, discuss and so. Consider this only as one way of doing REST on the .Net platform.&lt;/p&gt;
&lt;p&gt;My plan is to keep growing this example as well as write an entire series of posts about how I implemented each use case.&lt;/p&gt;
&lt;p&gt;Try online!&lt;/p&gt;
&lt;p&gt;Do you want to try the application online without installing anything? &lt;a href="http://restbuckson.net"&gt;
http://restbuckson.net&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Sat, 04 Jun 2011 14:28:03 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20110604022803P</guid></item><item><title>Updated Wiki: Home</title><link>http://restbucks.codeplex.com/wikipage?version=4</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;This a REST application inspired in the excellent example of the book &lt;a href="http://restinpractice.com"&gt;
Rest in Practice&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I started this application as an exploratory example of REST and the state of the art of the web frameworks in .Net.&lt;/p&gt;
&lt;p&gt;This is the result of a collaborative work. Even if I wrote almost all the code; I did it with the guidance of the amazing group of experts working at
&lt;a href="http://www.tellago.com"&gt;Tellago&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The example has served me to learn the REST theory as well as the new &lt;a href="http://wcf.codeplex.com"&gt;
Wcf Web API&lt;/a&gt;. It has also served to the team to discuss various different approaches and to explore different technologies.&lt;/p&gt;
&lt;p&gt;The purpose of the example is not to show &amp;ldquo;the&amp;rdquo; way of doing things but to have something where we can clone, change, discuss and so. Consider this only as one way of doing REST on the .Net platform.&lt;/p&gt;
&lt;p&gt;My plan is to keep growing this example as well as write an entire series of posts about how I implemented each use case.&lt;/p&gt;
&lt;p&gt;Do you want to try the application online without installing anything? &lt;a href="http://restbuckson.net"&gt;
http://restbuckson.net&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Sat, 04 Jun 2011 14:27:01 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20110604022701P</guid></item><item><title>Updated Wiki: Overview</title><link>http://restbucks.codeplex.com/wikipage?title=Overview&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;In this post I will give a brief overview of RestBucks on Net.&lt;/p&gt;
&lt;p&gt;Briefly RestBucks is a coffee shop as &lt;a href="http://www.starbucks.com"&gt;any other&lt;/a&gt;, well not as
&lt;strong&gt;any&lt;/strong&gt; other. They offer a tasty coffee and what is most interesting for us; a RESTfull interface where the customers can order the tasty black liquid. With this REST interface customers can make an order, customize, cancel, ask if it is ready,
 and so on. If you want to read a more (but not the whole book) you can read &lt;a href="http://www.infoq.com/articles/webber-rest-workflow"&gt;
this article in infoq&lt;/a&gt; (I strongly recommend you the book though).&lt;/p&gt;
&lt;h1&gt;Use cases&lt;/h1&gt;
&lt;p&gt;These are the use cases implemented so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The customer makes an order &lt;/li&gt;&lt;li&gt;The customer changes an order &lt;/li&gt;&lt;li&gt;The customer pays the order &lt;/li&gt;&lt;li&gt;The customer queries his order &lt;/li&gt;&lt;li&gt;The customer cancels his order &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;As well, as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The barista prepare an order &lt;/li&gt;&lt;/ul&gt;
&lt;h1&gt;Implementation details&lt;/h1&gt;
&lt;p&gt;Since I started to participate in the opensource world with examples, I always tried to avoid examples that doesn’t work in real life or to answer to a poignant question with “it is just an example”. My goal with this application is that you can take it
 and use it for a real world scenario. Thus I am using real world technology, patterns and methodologies:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A relational database to persist the data. &lt;/li&gt;&lt;li&gt;An &lt;a href="en.wikipedia.org/wiki/Inversion_of_control"&gt;Inversion of control&lt;/a&gt; container
&lt;/li&gt;&lt;li&gt;An &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping"&gt;Object to relational mapper&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;Test Driven Development&lt;/a&gt;.
&lt;/li&gt;&lt;li&gt;As well as other well known patterns that we will discuss in upcoming articles.
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The tools I have used for this example were not randomly chosen. They are ready to be used in real world applications.
&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 01 Jun 2011 17:50:34 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Overview 20110601055034P</guid></item><item><title>Updated Wiki: RestBucks on .Net: Ordering Coffee</title><link>http://restbucks.codeplex.com/wikipage?title=RestBucks on .Net: Ordering Coffee&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;Welcome to the second part of the RestBucks on .Net series. Today, I am going to take the first use case of the example: “Ordering Coffee”.&lt;/p&gt;
&lt;h1&gt;What we know?&lt;/h1&gt;
&lt;p&gt;We know (is on the book) that the customer will POST (http verb) an xml payload as follows:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246122"&gt;&lt;img title="2011-06-01_0900" border="0" alt="2011-06-01_0900" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246123" width="481" height="312" style="border-right-width:0px; margin:0px auto 18px; padding-left:0px; padding-right:0px; display:block; float:none; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is what we call in REST a REPRESENTATION. It is not the resource itself but just a representation of it.&lt;/p&gt;
&lt;p&gt;I have intentionally highlighted three different areas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(green) Location: this indicate whether the order will be consumed onsite or not. It only accept two different values; “takeAway” or “inShop”.
&lt;/li&gt;&lt;li&gt;(yellow) Product name: this is the name of the product. &lt;/li&gt;&lt;li&gt;(red) Product preferences: each product accept different preferences. &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I took this one step further from the book and we are going to suppose that at any moment of time, the coffee shop can change their menu and/or the possible preferences for a product.
&lt;/p&gt;
&lt;p&gt;Also notice that an order might have many items.&lt;/p&gt;
&lt;p&gt;The order will be POSTed to an URI like “http://restbucks.com/order&lt;u&gt;s&lt;/u&gt;”. &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In plural; in rest you create things on collections (plural). To GET/PUT/DELETE an order you generally do something like “http://restbucks.com/order/id” (singular)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;The domain&lt;/h1&gt;
&lt;p&gt;From the little piece of information on the above section, we can trace some core concepts:&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;&lt;strong&gt;Entity&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top" width="305"&gt;&lt;strong&gt;Attributes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;Product&lt;/td&gt;
&lt;td valign="top" width="305"&gt;Name, Possible preferences&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;Order&lt;/td&gt;
&lt;td valign="top" width="305"&gt;Location, Lines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;Order line&lt;/td&gt;
&lt;td valign="top" width="305"&gt;Product, Preferences, Quantity&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And then we can build an object-oriented :) domain model:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246124"&gt;&lt;img title="ClassDiagram1" border="0" alt="ClassDiagram1" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246125" width="640" height="463" style="border-right-width:0px; margin:0px auto 18px; padding-left:0px; padding-right:0px; display:block; float:none; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a pretty &lt;a href="http://martinfowler.com/bliki/AnemicDomainModel.html"&gt;
anemic&lt;/a&gt; domain model and it doesn’t have all the properties and methods that the current version on codeplex has. But it serve to us for the purpose of our specs so far.
&lt;/p&gt;
&lt;h1&gt;Persistence&lt;/h1&gt;
&lt;p&gt;For this example I have chosen to use a relational database thru an &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping"&gt;
ORM&lt;/a&gt;. I have used the last version of the more mature ORM in .Net; &lt;a href="http://nhforge.org"&gt;
NHibernate&lt;/a&gt;. For mapping the domain I have used only conventions with few particular exceptions.
&lt;/p&gt;
&lt;p&gt;The first thing I did is to write some persistence tests; like this one:&lt;/p&gt;
&lt;pre&gt;[Test]
public void CanStoreACustomization()
{
    long customizationId;
    using (var session = sessionFactory.OpenSession())
    using(var tx = session.BeginTransaction())
    {
        var repository = new Repository&amp;lt;Customization&amp;gt;(session);
        var customization = new Customization { Name = &amp;quot;Milk&amp;quot;, PossibleValues = { &amp;quot;skim&amp;quot;, &amp;quot;semi&amp;quot;, &amp;quot;whole&amp;quot; } };
        repository.MakePersistent(customization);
        customizationId = customization.Id;
        tx.Commit();
    }
    using (var session = sessionFactory.OpenSession())
    {
        var repository = new Repository&amp;lt;Customization&amp;gt;(session);
        var customization = repository.GetById(customizationId);
        customization.Satisfy(c =&amp;gt; c.Name == &amp;quot;Milk&amp;quot; &amp;amp;&amp;amp; c.PossibleValues.SetEquals(new[] { &amp;quot;skim&amp;quot;, &amp;quot;semi&amp;quot;, &amp;quot;whole&amp;quot; }));
    }
}&lt;/pre&gt;
&lt;p&gt;This kind of tests check that the properties are properly persisted, and for instance that inserting a customization automatically inserts the PossibleValues collection.&lt;/p&gt;
&lt;p&gt;Each test destroy and recreate the whole database schema (we use a separated database for testing purposes as explained
&lt;a href="http://restbucks.codeplex.com/wikipage?title=Preparing%20the%20environment"&gt;
here&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;It is not my plan to post all the tests I wrote, you can have a look to these two
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks.Tests%2fDataTests%2fDataTests.cs"&gt;
class&lt;/a&gt; &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks.Tests%2fDataTests%2fOrderTests.cs"&gt;
here&lt;/a&gt;. The result of these tests is the Customize method in this &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks%2fData%2fMapper.cs"&gt;
tiny class&lt;/a&gt;. The other method (Generate) define only few general purpose conventions.&lt;/p&gt;
&lt;h1&gt;The representation&lt;/h1&gt;
&lt;p&gt;Now, that we have our Order class you might wonder how and where to de-serialize the chunk of xml we see at the beginning.
&lt;/p&gt;
&lt;p&gt;This broadly discussed but at some point there is some level of agreement; &lt;a href="http://martinfowler.com/bliki/FirstLaw.html"&gt;
Entities&lt;/a&gt; &lt;a href="http://ayende.com/blog/4011/the-stripper-pattern"&gt;are&lt;/a&gt; &lt;a href="http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/"&gt;
not&lt;/a&gt; &lt;a href="http://www.southsearepublic.org/article/2157/read/data_transfer_objects_value_objects_and_domain_models_in_j2ee"&gt;
DTO&lt;/a&gt; (and representations are more or less like DTOs). &lt;/p&gt;
&lt;p&gt;We have different ways to de-serialize the representation. For this example I chose to write a typed class and use the standard
&lt;a href="http://msdn.microsoft.com/en-us/library/ms950721.aspx"&gt;.net xml serialization&lt;/a&gt; (another way might be to de-serialize into a
&lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx"&gt;
dynamic ExpandoObject&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246126"&gt;&lt;img title="2011-06-01_1145" border="0" alt="2011-06-01_1145" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246127" width="362" height="321" style="border-right-width:0px; margin:0px auto 18px; padding-left:0px; padding-right:0px; display:block; float:none; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you can see, this is quite different to our domain entity and in the long run the difference will be bigger because Representations doesn’t have methods.&lt;/p&gt;
&lt;p&gt;After I wrote those classes with their properties; the tests that we have to fix is this one:&lt;/p&gt;
&lt;pre&gt;[Test]
public void CanDeserialize()
{
    var xml =
        @&amp;quot;&amp;lt;?xml version=&amp;quot;&amp;quot;1.0&amp;quot;&amp;quot;?&amp;gt;
&amp;lt;order xmlns:xsd=&amp;quot;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&amp;quot; xmlns:xsi=&amp;quot;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&amp;quot; xmlns=&amp;quot;&amp;quot;http://restbucks.com&amp;quot;&amp;quot;&amp;gt;
  &amp;lt;location&amp;gt;inShop&amp;lt;/location&amp;gt;
  &amp;lt;cost&amp;gt;100.4&amp;lt;/cost&amp;gt;
  &amp;lt;items&amp;gt;
    &amp;lt;item&amp;gt;
      &amp;lt;name&amp;gt;latte&amp;lt;/name&amp;gt;
      &amp;lt;quantity&amp;gt;0&amp;lt;/quantity&amp;gt;
      &amp;lt;size&amp;gt;large&amp;lt;/size&amp;gt;
      &amp;lt;milk&amp;gt;skim&amp;lt;/milk&amp;gt;
    &amp;lt;/item&amp;gt;
  &amp;lt;/items&amp;gt;
&amp;lt;/order&amp;gt;&amp;quot;;
    var representation = XmlUtil.FromXmlString&amp;lt;OrderRepresentation&amp;gt;(xml);
    representation.Satisfy(r =&amp;gt;
                           r.Items.Any(i =&amp;gt; i.Name == &amp;quot;latte&amp;quot;
                                            &amp;amp;&amp;amp; i.Preferences.Any(p =&amp;gt; p.Key == &amp;quot;size&amp;quot;)
                                            &amp;amp;&amp;amp; i.Preferences.Any(p =&amp;gt; p.Key == &amp;quot;milk&amp;quot;)));
}&lt;/pre&gt;
&lt;p&gt;To pass this test we need a bunch of attributes and implementing the &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx"&gt;
IXmlSerializable&lt;/a&gt; interface for the preferences dictionary. &lt;br&gt;
I am not going to paste here the full implementation of those two classes but you can explore
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks%2fResources%2fOrders%2fRepresentations%2fOrderItemRepresentation.cs"&gt;
here&lt;/a&gt; and &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks%2fResources%2fOrders%2fRepresentations%2fOrderRepresentation.cs"&gt;
here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;The resource handler&lt;/h1&gt;
&lt;p&gt;This is the way I like to call the class that has the [ServiceContract] attribute in the current bits of the
&lt;a href="http://wcf.codeplex.com"&gt;wcf WebApi&lt;/a&gt;. It is not the resource it self, for me the resource is the Order class (the domain object) and I don’t like the name of “service” in REST.&lt;/p&gt;
&lt;p&gt;The main purpose of the resource handler is to expose an Http interface to our application. In order to do this the resource handler can take dependencies on different services that our internal API expose; e.g. a repository.
&lt;/p&gt;
&lt;p&gt;To make things clear, there is specific logic that belongs to our domain model; e.g. customer can’t pay an order that he has previously canceled. On the other hand there is specific logic that belongs to the handler; e.g. returns a 404-Not found response
 when the customer does a request for an order that doesn’t exist in our repository.&lt;/p&gt;
&lt;p&gt;This test validate that if the customer does a POST of a product that doesn't exist, then the resource handler must return 400 and a meaningful ReasonPhrase:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenAProductDoesNotExist_ThenReturn400AndTheProperREasonPhrase()
{
    var resourceHandler = CreateResourceHandler();
    var orderRepresentation = new OrderRepresentation
    {
        Items = { new OrderItemRepresentation { Name = &amp;quot;beer&amp;quot; } }
    };

    var result = resourceHandler.Create(orderRepresentation);

    result.Satisfy(rm =&amp;gt; rm.StatusCode == HttpStatusCode.BadRequest
                      &amp;amp;&amp;amp; rm.ReasonPhrase == &amp;quot;We don't offer beer&amp;quot;);
}&lt;/pre&gt;
&lt;p&gt;This test does another validation:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenItemHasQuantity0_ThenReturn400AndTheProperREasonPhrase()
{
    var resourceHandler = CreateResourceHandler();
    var orderRepresentation = new OrderRepresentation
    {
        Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 0 } }
    };

    var response = resourceHandler.Create(orderRepresentation);

    //NOTE: I am not sure if the proper response is NotFound or BadRequest. It is not clear in the book.
    response.Satisfy(rm =&amp;gt; rm.StatusCode == HttpStatusCode.BadRequest
                        &amp;amp;&amp;amp; rm.Content.ToStringContent() == &amp;quot;Item 0: Quantity should be greater than 0.&amp;quot;);
}&lt;/pre&gt;
&lt;p&gt;and so forth. When the order is ok:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenOrderIsOk_ThenInsertANewOrderWithTheProductsAndPrice()
{
    var orderRepository = new RepositoryStub&amp;lt;Order&amp;gt;();
    var handler = CreateResourceHandler(orderRepository);
    var orderRepresentation = new OrderRepresentation { Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 } } };

    //act
    handler.Create(orderRepresentation);

    var order = orderRepository.RetrieveAll().First();
    order.Satisfy(o =&amp;gt; o.Items.Any(i =&amp;gt; i.Product == latte &amp;amp;&amp;amp; i.UnitPrice == 2.5m &amp;amp;&amp;amp; i.Quantity == 1));
}

[Test]
public void WhenOrderIsOk_ThenInsertANewOrderWithTheDateTime()
{
    var orderRepository = new RepositoryStub&amp;lt;Order&amp;gt;();
    var handler = CreateResourceHandler(orderRepository);
    var orderRepresentation = new OrderRepresentation { Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 } } };

    //act
    handler.Create(orderRepresentation);

    var order = orderRepository.RetrieveAll().First();
    order.Date.Should().Be.EqualTo(DateTime.Today);
}

[Test]
public void WhenOrderIsOk_ThenInsertANewOrderWithTheLocationInfo()
{
    var orderRepository = new RepositoryStub&amp;lt;Order&amp;gt;();
    var handler = CreateResourceHandler(orderRepository);
    var orderRepresentation = new OrderRepresentation 
                                    { 
                                        Location = Location.InShop, 
                                        Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 } } 
                                    };

    //act
    handler.Create(orderRepresentation);

    var order = orderRepository.RetrieveAll().First();
    order.Location.Should().Be.EqualTo(Location.InShop);
}&lt;/pre&gt;
&lt;p&gt;The above tests specify that an order must be saved properly. As you can notice here our resource handler depends on a IRepository&amp;lt;T&amp;gt; and in those tests we are using
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks.Tests%2fUtil%2fRepositoryStub.cs"&gt;
RepositoryStub&amp;lt;T&amp;gt;&lt;/a&gt; which is an implementation of IRepository&amp;lt;T&amp;gt; with a simple in-memory collection.&lt;/p&gt;
&lt;p&gt;The last test for this use case is very interesting:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenOrderIsOk_ThenResponseHasStatus201AndLocation()
{
    var orderRepository = new Mock&amp;lt;IRepository&amp;lt;Order&amp;gt;&amp;gt;();
    orderRepository.Setup(or =&amp;gt; or.MakePersistent(It.IsAny&amp;lt;Order[]&amp;gt;()))
                   .Callback&amp;lt;Order[]&amp;gt;(o =&amp;gt; o.First().Id = 123);

    var handler = CreateResourceHandler(orderRepository.Object);

    //act
    var result = handler.Create(new OrderRepresentation { Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 }}});

    var expectedUriToTheNewOrder =
        resourceLinker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(or =&amp;gt; or.Get(0, null), new { orderId = &amp;quot;123&amp;quot; });

    result.Satisfy(r =&amp;gt; r.StatusCode == HttpStatusCode.Created
                                  &amp;amp;&amp;amp; r.Headers.Location.ToString() == expectedUriToTheNewOrder);
}&lt;/pre&gt;
&lt;p&gt;It means that after creating the order, the response status must be Created (201), and there must be a Location header.&lt;/p&gt;
&lt;p&gt;The protocol is very clear on this (&lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30"&gt;section 14.40&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource.
&lt;u&gt;&lt;font style="background-color:#ffff00"&gt;For 201 (Created) responses, the Location is that of the new resource which was created by the request&lt;/font&gt;&lt;/u&gt;. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection
 to the resource. The field value consists of a single absolute URI.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A client application can pull the representation from the header location and get more details of the resource it just created, but that is for my next post :).&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 01 Jun 2011 16:20:31 GMT</pubDate><guid isPermaLink="false">Updated Wiki: RestBucks on .Net: Ordering Coffee 20110601042031P</guid></item><item><title>Updated Wiki: Documentation</title><link>http://restbucks.codeplex.com/documentation?version=3</link><description>&lt;div class="wikidoc"&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Overview"&gt;Overview&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Preparing%20the%20environment"&gt;Preparing the environment&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=Running%20the%20Application"&gt;Running the Application&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;Implementation:
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://restbucks.codeplex.com/wikipage?title=RestBucks%20on%20.Net%3a%20Ordering%20Coffee"&gt;Ordering Coffee&lt;/a&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/li&gt;&lt;/ol&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 01 Jun 2011 16:19:57 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Documentation 20110601041957P</guid></item><item><title>Updated Wiki: RestBucks on .Net: Ordering Coffee</title><link>http://restbucks.codeplex.com/wikipage?title=RestBucks on .Net: Ordering Coffee&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;Welcome to the second part of the RestBucks on .Net series. Today, I am going to take the first use case of the example: “Ordering Coffee”.&lt;/p&gt;
&lt;h1&gt;What we know?&lt;/h1&gt;
&lt;p&gt;We know (is on the book) that the customer will POST (http verb) an xml payload as follows:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246122"&gt;&lt;img title="2011-06-01_0900" border="0" alt="2011-06-01_0900" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246123" width="481" height="312" style="border-right-width:0px; margin:0px auto 18px; padding-left:0px; padding-right:0px; display:block; float:none; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is what we call in REST a REPRESENTATION. It is not the resource itself but just a representation of it.&lt;/p&gt;
&lt;p&gt;I have intentionally highlighted three different areas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(green) Location: this indicate whether the order will be consumed onsite or not. It only accept two different values; “takeAway” or “inShop”.
&lt;/li&gt;&lt;li&gt;(yellow) Product name: this is the name of the product. &lt;/li&gt;&lt;li&gt;(red) Product preferences: each product accept different preferences. &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I took this one step further from the book and we are going to suppose that at any moment of time, the coffee shop can change their menu and/or the possible preferences for a product.
&lt;/p&gt;
&lt;p&gt;Also notice that an order might have many items.&lt;/p&gt;
&lt;p&gt;The order will be POSTed to an URI like “http://restbucks.com/order&lt;u&gt;s&lt;/u&gt;”. &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In plural; in rest you create things on collections (plural). To GET/PUT/DELETE an order you generally do something like “http://restbucks.com/order/id” (singular)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;The domain&lt;/h1&gt;
&lt;p&gt;From the little piece of information on the above section, we can trace some core concepts:&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;&lt;strong&gt;Entity&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top" width="305"&gt;&lt;strong&gt;Attributes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;Product&lt;/td&gt;
&lt;td valign="top" width="305"&gt;Name, Possible preferences&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;Order&lt;/td&gt;
&lt;td valign="top" width="305"&gt;Location, Lines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="95"&gt;Order line&lt;/td&gt;
&lt;td valign="top" width="305"&gt;Product, Preferences, Quantity&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And then we can build an object-oriented :) domain model:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246124"&gt;&lt;img title="ClassDiagram1" border="0" alt="ClassDiagram1" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246125" width="640" height="463" style="border-right-width:0px; margin:0px 0px 18px; padding-left:0px; padding-right:0px; display:inline; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a pretty &lt;a href="http://martinfowler.com/bliki/AnemicDomainModel.html"&gt;
anemic&lt;/a&gt; domain model and it doesn’t have all the properties and methods that the current version on codeplex has. But it serve to us for the purpose of our specs so far.
&lt;/p&gt;
&lt;h1&gt;Persistence&lt;/h1&gt;
&lt;p&gt;For this example I have chosen to use a relational database thru an &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping"&gt;
ORM&lt;/a&gt;. I have used the last version of the more mature ORM in .Net; &lt;a href="http://nhforge.org"&gt;
NHibernate&lt;/a&gt;. For mapping the domain I have used only conventions with few particular exceptions.
&lt;/p&gt;
&lt;p&gt;The first thing I did is to write some persistence tests; like this one:&lt;/p&gt;
&lt;pre&gt;[Test]
public void CanStoreACustomization()
{
    long customizationId;
    using (var session = sessionFactory.OpenSession())
    using(var tx = session.BeginTransaction())
    {
        var repository = new Repository&amp;lt;Customization&amp;gt;(session);
        var customization = new Customization { Name = &amp;quot;Milk&amp;quot;, PossibleValues = { &amp;quot;skim&amp;quot;, &amp;quot;semi&amp;quot;, &amp;quot;whole&amp;quot; } };
        repository.MakePersistent(customization);
        customizationId = customization.Id;
        tx.Commit();
    }
    using (var session = sessionFactory.OpenSession())
    {
        var repository = new Repository&amp;lt;Customization&amp;gt;(session);
        var customization = repository.GetById(customizationId);
        customization.Satisfy(c =&amp;gt; c.Name == &amp;quot;Milk&amp;quot; &amp;amp;&amp;amp; c.PossibleValues.SetEquals(new[] { &amp;quot;skim&amp;quot;, &amp;quot;semi&amp;quot;, &amp;quot;whole&amp;quot; }));
    }
}&lt;/pre&gt;
&lt;p&gt;This kind of tests check that the properties are properly persisted, and for instance that inserting a customization automatically inserts the PossibleValues collection.&lt;/p&gt;
&lt;p&gt;Each test destroy and recreate the whole database schema (we use a separated database for testing purposes as explained
&lt;a href="http://restbucks.codeplex.com/wikipage?title=Preparing%20the%20environment"&gt;
here&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;It is not my plan to post all the tests I wrote, you can have a look to these two
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks.Tests%2fDataTests%2fDataTests.cs"&gt;
class&lt;/a&gt; &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks.Tests%2fDataTests%2fOrderTests.cs"&gt;
here&lt;/a&gt;. The result of these tests is the Customize method in this &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks%2fData%2fMapper.cs"&gt;
tiny class&lt;/a&gt;. The other method (Generate) define only few general purpose conventions.&lt;/p&gt;
&lt;h1&gt;The representation&lt;/h1&gt;
&lt;p&gt;Now, that we have our Order class you might wonder how and where to de-serialize the chunk of xml we see at the beginning.
&lt;/p&gt;
&lt;p&gt;This broadly discussed but at some point there is some level of agreement; &lt;a href="http://martinfowler.com/bliki/FirstLaw.html"&gt;
Entities&lt;/a&gt; &lt;a href="http://ayende.com/blog/4011/the-stripper-pattern"&gt;are&lt;/a&gt; &lt;a href="http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/"&gt;
not&lt;/a&gt; &lt;a href="http://www.southsearepublic.org/article/2157/read/data_transfer_objects_value_objects_and_domain_models_in_j2ee"&gt;
DTO&lt;/a&gt; (and representations are more or less like DTOs). &lt;/p&gt;
&lt;p&gt;We have different ways to de-serialize the representation. For this example I chose to write a typed class and use the standard
&lt;a href="http://msdn.microsoft.com/en-us/library/ms950721.aspx"&gt;.net xml serialization&lt;/a&gt; (another way might be to de-serialize into a
&lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx"&gt;
dynamic ExpandoObject&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246126"&gt;&lt;img title="2011-06-01_1145" border="0" alt="2011-06-01_1145" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=restbucks&amp;DownloadId=246127" width="362" height="321" style="border-right-width:0px; margin:0px auto 18px; padding-left:0px; padding-right:0px; display:block; float:none; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you can see, this is quite different to our domain entity and in the long run the difference will be bigger because Representations doesn’t have methods.&lt;/p&gt;
&lt;p&gt;After I wrote those classes with their properties; the tests that we have to fix is this one:&lt;/p&gt;
&lt;pre&gt;[Test]
public void CanDeserialize()
{
    var xml =
        @&amp;quot;&amp;lt;?xml version=&amp;quot;&amp;quot;1.0&amp;quot;&amp;quot;?&amp;gt;
&amp;lt;order xmlns:xsd=&amp;quot;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&amp;quot; xmlns:xsi=&amp;quot;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&amp;quot; xmlns=&amp;quot;&amp;quot;http://restbucks.com&amp;quot;&amp;quot;&amp;gt;
  &amp;lt;location&amp;gt;inShop&amp;lt;/location&amp;gt;
  &amp;lt;cost&amp;gt;100.4&amp;lt;/cost&amp;gt;
  &amp;lt;items&amp;gt;
    &amp;lt;item&amp;gt;
      &amp;lt;name&amp;gt;latte&amp;lt;/name&amp;gt;
      &amp;lt;quantity&amp;gt;0&amp;lt;/quantity&amp;gt;
      &amp;lt;size&amp;gt;large&amp;lt;/size&amp;gt;
      &amp;lt;milk&amp;gt;skim&amp;lt;/milk&amp;gt;
    &amp;lt;/item&amp;gt;
  &amp;lt;/items&amp;gt;
&amp;lt;/order&amp;gt;&amp;quot;;
    var representation = XmlUtil.FromXmlString&amp;lt;OrderRepresentation&amp;gt;(xml);
    representation.Satisfy(r =&amp;gt;
                           r.Items.Any(i =&amp;gt; i.Name == &amp;quot;latte&amp;quot;
                                            &amp;amp;&amp;amp; i.Preferences.Any(p =&amp;gt; p.Key == &amp;quot;size&amp;quot;)
                                            &amp;amp;&amp;amp; i.Preferences.Any(p =&amp;gt; p.Key == &amp;quot;milk&amp;quot;)));
}&lt;/pre&gt;
&lt;p&gt;To pass this test we need a bunch of attributes and implementing the &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx"&gt;
IXmlSerializable&lt;/a&gt; interface for the preferences dictionary. &lt;br&gt;
I am not going to paste here the full implementation of those two classes but you can explore
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks%2fResources%2fOrders%2fRepresentations%2fOrderItemRepresentation.cs"&gt;
here&lt;/a&gt; and &lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks%2fResources%2fOrders%2fRepresentations%2fOrderRepresentation.cs"&gt;
here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;The resource handler&lt;/h1&gt;
&lt;p&gt;This is the way I like to call the class that has the [ServiceContract] attribute in the current bits of the
&lt;a href="http://wcf.codeplex.com"&gt;wcf WebApi&lt;/a&gt;. It is not the resource it self, for me the resource is the Order class (the domain object) and I don’t like the name of “service” in REST.&lt;/p&gt;
&lt;p&gt;The main purpose of the resource handler is to expose an Http interface to our application. In order to do this the resource handler can take dependencies on different services that our internal API expose; e.g. a repository.
&lt;/p&gt;
&lt;p&gt;To make things clear, there is specific logic that belongs to our domain model; e.g. customer can’t pay an order that he has previously canceled. On the other hand there is specific logic that belongs to the handler; e.g. returns a 404-Not found response
 when the customer does a request for an order that doesn’t exist in our repository.&lt;/p&gt;
&lt;p&gt;This test validate that if the customer does a POST of a product that doesn't exist, then the resource handler must return 400 and a meaningful ReasonPhrase:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenAProductDoesNotExist_ThenReturn400AndTheProperREasonPhrase()
{
    var resourceHandler = CreateResourceHandler();
    var orderRepresentation = new OrderRepresentation
    {
        Items = { new OrderItemRepresentation { Name = &amp;quot;beer&amp;quot; } }
    };

    var result = resourceHandler.Create(orderRepresentation);

    result.Satisfy(rm =&amp;gt; rm.StatusCode == HttpStatusCode.BadRequest
                      &amp;amp;&amp;amp; rm.ReasonPhrase == &amp;quot;We don't offer beer&amp;quot;);
}&lt;/pre&gt;
&lt;p&gt;This test does another validation:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenItemHasQuantity0_ThenReturn400AndTheProperREasonPhrase()
{
    var resourceHandler = CreateResourceHandler();
    var orderRepresentation = new OrderRepresentation
    {
        Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 0 } }
    };

    var response = resourceHandler.Create(orderRepresentation);

    //NOTE: I am not sure if the proper response is NotFound or BadRequest. It is not clear in the book.
    response.Satisfy(rm =&amp;gt; rm.StatusCode == HttpStatusCode.BadRequest
                        &amp;amp;&amp;amp; rm.Content.ToStringContent() == &amp;quot;Item 0: Quantity should be greater than 0.&amp;quot;);
}&lt;/pre&gt;
&lt;p&gt;and so forth. When the order is ok:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenOrderIsOk_ThenInsertANewOrderWithTheProductsAndPrice()
{
    var orderRepository = new RepositoryStub&amp;lt;Order&amp;gt;();
    var handler = CreateResourceHandler(orderRepository);
    var orderRepresentation = new OrderRepresentation { Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 } } };

    //act
    handler.Create(orderRepresentation);

    var order = orderRepository.RetrieveAll().First();
    order.Satisfy(o =&amp;gt; o.Items.Any(i =&amp;gt; i.Product == latte &amp;amp;&amp;amp; i.UnitPrice == 2.5m &amp;amp;&amp;amp; i.Quantity == 1));
}

[Test]
public void WhenOrderIsOk_ThenInsertANewOrderWithTheDateTime()
{
    var orderRepository = new RepositoryStub&amp;lt;Order&amp;gt;();
    var handler = CreateResourceHandler(orderRepository);
    var orderRepresentation = new OrderRepresentation { Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 } } };

    //act
    handler.Create(orderRepresentation);

    var order = orderRepository.RetrieveAll().First();
    order.Date.Should().Be.EqualTo(DateTime.Today);
}

[Test]
public void WhenOrderIsOk_ThenInsertANewOrderWithTheLocationInfo()
{
    var orderRepository = new RepositoryStub&amp;lt;Order&amp;gt;();
    var handler = CreateResourceHandler(orderRepository);
    var orderRepresentation = new OrderRepresentation 
                                    { 
                                        Location = Location.InShop, 
                                        Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 } } 
                                    };

    //act
    handler.Create(orderRepresentation);

    var order = orderRepository.RetrieveAll().First();
    order.Location.Should().Be.EqualTo(Location.InShop);
}&lt;/pre&gt;
&lt;p&gt;The above tests specify that an order must be saved properly. As you can notice here our resource handler depends on a IRepository&amp;lt;T&amp;gt; and in those tests we are using
&lt;a href="http://restbucks.codeplex.com/SourceControl/changeset/view/98e52bb914d5#RestBucks.Tests%2fUtil%2fRepositoryStub.cs"&gt;
RepositoryStub&amp;lt;T&amp;gt;&lt;/a&gt; which is an implementation of IRepository&amp;lt;T&amp;gt; with a simple in-memory collection.&lt;/p&gt;
&lt;p&gt;The last test for this use case is very interesting:&lt;/p&gt;
&lt;pre&gt;[Test]
public void WhenOrderIsOk_ThenResponseHasStatus201AndLocation()
{
    var orderRepository = new Mock&amp;lt;IRepository&amp;lt;Order&amp;gt;&amp;gt;();
    orderRepository.Setup(or =&amp;gt; or.MakePersistent(It.IsAny&amp;lt;Order[]&amp;gt;()))
                   .Callback&amp;lt;Order[]&amp;gt;(o =&amp;gt; o.First().Id = 123);

    var handler = CreateResourceHandler(orderRepository.Object);

    //act
    var result = handler.Create(new OrderRepresentation { Items = { new OrderItemRepresentation { Name = &amp;quot;latte&amp;quot;, Quantity = 1 }}});

    var expectedUriToTheNewOrder =
        resourceLinker.GetUri&amp;lt;OrderResourceHandler&amp;gt;(or =&amp;gt; or.Get(0, null), new { orderId = &amp;quot;123&amp;quot; });

    result.Satisfy(r =&amp;gt; r.StatusCode == HttpStatusCode.Created
                                  &amp;amp;&amp;amp; r.Headers.Location.ToString() == expectedUriToTheNewOrder);
}&lt;/pre&gt;
&lt;p&gt;It means that after creating the order, the response status must be Created (201), and there must be a Location header.&lt;/p&gt;
&lt;p&gt;The protocol is very clear on this (&lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30"&gt;section 14.40&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource.
&lt;u&gt;&lt;font style="background-color:#ffff00"&gt;For 201 (Created) responses, the Location is that of the new resource which was created by the request&lt;/font&gt;&lt;/u&gt;. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection
 to the resource. The field value consists of a single absolute URI.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A client application can pull the representation from the header location and get more details of the resource it just created, but that is for my next post :).&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Wed, 01 Jun 2011 16:18:14 GMT</pubDate><guid isPermaLink="false">Updated Wiki: RestBucks on .Net: Ordering Coffee 20110601041814P</guid></item><item><title>Updated Wiki: Preparing the environment</title><link>http://restbucks.codeplex.com/wikipage?title=Preparing the environment&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;Before I get to the core concepts; here is a short explanation of what you have to do to get the project working on your machine:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Clone the codeplex &lt;a href="http://restbucks.codeplex.com/SourceControl/list/changesets"&gt;
repository with mercurial&lt;/a&gt; &lt;/li&gt;&lt;li&gt;Create an empty database in a Sql Server instance &lt;/li&gt;&lt;li&gt;Open the Visual Studio solution &lt;/li&gt;&lt;li&gt;Edit the connection string named “Restbucks” in the Web.Config (web project) and App.config (test project) files.
&lt;/li&gt;&lt;li&gt;Run the test in RestBucks.Tests.DataInitializer, named InitializeData; this will create the database schema and populate some data.
&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;If you want to run the full suite of tests, you need to follow these two additional steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create another empty database &lt;/li&gt;&lt;li&gt;Modify the connection string named “Restbucks_Tests” in the App.Config (test project) to point to the database that you just created.
&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Now you can run the application! You will learn how it works in the following post.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/u&gt; all the dependencies were added with nuget, the dependencies are not under version control. The first time you build the solution, nuget will fetch all the dependencies from the main repository as explained
&lt;a href="http://joseoncode.com/2011/05/31/nuget-keep-your-package-folder-out-of-your-cvs/"&gt;
here&lt;/a&gt;. You don’t need nuget installed to do this, simply build the solution as any other solution.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;Contributors are welcome!&lt;/h1&gt;
&lt;p&gt;Want to contribute to this sample or show would you implement other RESTful patterns? follow the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fork the repository in codeplex (you will need a codeplex account) &lt;/li&gt;&lt;li&gt;Make changes to your local copy (clone in Mercurial jargon) &lt;/li&gt;&lt;li&gt;Do as many commit as you want, (within a new branch is the preferred way; but not mandatory)
&lt;/li&gt;&lt;li&gt;Push your changes to your fork in codeplex &lt;/li&gt;&lt;li&gt;Ping me in twitter, mail or wherever you want so I can see your changes (preferred codeplex
&lt;a href="http://restbucks.codeplex.com/discussions"&gt;discussion list&lt;/a&gt;) &lt;/li&gt;&lt;/ol&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Tue, 31 May 2011 16:28:21 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Preparing the environment 20110531042821P</guid></item><item><title>Updated Wiki: Overview</title><link>http://restbucks.codeplex.com/wikipage?title=Overview&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
&lt;p&gt;In this post I will give a brief overview of RestBucks on Net.&lt;/p&gt;
&lt;p&gt;Briefly RestBucks is a coffee shop as &lt;a href="http://www.starbucks.com"&gt;any other&lt;/a&gt;, well not as
&lt;strong&gt;any&lt;/strong&gt; other. They offer a tasty coffee and what is most interesting for us; a RESTfull interface where the customers can order the tasty black liquid. With this REST interface customers can make an order, customize, cancel, ask if it is ready,
 and so on. If you want to read a more (but not the whole book) you can read &lt;a href="http://www.infoq.com/articles/webber-rest-workflow"&gt;
this article in infoq&lt;/a&gt; (I strongly recommend you the book though).&lt;/p&gt;
&lt;h1&gt;Use cases&lt;/h1&gt;
&lt;p&gt;These are the use cases implemented so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The customer makes an order &lt;/li&gt;&lt;li&gt;The customer changes an order &lt;/li&gt;&lt;li&gt;The customer pay the order &lt;/li&gt;&lt;li&gt;The customer query his order &lt;/li&gt;&lt;li&gt;The customer cancel his order &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;As well, as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The barista prepare an order &lt;/li&gt;&lt;/ul&gt;
&lt;h1&gt;Implementation details&lt;/h1&gt;
&lt;p&gt;Since I started to participate in the opensource world with examples, I always tried to avoid examples that doesn’t work in real life or to answer to a poignant question with “it is just an example”. My goal with this application is that you can take it
 and use it for a real world scenario. Thus I am using real world technology, patterns and methodologies:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A relational database to persist the data. &lt;/li&gt;&lt;li&gt;An &lt;a href="en.wikipedia.org/wiki/Inversion_of_control"&gt;Inversion of control&lt;/a&gt; container
&lt;/li&gt;&lt;li&gt;An &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping"&gt;Object to relational mapper&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;Test Driven Development&lt;/a&gt;.
&lt;/li&gt;&lt;li&gt;As well as other well known patterns that we will discuss in upcoming articles.
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The tools I have used for this example were not randomly chosen. They are ready to be used in real world applications.
&lt;/p&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>jfromaniello</author><pubDate>Tue, 31 May 2011 15:29:54 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Overview 20110531032954P</guid></item></channel></rss>