In this topic, you will learn how to implement a singleton - a business class that can have a single instance that cannot be removed. For instance, you can have a singleton object that describes an end-user's company details or general application settings. Approaches that can be used to show a singleton Detail View are also illustrated.
在本主题中,您将学习如何实现单一实例 - 一个可以具有无法删除的单个实例的业务部门。例如,可以有一个描述最终用户的公司详细信息或常规应用程序设置的单例对象。还演示了可用于显示单例详细信息视图的方法。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E237
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E237
.
Implement a Singleton
实现单例
To prohibit singleton deletion and creation of additional singletons, use the Validation Module. Apply the following attributes.
要禁止单例删除和创建其他单例,请使用验证模块。应用以下属性。
using DevExpress.Persistent.Validation; // ... [RuleObjectExists("AnotherSingletonExists", DefaultContexts.Save, "True", InvertResult = true, CustomMessageTemplate = "Another Singleton already exists.")] [RuleCriteria("CannotDeleteSingleton", DefaultContexts.Delete, "False", CustomMessageTemplate = "Cannot delete Singleton.")] public class Singleton { // ... }
The Singleton class itself can be either an XPO persistent class or Entity Framework entity class - it does not matter. If you use Entity Framework, do not forget to add the Singleton type to your DbContext.
Singleton 类本身可以是 XPO 持久类或实体框架实体类 - 这并不重要。如果使用实体框架,请不要忘记将单例类型添加到 DbContext。
To create a singleton's instance, override the UpdateDatabaseAfterUpdateSchema method of your module's Updater class in the following manner.
Singleton 类本身可以是 XPO 持久类或实体框架实体类 - 这并不重要。如果使用实体框架,请不要忘记将单例类型添加到 DbContext。
public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); if (ObjectSpace.GetObjectsCount(typeof(Singleton), null) == 0) { ObjectSpace.CreateObject<Singleton>(); } ObjectSpace.CommitChanges(); }
After adding the code above, a singleton object will be created in the application database, if one does not yet exist.
添加上述代码后,如果应用程序数据库中尚不存在单个对象,则创建单个对象。
Note 注意
The UpdateDatabaseAfterUpdateSchema method is called each time the application runs in debugging mode. So, this 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 Supply Initial Data (XPO) topic.
每次应用程序在调试模式下运行时,都会调用 UpdateDatabase 后更新架构方法。因此,此方法旨在在部署应用程序或其更新时创建初始数据。要查看如何使用此方法的示例,请参阅提供初始数据 (XPO) 主题。
Provide Access to a Singleton Detail View
提供对单例详细信息视图的访问
An XAF application can display a singleton object in different ways, depending on the singleton's purpose. This topic details two possible techniques. The first one uses a PopupWindowShowAction and the second one adds an item in the main form's navigation control.
XAF 应用程序可以以不同的方式显示单例对象,具体取决于单例的用途。本主题详细介绍了两种可能的技术。第一个使用 PopupWindowShowAction,第二个在主窗体的导航控件中添加一个项。
Use the PopupWindowShowAction
使用弹出窗口显示操作
The code below illustrates the ShowSingleton Window Controller that contains the ShowSingleton Action. This Action displays a popup window with the Singleton object's Detail View.
下面的代码说明了包含"显示单一"操作的显示单一窗口控制器。此操作显示一个弹出窗口,其中包含 Singleton 对象的详细信息视图。
public class ShowSingletonController : WindowController { public ShowSingletonController() { //Comment out the following line if you implement this Controller for a Mobile application 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]); detailView.ViewEditMode = ViewEditMode.Edit; e.View = detailView; } }
Run the application and check that the Show Singleton Action is available and you can modify the singleton using this Action.
运行应用程序并检查显示单例操作是否可用,您可以使用此操作修改单例。
Add an Item to the Navigation Control
将项目添加到导航控件
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.
使用模型编辑器将导航项节点添加到应用程序模型(请参阅向导航控件添加项)。将新添加节点的 View 属性设置为Singleton_DetailView。
Run the application and check that the singleton navigation item is available.
运行应用程序并检查单例导航项是否可用。