Extend Deploy

How to extend Umbraco Deploy to synchronize custom data.

Umbraco Deploy supports the deployment of CMS schema information and definitions from the HQ's Forms package, along with managed content and media. Additionally, it can be extended by package or custom solution developers. This allows the deployment of custom data, such as that stored in your own database tables.

As a package or solution developer, you can hook into the disk-based serialization and deployment. It is similar to that used for Umbraco Document Types and Data Types. It's also possible to provide the ability for editors to deploy custom data via the Backoffice. In the same way that Umbraco content and media can be queued for transfer and restored.

Concepts and Examples

Entities

Entities are what you may be looking to transfer between two websites using Deploy. Within Umbraco, they are for example the Document Types, Data Types and Documents (content). In a custom solution or a package, there may be representations of some other data that's being stored separately from Umbraco schema or content. These can still be managed in the Backoffice using custom trees and editors.

For the purposes of subsequent code samples, we'll consider an example entity as a Plain Old Class Object (POCO) with a few properties.

circle-info

The entity has no dependency on Umbraco or Umbraco Deploy; it can be constructed and managed however makes sense for the package or solution. The only requirement is that it has an ID that will be consistent across the environments (normally a Guid) and a name.

public class Example
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

Artifacts

Every entity Deploy works with, whether from Umbraco core or custom data, needs to have an artifact representation. You can consider an artifact as a container capable of knowing everything there is to know about a particular entity is defined. They are used as a transport object for communicating between Deploy environments.

They can also be serialized to JSON representations. These are visible in the .uda files seen on disk in the /data/revisions/ folder for schema transfers. It is also used when transferring content between different environments over HTTP requests.

Artifact classes must inherit from DeployArtifactBase.

The following example shows an artifact representing the entity and it's single property for transfer:

Control Over Serialization

In most cases the default settings Umbraco Deploy uses for serialization will be appropriate. For example, it ensures that culture specific values such as dates and decimal numbers are rendered using an invariant culture. This ensures that any differences in regional settings between source and destination servers are not a concern.

If you do need more control, attributes can be applied to the artifact properties.

For example, to ensure a decimal value is serialized to a consistent number of decimal places you can use the following. RoundingDecimalJsonConverter is found in the Umbraco.Deploy.Serialization namespace

Service Connectors

Service connectors are responsible for knowing how to handle the mapping between artifacts and entities. They know how to gather all the data required for the type of entity they correspond to, including figuring out what dependencies are needed. For example, in Umbraco, how a Document Type depends on a Data Type. They are responsible for packaging an entity as an artifact and for extracting an entity from an artifact and persisting it in a destination site.

Service connectors inherit from ServiceConnectorBase and are constructed with the artifact and entity as generic type arguments.

The class is decorated with a UdiDefinition via which the name of the entity type is provided. This needs to be unique across all entities so it's likely worth prefixing with something specific to your package or application.

The following example shows a service connector, responsible for handling the artifact shown above and it's related entity. There are no dependencies to consider here. More complex examples involve collating the dependencies and potentially handling extraction in more than one pass to ensure updates are made in the correct order.

An illustrative data service is provided via dependency injection. This will be whatever is appropriate for to use for Create, Read, Update and Delete (CRUD) operations around reading and writing of entities.

circle-info

Service connectors support a caching mechanism via the IContextCache parameter. This allows connectors to read from and write to a cache during deploy operations, improving performance when the same data is requested multiple times.

It's recommended to only cache frequent lookups, like validating whether a parent entity exists. Deploy provides extension methods in Umbraco.Deploy.Core.ContextCacheExtensions for frequently used CMS service calls. If the IContextCache parameter is not available, you can create an instance from the IDeployContext using new DeployContextCache(deployContext).

Provide a GetUdi() extension method to generate the appropriate identifier for a specific ID, and ensure it's not an open/root UDI:

Handling Dependencies

Umbraco entities often have dependencies on one another, this may also be the case for any custom data you are looking to deploy. If so, you can add the necessary logic to your service connector to ensure dependencies are added. This will ensure Umbraco Deploy also ensures the appropriate dependencies are in place before initiating a transfer.

If the dependent entity is also deployable, it will be included in the transfer. Or if not, the deployment will be blocked and the reason presented to the user.

In the following illustrative example, if deploying a representation of a "Person", we ensure their "Department" dependency is added. This will indicate that it must exist to allow the transfer. We can also use ArtifactDependencyMode.Match to ensure the dependent entity not only exists but also matches in all properties.

Value Connectors

As well as dependencies at the level of entities, we can also have dependencies in the property values as well. In Umbraco, an example of this is the multi-node tree picker property editor. It contains references to other content items, that should also be deployed along with the content that hosts the property itself.

Value connectors are used to track these dependencies and can also be used to transform property data as it is moved between environments.

The following illustrative example considers a property editor that stores the integer ID of a media item. The integer ID of a media item is not consistent between environments, so we'll need to transform it. And we also want to ensure that the related media item itself is transferred as well as the integer ID reference.

Registration

With the artifact and connectors in place, the final step necessary is to register your entity for deployment.

Connectors do not need to be registered. The fact that they inherit from particular interfaces known to Umbraco Deploy is enough to ensure that they will be used.

Custom Entity Types

If custom entity types are introduced that will be handled by Umbraco Deploy, they need to be registered with Umbraco to parse the UDI references. This is done by calling UdiParser.RegisterUdiType in a composer, as shown in the Disk Based Transfers section below.

Disk-Based Transfers

To deploy the entity as schema, via disk-based representations held in .uda files, it's necessary to register the entity with the disk entity service. This is done in a composer.

Additionally, when an entity is saved, the disk artifact and signature cache should be updated. This is handled by implementing a notification handler that inherits from EntitySavedDeployRefresherNotificationAsyncHandlerBase.

First, create a custom notification for your entity that inherits from SavedNotification<T>:

Then create a composer that registers the UDI type and notification handlers:

The HandleDiskArtifacts property controls whether the artifact is written to disk as a .uda file, and the HandleSignatures property controls whether the signature cache is updated. Both default to true.

Finally, ensure your data service publishes the notification when an entity is saved:

Including Plugin Registered Disk Entities in the Schema Comparison Dashboard

The schema comparison feature available under Settings > Deploy, compares Umbraco’s Deploy-managed entities with the data held in the .uda files on disk.

All core Umbraco entities, such as Document Types and Data Types, will be shown. Custom entities registered with RegisterDiskEntityType are also included.

The entity type name is used to look up a localized display name via the deploy_entityTypes_{entityType} or general_{entityType} localization keys. If no translation is provided, the plain entity type is used as the display name.

Backoffice Integrated Transfers

If the optimal deployment workflow for your entity is to have editors control the deployment operations, the transfer entity service should be used. This would be instead of registering with the disk entity service. The process is similar, but a bit more involved. There's a need to also register details of the tree being used for editing the entities. In more complex cases, we also need to be able to handle the situation where multiple entity types are managed within a single tree.

An introduction to this feature can be found in the second half of this recorded session from Codegarden 2021arrow-up-right.

There's also a code sample, demonstrated in the video linked above, available at GitHubarrow-up-right.

The following code shows the registration of an entity for Backoffice deployment, where we have the simplest case of a single tree for the entity. Registration should be done in an UmbracoApplicationStartingNotification handler:

The RegisterTransferEntityType method on the ITransferEntityService takes the following parameters:

  • The name of the entity type, as configured in the UdiDefinition attribute associated with your custom service connector.

  • A set of options, allowing configuration of whether different deploy operations like queue for transfer and partial restore are made available from the tree menu for the entity.

  • An optional function (TryParseUdiRangeFromNodeIdDelegate) used to parse the UDI range from a string-based node ID. For a single entity, this will likely be parsing a GUID from a string. When you have more than one entity in a tree, you must distinguish which entity a particular node ID is for based on the ID. Hence, it's likely the node ID will need to have a prefix or similar that this function needs to parse to extract the GUID. A prefix could look like "product-[guid]" or "store-[guid]".

  • An optional RemoteTreeDetail parameter that adds support for implementing Deploy's "partial restore" feature.

circle-info

The entity type name is used to look up a localized display name via the deploy_entityTypes_{entityType} or general_{entityType} localization keys. If no translation is provided, the plain entity type is used as the display name.

Client-side entity types are tracked separately. If your client-side entity type differs from the server-side entity type, you can use a deployEntityTypeMapping manifest to map between them. See the Version-specific Upgrade Guide for an example.

The remoteTree optional parameter adds support for plugins to implement Deploy's "partial restore" feature. This gives the editor the option to select an item to restore from a tree picker displaying details from a remote environment. The parameter is of type DeployTransferRegisteredEntityTypeDetail.RemoteTreeDetail that defines three pieces of information:

  • A function responsible for returning a level of a tree.

  • The name of the entity (or entities) that can be restored from the remote tree.

  • The remote tree alias.

An example function that returns a level of a remote tree may look like this:

circle-info

Umbraco Deploy provides the ExternalEntityTreeController to serve the external entity tree for partial restore. When you register an entity type with a RemoteTreeDetail, Deploy automatically handles retrieving the tree from the remote environment using the provided TreeEntityGetter function. No custom tree controller implementation is required.

Refreshing Signatures

Umbraco Deploy improves the efficiency of transfers by caching signatures of each artifact in the database for each environment. The signature is a string-based, hashed representation of the serialized artifact. When an update is made to an entity, this signature value should be refreshed.

When using the EntitySavedDeployRefresherNotificationAsyncHandlerBase as shown in the Disk Based Transfers section above, signature refreshing is handled automatically when HandleSignatures is set to true (the default).

If you only need to refresh signatures without writing disk artifacts, you can set HandleDiskArtifacts = false. An example of this could be for content entities that are transferred via the backoffice rather than disk:

Last updated

Was this helpful?