How to: Display a Non-Persistent Object's Detail View

  • 5 minutes to read

Show a Non-Persistent Object from the Navigation

  1. Declare a non-persistent class, apply the DomainComponentAttribute to it, and add an object key property.

    using System.ComponentModel;
    using DevExpress.ExpressApp.DC;
    // ...
    [DomainComponent]
    public class NonPersistentObject {
        [Browsable(false)]
        [DevExpress.ExpressApp.Data.Key]
        public int Oid { get; set; }
        public string Name { get; set; }
    }
    
    NOTE

    The INotifyPropertyChanged, IXafEntityObject and IObjectSpaceLink interface implementations were omitted in this example. However, it is recommended to support these interfaces in real-world applications (see PropertyChanged Event in Business Classes and Non-Persistent Objects).

  2. Rebuild the solution.
  3. Run the Model Editor for a module project and add a new navigation item. Note that Mobile applications show only the Default navigation group content. Set the IModelNavigationItem.View property to the identifier of the Detail View to be displayed (for example, NonPersistentObject_DetailView). Set the IModelNavigationItem.ObjectKey property to an arbitrary integer value. Note that this value should be unique if you want to display different non-persistent objects of this type.

    NonPersistentKey

  4. Open the WinApplication.cs (WinApplication.vb), WebApplication.cs (WebApplication.vb), and/or MobileApplication.cs (MobileApplication.vb) file in a C#/VB Editor. Ensure that the NonPersistentObjectSpaceProvider is registered in the overridden CreateDefaultObjectSpaceProvider method (in addition to the existing XPObjectSpaceProvider or EFObjectSpaceProvider). The Solution Wizard adds this code automatically. Note that this code may be missing if you created your project in an older XAF version.

    protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
        // ...
        args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
    }
    
  5. Subscribe to the XafApplication.ObjectSpaceCreated event and, in its handler, subscribe to the NonPersistentObjectSpace.ObjectByKeyGetting event. In the ObjectByKeyGetting event handler, check if the requested object type is NonPersistentObject. If so, pass a NonPersistentObject instance with a key specified at step 3 as the e.Object event parameter.

    using System;
    using DevExpress.ExpressApp;
    // ...
    public sealed partial class MyModule : ModuleBase {
        //...
        public override void Setup(XafApplication application) {
            base.Setup(application);
            application.SetupComplete += Application_SetupComplete;
        }
        private void Application_SetupComplete(object sender, EventArgs e) {
            Application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
        }
        private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
            var nonPersistentObjectSpace = e.ObjectSpace as NonPersistentObjectSpace;
            if(nonPersistentObjectSpace != null) {
                nonPersistentObjectSpace.ObjectByKeyGetting += nonPersistentObjectSpace_ObjectByKeyGetting;
            }
        }
        private void nonPersistentObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) {
            IObjectSpace objectSpace = (IObjectSpace)sender;
            if(e.ObjectType.IsAssignableFrom(typeof(NonPersistentObject))) {
                if(((int)e.Key) == 138) {
                    NonPersistentObject obj138 = objectSpace.CreateObject<NonPersistentObject>();
                    obj138.Oid = 138;
                    obj138.Name = "Sample Object";
                    e.Object = obj138;
                }
            }
        }
    }
    

    To create a new non-persistent object for each Detail View, leave the ObjectKey value empty in the Model Editor and create the following View Controller instead of the code above:

    using DevExpress.ExpressApp;
    // ...
    public class NonPersistentObjectActivatorController : ObjectViewController<DetailView, NonPersistentObject> {
        protected override void OnActivated() {
            base.OnActivated();
            if ((ObjectSpace is NonPersistentObjectSpace) && (View.CurrentObject == null)) {
                View.CurrentObject = ObjectSpace.CreateObject(View.ObjectTypeInfo.Type);
                View.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit;
            }
        }
    }
    

    When you use the CreateObject(Type) method to create an object, the Object Space considers this object as new. In this case, the Save confirmation dialog is displayed when you close the object View. If you do not want to show this dialog, call the NonPersistentObjectSpace.RemoveFromModifiedObjects method that marks this object as existing and not changed.

    The NonPersistentObjectSpace.CreateObject method uses the default constructor to create an object within the Object Space. If you want to use another constructor, create a new object and add it to the Object Space manually. For this purpose, pass this object to the NonPersistentObjectSpace.GetObject method. This allows the Object Space to track changes made to the new object (if it implements the INotifyPropertyChanged interface and the AutoSetModifiedOnObjectChange option is enabled).

Show Non-Persistent Objects in a Dashboard View

To show non-persistent objects in a DashboardView, follow the steps from the previous section and create a dashboard item instead of the navigation item. Refer to the DashboardView class description for information on how a Detail View binds to a Dashboard View item.

Show a Non-Persistent Object in a Modal Dialog Window

  1. Implement a PopupWindowShowAction Action.
  2. Handle its PopupWindowShowAction.CustomizePopupWindowParams event.
  3. In the event handler:

Refer to the Ways to Show a View and Ways to Show a Confirmation Dialog topics for more information and examples.

Show a Non-Persistent Dialog from a Business Class

To execute simple business logic and prompt a user for parameters, use the ActionAttribute as shown in the How to: Create an Action Using the Action Attribute topic.

See Also