Functional way of implementing domain driven design
Asked Answered
A

4

28

I've had a lot of experience with writing domain driven applications using C#. The more applications I write the more I find that I want to take an approach that doesn't fit that well with standard C#/OO techniques:

  1. I want to write as many pure functions as possible because they are really easy to test.
  2. I want to write my business logic in a more declarative fashion.

So I've been looking at functional languages such as F#. After all there is no reason why domain driven design has to be implemented using OO.

I was wondering if anyone has any ideas/experience with doing Domain driven design design whilst using a functional language. Especially:

  • What would a functional domain model look like?
  • How would you abstract the data access layer from the domain model.
Appleton answered 16/1, 2011 at 11:12 Comment(2)
This is a very good question and is something I'm trying to work on as well. I think the traditional ORMs will not work in functional languages such as F# because values are immutable by default but there are always changes made to the data that would need to be persisted. As for the domain model, I guess it could be some sort of functional library composed of records and discriminated unions. There is also the view that FP may not be the right choice for writing end user apps, it is more of a technology for building parallel\async frameworks. I'll be sure to follow this discussion!Lindie
I'd recommend reading the "Designing with types" series on F# for fun and profit.Walkabout
S
15

Disclaimer: I have only a vague knowledge about domain driven design, so the answer may not use the right terms and may be overly focused on code rather than general concepts, but here are some thoughts anyway...

The focus on understanding the domain rather than designing specific features or objects to implement them seems very natural to how people use functional programming languages in general. Very often (at least in a part of a functional application) you start by designing data structure that describes (or models) the world you're working with. The data structure is separated from the implementation, so it nicely models the domain.

A very nice example is described in paper about composing financial contracts. The example is an application for valuation (and other processing) of financial contracts. The most important thing is to create model of the contracts - what are they actually? To answer that, the authors design a data structure for describing contracts. Something like:

type Contract = 
  | Zero                         // No trades
  | Single of string * float     // Single trade (buy something for some price)
  | And of Contract * Contract   // Combine two contracts 
  | Until of Contract * DateTime // Contract that can be executed only until...
  // (...)

There are a few other cases, but the data structure is very simple and models a wide range of pretty complex contracts that are used in the financial industry.

Summary I think the focus on data structures that are used to model the world (and are separated from the implementation that uses them) is very close to the key concepts of DDD.

Sural answered 16/1, 2011 at 19:19 Comment(1)
I've not gone through it in detail yet, but that paper looks pretty interesting. ThanksAppleton
S
6

Here is an example of an idiomatic F# implementation: Domain-Driven Design With F# and EventStore

Disclaimer: I'm the author.

Schwab answered 26/2, 2013 at 18:30 Comment(1)
While that may be a good article, it's dangerously close to a link only answer.Lala
B
5

There is a new idea of using Clojure (a modern version of Lisp), which is a functional language, to create domain models. This presentation is a quite good intro (and it is also an awesome demo of HTML5).

Long story short, functional attitude is great when combined with Event Sorcing. It lets you create fully testable models very easily. And if you don't want to jump into entirely new language right now, modern C# is a quite good language to write functional-like code (at least for implementing common domain models)

Bringhurst answered 16/1, 2011 at 19:18 Comment(0)
K
3

Old question but surprisingly still relevant today.

To my knowledge, the best (only?) book about Functional Domain Driven Design is Domain Modeling Made Functional, written by Scott Wlaschin, the author of F# for fun and profit (mentioned above).

Before diving into the book, the talk Functional Programming Design Patterns is a great summary of the concepts (Hint: there is no pattern :)

The examples are in F# but they are easy to translate into any other functional language with algebraic types (Haskell and PureScript in my case).

Kristoforo answered 27/8, 2018 at 1:42 Comment(2)
I would also highly recommend "Functional and Reactive Domain Modeling" by Debasish Ghosh, which is also great source of inspiration for the topic.Endoergic
Yes, this a great book that goes way deeper into the topic but it is less accessible... thus I would recommend Wlaschin as reading #1Kristoforo

© 2022 - 2024 — McMap. All rights reserved.