Skip to content

cs-lexliu/practice-event-sourcing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

practice-event-sourcing

Event sourcing is a way to update state and publish event. The traditional way to persist an entity is to save its current state. A business object with event sourcing is persisted by storing a sequence of state changing events. And CQRS (Command Query Responsibility Segregation) always comes with event sourcing, but that is not include in this article.

img.png

Why Event Sourcing?

  • accurate audit logging
  • release the responsibility from write operation
  • easy for the analysis the data
  • database would not be the bottleneck
  • support multiple read model

Why not use Event Sourcing?

  • increase the complexity on read operation
  • hard to build the data model

Event sourcing in practice

In this section, we are going to practice the event souring by implementing a bowling game.

img.png

Example code

Source code: cs-lexliu/practice-event-sourcing

In bowling_game.go, the BowlingGame model has been implemented as a event sourcing model.

  • Create and Roll are the command methods response to generate the event.
  • When method is the event handler contains most of the business logic of bowling game.
  • newBowlingGameFromEvent is a constructor to create the BowlingGame model from events.

In use case package, CreateBowlingGame and RollOneBall is the use case command service, they operate the domain model and store the state into the database. Repository is the interface of the database, this is the dependency inversion to decouple the database dependency from service.

If you are interested in the usage of this example, check main.go file. This is a console program for playing the bowling game.

Last, most of the magic in under ddd package. This package contains the reusable behavior by using theses package me can add new domain model easier.

References:

About

A project for practicing Event Sourcing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages