This may be the right time to adopt purely functional lazy data structures.
In short, this requires the prohibition of any mutating operations on your Objects, i.e. creating all instances of an immutable object. Then you reverse engineer all operations that modify the existing object to create a new instance of the object based on the old one.
For example, suppose you have an instance of Order
that contains an OrderItem
list, and you need to add a specific OrderItem
to this list. What you do in this case is to create a new instance of Order
, replacing its list of elements with a new list, which, in turn, creates a cons ' new OrderItem
in the old list.
Let me illustrate this example further in the photographs. Imagine a repository of objects (let it be RAM or a relational database, whatever):
Address | Object | Created by
-------- + -------------------- + -------------------- ----------------------
1000 | list of OrderItems | empty list constructor
1001 | Order | Order constructor, uses address 1000
...
1300 | OrderItem | ...
1501 | list of OrderItems | cons of addr 1300 to addr 1000
1502 | Order | replace order_items in addr 1001 by addr 1501
The data storage structure itself is thus constant (Chris Okasaki describes this in detail in his thesis ). You can restore any version of an object simply by following its creation history; version control is becoming trivial. Just remember the main thing: do not mutate the data, do not create new instances.
source share