学习011-04-05 How to: Perform CRUD Operations with Non-Persistent Objects(如何:对非持久化对象执行增删改查操作)

How to: Perform CRUD Operations with Non-Persistent Objects(如何:对非持久化对象执行增删改查操作)

This topic describes how to implement the create, read, update, and delete operations for non-persistent objects. This example uses ValueManager to access the storage of non-persistent objects.
本主题介绍如何实现非持久性对象的创建、读取、更新和删除操作。此示例使用ValueManager访问非持久性对象的存储。

Implement the following non-persistent class:
实现以下非持久类:

C#
using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Data;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
// ...
[DomainComponent, DefaultClassOptions]
public class NonPersistentClass : INotifyPropertyChanged {
    private Int32 id;
    private String name;
    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, Browsable(false)]
    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;
}

2.Open the WinApplication.cs, WebApplication.cs , and/or BlazorApplication.cs file in a C# 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.
在C#编辑器中打开WinApplication. cs、WebApplication.cs和/或BlazorApplication.cs文件。确保NonPersistentObjectSpaceProvider已在覆盖的CreateDefaultObjectSpaceProvider方法中注册(除了现有的XPObjectSpaceProvider或EFObjectSpaceProvider)。解决方案向导会自动添加此代码。请注意,如果您在较旧的XAF版本中创建项目,此代码可能会丢失。

C#
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
    // ...
    args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
}

3.In a module project, access the NonPersistentObjectSpace and handle its events listed below.
在模块项目中,访问NonPersistentObjectSpace并处理下面列出的事件。

  • NonPersistentObjectSpace.ObjectsGetting and NonPersistentObjectSpace.ObjectByKeyGetting - to provide objects that the UI requests;
    NonPersistentObjectSpace.ObjectsGetting and NonPersistentObjectSpace.ObjectByKeyGetting -提供UI请求的对象;
  • BaseObjectSpace.Committing - to store a result of the Create and Delete operations in memory;
    BaseObjectSpace.Committing -将创建和删除操作的结果存储在内存中;
    The following code shows how to do this:
    以下代码显示了如何执行此操作:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
// ...
public sealed partial class MyModule : ModuleBase {
    private static Dictionary<int, NonPersistentClass> ObjectsCache {
        get {
            var manager = ValueManager.GetValueManager<Dictionary<int, NonPersistentClass>>("NP");
            Dictionary<int, NonPersistentClass> objectsCache = (manager.CanManageValue) ? manager.Value : null;
            if(objectsCache == null) {
                objectsCache = new Dictionary<int, NonPersistentClass>();
                objectsCache.Add(0, new NonPersistentClass(0, "A"));
                objectsCache.Add(1, new NonPersistentClass(1, "B"));
                objectsCache.Add(2, new NonPersistentClass(2, "C"));
                objectsCache.Add(3, new NonPersistentClass(3, "D"));
                objectsCache.Add(4, new NonPersistentClass(4, "E"));
                if(manager.CanManageValue) {
                    manager.Value = objectsCache;
                }
            }
            return objectsCache;
        }
    }
    //...
    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) {
        NonPersistentObjectSpace nonPersistentObjectSpace = e.ObjectSpace as NonPersistentObjectSpace;
        if(nonPersistentObjectSpace != null) {
            nonPersistentObjectSpace.ObjectsGetting += NonPersistentObjectSpace_ObjectsGetting;
            nonPersistentObjectSpace.ObjectByKeyGetting += NonPersistentObjectSpace_ObjectByKeyGetting;
            nonPersistentObjectSpace.Committing += NonPersistentObjectSpace_Committing;
        }
    }
    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.Values) {
                objects.Add(objectSpace.GetObject<NonPersistentClass>(obj));
            }
            e.Objects = objects;
        }
    }
    private void NonPersistentObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) {
        IObjectSpace objectSpace = (IObjectSpace)sender;
        if(e.ObjectType == typeof(NonPersistentClass)) {
            NonPersistentClass obj;
            if(ObjectsCache.TryGetValue((int)e.Key, out obj)) {
                e.Object = objectSpace.GetObject(obj);
            }
        }
    }
    private void NonPersistentObjectSpace_Committing(Object sender, CancelEventArgs e) {
        IObjectSpace objectSpace = (IObjectSpace)sender;
        foreach(Object obj in objectSpace.ModifiedObjects) {
            NonPersistentClass myobj = obj as NonPersistentClass;
            if(obj != null) {
                if(objectSpace.IsNewObject(obj)) {
                    int key = ObjectsCache.Count;
                    myobj.ID = key;
                    ObjectsCache.Add(key, myobj);
                }
                else if(objectSpace.IsDeletedObject(obj)) {
                    ObjectsCache.Remove(myobj.ID);
                }
            }
        }
    }
}

Tip
Refer to the following GitHub repository to see the extended example: How to implement CRUD operations for Non-Persistent Objects stored remotely.(https://github.com/DevExpress-Examples/XAF-How-to-Implement-CRUD-Operations-for-Non-Persistent-Objects-Stored-Remotely)
请参阅以下GitHub存储库以查看扩展示例:如何为远程存储的非持久性对象实现CRUD操作。(https://github.com/DevExpress-Examples/XAF-How-to-Implement-CRUD-Operations-for-Non-Persistent-Objects-Stored-Remotely)

Important Notes(重要注意事项)

1.A non-persistent Object Space does not track changes of original objects and does not add changed objects to the ModifiedObjects collection. You can use the IObjectSpace.SetModified(Object) method to add changed objects to this collection explicitly. If a non-persistent object implements the INotifyPropertyChanged interface, you can set AutoSetModifiedOnObjectChange / AutoSetModifiedOnObjectChangeByDefault to true to automatically add the changed object to the ModifiedObjects collection when PropertyChanged is raised.
非持久性对象空间不会跟踪原始对象的更改,也不会将更改后的对象添加到ModifiedObjects集合中。您可以使用IObjectSpace. SetModified(Object)方法显式地将更改后的对象添加到该集合中。如果非持久性对象实现了INotifyPropertyChanged接口,您可以将AutoSetModifiedOnObjectChange/AutoSetModifiedOnObjectChangeByDefault设置为true,以便在引发Properties tyChanged时自动将更改后的对象添加到ModifiedObjects集合中。

2.If a non-persistent object implements IObjectSpaceLink and its ObjectSpace property value doesn’t match the Object Space that manipulates the object, the following exception is thrown: ‘An error with the number 1021 has occurred. Error message: The object that was edited belongs to a different ObjectSpace. This error can occur if you manipulate your objects via an ObjectSpace to which these objects do not belong.’
如果非持久对象实现了IObjectSpaceLink,并且其ObjectSpace属性值与操作该对象的Object Space不匹配,则会引发以下异常:“发生了编号为1021的错误。错误消息:编辑的对象属于不同的ObjectSpace。如果您通过这些对象不属于的ObjectSpace操作对象,则可能会发生此错误。”

We recommend that you do not share non-persistent objects when they implement IObjectSpaceLink or have linked persistent objects because of the following limitations.
由于以下限制,我们建议您在实现IObjectSpaceLink或链接了持久对象时不要共享非持久对象。

  • You can use a shared (static) storage for non-persistent objects only if a single NonPersistentObjectSpace works with these objects at any time. Non-persistent objects that implement IObjectSpaceLink cannot belong to multiple Object Spaces simultaneously. When you change the IObjectSpaceLink.ObjectSpace property to a different Object Space, dispose of the previously used Object Space. Otherwise, object changes can trigger events in multiple Views and cause conflicts or unexpected behavior.
    仅当单个NonPersistentObjectSpace在任何时候使用这些对象时,才能对非持久性对象使用共享(静态)存储。实现IObjectSpaceLink的非持久性对象不能同时属于多个对象空间。当您将IObjectSpaceLink. ObjectSpace属性更改为不同的对象空间时,处置以前使用的对象空间。否则,对象更改可能会在多个视图中触发事件并导致冲突或意外行为。
  • You cannot share persistent objects between Object Spaces.
    您不能在对象空间之间共享持久对象。

Instead of sharing non-persistent objects, create new instances in each NonPersistentObjectSpace. If you want to preserve data from all instances of a non-persistent object, store this data separately. See the example in the FeatureCenter.Module\NonPersistentObjects\Controllers.cs file (the NonPersistentObjectSpaceExtender class) in the Feature Center demo that is installed in the %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter.NETFramework.XPO folder.
不要共享非持久性对象,而是在每个NonPersistentObjectSpace中创建新实例。如果要保留来自非持久性对象所有实例的数据,请单独存储这些数据。请参阅安装在%PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter. NETFramework.XPO文件夹中的FeatureCenter.Module\NonPersistentObjects\Controller.cs文件(NonPersistentObjectSpaceExtender类)中的示例。

For more information, refer to the following Breaking Change description: Core - Error 1021 may occur if a non-persistent object implements IObjectSpaceLink and shared between different Object Spaces.
有关详细信息,请参阅以下重大更改描述:Core-如果非持久对象实现IObjectSpaceLink并在不同对象空间之间共享,则可能会发生错误1021。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值