Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simplified usage of ServiceStack REST services.
// 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.
// 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.
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