Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Complex objects modeling
Message
 
To
25/10/2018 23:33:01
General information
Forum:
Visual FoxPro
Category:
Visual FoxPro and .NET
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 10
Miscellaneous
Thread ID:
01662805
Message ID:
01663678
Views:
94
That's pretty cool - heck I had no idea you could do this. In fact I had no idea there was a `THIS_Access()` which is basically a 'method missing' accessor.

I think you can simplify that implementation a little bit yet, by removing the child object call to _() to create a child object. Instead in the PEMSTATUS() check just create your object as `nfdyno` and then you can just assign.


Here's what I ended up with:
CLEAR
loItem = CREATEOBJECT("wwDynamic")

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

? loItem.Bogus
? loItem.Entered


*loItem.oChild = CREATEOBJECT("wwDynamic")
loItem.oChild.Bogus = "Child Bogus"
loItem.oChild.Entered = DATETIME()

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


*************************************************************
DEFINE CLASS wwDynamic AS RELATION
*************************************************************

__oReference = null

*** Intercept access to unknown properties and create
FUNCTION THIS_Access(lcPropertyOrMethod)

If Lower(lcPropertyOrMethod) ==  "__oreference"
	Return This
Endif

If !Pemstatus(This.__oReference,lcPropertyOrMethod,5)
	AddProperty(this.__oReference,lcPropertyOrMethod,Createobject('wwDynamic'))
ENDIF
	
RETURN this.__oReference
ENDFUNC



************************************************************************
*  Init
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION Init(loBaseType)

IF !VARTYPE(loBaseType) = "O"
   this.__oReference = CREATEOBJECT("Empty")
ENDIF   

ENDFUNC
*   Init

ENDDEFINE
*EOC wwDynamic 
Notice the removed call of `this.oChild = CREATEOBJECT("wwDynamic")` which is replaced by the code inside of the PEMSTATUS. This allows cascading objects without having to do anything special.

This is pretty darn cool, although for programmatic/automated object construction (such as what I use in things like wwJsonSerializer) I'd still want to use raw ADDPROPERTY() because it'll be significiantly faster than this dynamic object construction. However, for user level code that needs to construct objects in user code this is certainly a heck of a lot nicer.

Thanks for posting this.

Learn something new every day :-)

+++ Rick ---






>Because of the Relational Database Orientation of VFP, and lack for native object serialization support ( xml , json ) complex objects were out of the VFP conversation, even rarely you see "scatter name". Last year I shared nfXmlRead/create & nfJsonRead/Create, today I want to share "_" a very simple, small , but powerful function* that allows you to model / modify complex objects using a cleaner, effortless and readable syntax .
>
>Forget about "addproperty" if you need to add more than 1 property to an object, Just put _.prg in your path and start using it like below: ( attached picture shows result )
>
>Marco Plaza
>@vfp2nfox
>
>
>
>public oMyPc && check later on your debugger
>
>oMyPc = Createobject('empty')
>
>With _( oMyPc )  && simply pass object you want to modify, any referenced property will be added to passed object if does not exist:
>
>	.madeBy = 'Marco Plaza, 2018 - nfTools'
>	.manufacturer = 'custom'
>	.basePrice = 699
>	.caseType  = 'ATX'
>	.modelName = 'Ryzen Performance Plus'
>
>	With _( .cpu )   && cpu will be a new object for oMyPc - check we pass ".cpu"  ( dot cpu ) because it's inside with - endwith 
>		.processorcount = 6
>		.brand = 'AMD'
>		.model = 'Ryzen 7'
>		.clockspeed = 4.3
>		.processorCount = 8
>	Endwith
>
>	With _(.motherboard)
>
>		.manufacturer = 'Asus'
>		.model = 'Prime B350-Plus AMD'
>		.formfactor = 'ATX'
>
>		With _(.slots)
>			.Memory = 4
>			.m2 = 1
>			.pcie = 4
>		Endwith
>
>		With _(.storage,'primaryDisk')  && here we create ".storage" and ".storage.primaryDisk"  in a single pass
>			.manufacturer = 'Samsung'
>			.model = '960 evo Series'
>			.Type = 'internal'
>			.connectivity = 'PCIe NVMe M.2'
>			.capacity = '250gb'
>		Endwith
>
>		With _(.storage,'backupDisk')
>			.manufacturer = 'Seagate'
>			.model = 'Barracuda ST3000DM008'
>			.Type = 'Internal'
>			.formfactor = 3.5
>			.capacity = '3tb'
>			.connectivity = 'Sata 6.0'
>			.rotationspeed = 7200
>		Endwith
>
>	Endwith
>
>Endwith
>
>
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform