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

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 PropertyChanged Event in Business Classes and Non-Persistent Objects).

  2. 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));
    }
    

    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 the created objects are removed when you reopen the List View.

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

    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.ObjectsGetting += ObjectSpace_ObjectsGetting;
            }
        }
        private void ObjectSpace_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 its 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