Need a more efficient construct for the callback dispatcher component

In the client application, I send the request / transaction (containing the operation to execute (and parameters) + transactionID) to the remote queue. The remote server deletes the request at some point and takes some time to process it.

Once processing is complete, it sends a response to the client's queue (containing the applicative response + transaction identifier) ​​... so this is a completely “disconnected” communication mode, the only way the client can display the response to the request is through the transaction identifier.

The response to the message is canceled on the client side and corresponds to the original request (based on the transaction identifier).

What I'm doing right now is that when a client sends a request to the server queue, it adds a callback to dictionnary, which saves transactionId and callback (delegate). This is the Dictionary<int, object>mapping of a transaction to a callback for a call to the result of an operation.

Callbacks / delegates are stored as objects due to the fact that the signature of the callback delegate is different depending on the request (for example, the response may return List<string>, while the other response may return int).

When the client queue cancels the response, it knows the type of response (and therefore the corresponding callback signature), so it gets the callback from the dictionary based on the transaction ID. It then returns the object back to the appropriate delegate type and calls the callback.

"", ​​.

?

, .

+5
4

Reactive Extensions - .

- , , IMessage. , .

public interface IMessage
{
    int TransactionId { get; }
}

public class UserListMessage : IMessage
{
    int TransactionId { get; set; }
    public List<string> Users { get; set; }
}

IObservable<IMessage>. Subject<T>, - .

Observable Subscribe(IObserver<IMessage> observer), - . , OnNext(IMessage message) .

, :

var subscription = myQueue
    .OfType<UserListMessage>()
    .Where(msg => msg.TransactionId == id)
    .Subscribe(callback);

UserListMessage . , . :

subscription.Dispose();

, Rx. .

+3

, , . , " ", . , , .

, , , "-" ... , ResponseAction , , , , , Response_Specific_Action ... , Response_Specific_Action.

ResponseAction, transactionID .

+1

- . , , ? WCF ?

, ?

List , , ? ? , .

+1

, . . .

, , ( , List<string> int). , . - :

public abstract class MessageCompleteHandler
{
    public abstract void Execute();
}

//name this one better, just an example :)
public class ListOfStringHandler : MessageCompleteHandler
{
    public override void Execute()
    {
        //get list of strings
        // do something with it
    }
}

public class MessageCompleteHandlerFactory
{
    public MessageCompleteHandler GetHandler(int transactionId)
    {
        //this replaces/uses your dictionary to match handlers with types.
    }
}

, , - . , , , .

+1

All Articles