c# - Making a proxy/service class more generic -
c# - Making a proxy/service class more generic -
i have 4 classes have modify each time want create phone call database, , process getting ridiculous. i'm wondering if there's easy way simplify process.
i have calling class such that:
public responseobjecta getclients() { requestobjecta req = new requestobjecta(); responseobjecta res = proxyclass.getclients(request); } public responseobjectb getvendors() { requestobjectb req = new requestobjectb(); responseobjectb res = proxyclass.getvendors(request); }
then in proxyclass:
public static responseobjecta getclients(requestobjecta request) [ responseobjecta res = new responseobjecta(); res = serviceclass.getclients(request); } public static responseobjectb getvendors(requestobjectb request) [ responseobjectb res = new responseobjectb(); res = serviceclass.getvendors(request); }
then in serviceclass:
public responseobjecta getclients(requestobjecta request) [ responseobjecta res = new responseobjecta(); dalclass.getclients(request, res); } public responseobjectb getvendors(requestobjectb request) [ responseobjectb res = new responseobjectb(); dalclass.getvendors(request, res); }
finally dalclass:
public void getclients(requestobjecta request, responseobjecta response) [ ..perform sql stuff.. } public void getvendors(requestobjectb request, responseobjectb response) [ ..perform sql stuff }
do of see problem here if i'm going add together responseobjectc , requestobjectc if i'm going create phone call called getsales? it's 5 more updates.
i simplified these methods illustrate point, each method useful. problem have repetitiveness of copying methods , changing 1 piece of code. wondering if there's way generic type proxyclass , serviceclass don't have modify them. calling class , dalclass.
side note: responseobjecta , responseobjectb both kid classes of baseresponse.. likewise requestobjecta , requestobjectb both kid classes of baserequest.
one solution: thought having method names called "handle" , utilize method overloading.
like end result of dalclass having next signatures:
public void handler(requestobjecta, responseobjecta) { } public void handler(requestobjectb, responseobjectb) { }
but i'm not sure proxy , service classes verify right handler beingness called.
it looks consolidate writing generic method or two:
public static tresponse get<trequest, tresponse>(trequest request, func<trequest, tresponse> getter) trequest : baserequest tresponse : baseresponse { tresponse res = getter(request); }
you still need write out each method, shorter, abstract mutual stuff out:
public static responseobjecta getclients(requestobjecta request) { homecoming get(request, serviceclass.getclients); } public static responseobjectb getvendors(requestobjectb request) { homecoming get(request, serviceclass.getvendors); }
also, if haven't already, take see whether t4 templates wouldn't useful -- looks might be. instead of robo-coding proxy , service classes, maintain tuple list (name of method, request type, response type), , write t4 template generates each of classes various methods each entry in list. (this how auto-generated code service proxy , database entity classes generated in visual studio.)
c# generics
Comments
Post a Comment