简体   繁体   English

在clojure中构建Web应用程序时,页面的预处理应该在哪里?

[英]When building web apps in clojure, where should preperations for a page belong?

In web frameworks like ruby on rails, any database queries needed to handle a specific request happen before a page is passed to the client in a controller class, but there's nothing like that for clojure. 在像rails上的ruby这样的Web框架中,处理特定请求所需的任何数据库查询都是在页面传递给控制器​​类中的客户端之前发生的,但是对于clojure来说没有类似的东西。 Where should database queries and stuff like that be handled in a clojure web app? 数据库查询和类似的东西应该在clojure Web应用程序中处理? My gut tells me to call a function within a hiccup page and generate the HTML within that function, but I'm not really sure. 我的直觉告诉我在一个打嗝页面中调用一个函数并在该函数中生成HTML,但我不太确定。 Thank you for your time and consideration. 感谢您的时间和考虑。

There aren't that many "full stack" web frameworks in Clojure and as far as I know most Clojure web applications aren't built with one. Clojure中没有那么多“全栈”Web框架,据我所知,大多数Clojure Web应用程序都没有构建。 There is a collection of frameworks and tools that handle a variety of things, but you'll likely develop your application using these components as building blocks. 有一系列框架和工具可以处理各种各样的事情,但您可能会使用这些组件作为构建块来开发应用程序。 You have a choice of routing functions, authentication, view rendering, templating, RESTful web services and persistence. 您可以选择路由功能,身份验证,视图呈现,模板,RESTful Web服务和持久性。

Where particular things should go in your application depends on your architecture - a typical, 3-tier MVC web application looks different from a full blown scalable app that's using CQRS , CEP and other fancy patterns that help you build the next Facebook or Twitter. 应用程序中应该包含的特定内容取决于您的体系结构 - 典型的3层 MVC Web应用程序与使用CQRSCEP和其他有助于构建下一个Facebook或Twitter的其他花哨模式的完整可扩展应用程序不同。

If you design your web app conceptually with a 3-tier MVC architecture in mind, you'll have a clear separation between your view layer, business logic, and persistence layer. 如果您在概念上使用3层MVC架构设计Web应用程序,则可以在视图层,业务逻辑和持久层之间进行明确分离。 Like Alex said in his answer - it's probably the Controller that ties these things together. 就像Alex在他的回答中所说 - 可能是控制器将这些东西联系在一起。 If you don't have complex business logic, your controller will likely call functions from your persistence layer directly before passing it on to the logic that builds your views. 如果您没有复杂的业务逻辑,您的控制器可能会直接从持久层调用函数,然后再将其传递给构建视图的逻辑。

For some situations, it might be useful to pull in data from your persistence layer in a "middleware" — that is a function that gets called by Ring every time a request comes in. This could be the stored information about a logged in user for example. 对于某些情况,在“中间件”中从持久层中提取数据可能很有用 - 这是每次请求进入时由Ring调用的函数。这可能是有关登录用户的存储信息例。

IMO, whichever code generates your views should call the function, and use the result of the function to dictate how to render the view. IMO,无论哪个代码生成您的视图都应该调用该函数,并使用函数的结果来指示如何呈现视图。 Looked at from the Clojure perspective, Controllers could be seen as functions that are called by the router. 从Clojure的角度来看,控制器可以看作是路由器调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM