在 Scala/Lift 中组织会话变量

发布于 2024-11-15 21:12:23 字数 429 浏览 2 评论 0原文

想要了解如何在 scala / lift 应用程序中最好地组织会话变量的一些想法。

我在线阅读了许多 scala 材料,并在引入会话变量的所有示例中发现了以下范式:

  1. 声明一个扩展 SessionVar 类的对象,
  2. 将该对象放入包含片段(或任何文件)
  3. 访问 的文件中从代码库中的任何位置获取该对象(l​​ift 将根据用户的 http 会话的生命周期处理会话变量的生命周期)

也许我不理解某些内容,但我担心这种方法会导致一大堆这些对象在不同的​​文件中到处都是。如果它是一个小应用程序,这没什么大不了的,但当项目变得更大时,这可能会导致混乱。

对于那些从事过大型 Scala 项目的人来说,是否有一种普遍接受的更好方法? (即使它很简单,比如将所有这些对象放入一个公共文件中?)

谢谢。

Would like to get some thoughts on how to best organize session vars within a scala / lift application.

I've read over a number of scala materials online and have found generally the following paradigm in all examples that introduce session vars:

  1. declare an object that extends the SessionVar class
  2. put that object into a file that contains a snippet (or any file)
  3. access that object from anywhere in the codebase (lift will take care of the session var's lifecycle based on the lifetime of the user's http session)

Perhaps I'm not understanding something, but I'm concerned that this approach would lead to a whole bunch of these objects in various files all over the place. Its not such a big deal if its a small app, but when a project gets larger this could lead to chaos.

For those who have worked on larger scala projects, is there a generally accepted better approach? (even if its something simple like putting all of these objects into a common file?)

Thanks.

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

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

发布评论

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

评论(2

过期情话 2024-11-22 21:12:23

这有点主观,但我会尝试一下。我认为这取决于会话变量在您的项目中的范围。

  • 如果您仅在一个代码片段中需要会话变量,则应该将其设为该类的私有成员。
  • 如果您需要在几个但不是所有片段中使用它,请将这些片段放入一个包中,并将该对象设为该包的私有对象。如果您有很多,您可以创建一个额外的文件来保存它们。
  • 如果您全局需要它,请将其放在一个中心位置,也许在一个包对象内。
  • 如果可能,请完全避免使用 SessionVars。

This is a bit subjective, but I'll give it a try. I think it depends on the scope the session var has in your project.

  • If you need the session var only in one snippet, you should make it a private member of that class.
  • If you need it in several but not all snippets, put those snippets in a package and make the object private to that package. If you have a lot of them, you could create an extra file to hold them.
  • If you need it globally, put it into a central location, maybe inside a package object.
  • If possible, avoid using SessionVars completely.
两相知 2024-11-22 21:12:23

在您的应用程序中应谨慎使用 SessionVars。它们与 Servlet 会话变量类似,但它们是类型安全的。

您需要多少个会话变量?就我个人而言,我有一个当前用户主键的会话变量,也许还有一个或两个。应用程序的其余状态应存储在闭包中(因为与 GUID 关联的函数超出了范围)。

SessionVars should be used sparingly in your application. They are similar to Servlet Session Variables, except they are type safe.

How many session variables do you need? Personally, I have a session variable for the primary key of the current user and maybe one or two more. The rest of the state of the application should be stored in closures (because functions associated with GUIDs close over scope).

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