Skip to content

The Scribbly broker is a framework for publishing messages in memory through an application utilizing the Mediator design pattern.

License

Notifications You must be signed in to change notification settings

Scribbly-Fun/Scribbly.Broker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

scribbly_banner.png

GitHub GitHub all releases Nuget GitHub issues GitHub Repo stars GitHub forks Tests GitHub last commit (branch)

Scribbly Broker

A publisher used for commands and queries based on the Mediator Design Pattern.

Static Badge Static Badge Static Badge

%% sequence displaying pipeline

sequenceDiagram
    Publish-->>Behavior: wraps handler with middleware
    Behavior-->>Handler: process notification
    Handler-->>Publisher: returns to caller
Loading

Table of Contents

  1. 🎁 Packages
  2. πŸ’ͺ Notifications
  3. πŸ›’ Handlers
  4. πŸ› Behaviors
  5. πŸ› Pipelines
  6. πŸŽ‰ Hosting

Example

Below is a brief snip of code to get you started before reading more.

  1. Add a reference to the Scribbly.Broker.MicrosoftHosting package
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddScribblyBroker(options =>
{
    options.AsScoped = true;

    options.AddHandlersFromAssembly<Program>();

    options
        .AddBehavior<TracingBehavior>()
        .AddBehavior<ExceptionBehavior>();
});
  1. Create some notifications INotification
public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) : INotification
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
  1. Create some queries INotification<TResult> (these accept data and return a result)
public record WeatherQuery(string Summary) : INotification<WeatherForecast>;
  1. Create handlers INotificationHandler for the notifications
public sealed class WeatherForecastHandler(ILogger<WeatherForecastHandler> logger) : INotificationHandler<WeatherForecast> 
{
    /// <inheritdoc />
    public Task Handle(WeatherForecast notification, CancellationToken cancellationToken = default)
    {
        logger.LogInformation("Handled Weather: {Weather}", notification);
        return Task.CompletedTask;
    }
}
  1. Create query handlers INotificationHandler for the messages that return results
public sealed class RandomQueryHandler: INotificationHandler<WeatherQuery, WeatherForecast>
{
    private static Random Random = new Random();
    
    /// <inheritdoc />
    public Task<WeatherForecast> Handle(WeatherQuery notification, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Next(1, 20))),
            Random.Next(12, 55),
            notification.Summary
        ));
    }
}
  1. Publish and query
app.MapGet("/weather", async (IBrokerStream streamer, IBrokerPublisher publisher) =>
{
    var forecasts = new List<WeatherForecast>();

    foreach (var summary in summaries)
    {
        await foreach (var forecast in streamer.QueryStream<WeatherQuery, WeatherForecast>(new WeatherQuery(summary)))
        {
            forecasts.Add(forecast);

            await publisher.Publish(forecast);
        }
    }
    
    return forecasts;
});

🎁 Packages

Scribbly.Broker.Contract

Simply public interfaces and delegates used to interface with the framework.

Scribbly.Broker

The core framework executing and routing notificaitons to handlers.

Scribbly.Broker.MicrosoftHosting

A wraper leveraging the Microsoft DI container to resolve services and handlers.

Scribbly.Broker.OpenTelemetry

An extension to allow asynconouse tracing and message tracking through the Broker framework

πŸ’ͺ Notifcations

INotification

πŸ›’ Handlers

INotificationHandler

πŸ› Behaviors

IBrokerBehavior

πŸ› Pipelines

INotificationPipeline

IQueryPipeline

πŸŽ‰ Hosting

AddScribblyBroker

About

The Scribbly broker is a framework for publishing messages in memory through an application utilizing the Mediator design pattern.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages