How to: Perform CRUD Operations with Non-Persistent Objects 如何:对非持久对象执行 CRUD 操作

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:

1.实现以下非持久性类:

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.

2.在 C#/VB 编辑器中打开WinApplication.cs (WinApplication.vb)、WebApplication.cs(WebApplication.vb)和/或MobileApplication.cs(MobileApplication.vb)文件。确保非持久对象空间提供程序已注册在重写的创建默认对象空间提供程序方法(除了现有的 XPObjectSpace 提供程序或 EFObjectSpace 提供程序)。解决方案向导会自动添加此代码。请注意,如果您在较旧的 XAF 版本中创建项目,则此代码可能丢失。

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:

  • NonPersistentObjectSpace.ObjectsGetting and NonPersistentObjectSpace.ObjectByKeyGetting - to provide objects the UI requests;
  • BaseObjectSpace.Committing - to store the Create and Delete operations' result in memory;
  • NonPersistentObjectSpace.ObjectGetting - to change the IObjectSpaceLink.ObjectSpace property when an object is transferred between Object Spaces.

此控制器处理以下事件:

  • 非持久对象空间.对象获取和非持久对象空间.ObjectByKey 获取 - 提供 UI 请求的对象;
  • BaseObjectSpace.提交 - 将创建和删除操作的结果存储在内存中;
  • 非持久对象空间.对象获取 - 在对象空间之间传输对象时更改 IObjectSpaceLink.ObjectSpace 属性。
原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Perform-CRUD-Operations-with-Non-Persistent-Objects.html