Giter Site home page Giter Site logo

bubdm / mediator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from simplesoft-pt/mediator

0.0 0.0 0.0 902 KB

Small .NET library that helps with the implementation of mediator pattern for commands, events and queries

License: MIT License

C# 100.00%

mediator's Introduction

Mediator

Small .NET library that helps with the implementation of mediator pattern for commands, events and queries.

Using a mediator instance, send commands, broadcast events and fetch queries from their respective generic handlers.

Articles

Installation

The library is available via NuGet packages:

NuGet Description Version
SimpleSoft.Mediator.Abstractions interfaces and abstract implementations (commands, events, queries, mediator, ...) NuGet
SimpleSoft.Mediator core implementation NuGet
SimpleSoft.Mediator.Microsoft.Extensions specialized methods and classes for the Microsoft dependency injection container and logging facades NuGet
SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline mediator pipeline to enforce SimpleSoft.Database transactions NuGet
SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline mediator pipeline to enforce Entity Framework Core transactions NuGet
SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline pipeline that serializes commands, queries, events and results into the logging NuGet
SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline pipeline that enforces validation into commands, queries and events before entering the handlers by using FluentValidation NuGet

Package Manager

Install-Package SimpleSoft.Mediator.Abstractions
Install-Package SimpleSoft.Mediator
Install-Package SimpleSoft.Mediator.Microsoft.Extensions
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline

.NET CLI

dotnet add package SimpleSoft.Mediator.Abstractions
dotnet add package SimpleSoft.Mediator
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline

Compatibility

This library is compatible with the following frameworks:

  • SimpleSoft.Mediator.Abstractions
    • .NET Framework 4.0+;
    • .NET Standard 1.0+;
  • SimpleSoft.Mediator
    • .NET Framework 4.0+;
    • .NET Standard 1.0+;
  • SimpleSoft.Mediator.Microsoft.Extensions
    • .NET Standard 1.1+;
  • SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline
    • .NET Standard 1.1+;
  • SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline
    • .NET Standard 1.3+;
  • SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline
    • .NET Standard 1.1+;
  • SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline
    • .NET Standard 1.1+;

Usage

Documentation is available via wiki or you can check the working examples or test code.

Here is an example of a command handler that also sends some events:

public class CreateUserCommand : Command {
  public string Email { get; set; }
  public string Password { get; set; }
}

public class UserCreatedEvent : Event {
  public User User { get; set; }
}

public class UserByIdQuery : Query<User> {
  public Guid UserId { get; set; }
}

public class User {
  public Guid Id { get; set; }
  public string Email { get; set; }
}

public class ExampleHandlers : ICommandHandler<CreateUserCommand>, IQueryHandler<UserByIdQuery,User> {
  
  private readonly IMediator _mediator;
  
  public UsersService(IMediator mediator) {
    _mediator = mediator;
  }
  
  public async Task HandleAsync(CreateUserCommand cmd, CancellationToken ct) {
    var userId = Guid.NewGuid();
    
    // try add the user to some store
    
    await _mediator.BroadcastAsync(new UserCreatedEvent {
      User = new User {
        Id = userId,
        Email = cmd.Email
      }
    }, ct);
  }
  
  public async Task<User> HandleAsync(UserByIdQuery query, CancellationToken ct) {
    User user = null;
    
    // search the store by user id
    
    return user;
  }
}

mediator's People

Contributors

gravity00 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.