DDD and CQRS Using the Functional Language F#
A focus on behaviour and a more declarative style of code are two benefits for Domain-Driven Design (DDD) when moving from an object-oriented language like C# to a functional one like F#, Lev Gorodinski claims in a recent presentation, using an example that includes event sourcing and Command-Query Responsibility Separation (CQRS).
Lev has been using F# heavily during the last year and exclusively for the last 8 months and in his presentation he uses an example based on Greg Young’s original CQRS example to demonstrate how a F# implementation contrasts with a C# one, showing some of the benefits and challenges.
Implementing his example in a typical object-oriented approach Lev finds a number of issues, e.g. it’s quite verbose, and the entities have a dependency to the event source implementation.
In the F# implementation though there are improvements, it’s a lot less code, e.g. boilerplate code, which makes it less verbose, and the code is more explicit, capturing what happens in a function without hidden side effects.
Using DDD with an F# approach gives for Lev some benefits, it lets him focus on behaviour as opposed to the infrastructure required to implement it, which also means moving away from a CRUD thinking. It also supports a more declarative code; instead of writing code, it’s more about declaring a specification, with F# implementing the specification.
Lev points out that the benefits are all tenants of DDD and listed in two of the DDD books, written by Eric Evans and Vaughn Vernon respectively.
One of the challenges Lev experienced when moving to F# was the new paradigm, when he started he was doing C# style of code but in F#, which took a while to overcome to really make a shift to functional programming. It’s also a language that supports both functional and object oriented programming and sometimes he finds it hard to decide which option to use.
Anatole Tresch Mar 03, 2015