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

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

问题描述

在 Ruby on Rails、Django 和 CakePHP 等 MVC Web 开发框架中,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.

这些框架都非常面向对象.我最近一直在阅读有关函数式编程的文章,它似乎吹捧了巨大的好处,例如可测试性、简洁性、模块化等.但是,我见过的大多数函数式编程示例都实现了诸如快速排序或斐波那契数列之类的微不足道的功能,而不是复杂的网络应用程序.我查看了一些功能性"Web 框架,它们似乎都很好地实现了视图和控制器,但在很大程度上跳过了整个模型"和持久性"部分.(我更多地谈论像 Compojure 这样的框架,它们应该是纯函数式的,而不是像 Lift 这样方便地将 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 查询来持久化数据的整个概念似乎违反了整个副作用"概念.有人可以解释一下如何在功能编程的 Web 框架中实现模型"层?

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?

推荐答案

不想打击面向对象的 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.

当然,在面向对象语言中,根据对象来考虑表似乎很自然,但在函数式语言中,根据表来考虑表是完全自然的.可以使用代数数据类型(在 Haskell 和其他静态类型函数语言中)或映射(又名字典;将键映射到值的关联结构)轻松表示单行;一个表然后变成一系列行,毕竟它甚至在数据库级别.因此,没有从表的 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 感兴趣,这是一种用于与以通用方式使用各种 DB.(请注意,它刚刚正在重新设计.)您可以使用一些这样的 DSL 来提取数据;使用纯函数操作由此获得的数据;然后显示一些结果,并可能将一些数据持久化回数据库(使用相同的 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 如果您认为将一项技术与越南战争进行比较有点极端,我想我同意,但这并不意味着该文章没有很好地说明原因人们可能不想陷入 ORM 的泥潭.

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 语言中使用相同的方法,并以与在 FP 语言中相同的方式抽象 DB 后端(请参阅下一段).当然,那么您的 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 类型的 webapp 中的“模型"将如何用函数式编程语言实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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