将逻辑从模板工具包转移到 Catalyst

发布于 2024-12-26 12:45:46 字数 198 浏览 0 评论 0原文

我认为我在 TT 模板中使用了太多的条件和计算。

我正在显示 DBIc 的项目结果集。对于每个项目,我需要使用检索到的值来计算内容,而模板似乎不是正确的位置。

但在 Catalyst 中,它是来自 DBIc 的厚对象。

那么如何将逻辑转移到模型中呢?我必须对所有项目运行整个循环并以某种方式更改对象吗?

问候: 米格,

I think that I am using too much conditionals and calculations in the TT templates.

I am displaying a result set of items from DBIc. For each item I need to calculate things using the retrieved values, and the template doesn't seems to be the right place.

But in Catalyst it is a thick object that comes from DBIc.

So how can I move logic to the model? Must I run a whole loop for all items and change the object somehow?

Regards:
Migue,

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

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

发布评论

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

评论(3

何以笙箫默 2025-01-02 12:45:46

首先,你想要正确地分离关注点,这是正确的轨道。如果您是 6-12 个月后的维护者,您会感谢自己。

恕我直言,您的 Catalyst 控制器应该尽可能精简,并包含各种模型中的业务逻辑。这使得测试变得更容易,因为您无需处理 Catalyst 的开销。我自己也一直在思考模型分离的问题。我遇到过两种思想流派:

1) 让您的 DBIx::Class Result 类具有业务逻辑。这种方法既方便又简单。

2) 创建一个由控制器实例化的独立模型,该模型具有 DBIx::Class 模式对象。该模型将使用 DBIC 模式来查询数据库,然后在其自己的业务逻辑方法中使用结果数据。如果您有大量业务逻辑,那么这种方法可能会更好,因为您将数据库访问与业务逻辑分开。

就我个人而言,我过去一直使用方法 #1,但对于大型应用程序,我倾向于使用方法 #2。

First, you're on the right track by wanting to properly separate concerns. You'll thank yourself if you're the maintainer 6-12 months down the road.

IMHO, your Catalyst controllers should be as thin as possible with the business logic in the various models. This makes it easier to test because you don't have the overhead of Catalyst to deal with. I've been thinking about model separation quite a bit myself. There are two schools of thought I've come across:

1) Make your DBIx::Class Result classes have the business logic. This approach is convenient and simple.

2) Make a standalone Model which is instantiated by the Controller, and which has a DBIx::Class schema object. The model would use the DBIC schema to query the database, and then use the resulting data in its own business logic methods. This approach might be better if you have a lot of business logic since you separate DB access from business logic.

Personally, I've historically used approach #1 but I'm leaning towards #2 for larger apps.

影子是时光的心 2025-01-02 12:45:46

两种可能性。

  1. 在相应的模式类中创建一个方法。

  2. (如果 1 不可能)将回调传递给将此对象作为参数的模板。

Two possibilities.

  1. Create a method in corresponding schema class.

  2. (if 1 is not possible) Pass a callback to template that would have this object as argument.

等风也等你 2025-01-02 12:45:46

您可以

  1. 创建一个结果集,从数据库中检索数据,然后计算所需的值
  2. (如果可能)计算数据库中所需的值,然后仅检索输出所需的数据

我个人更喜欢第二个。
我希望这有帮助。

You could

  1. create a resultset that retrieves the data from the database and then calculates the needed values
  2. if possible calculate the needed values within the database and then only retrieve the data needed for output

I personally would prefer the second one.
I hope that helps.

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