Created
August 26, 2012 19:50
-
-
Save vansha/3483088 to your computer and use it in GitHub Desktop.
Simplified usage of ServiceStack REST services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Response types omitted. | |
// Note that request types now marked with IRequest<TResponse> interface. | |
// This allows Send method to determine response type at compile time. | |
[RestService("/orders/{id}", "GET")] | |
[RestService("/orders/by-code/{code}", "GET")] | |
[RestService("/orders/search", "GET")] | |
public class GetOrders : IRequest<GetOrdersResponse> { | |
public int? Id { get; set; } | |
public string Code { get; set; } | |
public string Name { get; set; } | |
public string Customer { get; set; } | |
} | |
[RestService("/orders", "POST")] // to create new order. | |
[RestService("/orders/{id}", "PUT")] // to create or update existing order with specified Id. | |
public class SaveOrder : IRequest<SaveOrderResonse> { | |
public int? Id { get; set; } | |
// Order details. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Marker interface allowing to determine response type at compile time. | |
public interface IRequest<TResponse> { | |
} | |
public static class RestServiceExtensions | |
{ | |
public static TResponse Send<TResponse>(this IRestClient client, IRequest<TResponse> request) { | |
// Determine matching REST endpoint for specified request (via RestService attributes). | |
// Populate url with encoded variables and optional query parameters. | |
// Invoke corresponding REST method with proper Http method | |
// and return strongly typed response | |
// is automatically determined by compiler. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IRestClient client = new JsonServiceClient(); | |
// We don't specify response type - it is determined automatically based on request type. | |
// We don't specify URLs or HTTP verbs - they are determined based on request state. | |
// We don't write boilerplate code to encode url parts. It is done automatically. | |
// GET /orders/5 | |
var orderById = client.Send(new GetOrders { Id = 5 }).Orders.Single(); | |
// GET /orders/by-code/Code | |
var orderByCode = client.Send(new GetOrders { Code = orderById.Code }).Orders.Single(); | |
// GET /orders/search?Name=Name&Customer=Customer | |
var getOrders = new GetOrders { Name = orderById.Name, Customer = orderByCode.Customer }; | |
var foundOrders = client.Send(getOrders).Orders; | |
// POST /orders | |
var createOrderResponse = client.Send(new SaveOrder { /* Order details */}); | |
// PUT /orders/id | |
int orderId = createOrderResponse.Id; | |
var updateOrderResponse = client.Send(new SaveOrder { Id = orderById.Id, /* Order details */ }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment