InMemoryCommandQueue Class
Definition
- Assemblies
- Savvyio.Commands.Messaging.dll
Provides an in-memory implementation of the IPointToPointChannel<TRequest> interface useful for unit testing and the likes thereof.
public class InMemoryCommandQueue : IPointToPointChannel<ICommand>, ISender<ICommand>, IReceiver<ICommand>
- Inheritance
-
objectInMemoryCommandQueue
- Implements
Examples
The following example shows how to enqueue command messages and drain them from InMemoryCommandQueue in a test-friendly workflow.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Savvyio.Commands;
using Savvyio.Commands.Messaging;
using Savvyio.Messaging;
namespace ExampleApp.InMemoryCommandQueueSample;
public sealed class InMemoryCommandQueueUsage
{
public InMemoryCommandQueueUsage()
{
RunAsync().GetAwaiter().GetResult();
}
public IReadOnlyList<string> ReceivedInvoiceIds => _receivedInvoiceIds;
private readonly List<string> _receivedInvoiceIds = new();
private async Task RunAsync()
{
var queue = new InMemoryCommandQueue();
IMessage<ICommand>[] messages =
{
new CreateInvoiceQueueCommand("INV-42").ToMessage(new Uri("https://api.example.com/commands/invoices"), nameof(CreateInvoiceQueueCommand)),
new CreateInvoiceQueueCommand("INV-43").ToMessage(new Uri("https://api.example.com/commands/invoices"), nameof(CreateInvoiceQueueCommand))
};
await queue.SendAsync(messages);
await foreach (var message in queue.ReceiveAsync())
{
if (message.Data is CreateInvoiceQueueCommand command)
{
_receivedInvoiceIds.Add(command.InvoiceId);
}
}
}
}
public sealed record CreateInvoiceQueueCommand(string InvoiceId) : Command;
Methods
| Name | Description |
|---|---|
| ReceiveAsync(Action<AsyncOptions>) | Receive one or more command(s) asynchronous using Point-to-Point Channel/P2P MEP. |
| SendAsync(IEnumerable<IMessage<ICommand>>, Action<AsyncOptions>) | Sends the specified |