How to version dynamic business Objects/Data?

后端 未结 5 1614
情歌与酒
情歌与酒 2021-02-01 00:02

We are developing a large applications which are related to business. You can find these applications similar to some ERP, CRM, etc.

Now we have a requirement that we ne

5条回答
  •  既然无缘
    2021-02-01 00:09

    It may be just the right time to adopt purely functional lazy data structures.

    In a nutshell, this requires banning any mutating operations on your Objects, i.e. making all your Object instances immutable. Then you redesign all the operations which change existing Object to creating new Object instance based on the old one.

    For example, let you have an Order instance which contains a list of OrderItems and you need to add a specific OrderItem to that list. What you do in this case is creating new instance of Order by replacing its items list by a new list, which in turn is created by cons'ing the new OrderItem to the old list.

    Let me illustrate that example further in pictures. Imagine a storage of objects (let it be RAM or relational database, anything):

    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 very structure of storing data in this way is persistent (Chris Okasaki elaborates on this in his thesis, for example). You can restore any version of an Object by just following its creation history; versioning becomes trivial. Just remember the main point: don't mutate data, create new instances instead.

提交回复
热议问题