Many ways to relate Orchard things, part 4: taxonomies
Taxonomies in Orchard can be seen as glorified tags, and in a way, that’s what they are: you can define multiple classifications for your content items, and assign any number of these classifications to your types. For example, a song could be tagged with a “genre” taxonomy, that defines terms such as “rock”, “blues”, “metal”, “progressive”, or “electro”. The same songs could be tagged with a “mood” taxonomy, with terms such as “epic”, “soft”, “spooky”, or “trashy”. Each song may be tagged with any number of terms from each taxonomy. In other words, there are n-n relationships between songs and taxonomy terms.
What makes Orchard taxonomies so much better than simple tags is that taxonomy terms are content items, and as such can be extended with additional parts and fields. Our genres and moods, for example, can be enriched with a body part, in order to give them a rich-text description.
Depending on how you configure taxonomy fields, the UI to pick terms can look like a checkbox list:
Or, if only one term is allowed, it can look like radio buttons:
If there are too many terms in the taxonomy, however, things can get ugly. Fortunately, there is a third kind of UI available, and that’s auto-complete:
With this setting, you can start typing in the textbox, and the list of terms containing the characters you’ve entered so far will appear underneath the textbox. You can even configure the field so that if a term that doesn’t exist yet is typed into the box, and the item is published, a new term will be automatically created for you.
Pros
- No code, except for front-end template overrides in the theme.
- Complete, customizable admin UI (auto-complete rocks).
- Clean relational implementation enables flexible and fast queries from code (projections are also a great option).
- Terms are real content items, and as such can be extended at will.
- As many relationships as you want per content type.
Cons
- Potential for select n+1 performance bottlenecks.
- Learning curve: the model can be a little mind-bending at first, with those automatically created content types.
Use cases
Taxonomies are designed for classification. A term is supposed to be a category of content items. It may look like a simple question of semantics, but I would argue that semantics matter. While taxonomies can in principle be an almost universal way of setting relationships in Orchard, and have almost no real drawbacks, I would still limit their usage to cases where you can say “[item] is a [term] [type]”, as in “Anarchy in the UK is a punk song”.