0

I have many models that defines all my db tables; I wondering which is the best way to create one single CRUD ServiceStack interface for all these models without write the same code for each one. I'd like to keep it DRY to ease future maintaining.

Thank you.

pardie
  • 399
  • 2
  • 16

1 Answers1

0

Checkout AutoQuery which lets you expose a rich, queryable API's for each table by just declaring its Request DTO:

[Route("/movies")]
public class FindMovies : QueryBase<Movie> {}

You want a typed Request DTO for each Service, but other than that you can use a base class, shared extension or utility methods to execute common logic as you would in normal C#. The built-in Auto Mapping also reduces the boilerplate for populating a Table POCO from a request DTO.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thank you mythz I'm trying out with AutoQuery... the thing I don't understand is if AutoQuery implements only the GET request or the entire CRUD (POST, DELETE, UPDATE). – pardie Sep 24 '15 at 13:55
  • @wizzy it's just for querying, it's an advanced option but you can use the same approach to dynamically generate and register services: http://stackoverflow.com/a/28934744/85785 – mythz Sep 24 '15 at 17:39
  • yes... it's the same thing I'd need to accomplish but the first example is unfeasible (Single Service that delegates to multiple internal Services) because I would end with a very long list of "if...else" with the same code inside just to change the model type... I'd like to see an example, if you could, with the same "approach of AutoQuery" you mentioned, because I really can't understand how to add the POST, DELETE and UPDATE functionalities. – pardie Sep 24 '15 at 22:11
  • @wizzy don't have time to expand on the existing AutoQuery source code impl, esp. given it's not an approach I'd recommend. My pref is for each Service to have an [explicit Request DTO grouped by semantics and Response Type](http://stackoverflow.com/a/15941229/85785). So each Service gets an explicit Route/endpoint and Request DTO that you're in complete control of. Inside of the Service you then have the freedom to delegate to shared methods / mapping / reusable logic. Trying to auto-generate them by a single impl is IMO too restrictive and promotes magic/complexity/unintended behavior. – mythz Sep 24 '15 at 22:26