OCLOperators transform

object.transform(viewmodelname, <destination object>) uses the viewmodel to transform information from <object> into the <destination object>. Returns True if transform is full. The transform operator takes the object as the root of the viewmodel specified and use 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, are due to that the destination attribute could not be found or that the destination attribute 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 not correct in your transformation.

Example of how to use

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 lets see how objects of the OutgoingEmail and EmailAttachment can be created using transform.

EmailQueueClassDiagram.png

This viewmodel and transform will fill in the OutgoingEmail and EmailAttachment by name-matching each attribute and link from the "Självfaktura" onto OutgoingEmail.

TransformViewmodelForEmail.png

(please note the "trick" self->asSet because we need an list, not a link to fill in "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 wasn'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 serverside viewmodel that sends email and thus empties the queue.

ViewmodelForSendingEmail.png

Duck Typing

Transforming objects over the class border based on similarities - as transform does - is often refered 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 typecheck your transform in design time during normal model validation:

2019-11-03 19h15 37.png
This page was edited 84 days ago on 02/10/2024. What links here