Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Functional Event Sourcing using Sekiban

Functional Event Sourcing using Sekiban

https://www.youtube.com/watch?v=O_h_FGRkGPs

C# and .NET have been widely used since the early days of event sourcing. Additionally, F# has been introduced as a way to implement domain models functionally, as highlighted in the book Domain Modeling Made Functional. With the refinement of C# language features and the growing popularity of functional programming constructs like Result types, there is a growing interest in describing event sourcing in a more functional manner.

In this session, I will explain how I incorporated functional elements into the development of the open-source event sourcing and CQRS framework, Sekiban. We'll explore how adopting a functional approach can reduce cognitive load when developing complex domains, making your applications not only more robust but also easier to understand and maintain.

Also, this session will have brief introduction of the Railway Oriented Programming, using ResultBox library.

Tomohisa Takaoka

November 19, 2024
Tweet

More Decks by Tomohisa Takaoka

Other Decks in Programming

Transcript

  1. Feel free to share online. Functional Event Sourcing using Sekiban

    November 19, 2024 Solution2 C# Community Discord Conference. UTC 17:30 - .NET Foundation Youtube. 1 / 26
  2. Feel free to share online. About Speaker Tomohisa Takaoka X:

    @tomohisa Github: @tomohisa Works at : J-Tech Creations, Inc. , J-Tech Japan. Inc. JTS Group - Japan Technical Software Inc. Microsoft MVP for Developer Technologies (Web Development, .NET) new from November 2024 - OSS: Sekiban - Event Sourcing and CQRS Framework ResultBoxes - Railway Oriented Programing Library. 2 / 26
  3. Feel free to share online. Functional Event Sourcing using Sekiban

    3 / 26 Agenda 1. What is Event Sourcing? 2. What is “Functional” Event Sourcing? 3. Sekiban - Event Sourcing and CQRS Framework. 4. Live Coding 5. Summary
  4. Feel free to share online. 1. What is Event Sourcing?

    Event Sourcing is... “Event Sourcing is an architectural design pattern where changes that occur in a domain are immutably stored as events in an append-only log. This provides a business with richer data as each change that occurs within the domain is stored as a sequence of events which can be replayed in the order they occurred. This means you’re able to see more than just the current state of your domain - you can see what lead up to the current state.” - EventStore Blog 5 / 26 Add 1 Coffee Add 1 Orange Add 1 Tea Remove 1 Orange 1 Coffee Added 1 Coffee Added 1 Orange Added 1 Coffee Added 1 Orange Added 1 Tea Added 1 Coffee Added 1 Orange Added 1 Tea Added 1 Orange Removed 1. Can use removed data for the data analysis or adding features. 2. Can track histories of the data - audit and debug capabilities.
  5. Feel free to share online. What is CQRS? CQRS stands

    for Command Query Responsibility Segregation. It was also proposed by Greg Young, who released the “CQRS Documents” around 2010 (ish). The key point is that when saving data with a Command, it uses the write model. When reading data with a Query, we use the read model. The read model can be a materialized view or a live projection. Event Sourcing is most likely used with CQRS; however, in many cases, CQRS is used without Event Sourcing. 1. What is Event Sourcing? 6 / 26 Client Command Handler (Function) Event Store Projection Query Events Workflow CQRS Event Sourcing Materialized View
  6. Feel free to share online. Why Event Sourcing? • Audit

    and History by Default. ◦ By appending events without deleting old data, it becomes clear where data originates. This also makes debugging easier. • Separation of Concerns for Writing and Reading. ◦ Queries can sometimes get complex, but state sourcing forces developers to consider both reading and writing when designing tables. Event Sourcing allows developers to focus on writing first, with queries refined later. • Scalability ◦ We can adjust how data is stored and the infrastructure for events and each read model independently, making it easier to scale out an event-sourced system. ...etc 1. What is Event Sourcing? 7 / 26
  7. Feel free to share online. 1. What is Event Sourcing?

    Common Questions. • Can Event Sourcing and CQRS be applied to parts of a system? ◦ YES! It can be applied to just one microservice or even a single type of aggregate. • Is Event Sourcing only applicable to complex microservices? ◦ NO! It can be used in small systems or even in monolithic architectures. • Do Event Sourcing and CQRS systems always require streaming databases like Kafka or pub/sub streaming systems? ◦ NO! Event Sourcing and CQRS do not necessarily require event-driven or streaming databases. However, for larger systems, event streaming can solve many challenges associated with microservices. • Is the cost of infrastructure generally higher than traditional RDBMS-based systems? ◦ NOT always! For one of our clients, we implemented an ES/CQRS system that was cheaper than an RDBMS-based system of similar scale. It really depends on how efficiently you utilize your events. • Is Event Sourcing and CQRS difficult to learn? ◦ MAYBE? We train our interns to build systems using Event Sourcing, and they often develop at the same speed or even faster compared to traditional approaches. 8 / 26
  8. Feel free to share online. 2. What is “Functional” Event

    Sourcing? 10 / 26 Functional Programming using C# • Functional programming is a programming style that can be applied in nearly any language. It emphasizes a few key principles: ◦ Pure Functions: Functional programmers focus on the idea that pure functions are predictable and reliable because they depend only on their inputs. ◦ Immutability: Functional programming emphasizes immutability to avoid side effects, making code safer, especially in multithreaded or parallel processing scenarios. ◦ Function Composition: Experienced functional programmers often use function composition as a way to create more readable and maintainable code by chaining simple functions together. • Recent enhancements in C# make it easier for C# developers to leverage the power of functional programming. Some of these key improvements include: ◦ Records: Introduced to create immutable data structures more easily. Records focus on value equality rather than reference equality, making them ideal for scenarios where data immutability and simple data structures are needed. ◦ Pattern Matching: Extended pattern matching capabilities in C# allow for more concise and expressive code, enabling developers to decompose and match complex data structures seamlessly. This is particularly useful for implementing functional-style branching and control flow.
  9. Feel free to share online. 2. What is “Functional” Event

    Sourcing? Non Functional - Procedural 11 / 26 Functional • Not changing User state. • Immutable for record and command • Give action instead of log in the function to make function pure.
  10. Feel free to share online. 2. What is “Functional” Event

    Sourcing? "Event sourcing is naturally functional—it's an append-only log of facts that have happened in the past. You can say that any projection, any state, is essentially a left fold over your previous history. It is a purely functional model, always intended to be functional, and it fits really, really well with functional code. You're going to start seeing more and more people doing functional programming on top of event-sourced systems." — Greg Young 12 / 26 Concept of Event Sourcing really matches functional programming style. “Domain Modeling Made Functional” book.
  11. Feel free to share online. Decider Pattern 2. What is

    “Functional” Event Sourcing? Simple explanation, Command and Current state is input and “Decide” to produce event. State can “Evolve” by adding Event. 13 / 26
  12. Feel free to share online. Railway Oriented Programming. • Result

    Pattern : Always return Value OR Error. • Railway Oriented Programming: Connecting Function that returns Result multiple times. In functional language like F#, with Pipeline. In C#, method chaign will be used. • “Domain Modeling Made Functional” by Scott Wlaschin explains how developer can use functional programming to efficiently design domain. 2. What is “Functional” Event Sourcing? Error Value Type 2 Value Type 1 Function f1 return return Error Value Type3 Function f2 return return Bind Error Value Type 4 Function f3 return return Bind 14 / 26
  13. Feel free to share online. 3. Sekiban - Event Sourcing

    and CQRS Framework. 16 / 26 Basics of Sekiban. • Open Sourced in December 2023, now focus on SME. ◦ Everything we use, you can use under Apache 2.0 • Written in modern C#, Supports .NET 8 & .NET 9 new! ◦ Can be used in C# and F# • Multiple Infrastructure Support ◦ Azure Cosmos DB ◦ AWS Dynamo DB ◦ Postgres ◦ coming soon ... WASM Indexed DB, SQLite • “Functional” style interfaces. ◦ Command handler is “almost” pure function. ▪ Returns Result, can return errors or event. ◦ Event Handler is pure function ◦ Workflow style usecases • Github star makes us happy 🥹
  14. Feel free to share online. 3. Sekiban - Event Sourcing

    and CQRS Framework. Command and Command Handler 17 / 26
  15. Feel free to share online. 3. Sekiban - Event Sourcing

    and CQRS Framework. Event and Apply to the Aggregate 18 / 26
  16. Feel free to share online. 3. Sekiban - Event Sourcing

    and CQRS Framework. Domain testing made easy. 19 / 26
  17. Feel free to share online. 3. Sekiban - Event Sourcing

    and CQRS Framework. Web API generation (if you want) 20 / 26
  18. Feel free to share online. 3. Sekiban - Event Sourcing

    and CQRS Framework. Sekiban + Aspire 9 = 󰗨 • Database: Postgres, Cosmos DB can be used with Aspire and Sekiban. ◦ Cosmos DB Emulator is not working with Apple Silicone. ◦ We use Postgres on local and deploy Postgres or Cosmos DB. • Azure Blob Storage can be used for the Snapshots. ◦ Small Testing can be done without Blob Storage. • Sekiban has own Aspire (cosmos) integration ◦ Aspire 9.0.0 - can be used for .NET 8 or 9 • .NET 9 Open API • Simple Blazor frontend can be used for testing. ◦ Good developer experience. • Deployment can be done. ◦ We have not tested yet. 21 / 26
  19. Feel free to share online. 4. Live Coding 23 /

    26 Menu. • Aspire Starter Template ◦ Add Postgres • Simple Domain ◦ Aggregate ◦ Command and Handler ◦ Event • Generate Web API • Change Frontend Blazor • Add Test ... if time allows
  20. Feel free to share online. 5. Summary • Event Sourcing

    can be gradually adopted in specific parts of your system. • Functional programming is possible with C#, allowing you to focus on individual functions and reduce complex bugs. • Event Sourcing is naturally functional by design. • Sekiban leverages the power of functional programming within Event Sourcing and CQRS. • Add Sekiban or Event Sourcing in your Toolbox! 25 / 26
  21. Thank You 26 Sekiban Github https://github.com/J-Tech-Japan/Sekiban Resultbox Github https://github.com/J-Tech-Japan/ResultBoxes Today’s

    Sample https://github.com/tomohisa/Solution2_202411 19_Sample J-Tech Creations, Inc Website. https://www.jtechs.com English Blog https://tomohisa.hashnode.dev