Warning: no-coherent think out loud rambling ahead.
I've been knee deep in refactoring for Handel 1.x. For the most part, it's going
well and I've had to resort to minuscule amounts of magic to alter/enhance/molest
DBIC schemas on the fly. The current progress can be found
here.
It's been a long journey, mostly spent figuring out how to genericize the use
of DBIC schemas away from the actual Cart/Order/Item classes. The
Intro
and Storage
manual pages give a basic overview of how it is currently.
Unfortunately, I'm at a point where I can't see the forest any more because
I've been staring at the trees too long. From the Storage perspective, I can call
create/search methods, and get generic Storage::Result objects. it all works.
Everything is happy.
my $storage = Handel::Storage::Cart->new({ schema_class => 'Handel::Cart::Schema', schema_source => 'Carts', connection_info => ['dbi:SQLite:handel.db'], item_relationship => 'items' });
# returns Storage::Result object my $result = $storage->create({ shopper => 1, name => 'My Favorites' }); print $result->name;
# returns Handel::Iterator, that inflates to Storage::Result objects my $results = $storage->search; while (my $result = $results->next) { print $result->name; };
package Handel::Cart; __PACKAGE__->storage_class('Handel::Storage::Cart');
sub new { my ($self, $data) = @_; my $result = $self->storage->create($data); # creates a new Handel::Cart instance, storing storage result as needed return $self->create_instance($result); };
sub add { my ($self, $data) = @_; my $result = $self->result;
# which is worse here? my $item = $result->add($data); # or, where add_item in storage does $dbicresult->add_related('items', $data) $my $item = $self->storage->add_item($self->result, $data); # or $my $item = $result->storage->add_item($self->result, $data); ... };
sub add { my ($self, $data) = @_; my $result = $result;
# like Handel::Cart::Item->storage->create($data) my $item Handel::Cart::Item->new($data); $result->storage->add_item($item); };
package Handel::Cart; __PACKAGE__->storage_class('Handel::Google::CheckoutAPI::Cart');
So, people writing custom storage layers can either tweak behavour at the result level, or at the storage level, or both. A custom storage layer will know what to do with it's own stuff; be that adding through a relation on the result object, or adding a new item through a completely different storage object in the item class.Handel::Cart::add {
return $self->result->add_item($data);
};
...
$result::add_item {
return $self->storage->add_item($self, $item);
};
...
$storage::add_item {
return $result->add_related($self->item_relationship, $data);
};