object.transform(viewmodelname, <destination object>) uses the ViewModel to transform information from <object> into the <destination object>. Returns True if the transform is full. The transform operator takes the object as the root of the ViewModel specified and uses the ViewModel as a template to copy and create a new structure of objects.
Usually, a false return value, i.e. not a full transform, is due to the fact that the destination attribute could not be found or is read-only. Currently, there is no log available to pinpoint exactly why false is returned. Read about duck-typing below for hints on what is incorrect in your transformation.
Example of How to Use It
Often, when sending an email, you need to "collect" information from many objects to find all the information needed to create an outgoing email.
Using this model of an email queue, let's see how objects of the OutgoingEmail and EmailAttachment can be created using transform.
This ViewModel and transform will fill in the OutgoingEmail and EmailAttachment by name-matching each attribute and link from the "Självfaktura" onto OutgoingEmail.
(Please note the "trick" self->asSet because we need a list, not a link, to fill in the "EmailAttachments" multi-link, and Självfaktura only contains an attribute "Pdf".)
This is the EAL code in the "Send" action on "Självfaktura". Create the OutgoingEmail object and call transform to fill it in. If the transform isn't full (some attributes weren't stored), we back out by removing the newly created oe object.
let oe = OutgoingEmail.create in
(
if self.transform('TR_SjälvfakturaSkickaPDF', oe) then
true
else
oe.Delete
endif
)
For completeness, this is the server-side ViewModel that sends emails and thus empties the queue.
Association classes
When you need to clone/transform link objects (association classes), like C in the example below:
A*--------*B
|
C
It is not uncommon that you want to get new A and C objects but keep a reference to B. When this is the need, your viewmodel focuses on A and C and does not mention B - since C is a link object, it will discover and understand that it should look up the B of the original and re-use it on the new C.
If you also want to create new Bs, you also mention the details for B in the ViewModel with a nesting.
Duck Typing
Transforming objects over the class border based on similarities - as transform does - is often referred to as Duck Typing. In MDriven, the ViewModel is used as a vehicle to facilitate a type-safe way to the duck type transform. The ViewModel Editor has a property to enable you to type-check your transform at design time during normal model validation:



