Domain Modeling Made Functional (PragProg)

You want increased customer satisfaction, faster development cycles, and less wasted work. Domain-driven design (DDD) and functional programming is the innovative combo that will get you there. Applying the core principles of functional programming results in software designs that model real-world requirements elegantly and concisely. Examples from business domains, with code in F#, show you how to build software that is business-focused, flexible, and high quality.

Scott Wlaschin @swlaschin

Edited by Brian MacDonald @bmacdonald

You want increased customer satisfaction, faster development cycles, and less wasted work. Domain-driven design (DDD) and functional programming is the innovative combo that will get you there. Applying the core principles of functional programming results in software designs that model real-world requirements elegantly and concisely. Examples from business domains, with code in F#, show you how to build software that is business-focused, flexible, and high quality.

Model a complex domain using the F# type system, creating code that is also readable documentation. Encode business rules to create ā€œcompile-time unit tests,ā€ and eliminate potential bugs by making illegal states unrepresentable. Discover why functional programming and DDD lead naturally to service-oriented architectures. Create a functional domain model that works with traditional databases, NoSQL, and event stores, and safely expose your domain via a website or API.

Solve real problems by focusing on real-world requirements for your software.


ā€œScott Wlaschin is one of the most important communicators in practical, applied programming today. In this book, he brings clarity and simplicity to the process of bridging the gap between requirements, customers, and concrete designs and code. Enjoy!ā€

ā€“Don Syme, Researcher, Microsoft U.K.


  • Full details: Search
  • View this bookā€™s portal and details on how to post errata and suggestions here.

Donā€™t forget you can get 35% off with your Devtalk discount! Just use the coupon code ā€œdevtalk.com" at checkout :+1:

3 Likes

Just starting to read this book. Curious if there is any thought to modeling in F#, but implementing in C#? Woud like to hear thoughts on introducing F#-based domain modeling into teams largely centered around C# development.

3 Likes

Idea is to model and then use the model as is

2 Likes

@swlaschin any tips or links to resources how this applies to client side development (i.e. native [mobile] apps & SPAā€™s). F.i. does it make sense to share some domain model types between server and client side code when possible?

2 Likes

Iā€™m experimenting with this and it seems perfectly possible. Pain points are using F# Option and F# Lists in C# when also considering C# Option from Language.Ext. Handling Discriminated Unions with ā€˜select caseā€™ is usable but not ideal. Plus the constructor invariant rules will live in F#. Which can put off C# developers who donā€™t know F# and is quite a difficult thing to sell to the team.

Iā€™m thinking the value for me is as design communication tool and as a fast prototyping blueprint, with which I can write unit tests to explore. But I think Iā€™ll ultimately have to throw the F# away and rebuild as C# classes in the implementation to satisfy our engineering team who are not sold on F#.

Iā€™d also like to hear of other peopleā€™s thoughts and experience with this approach.

1 Like

F# can be transpiled to JS and there are a couple of front-end frameworks for F# (see below). And yes, sharing the domain model across back and front end is a big plus!

2 Likes

@swlaschin

At the preface of the book you mention, "If the emphasis of the domain is on combining elements together to make other elements, then itā€™s often useful to focus on what these composition rules are (the so-called ā€œalgebraā€) before focusing on the data. Domains like this are widespread, from financial contracts to graphic design tools, and the principle of ā€œcomposition everywhereā€ makes them especially suitable for being modeled with a functional approach. Unfortunately, due to space limitations, we will not be covering these kinds of domains here.ā€

The design example made sense (i.e. Iā€™m working with, say, Adobe Photoshop and I compose many small UI elements to create one large UI element), but the financial contracts one did not make sense to me. Could you elaborate on this and / or provide other examples? Thanks.

1 Like

Hi Gio,

Regarding design using algebras, itā€™s not common to be so formal in the F# world ā€“ Haskellers and FP-Scala people like to do this more!

ā€œFunctional and Reactive Domain Modelingā€ (in Scala) by Debasish Ghosh covers this approach, and he has also has a video of the same name if you want to get an overview.

ā€œAlgebraā€ implies an algebraic structure and thus an emphasis on ā€œlawsā€ such as the so-called monoid or monad laws. If you donā€™t care about having strict laws, a similar approach in practice is simply to use a Domain Specific Language with combinators. My talk on Parser Combinators is a good introduction to the principles of combinators (also available as a blog post).

/ 1 of 2

2 Likes

Re the financial contracts specifically, hereā€™s a post with a lot of good links to financial DSLs. As that post mentions, the canonical paper is Simon Peyton Jonesā€™ ā€œComposing Contracts: an Adventure in Financial Engineeringā€ which represents contracts using a combinator library in Haskell. That paper doesnā€™t really describe a formal algebra, but it can give you an idea of whatā€™s involved. Also Chapter 15 of Real World Functional Programming has examples with both trades and animation primitives. Thereā€™s a related talk Domain Specific Languages, the functional way and Composing Chrismas with F# - Tomas Petricek

Cheers,

Scott

3 Likes

2 posts were split to a new topic: Domain Modeling Made Functional questions