Dynamic Web Development with Seaside

26.6Accessors and Mementos

Accessors. In Smalltalk data can be accessed and stored in different ways. Most common data is stored within instance variables and read and written using accessor methods, but sometimes developers choose other strategies, for example to group data within a referenced object, to keep their data stored within a dictionary, or to calculate it dynamically from block closures. Magritte uses a strategy pattern to be able to access the data through a common interface, see Figure 167.

Accessors in Magritte

By far the most commonly used accessor type is the SelectorAccessor. It can be instantiated with two selectors: a zero argument selector to read, and a one argument selector to write. For convenience it is possible to specify a read selector only, from which the write selector is inferred automatically.

The MADictionaryAccessor is used to add and retrieve data from a dictionary with a given key. This access strategy is mainly used for prototyping as it allows one to treat dictionaries like objects with object-based instance variables.

When a memento writes valid data to its model, it does so through accessors which are also defined by the descriptions. MAAccessor subclasses allow you to define how a value gets written to your class.

Mementos. Magritte introduces mementos that behave like the original model and that delay modifications until they are proven to be valid. When components read and write to domain objects, they do it using mementos rather than working directly on the objects. The default memento is MACheckedMemento. The mementos give Magritte a place to store invalid form data prior to allowing the data to be committed to the domain object. It also allows Magritte to detect concurrency conflicts. By never committing invalid data to domain objects, there’s never a need to roll anything back. So mementos are good since editing might turn a model (temporarily) invalid, canceling an edit shouldn’t change the model and concurrent edits of the same model should be detected and (manually) merged. mementoClass is a class factory that you can specialize to specify your own memento. But normally you should not need that.

Mementos (simplified version)

Copyright © 21 April 2024 Stéphane Ducasse, Lukas Renggli, C. David Shaffer, Rick Zaccone
This book is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 license.

This book is published using Seaside, Magritte and the Pier book publishing engine.