Event sourcing: making it functional (2)

TL;DR

In the last post we covered the basic concept of DDD, the aggregate and its root. Before we move on with the non-functional approach, let’s try to define at least one aggregate, that could be used as an example for future posts and code samples.

All entries in this series:

A payment

For sake of providing an aggregate example let me reuse the payment example I introduced for the service kata purposes. The aggregate boundary is handling a single payment. This includes:

  • selecting payment method
  • registering an answer from the payment gateway
  • storing status

Having this we can easily imagine that the API of this aggregate could be similar to:

paymentapi

As you can see, there are no dummy setters or meaningless primitive properties. We exposed meaningful methods and properties for showing the part of the state we want to show. Additionally, the payment constructor shows all the parameters that are required to issue a payment.

With this example we can move forward and provide more insight into its initial implementation and refactoring it towards more functional design.

Event sourcing: making it functional (1)

TL;DR

This article starts a series of entries that will guide you through my experiences with making event sourcing functional. There are a few existing entries about a functional approach to event sourcing, but I want to share my path and a story behind migrating from one approach to another. I’ll start with some fundamentals. This will show from where I started, as well as it may help you to learn some basics of DDD and event sourcing if you’re not into the topic.

All entries in this series:

The Blue Book51szw87slrl-_sx258_bo1204203200_

There’s a book that is a must read, Domain-Driven Design by Eric Evans. It’s quite old and a lot of has changed in our industry. Still, the basic need of having developers understanding the domain they work on, the strategies and tactics they could use to embrace it, they are the same. Eric covers a lot of topics in there, one of them which is a really low level concept (and not the most important one) is the aggregate.

The aggregate

Let me quote Eric’s descriptions of the aggregate first:

An AGGREGATE is a cluster of associated objects that we treat as a unit for the purpose of data changes. Each AGGREGATE has a root and a boundary. The boundary defines what is inside the AGGREGATE. The root is a single, specific ENTITY contained in the AGGREGATE. The root is the only member of the AGGREGATE that outside objects are allowed to hold references to, although objects within the boundary may hold references to each other. ENTITIES other than the root have local identity, but that identity needs to be distinguishable only within the AGGREGATE, because no outside object can ever see it out of the context of the root ENTITY.

The vital part of this description is the boundary. That’s what aggregate is for – to distill boundaries existing in a domain you work on. So once you set a boundary of data changed together, you ensure that they stay together. Additionally, you operate on this data only via the Aggregate Root, which might be treated as the public API of an aggregate, something that others interacts with.

The aggregate boundaries

If an aggregate is defined by its boundaries, one could design a system, that would be just one aggregate. This would enable to access all the data and still have a defined boundary, right? Wrong. What you want to do is to define aggregates following these two simple rules:

  1. aggregates are as big as there have to
  2. aggregates are as small as possible

Aggregates are as big as there have to

The very first rule says that the size of an aggregate is determined by a domain and a model. A domain can require some consistency across different entities, making an aggregate bigger. The same with model. Frequently it’s up to the modeller (a model’s creator) to create bigger or smaller aggregate, but not bigger than needed.

Aggregates are as small as possible

Distilling just one aggregate would mean, that a system can execute one action at a time (when optimistic concurrency applied). This isn’t enough for the majority of applications. Making aggregates small improves your system’s scalability and performance, not to mention your ability to design and model in small chunks.

Summary

This ends the first entry of this series. We recalled the aggregate’s definition and discussed briefly two rules of modelling an aggregate. Still, there’s more to come on our way of making the event sourcing functional. First, we need to visit a non-functional approach.

2017: Microsoft MVP and more

Microsoft MVP

It’s official. I’m Microsoft MVP for Visual Studio and Development Technologies. It’s first time I got this award. I’m truly happy and wanted to thank you:

  • Microsoft for granting me the award
  • My family for supporting me through this year
  • All the members of Warsaw .NET User Group. 2016 was a very good year and we’ll make 2017 even better!
  • All my friends for all the support I received, both in organizing WG.NET meetings and just being there
  • All the groups and conference organizers for having me!

The award doesn’t mean it’s time to rest. Frankly speaking, it’s quite opposite.

More

The new year will bring some changes. The very next post will start a series of posts about making event sourcing functional. Don’t worry if you don’t know the event sourced approach or functional languages. I’ll start from the very foundations and build up on top of it. This might be not considered as a change, as I had a series about RampUp. This time it’s going to be a true series of well connected entries. If you find it interesting, a few series might appear in here.

There’s a new activity that I don’t want to talk to much about right now, but it will be a another game changer. It will take place in March and I’ll announce it in two weeks.

Finally, I’ve added more information about me and my incoming talks. I hope you’ll find it useful.

It’s time to start a brand new year!