Skip to content

Handle json messages - #5

Merged
lewisrenfrew merged 2 commits into
mainfrom
handle-json-messages
May 18, 2026
Merged

lewisrenfrew merged 2 commits into
mainfrom
handle-json-messages

Conversation

@lewisrenfrew

@lewisrenfrew lewisrenfrew commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Adds JsonMessageHandler< T > which is the consuming-side counterpart to JsonMessagePublisher< T >. It uses the Template Method pattern to handle deserialization internally and then invoke your custom handler code, which now only has to deal with strongly-typed objects.

So when writing a new handler, instead of implementing the raw IMessageHandler directly and having to call some code to deserialize bytes, just inherit from this new JsonMessageHandler< T > and override the typed HandleAsync.

For example

public class OrderCreatedHandler : JsonMessageHandler<Order>
{

    protected override Task HandleAsync(
        Order payload,
        IReadOnlyDictionary<string, object> headers,
        CancellationToken cancellationToken)
    {
     // payload is already deserialized - just do your thing in here
    }
}

@lewisrenfrew
lewisrenfrew requested a review from a team May 18, 2026 14:43
@lewisrenfrew

Copy link
Copy Markdown
Contributor Author

Adds JsonMessageHandler< T > which is the consuming-side counterpart to JsonMessagePublisher< T >. It uses the Template Method pattern to handle deserialization internally and then invoke your custom handler code, which now only has to deal with strongly-typed objects.

So when writing a new handler, instead of implementing the raw IMessageHandler directly and having to call some code to deserializing bytes, just inherit from this new JsonMessageHandler< T > and override the typed HandleAsync.

For example

public class OrderCreatedHandler : JsonMessageHandler<Order>
{

    protected override Task HandleAsync(
        Order payload,
        IReadOnlyDictionary<string, object> headers,
        CancellationToken cancellationToken)
    {
     // payload is already deserialized - just do your thing in here
    }
}

oh and you can still write non json handlers as before by just using the base IMesageHandler if you ever need to... although I expect cases where JSON deserializiation is not required be few and far between, if not non-existent

@lewisrenfrew
lewisrenfrew merged commit be34b58 into main May 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants