Skip to main content
.NET 8.0+

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

  • 4 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 The Importance of Property Change Notifications for Automatic UI Updates and Non-Persistent Objects).

  2. Rebuild the solution.
  3. Run the Model Editor for a module project and add a new navigation item. 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 application’s Startup.cs and ensure that the Non-Persistent Object Space Provider is registered in the Application Builder code. The Solution Wizard adds this code automatically.

    File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs, MySolution.WebApi/Startup.cs

    // ...
    builder.ObjectSpaceProviders
        // ...
        .AddNonPersistent();
    // ...
    
  5. In the Application Builder code, subscribe to the OnObjectSpaceCreated event. In the event 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 the key value specified in step 3 as the e.Object event parameter.

    File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs, MySolution.WebApi/Startup.cs

    using System;
    using DevExpress.ExpressApp;
    // ...
    builder.ObjectSpaceProviders.Events.OnObjectSpaceCreated = context => {
        var nonPersistentObjectSpace = context.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 following 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