我应该在哪里/如何对 Google App Engine 中的实体进行验证和转换?

发布于 2024-08-12 18:54:16 字数 395 浏览 5 评论 0原文

在 Ruby on Rails 中,每个模型实体都有一个“validate_on_something”挂钩方法,该方法将在实体实际保存到数据库之前调用。我希望 Google App Engine 具有类似的功能。我知道您可以通过在声明中向它们传递参数来对各个属性进行验证。但是,如果我希望进行更多验证,模型类声明中是否有某个位置可以执行此操作?

同样,有时实体在实际持久化到数据库之前需要进行修改。我可能需要在实体实际写入数据库之前修改(转换)实体。实体类声明中是否有某个地方允许我这样做?

我知道我可以将这些转换/验证放在课堂之外。但这看起来并不是一个好的面向对象设计。看起来确实应该有一些钩子方法可以在模型中自动调用以满足此类需求。

所以我的问题是,在持久化实体之前处理实体的验证和转换的最合适方法是什么?

In Ruby on Rails, each model entity has a "validate_on_something" hook method, that will be called before the entity is actually persisted to the database. I would like similar functionality in Google App Engine. I am aware that you can do validation on individual Properties by passing arguments to them in their declarations. However, if I wish to do more validation than that, is there some place within the model class declaration within which I can do that?

Also, along the same lines, sometimes a entity needs modification before it is actually persisted to the database. I might need to modify (transform) the entity right before it is actually written to the database. Is there some place in the entity class declaration that would allow me to do so?

I am aware that I can put these transformations/validations outside of the class. Bu this hardly seems like good OO design. It really seems like there should be hook methods that would automatically be called in a model for these sort of needs.

So my question is, what is the most appropriate way to handle the validation and transformation of entities before they are persisted?

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

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

发布评论

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

评论(2

胡大本事 2024-08-19 18:54:17

您是否在原始应用程序引擎 API 之上使用任何类型的 Web 框架? Rails 是一个非常高级的框架。您研究过 Django 或任何其他 Web 框架吗?您可能会发现它们比原始应用程序引擎实体更接近 Rails。

或者,如果您想要较低级别的东西,请查看这篇文章
挂钩

Are you using any kind of web framework on top of the raw app engine api's? Rails is a very high level framework. Have you looked into Django or any of the other web frameworks? You may find those are closer to rails than raw appengine entities.

Alternatively, if you want something lower level, have a look at this article on
hooks

谁对谁错谁最难过 2024-08-19 18:54:16

最好的答案取决于您需要进行什么样的转换。模型没有通用的预置/后置方法,但还有其他几种选择:

  • 正如您所提到的,您可以将验证函数传递给 Property 类构造函数
  • 您可以使用以编程方式生成值的自定义属性类,例如 这个
  • 您可以使用 api 调用挂钩

The best answer depends on what sort of transformations you need to do. There's no generalized pre-/post- put methods for models, but there are several other options:

  • As you mentioned, you can pass validation functions to Property class constructors
  • You can use a custom property class that generates values programmatically, such as this one.
  • You can modify entities as they are stored at the lowest level using api call hooks.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文