Skip to content

CQRS-Muflone/Muflone.Transport.InMemory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Muflone.Transport.InMemory

NuGet License: MIT

InMemory transport for Muflone, providing an in-process service bus to dispatch commands and publish events without any external broker dependency. Ideal for development, testing, and lightweight production scenarios.


Requirements

  • .NET 10.0+
  • Muflone 10.0.1+

Installation

Package Manager

Install-Package Muflone.Transport.InMemory

.NET CLI

dotnet add package Muflone.Transport.InMemory

Getting Started

1. Register the transport

Call AddMufloneTransportInMemory on your IServiceCollection during application startup:

builder.Services.AddMufloneTransportInMemory();

This registers the following services:

Service Implementation Lifetime
IMessageSubscriber InMemorySubscriber Singleton
IServiceBus InMemoryBus Singleton
IEventBus InMemoryBus Singleton
MessageHandlersStarter Hosted service

2. Send a command

Inject IServiceBus and call SendAsync:

public class MyService(IServiceBus serviceBus)
{
    public async Task DoSomethingAsync(CancellationToken cancellationToken)
    {
        var command = new MyCommand(Guid.NewGuid());
        await serviceBus.SendAsync(command, cancellationToken);
    }
}

3. Publish an event

Inject IEventBus and call PublishAsync:

public class MyService(IEventBus eventBus)
{
    public async Task NotifyAsync(CancellationToken cancellationToken)
    {
        var @event = new MyEvent(Guid.NewGuid());
        await eventBus.PublishAsync(@event, cancellationToken);
    }
}

How It Works

InMemoryBus

InMemoryBus implements both IServiceBus (commands) and IEventBus (events). When a message is dispatched, it looks up registered subscribers via InMemorySubscriber and writes the message directly into each subscriber's .NET Channel<object>.

InMemorySubscriber

InMemorySubscriber maintains a ConcurrentDictionary mapping message types to their IInMemoryChannel instances. Each subscriber gets an unbounded System.Threading.Channels.Channel that is read asynchronously and forwarded to the corresponding handler. Namespace-agnostic lookup is also supported, so commands/events with the same type name but different namespaces are matched correctly.

MufloneBroker (static)

MufloneBroker provides a lightweight static alternative that stores dispatched messages in ObservableCollection properties — useful in tests or simple scenarios:

MufloneBroker.Send(command);    // adds to MufloneBroker.Commands
MufloneBroker.Publish(@event);  // adds to MufloneBroker.Events

Sample Project

See a working example at BrewUp.


Authors

  • Alberto Acerbis
  • Alessandro Colla

License

This project is licensed under the MIT License.

About

Muflone extension to manage queues, and topics in memory.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages