2007-11-24

Using discriminators in adapter registration

Note: This entry has been updated to reflect API changes in version 0.2.

As a follow-up to my previous post on why discrimination is good, I'd like to share a new package that provides a formalism to register adapters with discriminators.

  easy_install z3c.discriminator
The package provides methods to register an adapter with zero or more adapted arguments designated as discriminators:
  >>> from z3c.discriminator import discriminator
>>> from zope.component import provideAdapter

>>> provideAdapter(Factory, (IFoo, discriminator(IBar)))
To look up the adapter you would provide both arguments, but the discriminators won't be passed on to the adapter factory.

This also works from a configuration file. Prefixing the dotted path argument with a minus will set that argument as a discriminator.
<adapter for="package.IFoo -package.IBar"
factory="package.SomeAdapter" />
See the README.txt for more information.

0 comments: