Tuesday, February 10, 2009

Thoughts on ORM in Perl

One thing that I really admire from Java world is the EJB3 Persistence annotations. A class can be made persistent with a simple mark-up:

@Entity
class Foo {
private int count;
private String name;
@Transient
private int temp_var;
....
}

The @Entity notation indicates that the class should map to a table (unless overridden, it will use the class name as the table name). Every field then automatically maps to a column in the table (column names can be overridden) unless marked with the @Transient annotation.

I was thinking that something similar could be executed in Perl using perl attributes. I could do something like

package Foo;
use Object::InsideOut;

my @int :Field :Entity;
my @string :Field :Entity;
my @temp_var :Field;
...

Using something like Object::InsideOut seems essential for this sort of attribute-based mark-up, although perhaps I should look at Moose instead (although that does seem to have some significant performance penalties). There are some questions about how to handle exporting the serialization routines. Should I work towards a DAO solution or set up serialization routines directly in the class (perhaps as protected by default with the class making a decision about whether to provide public methods to expose these to clients.

No comments: