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

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
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
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 think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
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
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
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1033 17470 383
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
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
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
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New

Sub Categories: