expressappframework-112916-ui-construction-views-ways-to-show-a-view-how-to-implement-a-singleton-business-object-and-show-its-detail-view.md
A singleton is a business class that has only one instance that cannot be deleted. Singletons are useful for storing application-wide settings, company information, or other unique configuration data. This scenario creates a singleton class and displays its Detail View in your application.
If you implement this solution in a multi-tenant application, singleton data is persisted in each tenant database (tenant-specific settings). If you need global settings shared by all tenants, use the following technique instead: Shared Data Support in a Multi-Tenant Application.
Tip
A complete sample project is available in the following GitHub Example: XAF - How to implement a singleton class.
To prohibit singleton deletion and creation of additional singletons, use the Validation Module. Apply the following attributes: RuleObjectExists and RuleCriteria:
If you use Entity Framework Core, add the Singleton type to your DbContext:
To create a singleton instance, override the UpdateDatabaseAfterUpdateSchema method of your module’s Updater class:
Note
XAF calls the UpdateDatabaseAfterUpdateSchema method each time the application runs in debugging mode. The method is targeted to create initial data when deploying the application or its update. To see an example of how you can use this method, refer to the following topic: Supply Initial Data.
This scenario details the following techniques to display a singleton object:
The following code snippet creates the ShowSingleton Window Controller that contains a PopupWindowShowAction Action. This Action displays a popup window with the Singleton object’s Detail View.
File: SingletonSolution.Module\Controllers\ShowSingletonController.cs
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using SingletonSolution.Module.BusinessObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace SingletonSolution.Module.Controllers {
public class ShowSingletonController : WindowController {
public ShowSingletonController() {
this.TargetWindowType = WindowType.Main;
PopupWindowShowAction showSingletonAction =
new PopupWindowShowAction(this, "ShowSingleton", PredefinedCategory.View);
showSingletonAction.CustomizePopupWindowParams += showSingletonAction_CustomizePopupWindowParams;
}
private void showSingletonAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Singleton));
DetailView detailView = Application.CreateDetailView(objectSpace, objectSpace.GetObjects<Singleton>()[0]);
e.View = detailView;
}
}
}
Run the application. Verify that the Show Singleton Action is available and you can modify the singleton.
Windows Forms ASP.NET Core Blazor
Add the NavigationItem node to the Application Model using the Model Editor (see Add an Item to the Navigation Control). Set the View property of the newly added node to Singleton_DetailView.
Run the application and verify that the singleton navigation item is available.
Windows Forms ASP.NET Core Blazor See Also
Validation (Prevent Data Errors)