Giter Site home page Giter Site logo

grandchamp / commandscaler Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 1.0 53 KB

A simple command handler pattern implementation, but fully scalable with RabbitMQ

License: MIT License

C# 100.00%
dotnet netcore aspnet aspnet-core dotnet-core command-pattern mediator rabbitmq csharp

commandscaler's Introduction

CommandScaler

A simple command handler pattern implementation, but fully scalable with RabbitMQ (maybe more?).

This app was heavily inspired by the amazing https://github.com/jbogard/MediatR.

With this app is possible to scale your command handlers to multiple machines (or processes) by using RabbitMQ as the responsible to distribute your commands.

Configuring

1. Configure RabbitConfiguration class on your Startup.cs

services.Configure<RabbitConfiguration>(Configuration.GetSection("RabbitMQ"));

JSON

{
  "RabbitMQ": {
    "Host": "127.0.0.1",
    "User": "admin",
    "Password": "123456"
  }
}

2. Add CommandScaler stuff

services.AddCommandScaler(new[] { typeof(YourCommand).Assembly })
        .AddRabbitMQCommandScaler()
        .ConfigureRabbitMQHandler();

For AddCommandScaler method you should pass the assemblies where your ICommandHandler are defined (you can have multiple here), so the lib will be able to scan and register them.

3. Start the CommandScaler handler

3.1 Start just the IBus

On your Configure method on Startup.cs add:

await app.StartCommandScaler();

3.2 Start IBus and RabbitGenericHandler

await app.StartRabbitMQHandler();

This will start the listener.

Using

To start using, you should inject IBus interface when you need to send commands:


    public class TestController : Controller
    {
        private readonly IBus _bus;
        public TestController(IBus bus)
        {
            _bus = bus;
        }

        [HttpGet]
        public async Task<IHttpActionResult> Test()
        {
            var result = await _bus.Send(new TestCommand { MyValue = 1 });
            return Ok(result);
        }
    }

    public class TestCommand
    {
        public int MyValue { get; set; }
    }

    public class TestCommandHandler : ICommandHandler<TestCommand, int>
    {
        public Task<int> Handle(TestCommand command)
        {
            return command.MyValue;
        }
    }

On result you'll have the value returned from the Handle method of your ICommandHandler.

Check the samples if you have any doubt.

commandscaler's People

Contributors

grandchamp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

1rhotquadwdiayo

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.