Code Monkey home page Code Monkey logo

bartender's Introduction

Bartender

Build status NuGet version

Bartender is a CQRS propose without any dependencies and available for all netstandard 1.1 compliant platform.

Installation

PM> Install-Package Bartender

Features

  • Synchronous/Asynchronous message dispatching
  • Suit well with IoC
  • Simple Publish-Subscribe mode
  • Automatic validation

Getting started

Getting started

Dependencies

First thing you have to do with Bartender is to implement the IDependencyContainer interface and register it with a IoC container.

You also need to register your handlers (IHandler, IAsyncHandler, ICancellableAsyncHandler) and bind dispatch interfaces (IDispatcher, IAsyncDispatcher, ICancellableAsyncDispatcher) to the Dispatcher class.

This step really depend on which IoC container you use in your project. Check out project samples, you can find dependencies registration with StructureMap here

Write a message and handle it !

A message is just an implementation of the IMessage interface. You can consider the interface to work as a marker and it never impose to write specific code. When mark as IMessage an Object can be dispatched with the library. For exemple if you want to retreive informations of a person who's got an identifier, you can define a message like this :

public class GetPersonByIdQuery : IMessage
{
    public int Id { get; }
    
    public GetPersonById(int id)
    {
        Id = id;
    }
}

By convention in CQRS when you define a message to read data it have to be suffix with Query and by opposition Command define a write operation message. The library don't force you to respect this principle but it is a good way to organise your project.

When your message is define, you have to write an handler for it. Handler can be implement with synchronous or ascynchronous execution :

//Synchronous handler
public class GetPersonByIdQueryHandler : IHandler<GetPersonByIdQuery, GetPersonReadModel>
{
    public GetPersonReadModel Handle(GetPersonByIdQuery message)
    {
        //add you logic code here
    }
}

//Asynchronous handler
public class GetPersonByIdQueryHandler : IAsyncHandler<GetPersonByIdQuery, GetPersonReadModel>
{
    public GetPersonReadModel HandleAsync(GetPersonByIdQuery message)
    {
        //add you logic code here
    }
}

The nature of queries is to retreive data, but for a command you can define to return an object or not (for Fire & Forget operation). To deal with this case, IHandler can be implement without return :

public class CreatePersonCommandHandler : IHandler<CreatePersonCommand>
{
    public void Handle(CreatePersonCommand message)
    {
        //add you logic code here
    }
}

When handlers are register with IoC and can be finded by your IDependencyContainer implementation, the message is dispatchable :

IDispatcher dispatcher = new Dispatcher();
dispatcher.Dispatch<GetPersonByIdQuery, GetPersonReadModel>(new GetPersonByIdQuery(1));

If your define an asynchronous handler, dispatch it with IAsyncHandler :

IAsyncDispatcher dispatcher = new Dispatcher();
await dispatcher.DispatchAsync<CreatePersonCommand>(new CreatePersonCommand(1, "Name"));

As you certainly notice, Dispatcher explicitly implement dispatching interfaces. This type of implementation force developer to use the instance as interface and suit well with IoC too :)

Advanced features

Publication

Validation

Cancellable dispatching

Licence

MIT

bartender's People

Contributors

julienpavon avatar spontoreau avatar

Watchers

 avatar  avatar

Forkers

vtek

bartender's Issues

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.