Path: redb-route/redb.Route/Core/Message.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/Message.cs ---- using System.Collections.ObjectModel; using redb.Route.Abstractions; namespace redb.Route.Core; /// /// Default implementation of IMessage. /// Holds a payload Body and a dictionary of Headers. /// public class Message : IMessage { private readonly Dictionary _headers = new(StringComparer.OrdinalIgnoreCase); private MessageAttachments? _attachments; /// public object? Body { get; set; } /// public IMessageAttachments Attachments => _attachments ??= new MessageAttachments(); /// The attachment list when one was created and holds something; null otherwise (no allocation). internal MessageAttachments? AttachmentsOrNull => _attachments is { Count: > 0 } a ? a : null; /// public string? ContentType { get; set; } /// public IDictionary Headers => _headers; /// Creates an empty message. public Message() { } /// Creates a message with the specified body. /// Initial payload. public Message(object? body) => Body = body; /// public T? GetHeader(string key) => _headers.TryGetValue(key, out var value) ? TypedValue.Convert(value, $"header '{key}'") : default; /// public IMessage Clone() { var clone = new Message(Body) { ContentType = ContentType }; foreach (var kvp in _headers) clone._headers[kvp.Key] = kvp.Value; // A new list over the same content sources, as the body reference is shared. clone._attachments = AttachmentsOrNull?.Copy(); return clone; } /// public IMessage Snapshot() { var snapshot = new Message(DeepCopyBody(Body)) { ContentType = ContentType }; // Header values are shared like Clone — they carry immutable metadata, not payload. foreach (var kvp in _headers) snapshot._headers[kvp.Key] = kvp.Value; // Attachments are payload: their content is copied, like the body. snapshot._attachments = AttachmentsOrNull?.DeepCopy(); return snapshot; } /// /// Deep-copies a message body for a checkpoint snapshot. Handles the faithful, non-lossy cases /// only: null / / value types are immutable and shared as-is; /// byte[] is copied; anything implementing is cloned via its own /// contract (the author is responsible for that clone being deep). Everything else throws — we /// never silently share a mutable reference behind a "snapshot", nor round-trip through a /// serializer (which would be lossy for many types). A serializer-based deep clone can be added /// as an opt-in strategy later without changing this contract. /// internal static object? DeepCopyBody(object? body) => body switch { null => null, string => body, // immutable ValueType => body, // int/bool/DateTime/Guid/decimal/enum/tuples/structs — boxed copy is safe to share byte[] bytes => bytes.Clone(), // must precede ICloneable (byte[] is ICloneable but we copy the array) ICloneable cloneable => cloneable.Clone(), _ => throw new NotSupportedException( $"Checkpoint snapshot cannot deep-copy a message body of type '{body.GetType().FullName}'. " + "A replayable body must be immutable, a byte[], or implement ICloneable (with deep semantics). " + "Materialize it into a snapshot-able form before the .Replayable() marker.") }; }