On 11/12/09 1:33 PM, Chris McDonough wrote:
F. Oliver Gathmann wrote:

On 11/12/09 1:00 PM, Tim Hoffman wrote:

I have done some fairly extensive projects using traversal over a
relational model and we did use intermediate container classes alot.
Mainly because each major entity could have about 6 different classes
of sub items that we needed to group into containers anyway

I reverse engineered the entire relational model from the existing
database and then autogenerated all of the intermediate classes.
Quite easy to do.  I was using storm as an orm over the top, which
made things a lot easier
Hmm... still, this feels like an artifact to me. Presumably, in most cases what you want to return when the URL asks for a collection of sub-items is the very collection you already defined in your ORM on the model object that is the current context (we are using SQLAlchemy, BTW). I cannot help but think that there should be a way to automatically return an adapted collection that supports further traversal if necessary... but I'm not sure of all the ramifications of that idea.

I suspect there should be a pattern for this. This pattern might be a good one, though I don't know to what extent it can be generalized:

class TraversableCollection(object):
    def __init__(self, parent, name, collection):
        self.__parent__ = parent
        self.__name__ = name
        self.collection = collection

    def __getitem__(self, name):
        return getattr(self.collection, name) # or whatever the API is

class Cars(object):
    def __getitem__(self, name):
        collection = get_collection(model, name) # some helper thing
        return TraversableCollection(self, name, collection)

Might be generalizable via a subclass:

class Collection(object):
    def __getitem__(self, name):
        collection = get_collection(model, name) # some helper thing
        return self.collection_class(self, name, collection)

class Cars(Collection):
    collection_class = TraversableCollection

Or via Zope-style adaptation.

- C

Cool - this is exactly what I had in mind.

I am really impressed by the number and quality of the comments I got on my little newbie question! Seems we made the right choice by basing our new server framework on repoze.bfg ;-)



Repoze-dev mailing list

Reply via email to