$30 off During Our Annual Pro Sale. View Details »

Introduction to MediatR

Introduction to MediatR

Presentation made at Criteo Full-Stack Tech Talks meetup!

Abstract:
CQRS, Messaging Handling, you do it in your .Net Core project and you don’t know MediatR?
This library developed by the American MVP Jimmy Bogard contains everything you need to organize your project correctly. In this presentation, we will focus on the issue dealt with by MediatR.
We will then discuss the capabilities of the library and its implementation to craft microservices in DDD.

[email protected]

March 14, 2019
Tweet

Other Decks in Programming

Transcript

  1. Criteo FullStack Tech Talks March, 2019

    View Slide

  2. Premier Field Engineer
    @spontoreau
    In ❤️ with
    Co-organizer

    View Slide

  3. View Slide

  4. View Slide

  5. Mediator
    obj obj
    obj obj
    obj obj
    obj obj

    View Slide

  6. View Slide

  7. Query
    Just a read operation
    Command
    Data validation
    Business logic
    Persistance

    View Slide

  8. GetOrderById
    Query
    UpdateProduct
    Command
    GetProducts
    Query
    DeleteOrder
    Command
    … …
    Dispatcher
    QueryHandler
    Read
    DB
    Dispatcher
    QueryHandler
    QueryHandler
    QueryHandler
    QueryHandler
    QueryHandler
    CommandHandler
    CommandHandler
    CommandHandler
    CommandHandler
    CommandHandler
    CommandHandler
    Write
    DB

    View Slide

  9. public class GetUserById : IRequest
    {
    public Guid Id { get; }
    public GetUserById(Guid id)
    {
    Id = id;
    }
    }
    public class DeleteUser : IRequest
    {
    public Guid Id { get; }
    public DeleteUser(Guid id)
    {
    Id = id;
    }
    }

    View Slide

  10. public class GetUserByIdHandler : IRequestHandler
    {
    public Task Handle(GetUserById request, CancellationToken cancellationToken)
    {
    // …
    }
    }

    View Slide

  11. IServiceCollection services = new ServiceCollection();
    services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);
    IServiceProvider provider = services.BuildServiceProvider();
    IMediator mediator = provider.GetService();
    User user = mediator.send(new GetUserById(
    "052ee87a-8ff5-4aa6-8f0d-3cd417e34e74"
    ));

    View Slide

  12. View Slide

  13. https://github.com/jbogard/MediatR
    https://ardalis.com/using-mediatr-in-aspnet-core-apps
    https://en.wikipedia.org/wiki/Mediator_pattern
    https://www.martinfowler.com/bliki/CQRS.html
    https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs
    https://github.com/spontoreau/MediatorPatternExample
    https://github.com/spontoreau/DemoMediatR

    View Slide

  14. View Slide

  15. View Slide