Skip to content

Added details of abstractions of database availability checks and JSON serialization encoder creation #7286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 16/umbraco-cms/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@
* [Custom Swagger API](reference/custom-swagger-api.md)
* [Umbraco Flavored Markdown](reference/umbraco-flavored-markdown.md)
* [Content Type Filters](reference/content-type-filters.md)
* [Database Availability Checks](reference/database-availability.md)
* [JSON Serialization](reference/json-serialization.md)

## Tutorials

Expand Down
45 changes: 45 additions & 0 deletions 16/umbraco-cms/reference/database-availability.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
description: Describes the checks Umbraco will do on startup to determine the availability of the database, and how this behavior can be customized.
---

# Database Availability Checks

When Umbraco boots it will check for a configured database and, if found, verify that a connection can be made.

The default behavior is to check five times with a one second delay between attempts. If, after that, a connection cannot be established, Umbraco will fail with a `BootFailedException`.

## Implementing Custom Behavior

For projects in development with the potential for misconfigured database settings, this is likely a reasonable approach to take.

In production, when you have stable configuration, you may prefer to override the behavior to better handle cases where your hosting infrastructure might restart.

We support this by abstracting the default behavior behind the `IDatabaseAvailabilityCheck` interface found in the `Umbraco.Cms.Infrastructure.Persistence` namespace.

You can implement your own version of this interface and register it via a composer. This is shown in the following, stub example:

```csharp
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Infrastructure.Persistence;

public class MyDatabaseAvailabilityCheckComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.AddUnique<IDatabaseAvailabilityCheck, MyDatabaseAvailabilityCheck>();
}
}

internal class MyDatabaseAvailabilityCheck : IDatabaseAvailabilityCheck
{
public bool IsDatabaseAvailable(IUmbracoDatabaseFactory databaseFactory)
{
// Provide your custom logic to check database availability, wait as required, and return true once a connection is established.
return true;
}
}

```

For reference and inspiration, the default implementation can be found [in the Umbraco CMS source code](https://github.com/umbraco/Umbraco-CMS/blob/main/src/Umbraco.Infrastructure/Persistence/DefaultDatabaseAvailabilityCheck.cs).

48 changes: 48 additions & 0 deletions 16/umbraco-cms/reference/json-serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
description: Describes how the JSON serialization within Umbraco can be customized.
---

# JSON Serialization

Umbraco uses JSON as a format to serialize information to the database and for output. For example, the configuration of data types and the property values of complex editors are serialized to JSON for persistence.

The serializers within Umbraco uses a `JavascriptEncoder` which only considers basic latin characters as unnecessary to encode.

## Implementing Custom Behavior

For projects making use of non-Latin characters you may want to amend this behavior. By doing so you can reduce the space the serialized information takes up in the database.

We support this by abstracting the default behavior behind the `IJsonSerializerEncoderFactory` interface found in the `Umbraco.Cms.Core.Serialization` namespace.

You can implement your own version of this interface and register it via a composer. This is shown in the following example that marks Cyrillic characters as excluded for encoding:

```csharp
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Infrastructure.Serialization;

namespace Umbraco.Cms.Web.UI.Custom.SystemTextConfigurationEditor;

public class SystemTextConfigurationEditorComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.AddUnique<IJsonSerializerEncoderFactory, MyConfigurationEditorJsonSerializerEncoderFactory>();
}
}

internal class MyConfigurationEditorJsonSerializerEncoderFactory : IJsonSerializerEncoderFactory
{
public JavaScriptEncoder CreateEncoder<TSerializer>()
where TSerializer : IJsonSerializer
{
return JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic);
}
}
```

For reference, the default implementation can be found [in the Umbraco CMS source code](https://github.com/umbraco/Umbraco-CMS/blob/main/src/Umbraco.Infrastructure/Serialization/DefaultJsonSerializerEncoderFactory.cs).