Hoppa till innehåll

Commands and commandHandlers

CQRS and Event sourcing – Resources

When looking at this video walk-through of OAuth Event Modeling in Go by Jamie there was a question about how to handle commands without the need of switches over the command types.

So my reply on that got a little longer and thought I would share the resources I pointed to here (to free the comment from being stuck in youtube)

Great example and walk through Jamie! (and thanks to the rest of you for value provided)

Regarding handling events without the switch, I do not know Go – but in C# you usually register EventHandlers with commands and you will have a dispatcher or similar as a single entry point where the handlers knows what commands they should act on. see examples in referred links below.

So, maybe this is not for Go devs, but you might get some ideas from it, anyhow.. here are some links for c# resources.

SuperSafeBank

”This repository shows how to implement Event Sourcing, CQRS and DDD in .NET Core, using a Bank as example.”

This (C#) repository looks like a great example application ( a little more complexity, I haven’t dived deep into it yet) https://github.com/mizrael/SuperSafeBank

CQRS in Practice (course and code)

Vladimir Khorikov, my go to guy when looking for quality examples, and learning the craft in C#, shares some high quality stuff (pluralsight corses and blog https://enterprisecraftsmanship.com/posts/types-of-cqrs/)

In this repository, https://github.com/vkhorikov/CqrsInPractice/blob/master/After (from the Pluralisght course CQRS in Practice) – you can see an implementation example; I’ll try to flesh out the command and commandhandler stuff for you here.

I like to look at things in the order of ”from entry point in the API”, Hope you can follow my logic =)

Command to Result flow.

API

start in ’Api\Controllers\StudentController.cs’.Register(…)

//called from UI to register student
Register(NewStudentDto dto)
            var command = new RegisterCommand(...); // (Logic\AppServices\RegisterCommand.cs)
            Result result = _messages.Dispatch(command); //finds handler, handles the command and returns the result.

The message Dispatcher

in ’Logic\Utils\Messages.cs’

in ’Messages.Dispatch(command)’ the matching handler for the command is looked up, via DI, and then executed (aka handled).

handler = _provider.GetService(handlerType); // System.IServiceProvider (built in)
result = handler.Handle(command);

The Registration of handlers

Startup.cs

DI, Dependency Injection, is configured in ’API\Startup.cs’ and handlers, for commands and queries; these are registered via services.AddHandlers() – where ’services’ is ’Microsoft.Extensions.DependencyInjection’

HandlerRegistration

’Api\Utils\HandlerRegistration.cs’.AddHandlers()

  • This class takes care of finding, and registering, all handlers of type ICommandHandler and IQueryHandler in the assembly,
  • making them available for use from IServiceProvider.GetService(serviceType)