On Fri, Mar 10, 2006 at 04:19:44PM +0100, Martijn Faassen wrote:
> Hi there,
> 
> I notice a pattern in code that uses annotations that looks like this:
> 
> class Foo(Persistent, Contained):
>     implements(interfaces.IFoo)
> 
> def getFoo(context):
>     annotations = IAnnotations(context)
>     try:
>         return annotations[FOO_KEY]
>     except KeyError:
>         foo = Foo()
>         annotations[FOO_KEY] = foo
>         # to please security...
>         zope.app.container.contained.contained(
>             foo, context, 'foo')
>         return foo
> 
> # Convention to make adapter introspectable
> getFoo.factory = Foo
> 
> If I'm doing this quite a bit, this looks like something that would be 
> better expressed in a... new ZCML directive (..waiting for the crowd to 
> start flinging stones).
> For instance, one that looks like this:
> 
> <zope:annotation for="IBar" factory="Foo" />
>
> where the factory actually points to whatever creates the real 
> annotation (such as the Foo class in this case), not to the boilerplate 
> to attach it to the proper object. The boilerplate would go away from my 
> code, you could use the facility too without thought, and we'd all be 
> happy. :)

-1

I'd prefer

    from zope.annotation.adapter import AnnotationAdapter

    getFoo = AnnotationAdapter(for_=IBar,
                               interface=IFoo,
                               factory=Foo,
                               key=FOO_KEY)
    # I suppose the key could be optional; you could use a dotted
    # interface name by default

and then the ordinary

    <zope:adapter factory=".foo.getFoo" />

I think this is exactly the same as Jeff Shell's suggestion, but its
3am, and I'm too tired to read his entire message.

Marius Gedminas
-- 
We have an advanced scalable groupware communication environment (email)
        -- Alan Cox

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to