Skip to content

An implementation of CQRS with configuring pipelines via GreenPipes

License

Notifications You must be signed in to change notification settings

velunin/CqrsVibe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CqrsVibe

build nuget version license

CqrsVibe is an implementation of CQRS with pipelines via GreenPipes

Getting started

Install CqrsVibe package with Microsoft DI abstractions support

Install-Package CqrsVibe.MicrosoftDependencyInjection

Register and configure CqrsVibe services:

services.AddCqrsVibe(options =>
{
    options.CommandsCfg = (provider, cfg) =>
    {
        //Commands handling pipeline configuration
        cfg.UseInlineFilter(async (context, next) =>
        {
            //Pre-processing logic

            await next.Send(context);

            //Post-processing logic
        });
    };
    options.QueriesCfg = (provider, cfg) =>
    {
        //Queries handling pipeline configuration
    };
    options.EventsCfg = (provider, cfg) =>
    {
        //Events handling pipeline configuration
    };
});

Register handlers:

services.AddCqrsVibeHandlers(
    fromAssemblies:new []
    {
        typeof(SomeCommand).Assembly
    });

Usage

Commands processing

Use ProcessAsync method of ICommandProcessor for performing command

//without result
await commandProcessor.ProcessAsync(new SomeCommand(), cancellationToken);

//with result
var result = await commandProcessor.ProcessAsync(new SomeCommandWithResult(), cancellationToken);

Command

public class SomeCommand : ICommand
{
}

public class SomeCommandHandler : ICommandHandler<SomeCommand>
{
    public Task HandleAsync(
        ICommandHandlingContext<SomeCommand> context, 
        CancellationToken cancellationToken = default)
    {
        return Task.CompletedTask;
    }
}

Command with result

public class SomeCommandWithResult : ICommand<string>
{
}

public class class SomeCommandWithResultHandler : ICommandHandler<SomeCommandWithResult, string>
{
    public Task<string> HandleAsync(
        ICommandHandlingContext<SomeCommandWithResult> context, 
        CancellationToken cancellationToken = default)
    {
        return Task.FromResult("Some command result");
    }
}

Queries execution

Call the QueryAsync method of IQueryService to execute your queries

var result = await queryService.QueryAsync(new SomeQuery(), cancellationToken);

Query sample

public class SomeQuery : IQuery<string>
{
    public SomeQuery(string someQueryParam)
    {
        SomeQueryParam = someQueryParam;
    }

    public string SomeQueryParam { get; }
}

public class SomeQueryHandler : IQueryHandler<SomeQuery, string>
{
    public Task<string> HandleAsync(
        IQueryHandlingContext<SomeQuery> context, 
        CancellationToken cancellationToken = default)
    {
        return Task.FromResult("Some query result");
    }
}

More usage examples: https://github.com/velunin/CqrsVibe/tree/master/samples