1

This is a really basic design question, but I can't seem to find an answer. I'll use a really simple example, but my concern is for cases where the model-level calculations, "2+2" in this example, become complex.

Let's say I'm designing an application to run in Zope or Plone; its job is to add two numbers. Dexterity lets me easily create a content type with fields addend1 and addend2, and will generate add, edit, display forms for me. I could modify the display form to also present a field named "sum," but "sum" is a result of extensive calculation in my model, and I don't want to present it in the "add" or "edit" forms because I don't need that as input, just as a result of calculation. I don't want to calculate it in my view, because it's expensive, I'd like to reuse it after initially calculating it, and I don't want my "view" to need any knowledge of how I do my calculations anyway.

So what is the proper "zca"ish approach for doing model-work that has nothing whatsoever to do with presentation of results? Do I create an interface like ISum and create an adapter that converts my content type to one including a sum, then do a view for the ISum interface? If not that, what? Searching PP4D and the Zope 3 Developers Handbook hasn't helped.

Thanks in advance for any insights.

mjtrac
  • 187
  • 9

1 Answers1

5

Use events; you can register event handlers for when your content type is added, or edited, and you set the value on the object whenever these events trigger your handler.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks. That's an approach I've experimented with; it feels like an odd way to do things, though. I'd welcome more opinions about whether this is considered the standard way to do what I'm talking about. If it is, I can get past my feelings. – mjtrac Nov 15 '12 at 19:22
  • OK, I see Martijn is a god, or at least a demi-god, so I'll go with the add and edit events unless I see counterarguments here. Thanks again. – mjtrac Nov 15 '12 at 19:35
  • + 1 for Martijn's answer, no counterargument, confirming your estimation about Martijn's god-state ;) – Ida Nov 16 '12 at 09:22