Replies: 1 comment 1 reply
-
Hey @Daafmans I suppose the reason its done is so that it can go both ways, one thing to think about is if you want to make a message mapper per Message, I know we are working on a Generic and I have one in my own code base public class MyMessageMapper<T> : IAmAMessageMapper<T> where T : class, IRequest
{
private readonly ITopicNameResolver _subscriptionTopics;
public MyMessageMapper(ITopicNameResolver subscriptionTopics)
{
_subscriptionTopics = subscriptionTopics;
}
public Message MapToMessage(T request)
{
MessageType messageType;
if (request is ICommand)
messageType = MessageType.MT_COMMAND;
else if (request is IEvent)
messageType = MessageType.MT_EVENT;
else
{
throw new ArgumentException("This message mapper can only map Commands and Events", nameof(request));
}
var topicName = _subscriptionTopics.GetTopicName<T>();
var header = new MessageHeader(messageId: request.Id, topic: topicName, messageType: messageType);
var body = new MessageBody(JsonSerializer.Serialize(request));
var message = new Message(header, body);
return message;
}
public T MapToRequest(Message message)
{
return JsonSerializer.Deserialize<T>(message.Body.Value, JsonSerialiserOptions.Options);
}
} And then Register it as such builder.Services.AddSingleton(typeof(IAmAMessageMapper<>), typeof(MyMessageMapper<>)); You will need to implement something like ITopicResovler that is effectively a dictionary of MessageType to TopicName Please let me know if you have any questions |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey folks,
I'm fairly new to the framework, and one thing that surprised me that in the IAmAMessageMapper (for which an implementation is required), I need to implement both the MapToMessage and the MapToRequest methods. For the thing I'm currently implementing (which uses Apache Kafka), it's supposed to only read message, not produce any.
If the IAmAMessageMapper interface were to be split up, it could eliminate the need for a message that really isn't supposed to be used in some cases.
I know there's the option of throwing something like a MethodNotImplementedException, but to me that's just a big ol' code smell.
Beta Was this translation helpful? Give feedback.
All reactions