[Zope3-Users] Re: [Zope3-dev] Stylistic question about using adapters

Jim Fulton jim at zope.com
Fri Jan 21 10:29:05 EST 2005


Florent Guillaume wrote:
> [reposting here as it seems nobody reads zope3-users]
> 
> I'm wondering if using adapters is the best way to do the following:
> 
> I have a MailBox class that holds central information, and it contains
> instances of MailFolder that themselve contain MailMessage. From the
> code of a MailMessage, I need to get some config parameters from the
> mailbox. So somewhere I have a function that goes up the containment
> hierarchy to find an IMailBox (I'd rather not make it a method of
> MailMessage because it's really got to do with the containment hierarchy
> and not the behavior of the message itself).

I could see either a method on the mail message to get it's mailbox,
or a function. It depends on whether mail messages are aware of
their mailboxes.

> Is it best to leave this function standalone and call it directly, or is
> it better somehow to make it into an adapter from IMailMessage to
> IMailBox ? I'm not really "adapting" here, I'm returning a completely
> different object, so I have philosophical problems...

I don't have any problem with this adaptation, however, I'm for
keeping things simple.

First, decide whether message objects "care" about their mailboxes
or folders.  If they do, then give them accessor methods or
attributes.  Otherwise, a standalone function or component is
best.

Use a component if you need indirection.  If you want
people to be able to override how mailboxes are looked up
or if different kinds of messages need different lookup
strategies, them use a component. Otherwise use a function.
If in doubt, use a function.  You can always switch to using
a component later if you need it.

If you decide to use a component, use an adapter if the
lookup strategy depends on the message type, otherwise,
use a utility.

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Zope3-users mailing list