Path: redb-route/redb.Route/Core/ListBody.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/ListBody.cs
----
using System.Collections;
using System.Collections.Concurrent;
using redb.Route.Abstractions;
namespace redb.Route.Core;
///
/// A body that is a list, at a producer. A list of (a batch consumer's body, for one) is sent
/// element by element, each element its own outgoing message with its own body and headers, as camel-kafka sends an
/// Iterable body; a connector that has no wire form for a plain list (a list of other values whose text would be
/// its type name) refuses it instead of sending that name. One implementation for every producer: the connector
/// supplies only the send.
///
public static class ListBody
{
private static readonly ConcurrentDictionary OwnToString = new();
/// The elements when is a collection of ; otherwise null.
public static IReadOnlyList? Messages(object? body)
=> body is IEnumerable messages ? messages as IReadOnlyList ?? messages.ToList() : null;
///
/// Whether is a plain list: a collection that is not a list of , not
/// text or bytes, and has no text form of its own (its ToString() is the type name). A JSON array or another
/// collection that writes itself as text is not one.
///
public static bool IsPlainList(object? body)
=> body is IEnumerable and not string and not byte[] and not IEnumerable
&& !OwnToString.GetOrAdd(body.GetType(),
static type => type.GetMethod(nameof(ToString), Type.EmptyTypes)?.DeclaringType != typeof(object));
///
/// The exchange one element is sent from, a linked child of (its properties, its scope):
/// an element is the child's message, a copy with its own headers and none of the parent's;
/// any other element is the body under a copy of the parent's headers and attachments, as a Split part is. Dispose
/// it once the element is sent.
///
public static IExchange ElementExchange(IExchange parent, object? element)
{
ArgumentNullException.ThrowIfNull(parent);
IMessage message;
if (element is IMessage own)
{
message = own.Clone();
}
else
{
message = new Message(element) { ContentType = parent.In.ContentType };
foreach (var header in parent.In.Headers)
message.Headers[header.Key] = header.Value;
MessageAttachments.CopyInto(parent.In, message);
}
return parent.CreateLinkedChild(message);
}
/// The refusal of a list at a request-reply producer: one request cannot wait for many replies.
public static InvalidOperationException RequestReplyRefusal(string endpoint, int count) => new(
$"Endpoint '{endpoint}' is request-reply and the body is a list of {count} messages: one request cannot wait for " +
"many replies. Split the list for request-reply.");
/// The refusal of a plain list at a producer that has no wire form for it.
public static InvalidOperationException PlainListRefusal(string endpoint, object body) => new(
$"Endpoint '{endpoint}' cannot send a body of type {body.GetType().Name}: a list has no wire form here, and its " +
"text would be the type name. Serialize the list (to JSON, for example), or make its elements IMessage to send " +
"each as its own message.");
}