Skip to main content

Attributes

Use a VoxAttributeValues property when an entity needs specifications whose definitions and templates can change at runtime.

For the team workflow, see Describe different assortments.

Vox is code first: entity types, their properties and their lists of choices are code, reviewed and deployed with the application. Some data doesn't fit that. A retailer's product specifications are defined by product data staff, who add templates and fields every day: hundreds of templates and thousands of fields, with ids such as GUIDs that are no use as property names, and values that mostly belong to their product, eg a weight of 340 g. No rule in code refers to "the battery capacity of this headlamp"; the values are shown, compared, exported and checked for completeness generically.

Attributes are values whose definitions are data. An entity type has a VoxAttributeValues property, and its values are keyed by the ids of attributes that are created at runtime, by people in the admin or by a connector, and take effect at once, without a deployment. The model doesn't change when an attribute is added.

These docs say "attributes" for this data and "C# attributes" for declarations such as [VoxFilterable].

Declaring the property​

public partial class Item : IVoxEntity
{
// ...
public virtual VoxAttributeValues Specifications { get; set; } = new();
}

Its value is the template the entity uses and one value per attribute, keyed by the attribute's id:

"specifications": {
"template": "headlamp-2026",
"battery-capacity": { "id": "2000mah" },
"89a5f395d1a84cba8b8ccb7a86cae3a6": 2000,
"ip-rating": { "id": "IP67" },
"beam-mode": "spot"
}

template is reserved. Values are written with their ids in ordinal order.

A VoxAttributeValues property:

  • belongs to an entity type stored in Entity Framework storage, light entity types included, and not to one stored in Redis;
  • is a property of the entity itself, not of a nested object, a list, a dictionary or a varying value, and isn't calculated;
  • can't be [VoxFilterable], [VoxSearchable] or [VoxSortable]. Values aren't indexed, apart from the relations of references, see below. In a search index the property is a regular field that isn't searchable or filterable.

Build() refuses each of these with an error that names the property. Attribute values don't vary by context either: a value that must differ per language, market or channel belongs in a property, or in a light entity it refers to.

The attributes are shown where the property is, and take its section and its topics. The property is a section of its own on the detail page, which can go on a tab of its own, eg layout.Tab("Specifications", item => item.Specifications), or into a named section with [Display(GroupName = ...)], see sections and tabs; the groups of a template stay inside it. [VoxRelevantTo(...)] on Item.Specifications shows or hides all the attributes together in a perspective, and so does the relation to the entities their values refer to, eg on the "Used in" side of a referenced entity.

Attributes​

An attribute is a definition saved in the database, not an entity. It has:

  • An id, which values, code and integrations refer to: lowercase letters, digits and -, unique ignoring case, not starting with _ and not template. Use readable ids such as battery-capacity; a migration can keep a source's ids.
  • A label per language, eg { "sv": "Batterikapacitet", "en": "Battery capacity" }, an optional description per language and an optional unit, eg mAh. Editors see the label of their selected language, or else the first label.
  • A kind: Boolean, Integer, Decimal, Text, Choice, Choices, Reference or References.
  • For a choice kind, its own list of allowed values, each a key and a label per language, eg fm "FM".
  • For a reference kind, the entity type its values refer to, eg a light entity type IpRating. It can't change once it's set, since stored references don't name their entity type: create another attribute instead.

Templates​

A template, eg headlamp-2026, is saved in the database too. It has an id, a label per language and ordered groups, each with a key, a label per language and an ordered list of the template's attributes, each required or optional. An attribute is in a template at most once.

A value selects one template, or none. It may have values of any attribute: the template decides which attributes an editor is offered, in which order and groups, and which ones a write that isn't by the system must fill. Values of attributes outside the selected template are kept and shown after its groups, so changing a template never loses values.

Light entities or an attribute's own list​

A curated vocabulary that code or several attributes rely on, eg IP ratings or battery types, is a light entity type in code, and the attributes that use it are of a reference kind. A list that only one attribute uses, eg the beam modes of a headlamp, is that attribute's own list of choices. There are no shared lists in the definitions: a list that several attributes need is a light entity type.

Reading and writing values in code​

An application declares the attributes its code relies on as VoxAttributeKey<T> static fields or properties of a class that's registered with Vox, like its entity types:

public static class Specs
{
public static readonly VoxAttributeKey<decimal> ScreenSize = new("screen-size", VoxAttributeKind.Decimal, unit: "\"",
labels: new Dictionary<string, string> { ["en"] = "Screen size" });
}

var size = item.Specifications.Get(Specs.ScreenSize); // null when it has none
item.Specifications.Set(Specs.ScreenSize, 6.1m);

At startup Vox creates a declared attribute, or updates a stored definition that differs from the code's, and never fails startup over it. The admin can't change or delete it. Every other attribute is read by its id: item.Specifications.TryGet("battery-capacity", out var value) gives the value converted to the attribute's kind, eg a decimal, a string, a list of keys or a VoxAttributeReference, and Set("battery-capacity", value) sets it.

Calculations, assessments and actions read values the same ways, and reading one records a dependency, as reading a field of a field set does, so a calculation is recalculated when the value it read changes.

Validation​

Every writer's added and changed values are validated against their attributes, connectors' too: an integer attribute can't take "3,5 mm", a choice must be a key of its list, and a value of an attribute that isn't defined is refused. Unchanged values aren't checked, so a definition change never blocks editing an entity whose other values are stale. The required attributes of the selected template are checked for writes that aren't by the system.

References are relations​

A reference value is a link of the property's relation, stored with the entity type's other relations, eg in Vox_Relation_Item_Specifications, with the attribute's internal number. Two attributes that refer to the same entity, eg a headlamp's and its charger's IP rating, are two links, and a link says which attribute it belongs to, so "which items have IP67 as their headlamp's IP rating" is an inverse lookup. Change events, drafts, releases, publishing and the deletion of a target work as for other relations. A moved value, eg from one attribute to another with the same target, is a removed and an added link. The relation isn't in search documents. Its names come from the property, eg specifications and itemSpecifications from the inverse side; [VoxRelation(Name = "specs", InverseName = "specifiedBy")] on the property names it otherwise, eg when the default inverse name collides with another relation.

Changing definitions​

Stored values are never rewritten when a definition changes. They're read as they are, converted when they're accessed, and written in their current form when their entity is next written:

  • Changing an attribute's kind is always allowed. A value converts when the conversion is exact, and otherwise reads as having no value: integer to decimal; decimal to integer when it's a whole number; anything to text; text to a number or a boolean when the whole text is one, eg "340" or "3,15" but not "ca 340"; text or a choice to a choice when it's a key of the list; a choice to a reference when its key is an entity's id; one value to a list of one and back. A comma is always a decimal comma, so an English "2,000" becomes 2: check text values with thousands separators before changing a text attribute to a number.
  • Removing a choice makes values with that key read as having no value.
  • Deleting an attribute makes its values read as absent and removes it from templates. Its id can't be used again.
  • Renaming an attribute keeps its previous ids: a value stored under one is read as the attribute's and written under the new id at its entity's next write.

When an entity is next written, values that can't be converted and values of deleted attributes are left out, and ids that the host doesn't know are kept, so a host whose definitions are behind never loses data. Until then, changing a kind back brings the old values back. Relation rows follow the values: the rows of an attribute that's deleted or no longer a reference are skipped by reads and removed at the owner's next write, and a value that converted to a reference gets its row then.

Results that depend on values catch up at the next write​

A definition change writes no entity, so nothing that depends on the values is told. When "Battery time" changes from text to decimal and 2,000 items' values can no longer be read, an assessment "battery time is required" shows no findings for them, and calculations and exports keep their old results, until each item is written or recalculated again. The admin warns before a change that values will read as having no value.

Where definitions live​

Definitions are stored in the Entity Framework database, with an append-only history of changes and who made them. Every host keeps all of them in memory, tens of thousands, since every write validates against them. A change is sent to the other hosts as an instance event, and each catches up from the history; a host that missed one catches up within a minute (VoxOptions.AttributeRefreshInterval), and a host that meets an unknown id or a value that fails its kind catches up before refusing it.

Definitions are read and changed through IVoxAttributeEditor in code and the API at /vox/api/attributes: attributes and templates are searched and paged, a set of changes is saved together and in order, and each has its history. Each attribute in a page of GET /vox/api/attributes comes with how many templates have it (templateCounts), ?ids= reads the attributes of a template in one request, and GET /vox/api/attributes/definitions/{id}/templates pages the ids of the templates that have one. The host counts them from an index of the definitions it keeps in memory. Reading is open; changing needs the vox:Attributes permission.

In the admin, attributes are changed on Studio's Specifications page, which every entity type with attribute values links to in Studio's sidebar, since the definitions are the same for all of them. It shows the templates, paged and searchable, with the chosen one's fields in their groups, and a search over every field. A field shared by several templates says that its name, kind and unit change in all of them, while its group and whether it's required are set per template. Taking a field out of a template keeps the entities' values; deleting it everywhere asks first. Attributes the code declares are shown locked. Studio is there for users with vox:Attributes, also without the Studio package. The admin edits an entity's values by its template, with groups, labels, units and an input per kind.

Choosing the right model​

NeedModel
A value every entity of the type can have, which code relies onA property
Many values of which each entity has a few, defined in code and deployedA field set
Many values whose definitions change as data, eg a PIM's specificationsAttributes
A curated vocabulary that code or several attributes shareA light entity type, referred to by attributes

Prefer a field set when the fields are few enough to review in code and filters or search need them. Choose attributes when the definitions belong to the people who edit the data, change often or come from another system.

See Light entities and Field sets.