1. Defining the Object Model
Starting with version 1.10, ReStore offers support for using and storing Dictionaries. Like the other collection classes, their use is straightforward and transparent. However, they are detailed separately as their specification is slightly different.
Specifying a Dictionary
The main difference between Dictionaries and other collection classes is that each element of a Dictionary is essentially storing two objects - the key and the value. Hence the specification of a Dictionary must include the class of both types of object. This is done with the Dictionary method of:keyClass:
define: #dailyHoursWorked as: (Dictionary of: Float keyClass: Date));
define: #productLocations as: (Dictionary of: Location keyClass: Product));
As with other collection classes, of:keyClass: is defined as both an instance method and a class method which creates an empty template collection. Also like other collections, the class of object held (for both key and value) will be the same for all elements of that collection, except in the case of inheritance.
Sometimes, for reasons of logic or efficiency, it is appropriate to hold a Dictionary where the key is some attribute of the value. For example, say you have a ClientCompany object - this has a potentially large collection of Addresses of individual offices:
Client Company Offices Widgets International, Inc. Schupstraat 24, Antwerp
The Widgets Centre, Zagreb
Transdiffussion Worldwide Westerstraat 93, Amsterdam
Transdifussion House, Zurich
Your application has a requirement to quickly find the Office in a particular city - hence you would like to store the collection of Addresses as a Dictionary mapping city name to actual Address. Since this arrangement is effectively a lookup cache, ReStore terms such structures Cache Dictionaries.
In this scenario, ReStore offers an optimised form of Dictionary storage - because the key is held in the value object, there is no need to hold it in a separate mapping table, and so less time is required both to read and write such a structure to the database. To specify such a Dictionary, you use the Dictionary method of:keyedOn:
define: #cityOffices as: (Dictionary of: Address keyedOn: #city)
The arguments for and against the use of a Cache Dictionary are similar to those of Owned Collections. In its favour is efficiency - if your application exhibits this requirement then it is sensible to consider using a Cache Dictionary arrangement. As with owned collections, if in doubt you can use a 'regular' Dictionary.
1. Defining Classes