The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Perform CRUD Operations with Non-Persistent Objects

  • 5 min to read

This example demonstrates how to create, read, update and delete non-persistent objects. In this example, the static cache is used as a temporary storage for non-persistent objects.

  1. Implement the following non-persistent class:

    using System;
    using System.ComponentModel;
    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Data;
    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, "") {
        }
        [Key]
        public Int32 ID {
            get { return id; }
            set { id = value; }
        }
        public String Name {
            get { return name; }
            set {
                if (name != value) {
                    name = value;
                    RaisePropertyChanged(nameof(Name));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        [Browsable(false)]
        public IObjectSpace ObjectSpace {
            get { return objectSpace; }
            set { objectSpace = value; }
        }
    }
    
  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));
    }
    
  3. Implement the following Controller:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using DevExpress.ExpressApp;
    // ...
    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 NonPersistentObjectSpace_ObjectsGetting(Object sender, ObjectsGettingEventArgs e) {
            if (e.ObjectType == typeof(NonPersistentClass)) {
                IObjectSpace objectSpace = (IObjectSpace)sender;
                BindingList<NonPersistentClass> objects = new BindingList<NonPersistentClass>();
                objects.AllowNew = true;
                objects.AllowEdit = true;
                objects.AllowRemove = true;
                foreach (NonPersistentClass obj in objectsCache) {
                    objects.Add(objectSpace.GetObject<NonPersistentClass>(obj));
                }
                e.Objects = objects;
            }
        }
        private void NonPersistentObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) {
            IObjectSpace objectSpace = (IObjectSpace)sender;
            foreach (Object obj in objectsCache) {
                if (obj.GetType() == e.ObjectType && Equals(objectSpace.GetKeyValue(obj), e.Key)) {
                    e.Object = objectSpace.GetObject(obj);
                    break;
                }
            }
        }
        private void NonPersistentObjectSpace_ObjectGetting(object sender, ObjectGettingEventArgs e) {
            if (e.SourceObject is IObjectSpaceLink) {
                ((IObjectSpaceLink)e.TargetObject).ObjectSpace = (IObjectSpace)sender;
            }
        }
        private void NonPersistentObjectSpace_Committing(Object sender, CancelEventArgs e) {
            IObjectSpace objectSpace = (IObjectSpace)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_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
            if (e.ObjectSpace is NonPersistentObjectSpace nonPersistentObjectSpace) {
                nonPersistentObjectSpace.ObjectsGetting += NonPersistentObjectSpace_ObjectsGetting;
                nonPersistentObjectSpace.ObjectByKeyGetting += NonPersistentObjectSpace_ObjectByKeyGetting;
                nonPersistentObjectSpace.ObjectGetting += NonPersistentObjectSpace_ObjectGetting;
                nonPersistentObjectSpace.Committing += NonPersistentObjectSpace_Committing;
            }
        }
        protected override void OnActivated() {
            base.OnActivated();
            Application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
        }
        protected override void OnDeactivated() {
            base.OnDeactivated();
            Application.ObjectSpaceCreated -= Application_ObjectSpaceCreated;
        }
        public NonPersistentClassWindowController()
             : base() {
            TargetWindowType = WindowType.Main;
        }
    }
    

    This Controller handles the following events:

See Also