Skip to content

Repository files navigation

Build

BranchStatus
MasterBuild status
DevBuild status

Table of contents

Overview

Simple CQRS library

This project composes of components for implementing the CQRS pattern (Command Handling). This library was built with simplicity, modularity and pluggability in mind.

Features

  • Send commands to registered command handlers.
  • Provides simple abstraction for hosted command handlers which can be registered just like an regular command handler.
  • Multiple ways of registering command handlers:
    • Simple handler registration (no IoC container).

    • IoC container registration

      • achieved by creating implementations of IContainerAdapter or using pre-made extensions packages for supported containers:
        • Microsoft.DependencyInjection

          NuGet

        • SimpleInjector

          NuGet

        • Autofac

          NuGet

    • Attribute registration

Installation

You can simply clone this repository, build the source, reference the dll from the project, and code away!

Xer.Cqrs.CommandStack library is available as a Nuget package:

NuGet

To install Nuget packages:

  1. Open command prompt
  2. Go to project directory
  3. Add the packages to the project:
    dotnetadd package Xer.Cqrs.CommandStack
  4. Restore the packages:
    dotnetrestore

Getting Started

(Samples are in ASP.NET Core)

Sample Command and Command Handler

// Example command.publicclassRegisterProductCommand{publicintProductId{get;}publicstringProductName{get;}publicRegisterProductCommand(intproductId,stringproductName){ProductId=productId;ProductName=productName;}}// Command handler.publicclassRegisterProductCommandHandler:ICommandAsyncHandler<RegisterProductCommand>{privatereadonlyIProductRepository_productRepository;publicRegisterProductCommandHandler(IProductRepositoryproductRepository){_productRepository=productRepository;}[CommandHandler]// This is in Xer.Cqrs.CommandStack.Extensions.Attributes. This allows the method to registered as a command handler through attribute registration.publicTaskHandleAsync(RegisterProductCommandcommand,CancellationTokencancellationToken=default(CancellationToken)){return_productRepository.SaveAsync(newProduct(command.ProductId,command.ProductName));}}

Command Handler Registration

Before we can delegate any commands, first we need to register our command handlers. There are several ways to do this:

1. Simple Registration (No IoC container)

// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){ ...// Repository.services.AddSingleton<IProductRepository,InMemoryProductRepository>();// Register command delegator.services.AddSingleton<CommandDelegator>((serviceProvider)=>{// Allows registration of a single message handler per message type.varregistration=newSingleMessageHandlerRegistration();registration.RegisterCommandHandler(()=>newRegisterProductCommandHandler(serviceProvider.GetRequiredService<IProductRepository>()));returnnewCommandDelegator(registration.BuildMessageHandlerResolver());});
...}

2. Container Registration

// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){ ...// Repository.services.AddSingleton<IProductRepository,InMemoryProductRepository>();// Register command handlers to the container. // The AddCqrs extension method is in Xer.Cqrs.Extensions.Microsoft.DependencyInjection package.services.AddCqrs(typeof(RegisterProductCommandHandler).Assembly);
...}

Delegating Commands To Command Handlers

After setting up the command delegator in the IoC container, commands can now be delegated by simply doing:

...
private readonly CommandDelegator _commandDelegator;publicProductsController(CommandDelegator commandDelegator){_commandDelegator=commandDelegator;}// POST api/products[HttpPost]publicasyncTask<IActionResult>RegisterProduct([FromBody]RegisterProductCommandDtomodel){RegisterProductCommandcommand=model.ToDomainCommand();await_commandDelegator.SendAsync(command);returnAccepted();}
...

About

A lightweight and easy-to-use CQRS command handling library

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages