Skip to content

HenrySaldanha/AspNet.SampleCQRS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

This repository aims to present a simple implementation of the CQRS pattern using MediatR.

Settings

In my Program class I added the following code snippets:

The AddMediatR function is responsible for setting the MediatR dependency injection into the solution. The parameter must be some object in the project where there are implementations of commands, queries and notifications.

public static void ConfigureServices(this IServiceCollection services)
{
        services.AddControllers();
        services.AddMediatR(typeof(CreateUserHandler).Assembly);
    ...
}

Command class implementation

I created the CreateUserCommand class for the command implementation. The purpose of this command is to create a new user. It is very important that we do not forget to implement the class IRequest and inform the return of our command, which in my case is User.

public class CreateUserCommand : IRequest<User>
{
    public CreateUserCommand(User user)
    {
        User = user;
    }

    public User User { get; set; }
}

Handler class implementation

The handler CreateUserHandler needs to implement the IRequestHandler interface and we need to set the input and output objects of the handler, in my case the input is the CreateUserCommand and the output is User.

public class CreateUserHandler : IRequestHandler<CreateUserCommand, User>
{
    public Task<User> Handle(CreateUserCommand request, CancellationToken cancellationToken)
    {
        request.User.Id = Guid.NewGuid();
        return Task.FromResult(request.User);
    }
}

Controller class implementation

To invoke the command we need to inject the dependency of the IMediator class and then use the Send() function passing a command as parameter. The [FromServices] tag injects a dependency on the IMediator.

    [HttpPost]
    public async Task<ActionResult> CreateUserAsync(
        [FromServices] IMediator _mediator,
        [FromBody] CreateUserRequest userRequest)
    {
        UserResponse user = await _mediator.Send(new CreateUserCommand(userRequest));

        ...
    }

Give a Star

If you found this Implementation helpful or used it in your Projects, do give it a star. Thanks!

This project was built with

My contacts

About

Simple implementation of the CQRS pattern using MediatR

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages