CodeCorrectCollective.Blocks.InProcBus 1.0.1

Prefix Reserved
dotnet add package CodeCorrectCollective.Blocks.InProcBus --version 1.0.1
                    
NuGet\Install-Package CodeCorrectCollective.Blocks.InProcBus -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeCorrectCollective.Blocks.InProcBus" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeCorrectCollective.Blocks.InProcBus" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CodeCorrectCollective.Blocks.InProcBus" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeCorrectCollective.Blocks.InProcBus --version 1.0.1
                    
#r "nuget: CodeCorrectCollective.Blocks.InProcBus, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CodeCorrectCollective.Blocks.InProcBus@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeCorrectCollective.Blocks.InProcBus&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=CodeCorrectCollective.Blocks.InProcBus&version=1.0.1
                    
Install as a Cake Tool

Overview

Build License Contributor Covenant

A library wrapper around Mediatr to manage executing commands, queries and events.

Commands

Commands are used to modify state in the system. As such, they do no return any values.

A Command Object

We use record types as the foundation for our command objects that are used to pass data to command processors.

public record CreateBlogCommand(
    string BlogName,
    string CorrelationId
) : DomainCommandBase(CorrelationId);

A Command Processor

We use class types as the foundation for our command processors.

public class CreateBlogCommandProcessor(
    IUnitOfWork uow,
    IDomainEventBus eventBus,
    ILogger<CreateBlogCommandProcessor> logger
) : DomainCommandProcessorBase<CreateBlogCommand>(logger)
{
    private readonly IUnitOfWork uow = uow;

    private readonly IDomainEventBus eventBus = eventBus;

    /// <inheritdoc/>
    public override async Task ProcessAsync(
        [NotNull] CreateBlogCommand command,
        CancellationToken cancellationToken
    )
    {
        ArgumentNullException.ThrowIfNull(command, nameof(command));

        Blog blog = new()
        {
            Name = command.BlogName
        };

        await this.uow.BlogRepository.AddAsync(blog, cancellationToken).ConfigureAwait(false);
        _ = await this.uow.CommitAsync(cancellationToken).ConfigureAwait(false);

        await this.eventBus.PublishAsync(
            cancellationToken,
            new BlogCreatedEvent(blog, command.CorrelationId)
        ).ConfigureAwait(false);
    }
}

Events

Events are used to send notifications that the system should handle.

An Event Object

Again we use recrord types to represent our event objects

public record class BlogCreatedEvent(
    Blog Blog,
    string CorrelationId
) : DomainEventBase(CorrelationId);
An Event Processor

We use a class type to represent our event processors

public class BlogCreatedEventProcessor(
    IUnitOfWork uow,
    ILogger<BlogCreatedEventProcessor> logger
) : DomainEventProcessorBase<BlogCreatedEvent>(logger)
{
    private readonly IUnitOfWork uow = uow;

    /// <inheritdoc/>
    public override async Task ProcessEventAsync(
        [NotNull] BlogCreatedEvent @event,
        CancellationToken cancellationToken
    )
    {
        ArgumentNullException.ThrowIfNull(@event, nameof(@event));

        await this.DoesSomeWork(@event);
    }

    private Task DoesSomeWork(BlogCreatedEvent @event)
    {
        // process the BlogCreatedEvent in some way.
    }
}

Queries

Queries are used to fetch data from the system in a well defined way.

A Query Object

public record class GetBlogByNameQuery(
    string BlogName,
    string CorrelationId
) : DomainQueryBase<Blog?>(CorrelationId);
A Query Processor

Again we use a class for our query processor object.

Note that DomainQueryProcessorBase<TQuery, TResult> takes a typeparam for both the query object and the expected return result of the query.

public class GetBlogByNameQueryProcessor(
    IUnitOfWork uow,
    ILogger<GetBlogByNameQueryProcessor> logger
) : DomainQueryProcessorBase<GetBlogByNameQuery, Blog?>(logger)
{
    private IUnitOfWork uow = uow;

    /// <inheritdoc/>
    public override async Task<Blog?> ProcessQueryAsync(
        [NotNull] GetBlogByNameQuery query,
        CancellationToken cancellationToken
    )
    {
        ArgumentNullException.ThrowIfNull(query, nameof(query));
        return await this.uow.BlogRepository.FindByNameAsync(
            query.BlogName,
            cancellationToken).ConfigureAwait(false);
    }
}

Registering the Events, Commands, and Queries with the system.

Perform the following registration code where you normally configure your application services, in Program.cs for example.

// Register In-Proc Bus
services.AddInProcBus()
    .AddCommands()
    // Needs the command and related processor types.
    .AddCommand<CreateBlogCommand, CreateBlogCommandProcessor>()  
    .And()
    .AddEvents()
    // Needs the event and related processor types.
    .AddEvent<BlogCreatedEvent, BlogCreatedEventProcessor>() 
    .And()
    .AddQueries()
    // Needs, the query object, result type, and related processor
    .AddQuery<GetBlogByNameQuery, Blog?, GetBlogByNameQueryProcessor>();  
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 218 3/18/2025
1.0.0 155 3/18/2025

Initial Release