GithubHelp home page GithubHelp logo

mikym / resultcqrs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 57 KB

Library featuring CQRS pattern and mediator implementations in conjuction with functional Results to achieve exception-safe operations.

License: MIT License

C# 100.00%

resultcqrs's Introduction

ResultCQRS

Build Status

Library featuring CQRS pattern and mediator implementations in conjuction with functional Results to achieve exception-safe operations.

Uses a functional Result approach for failure-prone operations.

To utilize all features using Autofac is required.

Features

  • ICommand and IQuery abstractions
  • ICommandHandler and IQueryHandler abstractions
  • ICommandDispatcher and IQueryDispatcher abstractions and default implementations
  • Supports decorators and adapters via Autofac's methods (for decorators with Microsoft's DI you may consider something like Scrutor)

Description

For both commands and queries, there are two return options - one that only returns a Result and one that returns an additional entity/DTO contained within the Result.

Every handler must return a Result struct which determines whether the operation succedeed or not, handlers may or may not return additional results contained within the Result struct.

Installation

To register the library with the DI container use the ContainerBuilder or IServiceCollection extension methods provided by the library:

builder.AddResultCQRS(assembliesToScan);

To register decorators (or adapters) use:

  1. the attributes provided by AttributeBasedRegistration
  2. or directly through Autofac, Scrutor or other libraries providing DI decoration support.

If you're using Autofac (or built-in attribute based Autofac) - you can register multiple decorators and they'll be applied in the order that you register them - read more at Autofac's docs regarding decorators and adapters.

Documentation

Documentation available at https://result-cqrs.mikym.me/.

Example usage

Library offers a simple error catching and logging within the provided default Dispatcher implementations.

A command without a concrete result:

public record SimpleCommand : ICommand
{
    public bool IsSuccess { get; }
    
    public SimpleCommand(bool isSuccess = true)
        => IsSuccess = isSuccess;
}

And a handler that handles it:

public class SimpleCommandHandler : ICommandHandler<SimpleCommand>
{
    public async Task<Result> HandleAsync(SimpleCommand command)
    {
        if (command.IsSuccess)
            return Result.FromSuccess();
            
        return new InvalidOperationError();
    }
}

A command with a concrete result:

public record SimpleCommandWithConcreteResult : ICommand<int>
{
    public bool IsSuccess { get; }
    
    public SimpleCommandWithConcreteResult(bool isSuccess = true)
        => IsSuccess = isSuccess;
}

And a handler that handles it:

public class SimpleCommandHandlerWithConcreteResult : ICommandHandler<SimpleCommandWithConcreteResult, int>
{
    public async Task<Result<int>> HandleAsync(SimpleCommandWithConcreteResult command)
    {
        if (command.IsSuccess)
            return 1;
            
        return new InvalidOperationError();
    }
}

Then in your service you can use:

public class Service : IService
{
    private readonly ICommandDispatcher _commandDispatcher;

    public Service(ICommandDispatcher commandDispatcher)
        => _commandDispatcher = commandDispatcher;

    public async Task<Result> DoAsync()
    {
        var result = await _commandDispatcher.DispatchAsync(command)
            
        return new InvalidOperationError();
    }
}

Using queries is pretty much same.

resultcqrs's People

Contributors

mikym avatar

Watchers

 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.