[Zope3-Users] How come no IView?

Chris McDonough chrism at plope.com
Sun Jan 1 12:50:04 EST 2006


It will probably not comfort you that the concept of a "view" (at  
least by that name) is going to disappear sometime post-3.2.

I hope I explain this properly; here goes.

A view is a registration for a "named multiadapter".  The thing that  
is registered ("the view") adapts two objects that implement  
interfaces to a different interface.  Part of what makes a "view" a  
"view" is that it's registered to accept two objects in its factory:  
a content object and a request object.  Another kind of "named  
multiadapter" might accept three, four, or five objects in its  
factory.  A view happens to accept two, and only by convention are  
these two objects that are "context" and "request" objects.

When you look up a "view", the two objects you pass to  
"getView" (context and request) conventionally implement,  
respectively, IBrowserRequest and a content class interface.
There is a class registered to adapt them to a marker interface  
(zope.Interface).  So when a view is looked up like this:

from zapi import getView
from zope.interface import Interface
from zope.interface import implements
from zope.publisher.interfaces.browser import IBrowserRequest

class IContent(Interface):
     pass
class Content(object):
     implements(IContent)
content = Content()
class Request(object):
     implements(IBrowserRequest)
request = Request()

getView(content, u'index.html', request)

... under the hood the lookup is translated to:

getMultiAdapter((content, request), Interface, name=u'index.html')

... and you get back a view class instance.  The view class  
constructor is passed "content" and "request" in its constructor  
because that's what getMultiAdapter is wired to do.  If you were  
adapting more than two, the class constructor would be passed three,  
or four, etc.

This is because the registration of a view class (when it happens via  
ZCML) is essentially:

from zope.component import registerAdapter

class SomeView(object):
     def __init__(self, context, request):
         self.context = context
         self.request = requests

registerAdapter(SomeView, (IContent, IBrowserRequest), Interface,  
name=u'index.html')

The concept of a "view" is purely convention.  getView is actually  
deprecated in the trunk at least, as a result.  I don't know if that  
makes anything clearer but I for one welcome our new simplification  
overlords.

On Jan 1, 2006, at 12:20 PM, Wade Leftwich wrote:

> Since Zope 3 is all about being self-documenting and discoverable, it
> seems odd that something as central as a View has the "implicit"
> attributes 'context' and 'request'. Is there an architectural reason
> that we don't say that a View class implements an IView interface that
> gives the names of the expected attributes?
>
> If this question is hopelessly naive, please be gentle.
>
> -- Wade Leftwich
> Ithaca, NY
>
>
>
> _______________________________________________
> Zope3-users mailing list
> Zope3-users at zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users
>



More information about the Zope3-users mailing list