Dyna Dicts: Using meta-data for "keyPath" memberKey

Forums for specific tips, techniques and example code
User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Dyna Dicts: Using meta-data for "keyPath" memberKey

Postby BeeJay » 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.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Dyna Dicts: Using meta-data for "keyPath" memberKey

Postby ConvertFromOldNGs » Fri Aug 14, 2009 9:52 am

Bump. (To make this message "more recent" than the historical messages recently converted from the old NNTP Newsgroups.)


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 9 guests

cron