Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional way of implementing domain driven design

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.
like image 762
Andrew Skirrow Avatar asked Jan 16 '11 11:01

Andrew Skirrow


People also ask

What is Domain-Driven Design methodology?

Domain-Driven Design(DDD) is a collection of principles and patterns that help developers craft elegant object systems. Properly applied it can lead to software abstractions called domain models. These models encapsulate complex business logic, closing the gap between business reality and code.

How do I use Domain-Driven Design?

DDD focuses on three core principles: Focus on the core domain and domain logic . Base complex designs on models of the domain . Constantly collaborate with domain experts , in order to improve the application model and resolve any emerging domain -related issues.

What are the strategies in extracting and formulating domain specific design principles?

Thus, we have developed three strategies for extracting and formulating domain-specific design principles: (1) analyze the best hand-designed visualizations in the domain, (2) examine prior research on the perception and cognition of visualizations, and, when necessary, (3) conduct new user studies that investigate how ...

What is Domain-Driven Design example?

An aggregate is a domain-driven design pattern. It's a cluster of domain objects (e.g. entity, value object), treated as one single unit. A car is a good example. It consists of wheels, lights and an engine.


1 Answers

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.

like image 174
Tomas Petricek Avatar answered Oct 24 '22 01:10

Tomas Petricek