1

I have a service for Employee with a DTO

[Route("/employee/{id}", "GET PUT DELETE")]
[Route("/employees", "POST")]
public class Employee : Person
{
    public Employee() : base() { 
        this.dependents = new List<Dependent>();
    }
    public List<Dependent> dependents { get; set; }

}

I would like to understand how to handle the case where I want to just return the dependents collection. I would expect the url to be /employee/{id}/dependents. Being new to ServiceStack I am having a hard time figure how to map that to a service handler within my EmployeeService. Much Thanks!

user2410939
  • 195
  • 1
  • 1
  • 7

1 Answers1

0

I would like to understand how to handle the case where I want to just return the dependents collection. I would expect the url to be /employee/{id}/dependents

First you need to provide a route that matches the url you want:

[Route("/employee/{id}/dependents")]
public class Employee : IReturn<List<Dependent>>
{
    public Employee() { 
        this.dependents = new List<Dependent>();
    }

    public List<Dependent> dependents { get; set; }
}

I would also encourage you not to use inheritance in DTOs as it hides the intent and declarative structure of your services.

Your service implementation should be pretty straight forward:

public class EmployeeService : Service
{
    public List<Dependent> Get(Employee request)
    {
        return request.dependents; //just return the dependents collection?
    }
}
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390
  • That makes sense. I guess my lack of understanding arises because EmployeeService already has a Get method for a singular Employee: [Route("/employee/{id}", "GET PUT DELETE")] [Route("/employees", "POST")] public class Employee : Person { public Employee() : base() { this.dependents = new List(); } public List dependents { get; set; } } – user2410939 May 23 '13 at 17:40
  • Thanks again. I love servicestack. I followed up my question with some clarification in my previous comment. Since there already is a Get method for an Employee object I cannot use Get(Employee rewquest). If I change the method name to something besides Get the routing does not match and I get a "Not Found" on the resource. – user2410939 May 23 '13 at 18:07
  • The **Get** name requirement is so it matches the **HTTP Verb** you wish to handle. See the [New Api](https://github.com/ServiceStack/ServiceStack/wiki/New-Api) for more info. – mythz May 23 '13 at 18:14
  • I have looked through the New Api and a bunch of the sample apps. If I want to have a service to match GET Verb for returning the Employee Object and also need to match another Method to GET Verb for Employee.Dependents do I have to have a different Service for EmployeeDependents? – user2410939 May 23 '13 at 18:20
  • If you want 2 different services returning 2 different responses, you should have 2 Request DTOs. See the answers on [recommended API Structure](http://stackoverflow.com/a/15235822/85785) and [How to design a Message-based API](http://stackoverflow.com/a/15941229/85785) for some more examples. – mythz May 23 '13 at 18:23