c# - ServiceStack REST API Design -
i'm starting play around servicestack , i'm enjoying far i'm thinking design flawed go. essentially, have mssql database i'm accessing via nhibernate. confusion coming due structure request / response dtos & services should take.
i have nhibernate mapping in separate project under myproject.common.models contains "client" class so:
namespace myproject.common.models { public class client { public virtual int clientid { get; set; } public virtual string name { get; set; } public virtual string acronym { get; set; } public virtual string website { get; set; } } public class clientmap : classmap<client> { public clientmap() { id(x => x.clientid, "clientid").generatedby.identity(); map(x => x.name, "name"); map(x => x.acronym, "acronym"); map(x => x.website, "website"); } } }
i want provide client ability crud single client, displaying list of clients. far, i've designed single client request so:
[route("/clients/{id}", "get")] public class clientrequest : ireturn<clientresponse> { public string id { get; set; } } public class clientresponse : ihasresponsestatus { public myproject.common.models.client client { get; set; } public responsestatus responsestatus { get; set; } public clientresponse() { this.responsestatus = new responsestatus(); } }
which can see returning model class client. sort of design, i'm @ loss of how post new client, or update existing client. also, if wanted return list of clients, i'm using following request/response dtos:
[route("/clients", "get")] public class clientsrequest : ireturn<clientsresponse> { } public class clientsresponse : ihasresponsestatus { public list<myproject.common.models.client> clients { get; set; } public responsestatus responsestatus { get; set; } public clientsresponse() { this.responsestatus = new responsestatus(); } }
with service so:
public clientsresponse get(clientsrequest request) { var result = currentsession.query<chronologic.eve.common.models.client>().tolist(); if (result == null) throw new httperror(httpstatuscode.notfound, new argumentexception("no clients exist")); return new clientsresponse { clients = result }; }
which works, though feel not best way achieve attempting do, , gives me ugly metadata page showing so:
i feel i'm overlooking quite simple design, , if suggest me how streamline design appreciated.
thank you.
you should check out these earlier posts api design servicestack:
rather re-hash of content contained above, i'll rewrite how i'd it.
you don't need responsestatus
property services can return clean dtos.
[route("/clients", "get")] public class allclients : ireturn<list<client>> {} [route("/clients/{id}", "get")] public class getclient : ireturn<client> { public string id { get; set; } }
the service implementation should straight forward based on above request dtos.
with above api, c# client call sites like:
list<client> clients = service.get(new allclients()); client client = service.get(new getclient { id = id });
Comments
Post a Comment