Skip to main content

Owned and mirrored data

One entity can expose locally edited, imported and calculated properties. Declare which writer owns each property.

For the team workflow, see Who owns each value.

Model the entity, then annotate the origins​

Every serialized property of an entity is a field with a JSON path, eg name, pricing.listPrice or availability.isSellable. Each field has one origin:

  • Owned: changed by the local application through actions. This is the default for an IVoxEntity.
  • Imported: changed by imports. This is the default for an IVoxExternalEntity. The entity is shaped for Vox; an import handler maps the source's payload onto it, so the two need not match.
  • Calculated: changed by one calculation, which can be rooted on another entity type, eg an order's calculation that distributes a discount to the order's lines.
  • Owned by Vox: changed only by Vox itself, for state it manages, eg the status of a content release. See Fields owned by Vox.

Mark the properties that don't use the default with an attribute. An attribute applies to the property and everything in it, and the nearest declaration wins, so an object can mix origins:

public class Product : IVoxEntity
{
// Built-in IVoxEntity properties omitted.
public required virtual string DisplayName { get; set; }
public virtual ProductPricing Pricing { get; set; } = new();

[VoxCalculated(typeof(ProductAvailabilityCalculation))]
public virtual ProductAvailability Availability { get; set; } = new();
}

public class ProductPricing
{
[VoxImportOwned(typeof(ErpProductImport))]
public virtual decimal ListPrice { get; set; }

[VoxImportOwned(typeof(ErpProductImport))]
public virtual int StockQuantity { get; set; }

// Owned, like the rest of the product.
public virtual string? PriceNote { get; set; }
}
  • [VoxOwned] makes a property owned, eg a local field on an external entity, or an exception inside an imported object.
  • [VoxImportOwned] makes it imported. Name the imports that may change it, or leave the list empty to let every import of the entity type change it.
  • [VoxCalculated] names the one calculation that writes it. The calculation can be rooted on another entity type that the entity belongs to, see Create entities and calculate related entities.

The types named in [VoxImportOwned] and [VoxCalculated] may be the import or calculation class, or a marker interface or base class that exactly one calculation (or any number of imports) implements. That keeps the model free of references to the code that fills it.

The built-in identity, timestamps, version and principal fields are managed by Vox and have no origin. Relations inlined into the entity data are fields like any other, also calculated ones, see calculated references, whose links follow the calculated value. Origins don't change the JSON of the entity or its exported contract: they describe how this application gets the values, not what the values mean. Only the fields Vox owns are marked in the contract, since they're read-only everywhere.

Index-backed relations are not serialized fields, but their ownership follows the same declarations. Use [VoxImportOwned] directly on a relation or inherit it from its containing object. A nearer [VoxOwned] can make a relation locally editable inside an imported object. Without a declaration, the entity's default applies; other fields beside the relation do not affect its ownership. [VoxSystemOwned] reserves a relation for Vox, and index-backed relations cannot be calculated. See Relations.

Vox enforces who writes what​

For fields, writers don't declare what they change. Vox detects it by comparing the entity before and after, and rejects a change to a field the writer isn't allowed to write:

  • An action may change owned fields only.
  • An import may change the imported fields that allow it.
  • Nothing but Vox may change the fields it owns.
  • A calculation may change only the fields it calculates, on its own entity or on the related entities it calculates fields of. It may also create entities of a type marked [VoxCalculated] with it, whose owned and calculated fields it initializes; imported fields stay with their imports.
public class ErpProductImport : IVoxImport<Product>
{
[VoxImportEntityId]
public required string ArticleNumber { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}

public class ErpProductImportHandler : IVoxImportHandler<ErpProductImport, Product>
{
public Task<Product> ApplyAsync(
ErpProductImport import,
Product product,
IVoxEntityChanges<Product> changes,
CancellationToken cancellationToken)
{
product.Pricing.ListPrice = import.Price;
product.Pricing.StockQuantity = import.Quantity;
return Task.FromResult(product);
}
}

The handler gets a copy of the whole entity, so it can read anything it needs. If it also set PriceNote or DisplayName, the import would fail with a message naming the fields. Imports work for entities owned by Vox as well as external entities. A new external entity that doesn't exist yet is created with default values and version 0.

An import may own fields, index-backed relations, or both. A relation-only import needs no ordinary imported field. Use changes.Relations.Link, Unlink or Replace in the handler; Vox checks each relation's ownership before accepting the mutation. Actions and cell writes can change only locally owned relations. See Importing relations for an example and the behaviour of large replacements.

Concurrency is per origin​

Version remains the monotonically increasing version of the complete entity and is used for history, events and API responses. Vox also stores, as persistence metadata, the version in which the owned, the imported and the calculated fields, and the fields Vox owns, last changed. The origins are independent concurrency boundaries.

When an action is submitted with an older entity version:

  • it succeeds if no owned field changed after the supplied version;
  • Vox applies it to the latest entity, keeping the imported and calculated values that changed concurrently;
  • it returns a conflict if an owned field changed after the supplied version.

So an ERP price import or an availability calculation never forces a user to repeat a display-name edit, while two stale edits of owned fields still conflict. Imports and calculations do the same in reverse: when their fields change concurrently they rerun on the newest entity, and an action that changes owned fields doesn't stop them.

If the entity changes between reading and committing it, Vox reloads it and reruns the handler, authorization and validation before retrying. Action handlers should therefore avoid non-idempotent side effects outside the returned entity; integrations belong after the durable entity change or in a subscriber.

Draft publication uses the same boundaries: imported and calculated values of the published entity are read by the draft, while a published change to an owned field must be resolved, field by field, before publishing. See Drafts and publishing. Deletes require an explicit published or draft version.

Index-backed relation mutations don't have versions of their own. When concurrent changes of a relation need optimistic concurrency, inline it into the entity data instead.

Permission scopes are separate​

Origins decide which kind of writer changes a field. Permissions decide which users may run actions. Product:Edit allows changing every owned field. Use [VoxPermissionScope] when some owned fields need a narrower permission:

public class Product : IVoxEntity
{
public required virtual string DisplayName { get; set; }

[VoxPermissionScope("Merchandising")]
public virtual ProductMerchandising Merchandising { get; set; } = new();
}

Changing merchandising.* then requires Product:Edit:Merchandising (or Product:Edit), and the other owned fields are in the default scope, Product:Edit:default. Scopes don't affect concurrency, contracts or serialization.

Scopes only decide who may edit. They don't restrict reading: everyone with Product:Read reads the merchandising fields. A field that some readers must not see, eg a purchase price, needs a read permission of its own, see sensitive fields.

An object with fields in more than one scope gets a default action per field instead of one for the whole object. If Markdown inside Merchandising is marked [VoxPermissionScope("Pricing")], the actions are SetMerchandisingMessage and the like for the merchandising fields, and SetMerchandisingMarkdown for the markdown, so a merchandiser with Product:Edit:Merchandising saves the merchandising fields while the markdown stays with pricing.

Sections in the admin UI​

/api/entity-types describes every field with its path, type, origin, calculation, imports, permission scope and whether it's synchronized, so generic clients can render the model without knowing its CLR types. It also returns the entity's sections: "General" with the top-level values, the sections named by [Display(GroupName = ...)], and one section per top-level object, ordered and labeled from [Display], see sections and tabs. A section can mix origins; the admin UI shows each field's origin and only lets the user edit the owned fields they have permission to change.

Fields owned by Vox​

[VoxSystemOwned] marks a property, and everything in it, that only Vox writes, for any role whose state Vox manages, eg the status, schedule and publication time of a content release:

public class Campaign : IVoxEntity
{
// Built-in IVoxEntity properties omitted.
public required virtual string Name { get; set; }

[VoxSystemOwned]
public virtual DateTime? PublishedAtUtc { get; set; }
}
  • It has no generated action, and isn't part of the create action.
  • A custom action, an import, a cell or a restore from history can't change it. An action or import that tries fails with a message naming the field.
  • It's read-only in the admin, which labels it as managed by Vox, and the exported contract marks it with isSystemOwned.
  • It's never part of a draft or release. A draft reads it from published content, like imported and calculated values, so publishing a draft can't bring back an earlier value.

Vox writes these fields through an internal writer that bypasses actions. A write is an update of those fields only: it's based on the current entity, retried when another write of them came first, and recorded in the history with the cause System, eg System:PublishRelease.