How to implement Delete service call using ServiceStack

前端 未结 2 1371
感动是毒
感动是毒 2020-12-05 22:14

I have couple of questions related to REST service implementation using ServiceStack.

  1. For GET operation, I define my request DTO as below :

    
    
            
相关标签:
2条回答
  • 2020-12-05 22:38

    See this earlier answer for details on how to design a REST-ful API with ServiceStack.

    The CustomerRestExample contains a complete stand-alone example of a Customer REST ServiceStack Service:

    Customer Service Definition

    Here's an example of the custom Routes and Request DTO's of what a typical Customer REST Service could look like:

    [Route("/customers", "GET")]
    public class GetCustomers : IReturn<GetCustomersResponse> {}
    
    public class GetCustomersResponse
    {
        public List<Customer> Results { get; set; } 
    }
    
    [Route("/customers/{Id}", "GET")]
    public class GetCustomer : IReturn<Customer>
    {
        public int Id { get; set; }
    }
    
    [Route("/customers", "POST")]
    public class CreateCustomer : IReturn<Customer>
    {
        public string Name { get; set; }
    }
    
    [Route("/customers/{Id}", "PUT")]
    public class UpdateCustomer : IReturn<Customer>
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    }
    
    [Route("/customers/{Id}", "DELETE")]
    public class DeleteCustomer : IReturnVoid
    {
        public int Id { get; set; }
    }
    

    OrmLite POCO Model:

    public class Customer
    {
        [AutoIncrement]
        public int Id { get; set; }
    
        public string Name { get; set; }
    }
    

    Essentially the Custom Routes identify the Resource whilst the HTTP VERB indicates the operation on that Resource. Looking at the HTTP Requests makes this a little clearer:

    GET    /customers   -> return all Customers
    POST   /customers   -> Create a new Customer
    GET    /customers/1 -> return Customer 1
    PUT    /customers/1 -> Update Customer 1
    DELETE /customers/1 -> Delete Customer 1
    

    Customer Service Implementation

    With the above DTO's definitions in-place, we can now implement this Customer REST Service by adding an implementation for each Request DTO - in this example using OrmLite:

    public class CustomerService : Service
    {
        public object Get(GetCustomers request)
        {
            return new GetCustomersResponse { Results = Db.Select<Customer>() };
        }
    
        public object Get(GetCustomer request)
        {
            return Db.SingleById<Customer>(request.Id);
        }
    
        public object Post(CreateCustomer request)
        {
            var customer = new Customer { Name = request.Name };
            Db.Save(customer);
            return customer;
        }
    
        public object Put(UpdateCustomer request)
        {
            var customer = Db.SingleById<Customer>(request.Id);
            if (customer == null)
                throw HttpError.NotFound("Customer '{0}' does not exist".Fmt(request.Id));
    
            customer.Name = request.Name;
            Db.Update(customer);
    
            return customer;
        }
    
        public void Delete(DeleteCustomer request)
        {
            Db.DeleteById<Customer>(request.Id);
        }
    }
    

    Customer Usage Example

    With the above Customer REST Service implementation, we can re-use the Request DTO's with ServiceStack's .NET Service Clients to provide an end-to-end Typed API without code-gen, i.e:

    var client = new JsonServiceClient(BaseUri);
    
    //GET /customers
    var all = client.Get(new GetCustomers());                         // Count = 0
    
    //POST /customers
    var customer = client.Post(new CreateCustomer { Name = "Foo" });
    
    //GET /customer/1
    customer = client.Get(new GetCustomer { Id = customer.Id });      // Name = Foo
    
    //GET /customers
    all = client.Get(new GetCustomers());                             // Count = 1
    
    //PUT /customers/1
    customer = client.Put(
        new UpdateCustomer { Id = customer.Id, Name = "Bar" });       // Name = Bar
    
    //DELETE /customers/1
    client.Delete(new DeleteCustomer { Id = customer.Id });
    
    //GET /customers
    all = client.Get(new GetCustomers());                             // Count = 0
    

    The comments above include the HTTP Operations performed in each Service Client example.

    0 讨论(0)
  • 2020-12-05 22:43

    I got the fix for my 2nd question from following two links : 1. Link1 2. Link2

    I don't fully understand this fix but doing above changes worked for me and now I can call Delete function from any clients.

    For 1st question, pls refer @mythz 's reply below in detail.

    0 讨论(0)
提交回复
热议问题