Skip to main content
.NET 8.0+

How to: Display a Non-Persistent Object's List View from the Navigation

  • 3 minutes to read

This example demonstrates how to display a List View with non-persistent objects from the Navigation. Note that this approach is compatible with Client data access mode only.

  1. Declare a non-persistent class (for example, MyNonPersistentObject), and decorate it with the DomainComponent and DefaultClassOptions attributes.

    using DevExpress.ExpressApp.DC;
    using DevExpress.Persistent.Base;
    // ...
    [DomainComponent, DefaultClassOptions]
    public class MyNonPersistentObject {
        // ...
    }
    

    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. 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();
    // ...
    

    At this step, you can run the application and see that the Navigation shows the My Non Persistent Object navigation item. It opens the empty List View and you can click the New Action to create non-persistent objects. However, all created objects are removed when you reopen the List View.

    Empty List View

  3. To fill the List View in code, subscribe to the OnObjectSpaceCreated event in the Application Builder code. In the event handler, subscribe to the NonPersistentObjectSpace.ObjectsGetting event. In the ObjectsGetting handler, check if the requested object type is MyNonPersistentObject and populate the e.Objects collection with new objects.

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

    using DevExpress.ExpressApp;
    using System.ComponentModel;
    // ...
    builder.ObjectSpaceProviders.Events.OnObjectSpaceCreated = context => {
        var nonPersistentObjectSpace = context.ObjectSpace as NonPersistentObjectSpace;
        if (nonPersistentObjectSpace != null) {
            nonPersistentObjectSpace.ObjectsGetting += NonPersistentObjectSpace_ObjectsGetting;
        }
    };
    // ...
    private void NonPersistentObjectSpace_ObjectsGetting(object sender, ObjectsGettingEventArgs e) {
        if (e.ObjectType == typeof(MyNonPersistentObject)) {
            BindingList<MyNonPersistentObject> objects = new BindingList<MyNonPersistentObject>();
            for (int i = 1; i < 10; i++) {
                objects.Add(new MyNonPersistentObject() { Name = string.Format("Object {0}", i) });
            }
            e.Objects = objects;
        }
    }
    

    Tip

    You can also sort and filter List View contents when the List View’s data source is shaped. To do this, use the DynamicCollection class instead of BindingList. Handle the DynamicCollection.FetchObjects event and pass a filtered collection to the e.Objects argument. This event is raised after a View’s data source is reloaded or its sort/filter parameters are changed. Use the Sorting, Criteria, and other event arguments to shape the collection. The following example demonstrates how to implement this: How to filter and sort Non-Persistent Objects.

The image below demonstrates the result.

NonPersistentListViewInNavigation

Tip

The New, Delete and Save Actions are available for non-persistent objects. To access all created, deleted and modified objects within NonPersistentObjectSpace, use the NonPersistentObjectSpace.ModifiedObjects property.

See Also