pythonzopezope.component

Understanding Zope Component Architecture and Component Dependency


It's quite difficult to have a good title of my question. From what I understand, the Adapter is to add more services to the components without changing it. The adapter can extends services from multiple components.

But what about the dependency between component? How can I set the dependency between the component A (Person) and the component B (Task) like this normal Python code

class Person:
 pass

class Task:
 def __init__(self, person):
  self.owner = person

If I implements the 2 classes

from zope.interface import Interface
from zope.interface import implements

class IPerson(Interface):
 pass

class Person(object):
 implements(IPerson)

class ITask(Interface):
 pass

class Task(object):
 implements(ITask)
 def __init__(self, person):
  self.owner = person

Is it a good implementation?


Solution

  • The point is that with the ZCA, you don't set a dependency to a concrete object. You use utilities instead.

    Object A would implement an interface, and you look up the interface in B to find a concrete implementation, which could be A, but that depends on the register.

    Note that the ZCA is meant to allow you to plug different implementations for a given interface, and is not always needed.

    If your Task object expects a specific type, then document that. Using the ZCA is not a requirement here. At most, you can try to adapt the passed in value to IPerson; if the passed in object already implements that interface, that action is a no-op:

    class Task(object):
        implements(ITask)
    
        def __init__(self, owner):
            self.owner = IPerson(owner)
    

    That would allow you the flexibility of passing in something else later, something that is not a Person itself, but could be adapted to that interface.