MDrivenWiki
Log in

Deepclone

From MDrivenWiki

Pretty often you have user scenarios that require you to copy an existing object structure. Like when handling revisions of things and the user wants a new revision but want to start with the old and just tweak a few values.

In cases like these it would be neat with a clone mechanism of your modeled objects. And once you have a clone mechanism you pretty soon will discover that cloning just one object will not suffice – you want to follow associations and clone owned child objects – but only in some cases – in other cases you don’t want to clone the child objects but rather reference the same ones from the clone.

Given this model;

Simpleclassstructure.png

We let a viewmodel act as a definition for what the clone should do,, like this for example;

Deepclonedefinition.png

Then in OCL you do;

self.deepclone('ViewModel_Person_DeepClone')

The deepclone operator returns the new Person object, complete with copies of OwnedBuildings and the Home links.

A good place to use this operator is in a method on a class, for example CreateClone() : Person. That way you just call self.CreateClone from a class action using the returned object as the root of the viewmodel the action opens.