Reputation: 1080
I am using ServiceStack to connect my thick client to our API server, and I like it a lot. However, I find having to write three classes for every request (Foo, FooResponse, and FooService) to be somewhat of an annoyance.
In my project, I have a lot of DAO interfaces, that look something like this:
public interface ICustomerDao {
public IList<Customer> FindCustomers(long regionId, string keyword);
}
I would like to be able to say something like this:
public interface ICustomerDao {
[AutoApi("Loads customers for the given region whose names match the keyword")]
[AutoRoute("/search/customer/{regionId}/{keyword}"]
public IList<Customer> FindCustomers(long regionId, string keyword);
}
public class SomeBusinessLogic {
[AutoService(typeof(ICustomerDao))]
public IList<Customer> FindCustomers(long regionId, string keyword) {
// lots of business logic here
}
}
And then, I would like the following classes to be auto-generated for me:
FindCustomers
: a ServiceStack DTO requestFindCustomersResponse
: the responseFindCustomersService
: a Service that takes a FindCustomers DTO, then calls SomeBusinessLogic.FindCustomers(req.RegionId, req.Keyword)
and wraps its return value in a FindCustomersResponse
ApiServiceCustomerDao
: Implements ICustomerDao
by auto-generating methods that construct FooRequest and contact the appropriate service, then receive FooResponse and unpack it automatically.Does something like this already exist ? If not, how hard would it be to implement ? Is there a better way ?
Upvotes: 0
Views: 1010
Reputation: 143399
I'd first recommend looking at AutoQuery to see whether it's suitable for quickly creating data-driven Services.
As ServiceStack promotes a code-first dev model and your Request and Response DTO's represent your Service Contract I'd strongly recommend against attempting dynamically generate them as you should retain full control of its definition, but you could use them as a template to dynamically generate your own Service implementations by following the same approach AutoQuery uses to generate your Services implementations and register them dynamically.
In AutoQuery you'd just need to define your Request DTO and the rest of the Service implementation are dynamically generated and registered. Since it returns a standard QueryResponse<T>
Response Type no DTO's are dynamically generated and thanks to the code-first Request DTO, clients still retain an end-to-end Typed API, e.g:
var movies = client.Get(new FindMovies { Ratings = new[]{"G","PG-13"} });
Upvotes: 2