Example E237
Visible to All Users

XAF - How to implement a singleton class

This example demonstrates how to implement a singleton - a class that can have a single instance that cannot be removed.
chrome_54BcDJmFyW

Implementation Details

For details, refer to the following help topic: How to: Implement a Singleton Business Object and Show its Detail View.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

EF/SingletonSolutionEF/SingletonSolutionEF.Module/Controllers/ShowSingletonController.cs
C#
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; } } }
EF/SingletonSolutionEF/SingletonSolutionEF.Module/BusinessObjects/Singleton.cs
C#
using DevExpress.Persistent.BaseImpl; using DevExpress.Persistent.BaseImpl.EF; using DevExpress.Persistent.Validation; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SingletonSolution.Module.BusinessObjects { [RuleObjectExists("AnotherSingletonExists", DefaultContexts.Save, "True", InvertResult = true, CustomMessageTemplate = "Another Singleton already exists.")] [RuleCriteria("CannotDeleteSingleton", DefaultContexts.Delete, "False", CustomMessageTemplate = "Cannot delete Singleton.")] public class Singleton : BaseObject { public virtual string Name { get; set; } public virtual string Description { get; set; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.