可以在Java/Spring应用程序中使用用Scala编写的领域模型吗?

huangapple go评论65阅读模式
英文:

Can we use Domain model written in Scala in a Java/Spring Application?

问题

有没有一种方法可以从一个使用Spring+Java+Maven的项目中使用用Scala编写的领域模型?

在观看了Scott Wlaschin关于函数式领域建模的视频后提出此问题。我希望能够借助Scala实现其中的一些内容,并将函数式领域驱动设计的优势带到我们那些单调的旧Spring代码中!🙏

任何帮助都将不胜感激!

英文:

Is there a way to use a Domain Model written in Scala from a Spring+Java+Maven project ?

Asking this after going through Scott Wlaschin's video on Functional Domain Modelling. Would love to implement some of it using Scala and bring the power of Functional DDD to our mundane old spring code! 🙏

Any help would be wonderful!

答案1

得分: 1

在视频中没有 "ADT magic"。该语言中有一个强大的类型系统和函数式方法。

你可以在一定程度上利用Java的类型系统,也可以使用一些函数式库,比如vavr,或者类似reactor的东西。这将极大地改善你的代码,但它不会像拥有强大类型系统的函数式语言(如F#、Haskell、Scala和其他类似ML的语言)那样强大。

如果你想利用Scala的类型系统(甚至比F#还要强大),你应该几乎在所有地方都使用Scala,因为类型系统的保证在编译器中。你可以从Scala中使用Spring(但我认为这是一个巨大的反模式)。

英文:

There is no "ADT magic" in the video. There is a powerful type system in the language and a functional approach.

You can leverage Java's type system to some degree and also use some functional libraries like vavr or maybe something like reactor. It will improve your code greatly, but it wouldn't be so powerful like in functional languages with great type system (like F#, Haskell, Scala, and other ML-like langs)

If you want to leverage Scala's type system (which is even more powerful then F#) you should use Scala almost everywhere because guarantees of the type system are in the compiler. And you can use Spring from Scala (but I think this is a huge antipattern).

答案2

得分: 1

这里有两个部分。
其中一部分与ADT无关,而是涉及Scala和Java的兼容性。
在Scala中编写的所有内容都会编译为JVM字节码,所以无论你提到的'魔法'是什么,都可以用Java编写,尽管代码会更加冗长和难以阅读。
因此是可用的。
第二部分是类型检查器。
ADT的一部分魔法是编译时的安全检查,比如全面的匹配检查等等...
当你在Java中对一个在Scala中设计的case类使用instanceof时,你将无法获得这种安全检查。

英文:

There are 2 parts here.
One part has nothing to do with ADT's, rather Scala and Java compatibility.
Everything written in scala compiles down to JVM bytecode, so whatever 'magic' you are referring to, can be written in Java, albeit much less concise and readable.
And therefore is usable.
The second part is the type-checker
Part of ADT magic is compile-time safety checks, such as exhaustive match checks amongst others...
You will not get that when you call instanceof in java on a case class designed in Scala

huangapple
  • 本文由 发表于 2020年9月11日 03:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63836310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定