Search code examples
c#genericsrequestdispatcher

How to correctly design a generic "request dispatcher" in C#


I have an object "Receiver" that is receiving/deserializing requests of many different types.

I would then like this receiver to make use of a "Dispatcher" object that would dispatch the request object to the correct request handler object (previously registered on the dispatcher), based on the type of the incoming request. Of course the type of the request is only known at runtime, not at compile time (would be too easy ;))

Here is some code to clarify things ...

I have a marker interface for request types :

public interface IRequest {}

Then a dummy request type :

public class DummyRequest : IRequest {}

This is the generic interface to be used by a request handler for a request type T.

public interface IRequestHandler<in T> where T : IRequest
{
   void HandleRequest(T request);
}

And a concrete implementation being a request handler for DummyRequest requests :

public class DummyRequestHandler : IRequestHandler<DummyRequest>
{
   public void HandleRequest(DummyRequest request) { }
}

Here is my very basic implementation of a dispatcher making use of dynamic, but which is working only if the request type is known at compile time, but can't work if the request type is known only at runtime (which is not of great interest in my context, but somehow shows the intent) :

public class Dispatcher
{
    private Dictionary<Type, dynamic> _handlers = new Dictionary<Type, dynamic>();

    public void Dispatch<T>(T requestObject) where T : IRequest
    {
       _handlers[requestObject.GetType()].HandleRequest(requestObject);
    }

    public void RegisterHandler<T>(IRequestHandler<T> handler) where T : IRequest
    {
       _handlers[typeof(T)] = handler;
    }
}

And the usage, considering dispatcher variable is a Dispatcher object :

Some code would register the handler on the dispatcher this way

dispatcher.RegisterHandler<DummyRequest>(new DummyRequestHandler());

And the receiver object would use the dispatcher to dispatch an incoming request this way :

dispatcher.Dispatch(new DummyRequest());            

Of course, here it works only if the request type is known at compile time, but in my context the request is received as an object type by the receiver such as :

// Here ReceiveMessage is not generic and therefore returns object type 
// (even if type is DummyRequest, it's just being returned as an object,
// i.e : "request is DummyRequest" returns true) 
// signature of ReceiveMessage is "object ReceiveMessage()"
var request = ReceiveMessage(); 

and I can't call the dispatcher like this

dispatcher.Dispatch(request); 

I can't find a way to work arround this without loosing type safety (for example not changing the HandleRequest signature to a non generic one taking an object) which I'd rather not lose.

I hope my question is clear and makes sense ... there is maybe no real way, I don't know.

Didn't find anything interesting by googling arround "request dispatcher C#". Maybe this kind of "pattern" is known under another name, but just make sense for me to call it a request dispatcher.

The finality behind this is to be able to handle new request types by just creating a new request handler for the specific request type and registering it on the dispatcher without having to change the code of the receiver, nor the dispatcher.

Thanks !


Solution

  • You cannot get away from the fact that you do not know the object type until runtime so you need a design that copes with that. You should have a look at the factory pattern. If you google it you will find loads of examples. But in essence you have to figure out the type of the object in your code at runtime and figure out what object type to instantiate. This could take the form of using Reflection or it could simply be a switch statement inside the factory method.