Class wwDynamic

This class allows you to create or extend an object with 'dynamic' properties by simply referencing non-existing properties to the instance. Any non-existing properties you reference will automatically created - this model is similar to the way JavaScript or Expando objects work.

In essence this is a cleaner and non-declarative way to write ADDPROPERTY() logic. This makes a great tool for creating complex objects on the fly in user code. It's also a create way to construct model objects or objects that you need to serialize to JSON or XML.

Here's an example of what you can do:

*** Extend an existing object
loCust = CREATEOBJECT("cCustomer")
loItem = CREATEOBJECT("wwDynamic",loCust)

*** Alternately you create a new object (EMPTY class) 
* loItem = CREATEOBJECT("wwDynamic")

loItem.Bogus = "This is bogus"
loItem.Entered = DATETIME()

? loItem.Bogus
? loItem.Entered

loItem.oChild.Bogus = "Child Bogus"
loItem.oChild.Entered = DATETIME()

? loItem.oChild.Bogus
? loItem.oChild.Entered

*** Access original cCustomer props and methods
? loItem.cFirstName
? loItem.cLastName
? loItem.GetFullName()

For JSON serialization you can also use the explicit AddProperty() method which automatically sets an internal __PropertyNameOverrides list which provides proper casing for variables when using JSON serialization.

loMessage = CREATEOBJECT("wwDynamic")
loMessage.AddProp("Sid", "")
loMessage.AddProp("DateCreated", DATETIME())
loMessage.AddProp("DateUpdated", DATETIME())
loMessage.AddProp("DateSent",DATETIME())
loMessage.AddProp("AccountSid","")
loMessage.AddProp("ApiVersion","")

* "Sid,DateCreated,DateUpdated,DateSent,AccountSid,ApiVersion"
? loMessage.__PropertyNameOverrides 

? loMessage.DateCreated
? loMessage.DateUpdated

loSer = CREATEOBJECT("wwJsonSerializer")
loSer.Serialize(loMessage) && produces properly cased property names

Remarks

Performance Notice:
This code uses an THIS_Access() accessor method to intercept every property and method access and then checks to see whether that property/method exists. This has a bit of overhead and is considerably slower than direct object property/method access. Use with care in critical path operations and test for performance carefully.

Class Members

MemberDescription

AddProp

o.AddProp(lcProperty, lvValue)

GetProp

Retrieves a value from a dynamic property.

o.GetProp(lcProperty)

Init

The constructor allows you to pass in an existing object reference that is then extended with any properties you assign.

o.Init(loExistingInstance)

__PropertyNameOverrides

If using AddProperty() to add properties the property name is added to this property. This property is used during JSON serialization to automatically provide proper casing.

__Reference

See also:

Class wwDynamic

© West Wind Technologies, 2019 • Updated: 02/05/19
Comment or report problem with topic