Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arkency/command_bus
Command Pattern - decoupling what is done from who does it in Ruby and Rails
https://github.com/arkency/command_bus
Last synced: 5 days ago
JSON representation
Command Pattern - decoupling what is done from who does it in Ruby and Rails
- Host: GitHub
- URL: https://github.com/arkency/command_bus
- Owner: arkency
- Created: 2016-02-05T22:25:47.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-15T16:18:58.000Z (almost 2 years ago)
- Last Synced: 2024-10-31T13:15:21.667Z (18 days ago)
- Language: Ruby
- Homepage:
- Size: 22.5 KB
- Stars: 56
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Command Bus
> Command Pattern - decoupling what is done from who does it.
## Installation
Notice this gem is namespaced.
gem install arkency-command_bus
## Usage
```ruby
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(®ister)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.
```ruby
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.
```ruby
a = User
a.object_id
# => 40737760reload!
# Reloading...
b = User
b.object_id
# => 48425300h = {a => 1, b => 2}
h[User]
# => 2a == 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`](http://api.rubyonrails.org/classes/Rails/Railtie/Configuration.html#method-i-to_prepare) callback which is executed before every code reload in development, and once in production.
```ruby
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(®ister)
end
```and call it with
```ruby
Rails.configuration.command_bus.call(FooCommand.new)
```## Convenience alias
```ruby
require 'arkency/command_bus/alias'
```From now on you can use top-level `CommandBus` instead of `Arkency::CommandBus`.
## About
Command Bus is funded and maintained by Arkency. Check out our other [open-source projects](https://github.com/arkency).
You can also [hire us](http://arkency.com) or [read our blog](http://blog.arkency.com).
## Learn more about DDD & Event Sourcing
Check our **Rails + Domain Driven Design Workshop** [more details](http://blog.arkency.com/ddd-training/).
Why You should attend? Robert has explained this in [this blogpost](http://blog.arkency.com/2016/12/why-would-you-even-want-to-listen-about-ddd/).
Next edition will be held in **September 2017** (Thursday & Friday) in Berlin, Germany.
Workshop will be held in English.