By Peter Bell

Required/Optional/Custom

This is a pattern where a given user or site should have access to some shared data but should be able to extend it with custom data and perhaps select a subset of optional data. I use it as part of my software product line for assigning metadata to a project but I've also used it for assigning different categories and products to different stores running off of a central product database and am using it right now to allow different personal trainers to create their own customized exercise library based on a master list of exercises. This posting describes a generalized solution to this class of problems in a way that would make it fairly easy to generate such solutions.

Lets take the exercise library as an example. We want to have a MasterExercise library which the master admin can list, add, edit and delete. We also want a CustomExercise library with the ability for each trainer to list, add, edit and delete their own custom exercises. We need a "SelectExercise" action where the trainer can list and select a subset of master exercises and finally we need an Exercise view in the database that combines all required master exercises (for this use case there are probably no required master records, but for some problem spaces there are so it is easiest just to generally support required master elements and set the number of them to zero for this special case) together with any master exercises associated to this trainer and all custom exercises created by this customer which ends up being a pretty simple UNION query.

Comments
Peter,

I don't know if it is or not, but that entry seems incomplete to me. I think I understand it because it sounds quite similar to something we just got done implementing (though ours was a specific solution like your example), but I'm not sure I would have understood it if I hadn't had that experience recently.
# Posted By Sammy Larbi | 2/10/07 8:21 PM
Yeah, I had a feeling I'd been a little too brief, but was hoping someone could complain so I could figure out what I'd neglected to cover.

Basically imagine Exercise with properties of (say) title and description (to simplify).

Create tbl_MasterExercise with ExerciseID int, Title varchar(100) Description ntext and Required bit.
Add tbl_CustomExercise with ExerciseID int, Title varchar(100) Description ntext and TrainerID int
Then tbl_TrainerToExercise with TrainerID and ExerciseID (both int).

Then for add, edit and delete, build admins around the master table (for master admins) and custom table (for trainers). Then create a screen for trainers to list optional master exercises (where required = 0) and assicate any they want to their library (those associations populate the tbl_TrainertoExercise).

Finally write a SQL view that selects from Master table where Required = 1 or where join to the current trainer ID via the tbl_TrainertoExercise and then union with all records in custom exercise table for that trainer ID and you get your combined view of required, selected optional and custom exercises for a given trainer.

Did that make it *any* clearer?
# Posted By Peter Bell | 2/10/07 9:22 PM
I noticed it reads better if I focus on the pattern aspect of it. It does sound like the solution we came up with. So, that's 2 independent times we've seen it in "the wild*." Indeed, you may have stumbled on to something! (how many do we need for it to be a bona fide pattern?)

* I thought that phrase came up within the first couple of pages of the GoF Design Patterns book, but I can't seem to find it. Can anyone confirm or deny that?
# Posted By Sammy Larbi | 2/11/07 1:14 AM
I'm sure I've seen "in the wild" in GoF, but don't have it with me this weekend, so can't confirm. I've actually used this pattern three times already for different use cases: associating metadata to projects (project c gets these product attributes), associating exercises to trainers and associating products to stores in what is basically a system that allows vets to build their own online store with both shared and custom products, so now we just need a good name and to check some of the pattern repositories (as it's *probably* already been categorized).
# Posted By Peter Bell | 2/11/07 9:59 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.