Table of Contents

Class InMemoryCommandQueue

Namespace
Savvyio.Commands.Messaging
Assembly
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
InMemoryCommandQueue
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

ReceiveAsync(Action<AsyncOptions>)

Receive one or more command(s) asynchronous using Point-to-Point Channel/P2P MEP.

public IAsyncEnumerable<IMessage<ICommand>> ReceiveAsync(Action<AsyncOptions> setup = null)

Parameters

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

IAsyncEnumerable<IMessage<ICommand>>

A task that represents the asynchronous operation. The task result contains a sequence of IMessage<T> whose generic type argument is ICommand.

SendAsync(IEnumerable<IMessage<ICommand>>, Action<AsyncOptions>)

Sends the specified messages whose generic type argument is ICommand asynchronous using Point-to-Point Channel/P2P MEP.

public Task SendAsync(IEnumerable<IMessage<ICommand>> messages, Action<AsyncOptions> setup = null)

Parameters

messages IEnumerable<IMessage<ICommand>>

The ICommand enclosed messages to send.

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

Task

A Task that represents the asynchronous operation.