How to: Implement a Singleton Business Object and Show its Detail View How to: Implement a single business object and display its details view

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.

In this topic, you will learn how to implement a single instance - can have a business sector can not delete a single instance. For example, there may be a single embodiment where end user's company or conventional details described application settings. Also demonstrates a method for displaying a single embodiment of the detail view.

Tip Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E237
Complete sample project can be found in the code sample database DevExpress, http://www.devexpress.com/example=E237

.

Implement a Singleton

Example achieve single

To prohibit singleton deletion and creation of additional singletons, use the Validation Module. Apply the following attributes.

To disable Singleton delete and create other single cases, use the authentication module. 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 class itself can be XPO persistent class or Entity Framework entity class - it does not matter. If you use Entity Framework, please do not forget to add a single case of type to DbContext.

To create a singleton's instance, override the UpdateDatabaseAfterUpdateSchema method of your module's Updater class in the following manner.

Singleton class itself can be XPO persistent class or Entity Framework entity class - it does not matter. If you use Entity Framework, please do not forget to add a single case of type to 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.

After adding the above code, if the application database does not yet exist a single object, a single object is created.

Note 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.
Each time the application is running in debug mode, the update will be called UpdateDatabase architectural approach. Therefore, this method is designed to create the initial data when deploying applications or updates. To see an example of how to use this method, see Providing initial data (XPO) theme.

Provide Access to a Singleton Detail View

It provides access to the single view of the embodiment details

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 application singleton object may be displayed in different ways, depending on the use of single embodiment. This topic details the two possible techniques. The first use PopupWindowShowAction, adding a second entry in the main form of navigation controls.

Use the PopupWindowShowAction

Use popup display operation

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.

The following code shows with "displaying a single" single-window display operation of the controller. This displays a pop-up window containing details view Singleton object.

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.

Run the application and examination revealed a single case of operation is available, you can use this operation to modify a single case.

ShowSingletonActionWin

ShowSingletonActionWeb

Add an Item to the Navigation Control

Add items to the navigation controls

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.

Using the model editor to add the item to navigate to the application node model (see Adding items to the navigation controls). View property is set to add a new node is Singleton_DetailView.

SingletonNavItemME

Run the application and check that the singleton navigation item is available.

Run the application and check the singleton navigation item is available.

SingletonNavItemWin

SingletonNavItemWeb

 

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Implement-a-Singleton-Business-Object-and-Show-its-Detail-View.html