Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQRS and Commands Java + Spring similar to MediatR

Tags:

java

spring

cqrs

While working on a .NET project I come across a library MediatR which made CQRS and Commands simple to implement. I really like using commands and commands handlers as I've worked on far too many projects that have giant procedural style service classes that inject way to many dependencies making unit testing painful. I am looking for something similar to MediatR for Spring + Java. Essentially I would like to inject a single dependency into the controller class and have it delegate commands to the appropriate command handler. I provided a few snippets of what MediatR looks like below. I prefer the way mediator does it as injecting the CommandHandlers into the controller class can lead to the same issue with the class having tons of dependencies injected.

I've came across this library but it seems more like a side project that something that is production ready. https://github.com/sleroy/spring-cqrs-arch. I am aware of the Axon framework, but I'm not looking to go full blown event sourcing at this point. Are there any libraries out there already for this that maybe I'm haven't stumbled across yet? I suppose I could just use the Guava EventBus.

Below is a C# example of what MediatR usage looks like.

Controller

namespace DAB.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PersonController : ControllerBase
    {
        private readonly IMediator mediator;

        public PersonController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        // GET api/values
        [HttpPut("{id}/changename")]
        public async Task<ActionResult> ChangeName([FromBody] ChangeNameCommand command)
        {
            await this.mediator.Send(command);
            return Ok();
        }
    }
}

Command

public class ChangeNameCommand: IRequest<bool>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

CommandHandler

public class ChangeNameHandler: IRequestHandler<ChangeNameCommand, bool>
{
    public Task<bool> Handle(ChangeNameCommand request, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Changing name to {request.FirstName} {request.LastName}");
        return Task.FromResult(true);
    }
}
like image 284
jkratz55 Avatar asked Oct 08 '18 14:10

jkratz55


People also ask

What is CQRS and MediatR?

How MediatR facilitates CQRS and Mediator Patterns. You can think of MediatR as an “in-process” Mediator implementation, that helps us build CQRS systems. All communication between the user interface and the data store happens via MediatR. The term “in process” is an important limitation here.

Is MediatR an anti pattern?

Most of the time it's used like a glorified Service Locator, which is notoriously an anti-pattern. Actually, no, it's never used as a service locator. No one uses the IMediator interface to locate services. They use it to dispatch requests and notifications to handlers.

Do you need MediatR?

MediatR provides you with other functionality as well. It supports notifications mechanism. It may be very useful if you use domain events in your architecture. All classes of your events must implement INotification marker interface.


1 Answers

Check out PipelinR. It's 15KB, zero-dependency library, with a nice Spring and Spring Boot integration.

like image 118
Eduards Sizovs Avatar answered Nov 12 '22 00:11

Eduards Sizovs