views:

35

answers:

2

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?

+1  A: 

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

Peter Recore
No, just raw app engine unfortunately. I should look at Django, but I just wanted to get started at the time. :)Article on hooks is interesting. But I don't see a mention in it about what things you can hook into, any idea where to find that?
Stephen Cagle
+2  A: 

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.
Nick Johnson
I started reading the article about derived properties, and it seems like a good idea. Thanks.
Stephen Cagle