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.

Popular Pragmatic topics Top

johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
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

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
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Latest in PragProg

View all threads ❯