实现领域驱动设计的函数式方法

发布于 2024-10-12 03:20:22 字数 314 浏览 7 评论 0原文

我在使用 C# 编写领域驱动应用程序方面拥有丰富的经验。我编写的应用程序越多,我就越发现我想采用一种不太适合标准 C#/OO 技术的方法:

  1. 我想编写尽可能多的纯函数,因为它们确实很容易测试。
  2. 我想以更具声明性的方式编写我的业务逻辑。

所以我一直在研究函数式语言,例如 F#。毕竟,领域驱动设计没有理由必须使用面向对象来实现。

我想知道是否有人对使用函数式语言进行领域驱动设计有任何想法/经验。特别是:

  • 功能域模型会是什么样子?
  • 您将如何从域模型中抽象数据访问层。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无需解释 2024-10-19 03:20:22

免责声明:我对领域驱动设计只有模糊的了解,因此答案可能没有使用正确的术语,并且可能过度关注代码而不是一般概念,但无论如何,这里有一些想法...

关注于理解领域而不是设计特定的功能或对象来实现它们对于人们使用函数式编程语言的一般方式来说似乎是很自然的。通常(至少在功能应用程序的一部分中),您首先要设计描述(或建模)您正在使用的世界的数据结构。数据结构与实现分离,因此它可以很好地建模领域。

中描述了一个非常好的示例关于撰写金融合约的论文。该示例是金融合同评估(和其他处理)的应用程序。最重要的是创建合同模型——它们实际上是什么?为了回答这个问题,作者设计了一个用于描述合约的数据结构。类似的:

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...
  // (...)

还有一些其他情况,但数据结构非常简单,并且对金融行业使用的各种相当复杂的合约进行了建模。

总结 我认为对用于对世界进行建模的数据结构(并与使用它们的实现分离)的关注与 DDD 的关键概念非常接近。

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.

烟若柳尘 2024-10-19 03:20:22

以下是惯用的 F# 实现示例: 使用 F# 和 EventStore 进行领域驱动设计

免责声明:我是作者。

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

Disclaimer: I'm the author.

疏忽 2024-10-19 03:20:22

有一个新想法是使用 Clojure(Lisp 的现代版本)(一种函数式语言)来创建领域模型。 这个演示文稿是一个非常好的介绍(而且它也是一个很棒的演示HTML5)。

长话短说,功能态度与事件排序相结合非常有用。它可以让您非常轻松地创建完全可测试的模型。如果您现在不想跳入全新的语言,现代 C# 是一种非常好的编写类似函数式代码的语言(至少对于实现公共领域模型而言)

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)

叹倦 2024-10-19 03:20:22

老问题但令人惊讶的是今天仍然有意义。

据我所知,关于功能域驱动设计的最好(唯一?)的书是Domain Modeling Made Function,编写作者:Scott Wlaschin,F# for fun andprofit(如上所述)的作者。

在深入了解本书之前,演讲函数式编程设计模式是对这些概念的一个很好的总结(提示:有没有模式 :)

这些示例是用 F# 编写的,但它们很容易翻译成任何其他具有代数类型的函数语言(在我的例子中是 Haskell 和 PureScript)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文