Implicit or Explicit getter and setter in behavior?

Stupid question... What are the implications of:

registering a behavior without a factory, as documented here: https://docs.plone.org/external/plone.app.dexterity/docs/behaviors/schema-only-behaviors.html#storing-attributes - which causes the attribute to be stored directly on the object

vs

the structure provided by mr.bob seen here https://github.com/plone/bobtemplates.plone/blob/master/bobtemplates/plone/behavior.py and here https://github.com/plone/bobtemplates.plone/blob/master/bobtemplates/plone/behavior/behaviors/%2Bbehavior_name_normalized%2B.py.bob - which includes a marker interface and registration of the factory and marker, and therefore requires an explicit property getter and setter?

1 Like

My approach is using this context_property() factory in behaviors:


def context_property(name, default=None):
    def getter(self, default=default):
        return getattr(self.context, name, default)

    def setter(self, value):
        setattr(self.context, name, value)

    def deleter(self):
        delattr(self.context, name)

    return property(getter, setter, deleter)
```

...

```

@implementer(IGuideline)
@adapter(IConnector)
class Guideline(object):
    """ Adapter for Guideline """

    def __init__(self, context):
        self.context = context

    notice = context_property("notice")
    portlet_text = context_property("portlet_text", "")
    portlet_title = context_property("portlet_title", "")
    gl_language = context_property("gl_language")
    gl_state = context_property("gl_state", "")
    gl_date = context_property("gl_date", "")

```
3 Likes

Disclaimer : the handling of default value is possible wrong and not working in reality.

Looks like others have wondered about this situation before:

answered by @jensens - Cheers!

At the end the simple rule:

If you use schema only, the context provides the schema interface (because it stores the values as attributes)
If you use behavior-adapters, always provide a marker. Its semantically correct. If you use the adapters interface as a marker it might work, but its semantically wrong.

and a rather good explanation exists in the docs...

1 Like

Plone Foundation Code of Conduct