Field sets
A field set stores a sparse set of typed properties grouped into templates. Its fields and templates are part of the application code.
For the team workflow, see Describe different assortments.
Most data in Vox is ordinary properties of entity types. Some data is different: a product has a dozen specification values out of hundreds or thousands that products of all kinds can have, and which ones depend on the product's template, eg Tent or Jacket. Vox models that as a field set: a property whose value holds only the fields that are set, and whose fields are grouped into templates that an entity selects.
A field set is code, like the rest of the model. Its fields are properties, reviewed and deployed with the application, and every model change to them, eg a new field, a new template or a new label, is a deployment.
Declaring a field set
A field set is a partial class that derives from VoxFieldValues. Its templates are interfaces with [VoxTemplate]
that extend IVoxFieldsOf<TheClass>, and their properties are the fields:
public partial class ProductSpecifications : VoxFieldValues
{
// A field of the class itself is in every template
[Display(Name = "Warranty")]
[VoxUnit("years")]
public partial int? WarrantyYears { get; set; }
}
[VoxTemplate("tent", DisplayName = "Tent")]
public interface ITentSpecifications : IVoxFieldsOf<ProductSpecifications>
{
[Display(Name = "Sleeping places")]
[VoxFilterable]
[Required]
int? SleepingPlaces { get; set; }
[VoxUnit("mm")]
[VoxFilterable]
int? WaterColumnMm { get; set; }
VoxReference<SeasonRating>? SeasonRating { get; set; }
}
[VoxTemplate("jacket", DisplayName = "Jacket")]
public interface IJacketSpecifications : IVoxFieldsOf<ProductSpecifications>
{
[VoxUnit("mm")]
[VoxFilterable]
int? WaterColumnMm { get; set; }
List<VoxReference<CareInstruction>>? CareInstructions { get; set; }
}
public class Product : IVoxAuthoredEntity
{
// ...
public virtual ProductSpecifications Specifications { get; set; } = new();
}
Vox's source generator implements the fields. Each property reads and writes the values of the class by the field's JSON
name, so an instance holds what's set and nothing for the other fields. A property of the class is implemented
publicly; a property of a template is implemented explicitly, so the class doesn't offer every field of every
template, eg when typing product.Specifications. in an editor. A field set with thousands of fields is therefore one
small interface per template, which people and the model assistant work with one at a time.
An interface of fields without [VoxTemplate] is a group: it organizes fields in code, eg the attributes of a set
without templates by attribute group, or fields that templates share and extend. A group isn't stored, isn't part of a
field's path and isn't shown to editors.
Reading and writing fields
Code reaches a template's fields through the template:
// The tent fields, when the product's values have selected the tent template
if (product.Specifications.Is<ITentSpecifications>(out var tent))
{
total += tent.SleepingPlaces ?? 0;
}
// Selects a template and sets its fields, eg in a seeder or an import
var selected = specifications.Select<ITentSpecifications>();
selected.SleepingPlaces = 2;
// The fields of any template, whatever the values have selected, eg values kept from an earlier template
var jacket = product.Specifications.As<IJacketSpecifications>();
Some things to know:
- Use
Is<T>()to check the selected template, not C#'sis. The class implements every template, sospecifications is ITentSpecificationsis always true. - An object initializer can't set a template's fields, since they're implemented explicitly. Call
Select<T>()and set them on what it returns. - Reading through any template works. A field the values don't have reads as null, or the default of a value type.
- Absent and null read the same. A field that has no value and a field whose value was cleared both read as null. Vox keeps the difference in storage, since a draft clears a published value with an explicit null.
Fields that templates share
A field is its JSON name: declared in several templates, it's one field with one value. In the example, WaterColumnMm
is in Tent and Jacket. Its declarations must have the same type and the same attributes, apart from
[Required], or the model doesn't start, with an error that names both. [Required] on a declaration makes the field
required in the templates that include it, so a field can be required in one template and optional in another. A group
that templates extend can declare a shared field once instead; its [Required] applies in every template that extends
it.
No field can be named fieldSet, which is the selected template in JSON.
Selecting a template
Values select a template by its id, fieldSet in JSON:
{ "specifications": { "fieldSet": "tent", "sleepingPlaces": 2, "waterColumnMm": 3000 } }
The Admin UI offers the templates, and then the fields of the selected one, with its required fields always shown. When a user changes the values, the fields they add or change must be fields of the selected template, and its required fields must have values. Values the entity already had outside the template are kept and shown, so selecting another template doesn't invalidate them. Without a template, any field of the set can be added. Writes by the system, such as an integration that mirrors an authoritative source, aren't restricted, since the source may keep values outside an entity's template.
Large field sets
A set can have thousands of fields, eg the specification fields of a PIM. Only the values that are set are read,
written and validated, so an entity with a dozen specification values costs the same to save, validate, publish or open
whether its field set has fifty fields or six thousand. The entity type's metadata names the set, counts its fields and
lists its templates, but doesn't list the fields: an editor loads the ones it needs, those of the selected template,
those the entity has values in and those it searches for, with
POST /vox/api/entity-types/{entityType}/properties/{property}/fields. A type's page in Studio shows a set the same way:
one template, or one page of a search, at a time.
Presenting fields
A field takes the same attributes as a property: [Display] for its label, description and order, [VoxUnit],
[VoxShortText], [AllowedValues] and validation attributes such as [StringLength] and [Range], which are checked
for the values that are set. See Editor metadata.
The fields of a set are shown where their VoxFieldValues property is, and take its section and its topics. The
property is a section of its own, unless [Display(GroupName = ...)] puts it in a named one, see
sections and tabs, and [VoxRelevantTo("Content")] on
Product.Specifications shows or hides all of a template's fields together in a
perspective. A GroupName on a field of the set
stops the application when it starts, and [VoxRelevantTo] on a field of the set has no effect.
Types and query behavior
A field can have the types a property can: strings, numbers, booleans, dates, rich text, lists, references and values
that vary by context. It can't be an index-backed relation (IVoxRelationReferences<T>) or a cached reference, which
Vox refuses when it builds the model. A reference field usually refers to a list of choices, eg the colors a field's values are chosen
from, which is usually a light entity type.
The references in the fields of a field set are one relation of the property, as the references in a dictionary
are, not a relation per field. Its value path is the property followed by {field}, eg specifications{field}, and it's
named after the property: specifications from the entity, and eg productSpecifications from each entity type that
one of the fields references. Each referenced entity is one link, also when it's the value of two fields or an item of
a field's list. So a set with a thousand reference fields has one relation and one table, and a product with a dozen
specification values is saved and published with its ordinary relations plus one.
That gives up filtering by a single reference field: "the products that reference Backpack in any specification
field" is a filter on the relation, "the products whose bag kind is Backpack" isn't. When a reference field needs a
filter of its own, mark it [VoxFilterable]: it then gets a relation and query field of its own, like a reference
property. Other fields are filterable, sortable or searchable with the same attributes as properties.
Changing a field set
A change of a field set is a change of the model, see Model evolution. A field's path is the
property's path and its JSON name, eg specifications.sleepingPlaces. A field renamed with [VoxRenamedFrom] keeps its
stored values, and a removed field is declared like a removed property. Renaming the field-values property with
[VoxRenamedFrom] renames its fields with it, also one that's renamed in the same release. Moving a field to another
template or group doesn't change its path. Strings in data that name a field, eg a product's list of variant dimensions, aren't rewritten
when it's renamed.
Contracts
An exported entity contract describes a field set as fieldValues, with its fields, its templates and which fields
each template has and requires. A receiving application's generated class is a sparse field set with the same
templates, so it stores, reads and edits the values the same way; the references of its fields are its one relation
there too. Labels, units and groups aren't in contracts, as for other properties. See
Share entities between Vox apps.
Choosing the right model
| Need | Model |
|---|---|
| A value every entity of the type can have | A property |
| Many values of which each entity has a few, eg specifications by template | A field set |
| Many values whose definitions are data, eg a PIM's specifications that staff add daily | Attributes |
A field set is worth it when an entity has a few of many fields: it stores only what's set, keeps one relation for its references and offers editors the fields of a template. A handful of fields that every entity has are simpler as properties.
A field set is code: a new field or template is a deployment, and its fields can be filtered, sorted and searched. When the definitions belong to the people who edit the data, change every day or come from another system, eg a retailer's thousands of specification templates in its PIM, use attributes instead: their definitions are data that take effect at once, at the cost of filters and search on their values.
See Owned and mirrored data, Querying and search and Model evolution.