StevenNunez

StevenNunez

Real-World Event Sourcing: Command Tense (page 6)

In v2 of the calculator, does it make sens for the command tenses to be present?

defmodule EventSourcedCalculator.V2 do                                              
  def handle_command(%{value: _val}, %{cmd: :add, value: v}) do                     
    %{event_type: :value_added, value: v}                                           
  end                                                                               
                                                                                    
  def handle_command(%{value: _val}, %{cmd: :sub, value: v}) do                     
    %{event_type: :value_subtracted, value: v}                                      
  end                                                                               
                                                                                    
  def handle_command(%{value: _val}, %{cmd: :mul, value: v}) do                     
    %{event_type: :value_multiplied, value: v}                                      
  end                                                                               
                                                                                    
  def handle_command(%{value: _val}, %{cmd: :div, value: v}) do                     
    %{event_type: :value_divided, value: v}                                         
  end                                                                               
                                                                                    
  def handle_event(%{value: val},                                                   
                   %{event_type: :value_added, value: v}) do                        
    %{value: val + v}                                                               
  end                                                                               
                                                                                    
  def handle_event(%{value: val},                                                   
                   %{event_type: :value_subtracted, value: v}) do                   
    %{value: val - v}                                                               
  end                                                                               
                                                                                    
  def handle_event(%{value: val},                                                   
                   %{event_type: :value_multiplied, value: v}) do                   
    %{value: val * v}                                                               
  end                                                                               
                                                                                    
  def handle_event(%{value: val},                                                   
                   %{event_type: :value_divided, value: v}) do                      
    %{value: val / v}                                                               
  end                                                                               
end 

For instance the command to add a value is value_added, however, making it add_value does 2 things. Communicates that the value has not been added yet and also allows consumers to more naturally respond to a command to add a value. It makes sense for handlers upon adding the numbers to emit value_added as an event.

Am I misunderstanding something?

First Post!

StevenNunez

StevenNunez

I wonder if a better example would have been to model the actions from a calculator.

commands = [
  %{action: "press button", value: 1},
  %{action: "press button", value: :plus},
  %{action: "press button", value: 1},
  %{action: "press button", value: :equal},
]

When you run through these commands, the event handler for a button press with the value of equal would attempt to run the calculation. All other events would build a buffer.

This isn’t perfect either though since you’d need to introduce taking input into the example with complicates things in addition to holding state.

Where Next?

Popular Pragmatic Bookshelf topics Top

jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New

Sub Categories: