|
| 1 | +# Message Translator |
| 2 | + |
| 3 | +`MessageTranslator<TInput,TOutput>` translates one message contract into another while applying an explicit header policy. Use it at integration boundaries where partner, vendor, or transport-specific events need to become application-owned contracts before they enter routers, sagas, mailboxes, or reliability pipelines. |
| 4 | + |
| 5 | +## Runtime Path |
| 6 | + |
| 7 | +```csharp |
| 8 | +using PatternKit.Messaging; |
| 9 | +using PatternKit.Messaging.Transformation; |
| 10 | + |
| 11 | +var translator = MessageTranslator<PartnerOrderAccepted, CommerceOrderAccepted> |
| 12 | + .Create("partner-order-translator") |
| 13 | + .TranslateWith(static (message, context) => new CommerceOrderAccepted( |
| 14 | + $"commerce-{message.Payload.ExternalOrderId}", |
| 15 | + message.Payload.Amount, |
| 16 | + message.Payload.PartnerId)) |
| 17 | + .DropHeader("raw-signature") |
| 18 | + .SetHeader(MessageHeaderNames.ContentType, "application/vnd.myapp.order+json") |
| 19 | + .Build(); |
| 20 | + |
| 21 | +var result = translator.Translate(partnerMessage); |
| 22 | +``` |
| 23 | + |
| 24 | +The translator returns `MessageTranslationResult<TOutput>` instead of leaking transformation exceptions into routing code. Invalid translator output or header policy failures produce a failed result with the captured exception. |
| 25 | + |
| 26 | +## Header Policies |
| 27 | + |
| 28 | +Headers are preserved by default so correlation, causation, message identifiers, and tenant metadata survive the contract change. Fluent policies can remove sensitive transport headers, keep only an allow-list, or set normalized headers: |
| 29 | + |
| 30 | +```csharp |
| 31 | +var translator = MessageTranslator<RawEvent, NormalizedEvent> |
| 32 | + .Create("normalizer") |
| 33 | + .TranslateWith(static (message, _) => new NormalizedEvent(message.Payload.Id)) |
| 34 | + .KeepHeaders(MessageHeaderNames.CorrelationId, "tenant-id") |
| 35 | + .SetHeader(MessageHeaderNames.ContentType, "application/vnd.myapp.normalized+json") |
| 36 | + .Build(); |
| 37 | +``` |
| 38 | + |
| 39 | +## Source-Generated Path |
| 40 | + |
| 41 | +Use `[GenerateMessageTranslator]` when a translator contract should be compile-time visible and reusable from dependency injection setup: |
| 42 | + |
| 43 | +```csharp |
| 44 | +using PatternKit.Generators.Messaging; |
| 45 | +using PatternKit.Messaging; |
| 46 | + |
| 47 | +[GenerateMessageTranslator(typeof(PartnerOrderAccepted), typeof(CommerceOrderAccepted), TranslatorName = "partner-order-translator")] |
| 48 | +[MessageTranslatorDropHeader("raw-signature")] |
| 49 | +[MessageTranslatorHeader(MessageHeaderNames.ContentType, "application/vnd.myapp.order+json")] |
| 50 | +public static partial class PartnerOrderTranslator |
| 51 | +{ |
| 52 | + [MessageTranslatorHandler] |
| 53 | + private static CommerceOrderAccepted Translate(Message<PartnerOrderAccepted> message, MessageContext context) |
| 54 | + => new($"commerce-{message.Payload.ExternalOrderId}", message.Payload.Amount, message.Payload.PartnerId); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The generated factory returns `MessageTranslator<PartnerOrderAccepted, CommerceOrderAccepted>` and emits the same runtime builder calls. |
| 59 | + |
| 60 | +## DI Integration |
| 61 | + |
| 62 | +Examples can be imported through `Microsoft.Extensions.DependencyInjection`: |
| 63 | + |
| 64 | +```csharp |
| 65 | +services.AddPartnerEventTranslatorExample(); |
| 66 | + |
| 67 | +var service = provider.GetRequiredService<PartnerOrderImportService>(); |
| 68 | +var summary = service.Import(partnerMessage); |
| 69 | +``` |
| 70 | + |
| 71 | +Production example: |
| 72 | + |
| 73 | +- `src/PatternKit.Examples/Messaging/PartnerEventTranslatorExample.cs` |
| 74 | +- `test/PatternKit.Examples.Tests/Messaging/PartnerEventTranslatorExampleTests.cs` |
0 commit comments