<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: atipiJ</title>
    <description>The latest articles on DEV Community by atipiJ (@atipij).</description>
    <link>https://dev.to/atipij</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F390881%2F8a5b0e3f-7334-43c3-8034-aa9b338a78e2.png</url>
      <title>DEV Community: atipiJ</title>
      <link>https://dev.to/atipij</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atipij"/>
    <language>en</language>
    <item>
      <title>CQRS and Event Sourcing</title>
      <dc:creator>atipiJ</dc:creator>
      <pubDate>Mon, 21 Dec 2020 12:57:00 +0000</pubDate>
      <link>https://dev.to/cherrychain/cqrs-and-event-sourcing-398n</link>
      <guid>https://dev.to/cherrychain/cqrs-and-event-sourcing-398n</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The purpose of this article is to go through some basic concepts of Command Query Responsibility Segregation (CQRS) and Event Sourcing design patterns.&lt;br&gt;
There's going to be an analysis of these patterns and then we will implement these to the use case.&lt;br&gt;
We will apply CQRS to the use case and we will apply Event Sourcing to fix some shortcomings of CQRS.&lt;/p&gt;

&lt;p&gt;Repository containing the Kata that we solved to implement the patterns: &lt;a href="https://gitlab.com/cherry-chain/bank-account-kata" rel="noopener noreferrer"&gt;CherryChain bank account kata&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  CQRS
&lt;/h1&gt;

&lt;p&gt;CQRS is a pattern used for modeling the domain of software. Its name stands for Command Query Responsibility Segregation and, as you can guess, its goal is to separate the writing part of the data from the reading part.&lt;br&gt;
Often in projects, these two concepts are enclosed in a single domain model. &lt;br&gt;
However, as the application begins to grow, it could easily become difficult to manage and expensive to maintain.&lt;br&gt;
On the contrary, by separating these two concepts of commands and queries, it is possible to maintain a low complexity even in very large projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevdcbx6hc1hobfu0rt9p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevdcbx6hc1hobfu0rt9p.jpg" alt="cqrs"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Event Sourcing
&lt;/h1&gt;

&lt;p&gt;Event Sourcing is an event-centric technique for persisting the &lt;strong&gt;Application State&lt;/strong&gt;. In this approach, the state is stored as a series of events, in an event store. When the state is updated, events are emitted and get inserted into the event store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4h0rph03o2wtrsi6n8g3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4h0rph03o2wtrsi6n8g3.jpg" alt="event_sourcing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Loading the state consists of iterating through all the saved events and summing them to reconstruct the state in its entirety. Whenever the state gets modified, an event is emitted. This means that any consumer that is listening to these events can handle them accordingly, using the data inside the event. A consumer is responsible for listening and consuming events.&lt;/p&gt;

&lt;p&gt;Event Sourcing has some inherent benefits when used as an application architecture, for example, it makes write operations much faster since it only has to generate and save a lightweight event.&lt;/p&gt;

&lt;p&gt;Using Event Sourcing alongside CQRS brings some advantages. In CQRS there is an application write repository which normally saves as-is when commands arrive. By substituting this part of the application with Event Sourcing, writing operations will become faster, and it will also be possible to have application state snapshots at any point in time.&lt;/p&gt;
&lt;h1&gt;
  
  
  Use case
&lt;/h1&gt;

&lt;p&gt;The scenario in which we're going to apply these patterns is the &lt;a href="https://github.com/dpanza/bank-kata-event-sourcing-cqrs" rel="noopener noreferrer"&gt;Bank account kata&lt;/a&gt; using the Kotlin programming language.&lt;br&gt;
The goal is to create a service that allows basic "banking" operations such as deposit and withdrawal. Of course everything&lt;br&gt;
has to be programmed following the patterns accordingly.&lt;/p&gt;

&lt;p&gt;Here is a list of features that we are going to develop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The bank can register a customer&lt;/li&gt;
&lt;li&gt;The bank can create an account for a customer&lt;/li&gt;
&lt;li&gt;The bank can deposit money to an account&lt;/li&gt;
&lt;li&gt;The bank can withdraw money from an account&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The bank can register a customer
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnwrlage0e8v7dghiv3zs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnwrlage0e8v7dghiv3zs.jpg" alt="register_customer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first step was to create a class representing the bank and allowing it to register a customer.&lt;br&gt;
Following the TDD (Test Driven Development) methodology we started by creating the following test and then proceeding with the implementation of the feature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Test
  internal fun `it should register a customer`() {
    val customerId = bank.registerCustomer("Firstname Lastname")

    assertThat(bank.getCustomer(customerId).fullName).isEqualTo("Firstname Lastname")
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what we need is a function to register a customer and another one to read a customer, given his identifier.&lt;br&gt;
From now we can already begin to think about the CQRS pattern, which allows us to keep the concepts of writing and reading data isolated.&lt;br&gt;
When we want to register a customer we will have to launch a command, while when we want to read a customer we will have to make a query.&lt;/p&gt;

&lt;p&gt;At the moment we can focus on the registration function, in which the customer's full name is passed as input. The function then will generate an id (the id will be then returned by the function) and launch a command to create the new user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  fun registerCustomer(fullName: String): UUID {
    val id = UUID.randomUUID()
    commandBus.publish(RegisterCustomer(id, fullName))
    return id
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our kata, to implement the registration of a new customer, we created the &lt;strong&gt;RegisterCustomer&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sealed class Command

data class RegisterCustomer(val id: UUID, val fullName: String): Command()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the &lt;strong&gt;DefaultCommandBus&lt;/strong&gt; communication channel which is used to publish the commands that reach a class (which extends the &lt;strong&gt;CommandHandler&lt;/strong&gt; interface) that knows how to handle them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CommandBus {
  fun publish(command: Command)
}

class DefaultCommandBus(private val handler: CommandHandler) : CommandBus {
  override fun publish(command: Command) {
    handler.handle(command)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have called the class used to manage these commands &lt;strong&gt;DefaultCommandHandler&lt;/strong&gt;, which, upon receiving &lt;strong&gt;RegisterCustomer&lt;/strong&gt; command, creates a new customer, registers it, and then saves it in the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CommandHandler {
  fun handle(command: Command)
}

class DefaultCommandHandler(private val customerRepository: Repository&amp;lt;Customer&amp;gt;) : CommandHandler {
  override fun handle(command: Command) {
    when (command) {
      is RegisterCustomer -&amp;gt; registerCustomer(command)
    }
  }

  private fun registerCustomer(command: RegisterCustomer) {
    val customer = Customer()
    customer.register(command.id, command.fullName)
    customerRepository.put(customer)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Customer&lt;/strong&gt; state contains information regarding the bank's customer. There are an identification code and full name. Since we have used the Event Sourcing pattern to make sure our client's story is not lost, we don't update the information directly. We save it as a list of events. As we can see, the function used for registration adds the &lt;strong&gt;CustomerRegistered&lt;/strong&gt; event&lt;br&gt;
to the customer state's &lt;strong&gt;changes&lt;/strong&gt; list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Customer {
  lateinit var id: UUID
  val changes = mutableListOf&amp;lt;Event&amp;gt;()

  fun register(id: UUID, fullName: String) {
    changes.add(CustomerRegistered(id, fullName))
  }
}

interface Event { val id: UUID }

data class CustomerRegistered(override val id: UUID, val fullName: String) : Event
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Repository&lt;/strong&gt; where our new customer is stored is made up of an &lt;strong&gt;EventStore&lt;/strong&gt; and an &lt;strong&gt;EventBus&lt;/strong&gt;. When the function to save the customer is called, each of the events regarding that particular customer (identified by an id) are saved in the Store and then sent via the dedicated Bus.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Repository&amp;lt;T&amp;gt; {
  fun put(entity: T)
}

class InMemoryCustomerRepository(private val eventStore: EventStore, private val eventBus: EventBus): Repository&amp;lt;Customer&amp;gt; {
  override fun put(customer: Customer) {
    customer.changes.forEach {
      eventStore.append(it)
      eventBus.emit(it)
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;InMemoryEventStore&lt;/strong&gt; is our class that takes care of keeping events in memory and then being subsequently interrogated when necessary. (It doesn't necessarily need to be an in-memory implementation, the &lt;strong&gt;EventStore&lt;/strong&gt; interface can be implemented as any one wishes).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface EventStore {
  fun append(event: Event)
}

class InMemoryEventStore : EventStore {
  private val list = mutableListOf&amp;lt;Event&amp;gt;()

  override fun append(event: Event) {
    list.add(event)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DefaultEventBus&lt;/strong&gt; is the communication channel where all the events are sent which are then managed by the &lt;strong&gt;EventHandler&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface EventBus {
  fun emit(event: Event)
}

class DefaultEventBus(private vararg val handlers: EventHandler) : EventBus {
  override fun emit(event: Event) {
      handlers.forEach { it.handle(event) }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CustomerViewsEventHandler&lt;/strong&gt; is a class that handles customer events and is has a collaborator called &lt;strong&gt;CustomerViews&lt;/strong&gt;. When this handler receives a &lt;strong&gt;CustomerRegistered&lt;/strong&gt; event, it creates a &lt;strong&gt;CustomerView&lt;/strong&gt; with the data it receives and projects it in the view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface EventHandler {
  fun handle(event: Event)
}

class CustomerViewsEventHandler(private val customerViews: CustomerViews) : EventHandler {
  override fun handle(event: Event) {
    when(event) {
      is CustomerRegistered -&amp;gt; handle(event)
    }
  }

  fun handle(event: CustomerRegistered) {
    customerViews.add(CustomerView(event.id, event.fullName))
  }
}

data class CustomerView(val id: UUID, val fullName: String)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;InMemoryCustomerViews&lt;/strong&gt; is the view where all customer information is projected into. The views can then be queried by the bank using the get function. This keeps the command and query logic separate as indicated by the CQRS pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CustomerViews {
  fun add(customerView: CustomerView)
  fun get(customerId: UUID): CustomerView
}

class InMemoryCustomerViews: CustomerViews {
  private val data = mutableMapOf&amp;lt;UUID, CustomerView&amp;gt;()

  override fun add(customerView: CustomerView) {
    data[customerView.id] = customerView
  }

  override fun get(customerId: UUID): CustomerView {
    return data[customerId]!!
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, the first point of the kata was completed following the CQRS and Event Sourcing pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bank can create an account for a customer
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzm6epyiyav3chc2p0q28.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzm6epyiyav3chc2p0q28.jpg" alt="create_account"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we want to make sure that the bank can create one or more accounts for each customer where the balance will be stored.&lt;br&gt;
After creating the test we went on to write a function to create an account on a specific customer, in which an id is generated to identify the account and then publish the &lt;strong&gt;CreateAccount&lt;/strong&gt; command on the command bus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  fun createAccount(customerId: UUID): UUID {
    val id = UUID.randomUUID()
    commandBus.publish(CreateAccount(id, customerId));
    return id;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we saw in the previous chapter, the class that will handle the command we published is &lt;strong&gt;DefaultCommandHandler&lt;/strong&gt;.&lt;br&gt;
So in addition to the management of the "RegisterCustomer" command, we have added the management of the new CreateAccount command.&lt;br&gt;
A new account is created with the id and customer id passed by the command and finally stored in the repository in the same way we saw in the previous chapter (using EventStore and EventBus).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DefaultCommandHandler(private val accountRepository: Repository&amp;lt;Account&amp;gt;, private val customerRepository: Repository&amp;lt;Customer&amp;gt;) : CommandHandler {

  override fun handle(command: Command) {
    when (command) {
      is RegisterCustomer -&amp;gt; registerCustomer(command)
      is CreateAccount -&amp;gt; create(command)
    }
  }

  private fun registerCustomer(command: RegisterCustomer) {
    val customer = Customer()
    customer.register(command.id, command.fullName, command.type)
    customerRepository.put(customer)
  }

  private fun create(createAccount: CreateAccount) {
    val currentAccount = Account()
    currentAccount.create(createAccount.id, createAccount.customerId)
    accountRepository.put(currentAccount)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Account&lt;/strong&gt; class is composed as follows: an identifier, a customer id, and a list of events.&lt;br&gt;
When the creation method is used, the &lt;strong&gt;AccountCreated&lt;/strong&gt; event is produced which will then be published by the &lt;strong&gt;Repository&lt;/strong&gt; on the event bus.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Account() {
  lateinit var id: UUID
  lateinit var customerId: UUID
  val change = mutableListOf&amp;lt;Event&amp;gt;()

  fun create(id: UUID, customerId: UUID) {
    change.add(AccountCreated(id, customerId))
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, the &lt;strong&gt;AccountViewsEventHandler&lt;/strong&gt; class will receive the &lt;strong&gt;AccountCreated&lt;/strong&gt; event with which the &lt;strong&gt;AccountViews&lt;/strong&gt; projection will be populated. It will then be possible to make queries to find out the balance of each customer.&lt;br&gt;
As you can see from the code, in the phase of creating the balance it is initialized to zero.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AccountViewsEventHandler(private val accountViews: AccountViews) : EventHandler {
  override fun handle(event: Event) {
    when (event) {
      is AccountCreated -&amp;gt; handle(event)
    }
  }

  private fun handle(accountCreated: AccountCreated) {
    accountViews.insert(AccountView(
      id = accountCreated.id,
      customerId = accountCreated.customerId,
      balance = 0
    ))
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also in this new feature that we added to the bank, we were able to follow the CQRS and ES pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bank can deposit money to an account
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3i5ua5mur8bxej6qp115.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3i5ua5mur8bxej6qp115.jpg" alt="deposit_money"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To allow the bank to deposit money to an account, we allowed our application to handle the &lt;strong&gt;DepositMoney&lt;/strong&gt; command. The handler of this command will update the &lt;strong&gt;Account&lt;/strong&gt; state by increasing its balance based on the amount deposited. Before doing this update operation, it first has to load the existing account information. This is the function (present in the &lt;strong&gt;DefaultCommandHandler&lt;/strong&gt;) that handles the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private fun deposit(depositMoney: DepositMoney) {
    val currentAccount = accountRepository.get(id = depositMoney.accountId)

    if (currentAccount!=null) {
      currentAccount.deposit(depositMoney.amount)
      accountRepository.put(currentAccount)
    } else {
      throw BankException("account does not exist")
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know that this information is stored as a series of events in our event store. So before doing any update operation, the application first reads all the events, saved in the event store, relative to the specific account in which the money will be deposited.&lt;br&gt;
We then create an empty instance of  &lt;strong&gt;Account&lt;/strong&gt;. These events are then used in the &lt;strong&gt;hydrate&lt;/strong&gt; function present in the empty &lt;strong&gt;Account&lt;/strong&gt; instance, which iterates through the events and creates the state information from scratch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class InMemoryAccountRepository(private val eventStore: EventStore, private val eventBus: EventBus) : Repository&amp;lt;Account&amp;gt; {
  override fun get(id: UUID): Account? {
    val events = eventStore.getEventsBy(id = id)
    return if (events.isEmpty()) {
      null
    } else {
      val account = Account()
      account.hydrate(events)
      account
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;hydrate()&lt;/strong&gt; function works as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  fun hydrate(events: List&amp;lt;Event&amp;gt;) {
    events.forEach {event -&amp;gt;
      when (event) {
        is AccountCreated -&amp;gt; {
          this.id = event.id
          this.customerId = event.customerId
        }
        is MoneyDeposited -&amp;gt; this.balance += event.amount
      }
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a key step in &lt;strong&gt;Event Sourcing&lt;/strong&gt;. As we can see, all the events are processed and based on their type, an update operation is applied to the &lt;strong&gt;Account&lt;/strong&gt; instance. This means that the first event must be an &lt;strong&gt;AccountCreated&lt;/strong&gt; event, which will set the account's &lt;strong&gt;id&lt;/strong&gt; and &lt;strong&gt;customerId&lt;/strong&gt;. Every other &lt;strong&gt;MoneyDeposited&lt;/strong&gt; event that might have been emitted in the past will be iterated through, and will update the current &lt;strong&gt;Account&lt;/strong&gt; instance. After the hydration is done, we will have in our hands the instance of the account that had been stored as a series of events in the event store. We update the &lt;strong&gt;changes&lt;/strong&gt; of this instance by putting in our new &lt;strong&gt;MoneyDeposited&lt;/strong&gt; event, which is then emitted to the event bus and appended to the event store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class InMemoryAccountRepository(private val eventStore: EventStore, private val eventBus: EventBus) : Repository&amp;lt;Account&amp;gt; {
  override fun put(account: Account) {
    account.change.forEach { event -&amp;gt;
      eventStore.append(event)
      eventBus.emit(event)
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is intercepted by the &lt;strong&gt;AccountViewsEventHandler&lt;/strong&gt;. This handler is responsible for keeping the &lt;strong&gt;Account Views&lt;/strong&gt; synchronized with the actual state. The &lt;strong&gt;Views&lt;/strong&gt; are the part of the application that are visible externally. The views don't necessarily store the state as-is the state can receive any desired transformation before being exposed externally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AccountViewsEventHandler(private val accountViews: AccountViews) : EventHandler {
  private fun handle(moneyDeposited: MoneyDeposited) {
    val accountView = accountViews.get(moneyDeposited.id)
    val updatedView = accountView.copy(balance = accountView.balance + moneyDeposited.amount)
    accountViews.delete(moneyDeposited.id)
    accountViews.insert(updatedView)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this feature, it is possible to deposit money into an account. We also encountered the &lt;strong&gt;hydrate()&lt;/strong&gt; function, which helps us to update an existing account by iterating through the events present in the event store.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bank can withdraw money from an account
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnxbhisezfl073t1oqjlg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnxbhisezfl073t1oqjlg.jpg" alt="withdraw_money"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To allow the bank to withdraw money from an account, the application handles the &lt;strong&gt;WithdrawMoney&lt;/strong&gt; command. This operation is analogous to the depositing of money. This function is present in the &lt;strong&gt;DefaultCommandHandler&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private fun withdraw(withdrawMoney: WithdrawMoney) {
  val currentAccount = accountRepository.get(id = withdrawMoney.accountId)

  if (currentAccount.hasEnough(withdrawMoney.amount)) {
    currentAccount.withdraw(withdrawMoney.amount)
    accountRepository.put(currentAccount)
  } else {
    throw BankException("not enough money")
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It updates the &lt;strong&gt;Account&lt;/strong&gt; by decreasing its balance based on the amount withdrawn. The withdraw process first loads the state information by iterating through its stored events (&lt;strong&gt;hydrate()&lt;/strong&gt; function):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun hydrate(events: List&amp;lt;Event&amp;gt;) {
  events.forEach {event -&amp;gt;
    when (event) {
      is AccountCreated -&amp;gt; {
        this.id = event.id
        this.customerId = event.customerId
      }
      is MoneyDeposited -&amp;gt; this.balance += event.amount
      is MoneyWithdrawn -&amp;gt; this.balance -= event.amount
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the events have been iterated through completely, then we have the &lt;strong&gt;Account&lt;/strong&gt; instance that we wanted and we can proceed with updating it. At this point a &lt;strong&gt;MoneyWithdrawn&lt;/strong&gt; event is created, containing the necessary information of the withdrawal it is then appended to the &lt;strong&gt;changes&lt;/strong&gt; of the account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun withdraw(amount: Int, fee: Int) {
  change.add(MoneyWithdrawn(id, amount, fee))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This event is then saved to the event store, emitted to the event bus and intercepted by the &lt;strong&gt;AccountViewsEventHandler&lt;/strong&gt;, which projects the changes to the &lt;strong&gt;AccountViews&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private fun handle(moneyWithdrawn: MoneyWithdrawn) {
  val accountView = accountViews.get(moneyWithdrawn.id)
  val updatedView = accountView.copy(balance = accountView.balance - moneyWithdrawn.amount)
  accountViews.delete(moneyWithdrawn.id)
  accountViews.insert(updatedView)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this feature, it is possible to withdraw money from an account.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusions
&lt;/h1&gt;

&lt;p&gt;As we have seen, by using the CQRS and ES pattern, we managed to solve this kata. &lt;br&gt;
Some pros of this pattern are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is a single source of truth&lt;/li&gt;
&lt;li&gt;Views can be generated with high customization&lt;/li&gt;
&lt;li&gt;The separation of actions that change the state and queries can bring high optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However there are some obstacles (or &lt;em&gt;opportunities!&lt;/em&gt;) that need to be conquered when working with this pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kickstarting a project can prove difficult&lt;/li&gt;
&lt;li&gt;There is a necessity to have a good model of the domain of the project&lt;/li&gt;
&lt;li&gt;Events might lose meaning over time if the domain isn't modeled properly&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kotlin</category>
      <category>eventsourcing</category>
      <category>cqrs</category>
      <category>kata</category>
    </item>
  </channel>
</rss>
