Skip to main content
A newer version of this page is available. .

How to: Perform CRUD Operations with Non-Persistent Objects

  • 5 minutes to read

This example demonstrates how you can create, read, update end delete (perform CRUD operations) with non-persistent objects. The static cache is used here as a temporary storage for non-persistent objects. However, you can implement any other storage that meets your requirements.

Note

Mobile applications do not support CRUD operations for non-persistent objects, so the approach described in this topic is not supported by the Mobile platform. If it is necessary to implement this scenario in your Mobile application, contact us using the Support Center.

  • Implement the following non-persistent class.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.DC;
    using DevExpress.Persistent.Base;
    // ...
    [DomainComponent, DefaultClassOptions]
    public class NonPersistentClass : IObjectSpaceLink, INotifyPropertyChanged {
        private Int32 id;
        private String name;
        private IObjectSpace objectSpace;
        protected void RaisePropertyChanged(String propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public NonPersistentClass(Int32 id, String name)
            : base() {
            this.id = id;
            this.name = name;
        }
        public NonPersistentClass()
            : this(0, "") {
        }
        public Int32 ID {
            get { return id; }
            set { id = value; }
        }
        public String Name {
            get { return name; }
            set {
                if(name != value) {
                    name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        [Browsable(false)]
        public IObjectSpace ObjectSpace {
            get { return objectSpace; }
            set { objectSpace = value; }
        }
    }
    
  • Open the WinApplication.cs (WinApplication.vb) and/or WebApplication.cs (WebApplication.vb) code. Ensure that the NonPersistentObjectSpaceProvider is registered in the overridden CreateDefaultObjectSpaceProvider method (in addition to the existing XPObjectSpaceProvider or EFObjectSpaceProvider). Currently, this code is added automatically by the Solution Wizard, but it may be missing if you have created your project using an older version of XAF.

    protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
        // ...
        args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
    }
    
  • The New, Delete and Save Actions are available for non-persistent objects. With the following Controller, you can access modified non-persistent objects using the NonPersistentObjectSpace.ModifiedObjects property.

    using DevExpress.ExpressApp;
    using System.Collections.Generic;
    // ...
    public class NonPersistentClassWindowController : WindowController {
        private static List<NonPersistentClass> objectsCache;
        static NonPersistentClassWindowController() {
            objectsCache = new List<NonPersistentClass>();
            objectsCache.Add(new NonPersistentClass(1, "A"));
            objectsCache.Add(new NonPersistentClass(2, "B"));
            objectsCache.Add(new NonPersistentClass(3, "C"));
            objectsCache.Add(new NonPersistentClass(4, "D"));
            objectsCache.Add(new NonPersistentClass(5, "E"));
        }
        private void ObjectSpace_ObjectsGetting(Object sender, ObjectsGettingEventArgs e) {
            BindingList<NonPersistentClass> objects = new BindingList<NonPersistentClass>();
            objects.AllowNew = true;
            objects.AllowEdit = true;
            objects.AllowRemove = true;
            foreach(NonPersistentClass obj in objectsCache) {
                objects.Add(obj);
            }
            e.Objects = objects;
        }
        private void ObjectSpace_Committing(Object sender, CancelEventArgs e) {
            NonPersistentObjectSpace objectSpace = (NonPersistentObjectSpace)sender;
            foreach(Object obj in objectSpace.ModifiedObjects) {
                if(obj is NonPersistentClass) {
                    if(objectSpace.IsNewObject(obj)) {
                        objectsCache.Add((NonPersistentClass)obj);
                    }
                    else if(objectSpace.IsDeletedObject(obj)) {
                        objectsCache.Remove((NonPersistentClass)obj);
                    }
                }
            }
        }
        private void Application_ViewCreated(Object sender, ViewCreatedEventArgs e) {
            ObjectView objectView = e.View as ObjectView;
            if((objectView != null) && (objectView.ObjectTypeInfo.Type == typeof(NonPersistentClass))) {
                objectView.ObjectSpace.Committing += ObjectSpace_Committing;
                if(objectView is ListView) {
                    ((NonPersistentObjectSpace)objectView.ObjectSpace).ObjectsGetting += ObjectSpace_ObjectsGetting;
                    ((ListView)objectView).CollectionSource.ResetCollection();
                }
            }
        }
        protected override void OnActivated() {
            base.OnActivated();
            Application.ViewCreated += Application_ViewCreated;
        }
        protected override void OnDeactivated() {
            base.OnDeactivated();
            Application.ViewCreated -= Application_ViewCreated;
        }
       public NonPersistentClassWindowController()
            : base() {
            TargetWindowType = WindowType.Main;
        }
    }
    
See Also