ISettings
A settings class holds configuration that an administrator edits in the admin, for the whole installation, one website or one user. Each registered class becomes a panel in the settings, and its public properties marked EditorBrowsable become the fields. Implement ISettings when your module needs values set by an administrator, such as an API key.
Interface declaration
namespace Veva.Core.Domain.Interfaces; public enum SettingsScope { Website = 0, Global = 1, User = 2, } public interface ISettings { bool RequiresAppRecycle { get; } SettingsScope Scope { get; } }
Example
These global settings hold the address and API key of a weather service, in an Integrations group of the settings. The key is a secret, and a Test connection button calls the service with the values on screen. The class is modelled on Veva's Anthropic settings.
using System.ComponentModel; using Veva.Core.Domain.Attributes; using Veva.Core.Domain.Interfaces; using Veva.Core.Domain.Model; [DisplayName("Weather service")] [SettingsGroup("Integrations", Icon = "fas fa-plug", Order = 40)] public class WeatherServiceSettings : ISettings { public bool RequiresAppRecycle => false; public SettingsScope Scope => SettingsScope.Global; [EditorBrowsable] [DisplayName("API address")] [Description("The base address of the weather service")] [DefaultValue("https://api.example.com/")] public string BaseAddress { get; set; } = "https://api.example.com/"; [EditorBrowsable] [DisplayName("API key")] [Description("The key the weather service gave you")] [Secret] public string ApiKey { get; set; } [EditorBrowsable] [DisplayName("Test connection")] [Description("Calls the weather service with the address and key above")] public async Task<SettingsActionResult> TestConnection(CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(ApiKey)) return SettingsActionResult.Failed("No API key is set."); using var client = new HttpClient { BaseAddress = new Uri(BaseAddress) }; client.DefaultRequestHeaders.Add("X-Api-Key", ApiKey); var response = await client.GetAsync("status", cancellationToken); return response.IsSuccessStatusCode ? SettingsActionResult.Ok("The weather service answered.") : SettingsActionResult.Failed("The weather service answered with status " + (int)response.StatusCode + "."); } }
Registration
This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).
using Veva.Core.Application.DependencyInjection; using Veva.Core.Domain.Interfaces; public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { // The key is the class name; the stored values are found by it. services.AddNamed<ISettings, WeatherServiceSettings>(); }
Where it shows. Registering the class with AddNamed<ISettings, T>() makes it a panel in the admin. Scope decides where: a Global panel is in the admin's settings for the whole installation, a Website panel in each website's settings, and a User panel in each user's own profile. The panel's title is the class's [DisplayName]. [SettingsGroup], from Veva.Core.Domain.Attributes, puts the panel in a named group with an icon and an order; a class without one is in General, which comes first. Its PanelOrder places the panel within the group, and the properties' [Category] become sections of the panel.
Who can edit. A Global panel needs Backend permission on the root to read and Admin permission on the root to save. A Website panel needs the same permissions on that website. A User panel is open only to the signed-in user, whatever their permissions.
Fields and values. The fields are the public read/write properties marked [EditorBrowsable], each with the editor for its type (see IPropertyEditorResolver). A save sends only the changed values. They are checked against the property types and data annotations such as [Required], and nothing is stored while one is invalid. A value nobody has set has no stored key: Veva builds the object with its initializers and [DefaultValue] and applies only the stored keys. So an unset value follows your default, also after you change the default in code. The editor shows the default as a placeholder.
Secrets. Mark an API key, password or client secret with [Secret] (Veva.Core.Domain.Attributes). Its stored value is never sent to the browser: the admin only shows that a value is set, and a save that leaves it alone keeps it. It is still stored as plain text, and server code reads the real value. [Secret] does not cover the settings of an extension chosen inside a panel, such as the mail sender service in E-mail settings. Keep such secrets in a global settings class of their own.
Actions. A public method marked [EditorBrowsable] that returns SettingsActionResult (Veva.Core.Domain.Model), or a Task or ValueTask of it, shows as a button on the panel. It runs against the values on screen, including stored secrets. Its parameters are filled from the service container, and it can take a CancellationToken. Return SettingsActionResult.Ok or SettingsActionResult.Failed with a message and optional details. Veva's E-mail settings use an action to send a test e-mail.
Reading the values. Inject ISettingsService (Veva.Core.Domain.Services) and call GetGlobalSettings<T>(), GetWebsiteSettings<T>(websiteId) or GetUserSettings<T>(userId). GetSettings<T>(websiteId, userId) does the same, with null for the parts that do not apply. The service finds the stored values by the class's registration name, which AddNamed sets to the class name. Renaming the class, or registering it under another name, leaves the stored values behind.
Restart. Set RequiresAppRecycle to true when your module reads the values only once, at startup, for example to set up authentication in ConfigureServices. The panel then warns that a change takes effect after the site restarts. The Microsoft and Google sign-in packages in veva-integrations read their settings at startup in this way, through StartupSettingsService.