MSHelper.CQRS.Commands 1.0.0

dotnet add package MSHelper.CQRS.Commands --version 1.0.0
NuGet\Install-Package MSHelper.CQRS.Commands -Version 1.0.0
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="MSHelper.CQRS.Commands" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MSHelper.CQRS.Commands --version 1.0.0
#r "nuget: MSHelper.CQRS.Commands, 1.0.0"
#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.
// Install MSHelper.CQRS.Commands as a Cake Addin
#addin nuget:?package=MSHelper.CQRS.Commands&version=1.0.0

// Install MSHelper.CQRS.Commands as a Cake Tool
#tool nuget:?package=MSHelper.CQRS.Commands&version=1.0.0

MSHelper.CQRS.Commands : Commands

⭐ Star us on GitHub � it motivates us a lot!

Overview

Adds an ability to create and process Commands in the sense of CQRS.

Installation

This document is for the latest MSHelper.CQRS.Commands 1.0.0 release and later.

dotnet add package MSHelper.CQRS.Commands

Dependencies

-- MSHelper

Usage

Implement ICommand (marker) interface in the selected class. Since the command represents the user�s intention you should follow the convention:

--- keep all the commands immutable --- name of your commands should be imperative

public class CreateAccount : ICommand
{
    public Guid Id { get; }
    public string Email { get; }
    public string Password { get; }

    public CreateUser(id, email, password)
    {
        Id = id;
        Email = email;
        Password = password;
    }
}

Create dedicated command handler class that implements ICommandHandler<TCommand> interface with HandleAsync() method:

public class CreateAccountHandler : ICommandHandler<CreateAccount>
{
    public Task HandleAsync(CreateAccount command)
    {
        //put the handling code here
    }
}

You can easily register all command handlers in DI container by calling AddCommandHandlers() method on IMSHelperBuilder:

public IServiceProvider ConfigureServices(this IServiceCollection services)
{
    var builder = services.AddMSHelper()
        .AddCommandHandlers();

    //other registrations    
    return builder.Build();
}

Dispatching a particular command object can be also done using MSHelper package. Start with registering in-memory dispatcher on your IMSHelperBuilder by calling a AddInMemoryCommandDispatcher() method:

public IServiceProvider ConfigureServices(this IServiceCollection services)
{
    var builder = services.AddMSHelper()
        .AddCommandHandlers()
        .AddInMemoryCommandDispatcher();

    //other registrations    
    return builder.Build();
}

Then simply inject ICommandDispatcher into a class and call DispatchAsync() method:

public class AccountsService
{
    private readonly ICommandDispatcher  _dispatcher;

    public AccountsService(ICommandDispatcher  dispatcher)
    {
        _dispatcher = dispatcher;
    } 

    public Task<AccountDto> GetAccountAsync(Guid id)
        => _dispatcher.DispatchAsync(new GetAccount { Id = id });
}

Important Note:

All the MSHelper packages are for self learning purposes inspired by Devmentors.io

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on MSHelper.CQRS.Commands:

Package Downloads
MSHelper.WebApi.CQRS

MSHelper.WebApi.CQRS - CQRS Integration.

MSHelper.Logging.CQRS

MSHelper.Logging.CQRS - Centralized logging for CQRS with Serilog and Seq.

MSHelper.MessageBrokers.CQRS

MSHelper.MessageBrokers.CQRS - Asynchronous messaging for CQRS using RabbitMQ.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 266 10/25/2022