Domain-driven event sourcing with Akka Typed
September 24, 2019 | Technical Blog
Pure domain code at the core, Akka Persistence on the outside
When following a domain-driven design approach, it is considered good practice to have the domain code at the core of the application, self-contained and with no dependencies on platform-specific aspects (onion architecture). Domain code is as business-centric as possible, maximizing the ratio of business logic versus “plumbing” code. This makes business logic expressive and resilient to changes.
Although at first glance related to persistence and thus infrastructure aspects, adoption of event sourcing has a deep impact on domain expression. More than a particular technology, it is a design philosophy with an emphasis on immutable data and state evolution. In event sourcing, the state of entities is the consequence of a sequence of events applied to an initial state. Events are generated by commands sent to the entity, which represent actions in the system. This has the following implications on the “shape” of domain code:
– business logic is expressed in the form of command processing and event handling
– system data is described by entity state, commands and persistent events
This article introduces light abstractions that allow coding domain entities without any direct reference to Akka Typed. This makes it possible to structure the application with separate domain and infrastructure packages. Within infrastructure code, domain entities and repositories are implemented using Akka Persistence Typed together with Cluster Sharding Typed. Infrastructure boilerplate is minimized thanks to a couple of generic base classes that take advantage of the recently introduced persistent entity DSL.