Dyna Dicts: Using meta-data for "keyPath" memberKey
Posted: Fri Jul 31, 2009 12:29 pm
There are times where working with dynamic dictionaries that you want to use a keyPath for the memberkey. Suppose you have two classes, Customer and Branch, with a couple of properties that you want to use in your keyPath memberKey: Customer::myBranch & Branch::name
You may want to have a dyna dict of all customers keyed by the name of their branch. People will often code this as per the following code snippet:
create dynaDict transient
dynaDict.setMembership( Customer );
dynaDict.addMemberKey( "myBranch.name" );
dynaDict.endKeys( true );
The problem with this code is that if someone changes the name of the Customer::myBranch reference or the Branch::name attribute, or deletes them, this code will not get changed or marked in error as it is using a string literal for the memberkey definition. You can achieve the same end result, but with the advantage of your code being adjusted or marked in error, with a little bit of outside-of-the-box thinking as follows:
create dynaDict transient
dynaDict.setMembership( Customer );
dynaDict.addMemberKey( Customer::myBranch.name & "." & Branch::name.name );
dynaDict.endKeys( true );
This code uses meta-data constructs to get to the "string" that represents each of the steps along the keyPath, and then concatenates it with the necessary '.' characters. Because it's using meta-data constructs, any changes to the names of the underlying properties, or the deletion thereof, will result in this code being changed or marked in error as appropriate. You won't then have a runtime error waiting silently in the wings to spring out at you the first time someone runs this code after the change was made.
Cheers,
BeeJay.
You may want to have a dyna dict of all customers keyed by the name of their branch. People will often code this as per the following code snippet:
create dynaDict transient
dynaDict.setMembership( Customer );
dynaDict.addMemberKey( "myBranch.name" );
dynaDict.endKeys( true );
The problem with this code is that if someone changes the name of the Customer::myBranch reference or the Branch::name attribute, or deletes them, this code will not get changed or marked in error as it is using a string literal for the memberkey definition. You can achieve the same end result, but with the advantage of your code being adjusted or marked in error, with a little bit of outside-of-the-box thinking as follows:
create dynaDict transient
dynaDict.setMembership( Customer );
dynaDict.addMemberKey( Customer::myBranch.name & "." & Branch::name.name );
dynaDict.endKeys( true );
This code uses meta-data constructs to get to the "string" that represents each of the steps along the keyPath, and then concatenates it with the necessary '.' characters. Because it's using meta-data constructs, any changes to the names of the underlying properties, or the deletion thereof, will result in this code being changed or marked in error as appropriate. You won't then have a runtime error waiting silently in the wings to spring out at you the first time someone runs this code after the change was made.
Cheers,
BeeJay.