Handling business logic

Business logic often ends up spread across large classes, mixed with state handling and database code. Then it’s hard to see what the code decides, and harder to test it. You’ll learn another way: describe the possible states and requests as explicit types, and write each decision as a small function that takes a request and the current state and returns what happened.

Many examples use the same shopping cart, so you can compare styles. Start with the first article: it takes a classic aggregate, a class holding data and rules together, and slims it down step by step. Others cover handling the same request arriving twice, saving two related changes in one transaction, validating input, and reporting failures: throw, return an error value, or record the failure as a fact.

Code is in C#, F#, Java and TypeScript. Pick your language; the ideas carry over.

  1. Slim your aggregates with Event Sourcing!

    2022-08-31In the traditional approach, what we get is what we store. Our write model is too often used directly as our read model, because the typical set of functionality in a…
  2. How to effectively compose your business logic

    2022-07-20Let’s say that we’d like to implement Shopping Cart. We have the following requirements: The customer may only add a product to the shopping cart after opening it. When…
  3. Idempotent Command Handling

    2024-11-15“Is your command handling idempotent?” Sounds like a douchebag question to ask. Actually, that’s not a question but a statement that we want to sound smart. Maybe a…
  4. How to handle multiple commands in the same transaction

    2023-03-12Let’s say that we’re starting a new project. It’s a small tool for internal needs, maybe even some sort of shadow IT project. It may also be the project that won’t have…
  5. This is not your uncle's Java! Modelling with Java 22 records pattern matching in practice

    2024-05-04I like learning new things. It stimulates my creativity, helps me gain diverse perspectives, and helps me be humble. When you’re a notorious debutant, you learn to…
  6. How events can help in making the state-based approach efficient

    2022-11-13In my last few articles, I showed the power of functional composition. That’s a foundation for effective modelling of your business logic. It allows for having more…
  7. Prototyping, an underestimated design skill

    2023-08-25Coding is an underestimated part of the design. When we think about the design, we immediately fall to whiteboard diagrams, sticky notes, or endless discussions. That’s…
  8. Throw, Result, or neither?

    2026-07-20There is one question I keep getting about my event-sourced code: why don’t I use ? It usually comes after someone sees a decision throwing an exception. They would put…
  9. My journey from Aggregates to Functional Composition

    2023-09-08I was recently asked what drove me to mostly use immutable data and methods rather than the Aggregate Pattern in my samples and videos. I decided that it might be worth…
  10. Union types in C#

    2022-07-27In the article “How to effectively compose your business logic”, I explained how explicit types definition can help in making our codebase closer to the business domain…
  11. Writing and testing business logic in F#

    2022-10-23My road to functional programming was pretty long. I started with structural programming in C++ and then rebranded into an Object-Oriented approach with Java and C#. At…
  12. How to validate business logic

    2023-01-15Fox Mulder got advice: “trust no one”. I’m claiming that each software developer should define their level of paranoia. The thing that we should never trust is the…