Skip to content

samw2703/CommandBus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CommandBus

Provides a simple reflection based service setup for commands within a CQRS architecture.

How to use

Simply define your command and a corresponding CommandHandler...

public class CreateFooCommand
{
    public string Name { get; }

    public CreateFooCommand(string name)
    {
        Name = name;
    }
}

internal class CreateFooCommandHandler : CommandHandler<CreateFooCommand>
{
    public override Task Handle(CreateFooCommand command)
    {
        //Implement Logic
    }
}

Optionally you can also define a custom command result...

public class DeleteFooCommand
{
    public int Id { get; }

    public DeleteFooCommand(int id)
    {
        Id = id;
    }
}

public class DeleteFooCommandResult
{
    public bool Success { get; set; }
}

internal class DeleteFooCommandHandler : CommandHandler<DeleteFooCommand, DeleteFooCommandResult>
{
    public override Task<DeleteFooCommandResult> HandleAndGetResult(DeleteFooCommand command)
    {
        //Implement Logic
    }
}

You can also define a validator that will execute before the handler...

public class UpdateFooCommand
{
    public int Id { get; }
    public string Name { get; }

    public UpdateFooCommand(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

internal class UpdateFooCommandValidator : Validator<UpdateFooCommand>
{
    public override Task Validate(UpdateFooCommand command)
    {
        if (command.Id == 0)
            AddErrorMessage("Invalid id provided");
    }
}

internal class UpdateFooCommandHandler : CommandHandler<UpdateFooCommand>
{
    public override Task Handle(UpdateFooCommand command)
    {
        //Implement Logic
    }
}

Then use the provided IServiceCollection extension method, passing in the assemblies containing your commands...

var sc = new ServiceCollection();
sc.AddCommandBus(typeof(CreateFooCommand).Assembly, typeof(DeleteFooCommand).Assembly, typeof(UpdateFooCommand).Assembly);

You can then use the service provider to get your ICommandBus that can be used to execute your commands...

var sp = sc.BuildServiceProvider();
var commandBus = sp.GetRequiredService<ICommandBus>();

var createFooResult = commandBus.Execute(new CreateFooCommand("Foo"));
var updateFooResult = commandBus.Execute(new UpdateFooCommand(1, "Foo"));
var deleteFooResult = commandBus.Execute<DeleteFooCommand, DeleteFooCommandResult>(new DeleteFooCommand(1));

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages