Rails类型Web应用程序中的“模型”如何在函数式编程语言中实现? [英] How would the 'Model' in a Rails-type webapp be implemented in a functional programming language?

查看:71
本文介绍了Rails类型Web应用程序中的“模型”如何在函数式编程语言中实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC Web开发框架(如Ruby on Rails,Django和CakePHP)中,HTTP请求被路由到控制器,控制器获取通常保存到后端数据库存储中的对象。这些对象表示用户,博客帖子等内容,并且通常在其方法中包含用于权限,获取和/或变更其他对象,验证等的逻辑。

In MVC web development frameworks such as Ruby on Rails, Django, and CakePHP, HTTP requests are routed to controllers, which fetch objects which are usually persisted to a backend database store. These objects represent things like users, blog posts, etc., and often contain logic within their methods for permissions, fetching and/or mutating other objects, validation, etc.

这些框架都非常面向对象。我最近一直在阅读函数式编程,它似乎吹嘘巨大的好处,如可测试性,简洁性,模块化等。然而,我看到的大多数例子我已经看到的功能编程实现琐碎的功能,如quicksort或fibonnacci序列,不是复杂webapps。我看了几个功能的Web框架,他们似乎实现视图和控制器很好,但大部分跳过了整个模型和持久性部分。 (我正在谈论更多的框架,如Compojure应该是纯粹的功能,而不是东西电梯,方便地似乎使用Scala的OO部分的模型 - 但纠正我,如果我在这里错了。)

These frameworks are all very much object oriented. I've been reading up recently on functional programming and it seems to tout tremendous benefits such as testability, conciseness, modularity, etc. However most of the examples I've seen for functional programming implement trivial functionality like quicksort or the fibonnacci sequence, not complex webapps. I've looked at a few 'functional' web frameworks, and they all seem to implement the view and controller just fine, but largely skip over the whole 'model' and 'persistence' part. (I'm talking more about frameworks like Compojure which are supposed to be purely functional, versus something Lift which conveniently seems to use the OO part of Scala for the model -- but correct me if I'm wrong here.)

我没有看到一个很好的解释,如何使用函数式编程提供OO编程提供的隐喻,即表映射到对象,对象可以有方法提供强大,封装逻辑,例如许可和验证。此外,使用SQL查询来保留数据的整个概念似乎违反了整个副作用概念。

I haven't seen a good explanation of how functional programming can be used to provide the metaphor that OO programming provides, i.e. tables map to objects, and objects can have methods which provide powerful, encapsulated logic such as permissioning and validation. Also the whole concept of using SQL queries to persist data seems to violate the whole 'side effects' concept. Could someone provide an explanation of how the 'model' layer would be implemented in a functionally programmed web framework?

推荐答案

不想要在一个功能编程的web框架中实现模型 bash面向对象的MVC框架 - 我不知道Rails,但Django是一个优秀的软件,我的眼睛 - 我不确定对象关系映射是一个特别好的隐喻 1

Without wanting to bash object oriented MVC frameworks -- I don't know Rails, but Django is an excellent piece of software to my eye -- I'm not sure that Object-Relational Mapping is a particularly good metaphor1.

当然,在OO语言中,想要根据对象来考虑表格似乎很自然,但在功能语言中,根据表格来考虑表格是非常自然的。可以使用代数数据类型(在Haskell和其他静态类型函数语言中)或映射(a.k.a.字典;将键映射到值的关联结构)来轻松表示单个行;一个表然后成为一个行的序列,毕竟它甚至在DB级别。因此,没有从表的DB构造到编程语言中可用的某个其他构造的特殊映射;你可以简单地使用两边的表。 2

Of course in an OO language it may seem natural to want to think of tables in terms of objects, but in a functional language it is perfectly natural to think of tables in terms of tables. A single row can be represented easily using an algebraic data type (in Haskell and other statically typed functional languages) or a map (a.k.a. a dictionary; an associative structure mapping keys to values); a table then becomes a sequence of rows, which after all it is even at the DB level. Thus there is no special mapping from the DB construct of a table to some other construct available in the programming language; you can simply use tables on both sides.2

现在这不意味着有必要使用SQL查询来操作DB中的数据,前面描述了抽象对RDBMS的奇怪的优点。由于您使用的是Clojure代码,因此您可能对 ClojureQL 感兴趣,这是一种嵌入式DSL,用于与各种DB以一种通用的方式。 (注意,它刚刚被重做。)你可以使用一些这样的DSL来提取数据;使用纯函数来操纵这样获得的数据;然后显示一些结果,并可能将一些数据保存到DB(使用相同的DSL)。

Now this does not in any way mean that it is necessary to use SQL queries to manipulate the data in the DB, foregoing the benefits of abstraction over varios RDBMSs' quirks. Since you're using the Clojure tag, perhaps you might be interested in ClojureQL, an embedded DSL for communicating with various DBs in a generic way. (Note that it's being reworked just now.) You can use some such DSL for extracting data; manipulate the data thus obtained using pure functions; then display some results and maybe persist some data back to the DB (using the same DSL).

1 如果你认为将技术与越南战争进行比较有点极端,我想我同意,但这并不意味着文章没有做一个很好的工作,描述为什么一个人可能不想下沉

1 If you think comparing a technology to the Vietnam war is a bit extreme, I guess I agree, but that doesn't mean that article doesn't do a very good job of discribing why one might not want to sink in the ORM quagmire.

2 请注意,您可以在OO语言中使用相同的方法,并以同样的方式抽象DB后端它是用FP语言完成的(见下一段)。当然,你的MVC框架将不再像Rails。

2 Note that you could use the same approach in an OO language and abstract over DB backends in the same way in which it's done in FP languages (see the next paragraph). Of course then your MVC framework would no longer look quite like Rails.

这篇关于Rails类型Web应用程序中的“模型”如何在函数式编程语言中实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆