[Zope3-dev] Providing flexible adapters

Jim Fulton jim@zope.com
Tue, 26 Feb 2002 17:07:08 -0500


Steve Alexander wrote:
> 
> If only there were not the constraint on the Adapter Service that you
> provide an adapter between two particular interfaces...
> 
> Otherwise, I think what I've called IReferenceGenerator, below, is
> really just a matter of registering an adapter to provide an IReference
> for anything that it is given.
> 
> I can think of other scenarios where I'd want to provide generated
> adapters, such as using TransWarp (or another generative programming
> tool) with Zope 3.
> 
> Here's how I'd like to change the IAdapterService interface.
> This is what we have now. I've shortened the docstrings.
> 
> class IAdapterService(Interface):
> 
>      def provideAdapter(forInterface, providedInterface, maker):
>          "Provide an adapter"
> 
>      def getAdapter(object, interface, default=None):
>          "Look up an adapter that provides an interface for an object"
> 
> I'd like to either
> 
>    * Split this into two interfaces: a getter and a mutator

Yes, they should be separated. In general, the provide methods
should not be in the interfaces.

>    * Add a method for providing flexible adapters
> 
> An example of such a method is:
> 
>      def provideFlexibleAdapter(forInterface, maker):
>          """Provide an adapter that can provide "forInterface" objects
>             for a variety of provided interfaces"""
> 
> Or, we could keep the same provideAdapter method, and say that if None
> is given for providedInterface, we are providing a flexible adapter.

You are mixing for and provided. The "provided" interface is the interface
you want to get. The "for" interface can already be None.
 
> This could be implemented by a small specialisation of the IToIRegistry.
> If the IToIRegistry finds no better match for (ob_interface, provide),
> it returns the match for (ob_interface, None), if there is one.
> 
> Then again, perhaps this is just a Utility rather than an adapter...
> It looks similar to a utility by the way it is provided, but it provides
> the service of adapting an object to a particular interface. Hmm... I
> think it is still a kind of adapter.

It *is* an adapter because it only makes sense in the context of the
object you are providing a reference for.

Jim

--
Jim Fulton           mailto:jim@zope.com       Python Powered!        
CTO                  (888) 344-4332            http://www.python.org  
Zope Corporation     http://www.zope.com       http://www.zope.org