Notes: Using Structures With CFCs
By Selene Bainum (WebTricks.com).
Structure refresher: They are complex data types that can help you provide a logical grouping of data. They can also be called associative arrays and are made up of key/value pairs. Server, Application, Session, Request, URL scopes are all examples of structures.
CFC refresher: Contain data and functions. Zero or many arguments may be passed to the functions, but only zero or ONE values can be returns. And the values in and out of CFCs can be typed (any, array, binary, boolean, date, numeric, query, string, struct, UUID, Variablename, Void, XML).
Recall that you can call functions with the cfinvoke tag (passing in arguments with cfinvokeargument or with the argumentcollection attribute). Via script, you can use CreateObject() to instantiate the component, and then call the function. Not necessarily more efficient, but it looks cleaner, easier to read.
Why use structures with the CFC functions? Less arguments to declare in the function and the function calls. New elements can be added to the structure without having to change the function declaration or calls. However, doing this subverts the argument typing validation and security. You can’t type the individual keys of the elements in the struct. So using cfqueryparam is even more important in this scenario. And there is no introspective documentation of the CFC. Available arguments must be manually documented with comments in the code if you want. Very nice!
She didn’t discuss any way to do more detailed filtering and encapsulate that in the struct data, for instance, “is equal to”, “is not equal to”, “is greater than”, “is less than”, etc. Her example pretty much did an “is equal to” match, or at least a “contains” type of search.
Nevertheless, this is a great idea, and perfect for the example of submitting search criteria into a search function.
The arguments scope is already a structure. Each cfargument tag just defines a key of the structure. So passing the argumentsCollection attribute allows you to send all the arguments at once.
Structs for Form Population/Submission
When preparing to display an add/edit form, can populate a struct that contains all the data in the form. Can write some pretty flexible code to do this based on the queries you pass your code. Just loop through the query fields and assign the elements correspondingly in the struct. Joined tables that may have a one-to-many relationship can be structs within the main struct. Each element can be named as the ID of the joined table records, and then that element contains a struct mimicking the joined table query, in the same manner as the main table.
BUT NOTE: When you submit the form, you cannot maintain this nested struct methodology. So, Selene tacks on an “_n” at the end of the field names, where n is the key of the record for the one-to-many records. For instance, phone_1, phone_2, etc. The code that retrieves the form fields can then rebuild these fields back into the struct structure as it was originally.
Structs for Search Criteria
Use a search struct for submitting search criteria to a search function. Various fieldnames will correspond to the files in the table to filter against. Search function can know which ones to use based on the struct. Naturally, the alternative is a nasty list that you have to parse, or a huge list of arguments. Struct approach is much cleaner to implement.
Conclusion
Course material is posted at Selene’s Presentation Download page.
