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

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
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
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
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
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Sub Categories: