c# - Need a better design for a "callback dispatcher" component -
from client application sending request/transaction (containing operation perform (and parameters) + transactionid) remote queue. remote server dequeue request @ point , takes time process it.
once done processing it, sends reponse on client queue (containing applicative response + transactionid) ... totally "disconnected" communication mode, way client can map response request via transactionid.
message response dequeued on client side, , matched original request (based on transactionid).
what doing right when client post request server queue, adds callback dictionnary keeping transactionid , callback (delegate). dictionary<int, object>
mapping transactionid callback call result of operation.
the callbacks/delegates stored object due fact depending of request, callback delegate signature different (for example response may return list<string>
whereas response may return int
).
when client queue dequeues response, knows type of response (and therefore corresponding signature of callback), therefore gets callback dictionary, based on transactionid. casts object corresponding delegate type , invoke callback.
i find approach not "sexy" not see way of performing such task.
is there better way of performing ?
if problem not clear enough, please let me know , clarify edits.
you might find reactive extensions handy tool kind of messaging.
first, make responses implement interface or base class, imessage. each kind of response should encapsulated in separate class here
public interface imessage { int transactionid { get; } } public class userlistmessage : imessage { int transactionid { get; set; } public list<string> users { get; set; } }
then make message queue implement iobservable<imessage>
. reactive extensions provides ready use implementation called subject<t>
, might want wrap or something.
observable implements subscribe(iobserver<imessage> observer)
method stores observers somewhere in internal list. when new response arrives onnext(imessage message)
method called on each subscribed observer.
finally, response handling registration code might like:
var subscription = myqueue .oftype<userlistmessage>() .where(msg => msg.transactionid == id) .subscribe(callback);
that register callback message of type userlistmessage given transaction id. want unsubscribe somewhere. be:
subscription.dispose();
that brief sample how rx. go , find more detailed tutorial.
Comments
Post a Comment