c# - ServiceStack multiple services web API -
i'm newbie servicestack , learn how works, i'll develop web api northwind database (using repository pattern).
i've checked sample project servicestack.northwind , there 2 services (customers , orders). i'd develop complete api (customers, orders, products, etc..). matt cowan has done.
basically, services same operation:
- receive request.
- execute (repository.get, repository.add, repository.update, repository.delete).
- send response.
for this, thought making base class work. first started like:
public class baseservice<trepository, tentity, tdto> : service { ... }
the problem of class don't know types request , response each operation. thought i'd pass them type arguments:
public class baseservice<trepository, tentity, tdto, trequest, tsingleresponse, tcollectionresponse> : service { ... }
i don't this. i'm sure can done without passing n type arguments class.
how approach development of base class?.
thank in advance.
you may reduce number of type arguments using following suggestions:
- use
tentity
request/response operations on single entity - declare repository interface
irepository<tentity>
avoid repository type argument
as operations return list entities (eg. findcustomers, findorders) - each operation have unique search parameters , need implement in derived class anyway.
public class baseentity { public int id { get; set; } // ... } public interface irepostory<tentity> tentity : baseentity { ilist<tentity> getall(); tentity get(int id); void save(tentity entity); // ... } public class baseservice<tentity, tcollectionrequest> : service tentity : baseentity { public irepository<tentity> repository { get; set; } public virtual object get(tentity request) { return repository.get(request.id); } public virtual object get(tcollectionrequest request) { return repository.getall(); } public virtual object post(tentity request) { repository.save(request); return request; } public virtual object put(tentity request) { // ... } // ... }
Comments
Post a Comment