You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to implement multiple IRequestHandlers for the same IRequest, and use pattern matching to determine which IRequestHandler to route the Request to?
#895
In my use case (a fax handling service), I need to have multiple implementations of the IRequestHandler<>, and differentiate as to which one will handle the request based on data inside the message. For example, my IRequest is below:
public class ReceiveFaxCommand : IRequest<FaxReceipt>
{
public string FaxId { get; set; } = string.Empty;
public string ChannelName { get; set; } = string.Empty;
private ReceiveFaxCommand()
{ }
public ReceiveFaxCommand(string faxId, string channelName)
{
FaxId = faxId;
ChannelName = channelName;
}
}
For ChannelName == "This", I want the IRequestHandler<> in one implementation (specific to a particular fax services vendor) to handle the request. For ChannelName == "That", I'd like it to be handled by a different implementation. Ultimately, however, the domain model is the same across the different implementations, so it doesn't immediately make sense to me to create a different message type for each vendor or channel, especially since I'd like to be able to add additional channels without changing the code.
I know from the documentation that IRequest is designed to have at most one handler; is there a clever way I haven't thought of to have my cake and eat it too?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In my use case (a fax handling service), I need to have multiple implementations of the
IRequestHandler<>
, and differentiate as to which one will handle the request based on data inside the message. For example, myIRequest
is below:For
ChannelName == "This"
, I want theIRequestHandler<>
in one implementation (specific to a particular fax services vendor) to handle the request. ForChannelName == "That"
, I'd like it to be handled by a different implementation. Ultimately, however, the domain model is the same across the different implementations, so it doesn't immediately make sense to me to create a different message type for each vendor or channel, especially since I'd like to be able to add additional channels without changing the code.I know from the documentation that
IRequest
is designed to have at most one handler; is there a clever way I haven't thought of to have my cake and eat it too?Beta Was this translation helpful? Give feedback.
All reactions