2

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 !

darkey
  • 3,672
  • 3
  • 29
  • 50

2 Answers2

2

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.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
2

What you could do is implement abstract factory pattern (incorporating generics), which very simply outlined could look something like the following

public interface IRequest
{
   void Dispatch();
}

Where you would use the following class to obtain the correct 'strategy' to use at runtime

public static class DispatchStrategyfactory
{
    public static void Dispatch<T>(T instance) where T : IRequest 
    {
        instance.Dispatch();
    } 
}

However for this pattern to work you would have to create the relevent concrete classes as

public static TestDispatcher : IRequest
{
    public void Dispatch()
    {
        // Do stuff.
    }
}

This should enable you to do what you require. For more information on the Strategy/[Abstract]Factory patterns see here. I hope I have not missed the point here.

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277