Giter Club home page Giter Club logo

command_bus's Introduction

Command Bus

Command Pattern - decoupling what is done from who does it.

Installation

Notice this gem is namespaced.

gem install arkency-command_bus

Usage

require 'arkency/command_bus'

command_bus = Arkency::CommandBus.new
register    = command_bus.method(:register)

{ FooCommand => FooService.new(event_store: event_store).method(:foo),
  BarCommand => BarService.new,
}.map(&register)


command_bus.(FooCommand.new)

New instance of a service for every command

If need a new instance of a service every time it is called with a command or you want to lazily load the responsible services, you can just use Proc during registration.

command_bus = Arkency::CommandBus.new
command_bus.register(FooCommand, -> (foo_cmd) { FooService.new(event_store: event_store).foo(foo_cmd) })
command_bus.register(BarCommand, -> (bar_cmd) { BarService.new.call(bar_cmd) })

Working with Rails development mode

In Rails development mode when you change a registered class, it is reloaded, and a new class with same name is constructed.

a = User
a.object_id
# => 40737760 

reload!
# Reloading...
 
b = User
b.object_id
# => 48425300

h = {a => 1, b => 2}
h[User]
# => 2 

a == b
# => false 

so your Hash with mapping from command class to service may not find the new version of reloaded class.

To workaround this problem you can use to_prepare callback which is executed before every code reload in development, and once in production.

config.to_prepare do
  config.command_bus = Arkency::CommandBus.new
  register = command_bus.method(:register)

  { FooCommand => FooService.new(event_store: event_store).method(:foo),
    BarCommand => BarService.new,
  }.map(&register)
end

and call it with

Rails.configuration.command_bus.call(FooCommand.new)

Convenience alias

require 'arkency/command_bus/alias'

From now on you can use top-level CommandBus instead of Arkency::CommandBus.

About

Arkency

Command Bus is funded and maintained by Arkency. Check out our other open-source projects.

You can also hire us or read our blog.

Learn more about DDD & Event Sourcing

Check our Rails + Domain Driven Design Workshop more details.

Why You should attend? Robert has explained this in this blogpost.

Next edition will be held in September 2017 (Thursday & Friday) in Berlin, Germany. Workshop will be held in English.

command_bus's People

Contributors

andrzejkrzywda avatar deepj avatar fidel avatar mostlyobvious avatar paneq avatar porbas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.