• How to: Show Persistent Objects in a Non-Persistent Object's View 如何:在非持久对象的视图中显示持久对象


    This topic describes how to declare a persistent type reference or collection property in a non-persistent class and display it in the user interface, and optionally assign a default value to it.

    • Persistent Reference Property
    • Persistent Collection
    • Initialize Persistent Property Values
    • Important Notes

    本主题介绍如何在非持久性类中声明持久性类型引用或集合属性并将其显示在用户界面中,并可以选择为其分配默认值。

    • 持久引用属性
    • 持久收集
    • 初始化持久性属性值
    • 重要说明

    Persistent Reference Property

    持久引用属性

    Consider the following non-persistent class:

    请考虑以下非持久性类:

    using DevExpress.ExpressApp.DC;
    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.BaseImpl;
    // ...
    [DomainComponent, DefaultClassOptions]
    public class NonPersistentObject {
        // ...
        public string Name { get; set; }
        public Person Owner { get; set; }
        // ...
    }
    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).
    本示例中省略了 INotifyPropertyChanged、IXafEntityObject 和 IObjectSpaceLink 接口实现。但是,建议在实际应用程序中支持这些接口(请参阅业务类和非持久性对象中的属性更改事件)。
     
     
    Tip 提示Use the approach demonstrated in the How to: Perform CRUD Operations with Non-Persistent Objects topic to support saving and loading non-persistent objects from a local cache.
    使用"如何:使用非持久性对象执行 CRUD 操作"主题中演示的方法,支持从本地缓存保存和加载非持久性对象。

    Here, Person is a persistent class from the Business Class Library (for Entity Framework or XPO). Refer to the Add a Class from the Business Class Library (EF) or Add a Class from the Business Class Library (XPO) topic to learn how to add a class from this library. Also, you can use your custom business class instead of Person.

    此处,Person 是来自 Business 类库的持久类(对于实体框架或 XPO)。请参阅从商务舱库 (EF) 添加类或从 Business 类库 (XPO) 主题添加类,了解如何从该库添加类。此外,您还可以使用自定义业务类而不是人员。

    At this stage, the Detail View invoked when creating a new NonPersistentObject, displays the lookup editor for the Owner property. However, the lookup is empty, and you cannot choose any existing Person.

    在此阶段,在创建新的非持久对象时调用的详细信息视图将显示"所有者"属性的查找编辑器。但是,查找为空,您不能选择任何现有的人员。

    PersistentInNonPresistent1

    The NonPersistentObjectSpace created for the current View cannot process the Person persistent object. Follow the steps below to create a persistent Object Space for this type:

    为当前视图创建的非持久对象空间无法处理 Person 持久对象。按照以下步骤为此类型创建持久对象空间:

    1.Handle the XafApplication.ObjectSpaceCreated event in a module or Controller.

    2.In the event handler, create an additional Object Space for the Person type and add this Object Space to the NonPersistentObjectSpace.AdditionalObjectSpaces collection.

    1.在模块或控制器中处理 Xaf 应用程序.ObjectSpace 创建事件。

    2.在事件处理程序中,为 Person 类型创建一个额外的对象空间,并将此对象空间添加到非持久对象空间。"附加对象空间"集合。

     

    The following example demonstrates how to do this in a platform-agnostic module (MySolution.Module/Module.cs(vb)):

    下面的示例演示如何在与平台无关的模块(MySolution.模块/模块.cs(vb)中执行此操作:

    using DevExpress.ExpressApp;
    //...
    public class MySolutionModule : ModuleBase {
        private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
            if(e.ObjectSpace is NonPersistentObjectSpace) {
                IObjectSpace additionalObjectSpace = Application.CreateObjectSpace(typeof(Person));
                ((NonPersistentObjectSpace)e.ObjectSpace).AdditionalObjectSpaces.Add(additionalObjectSpace);
                e.ObjectSpace.Disposed += (s, args) => {
                    additionalObjectSpace.Dispose();
                };
            }
        }
        public override void Setup(XafApplication application) {
            base.Setup(application);
            application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
        }
    }

    The result is demonstrated below.

    结果如下。

    PersistentInNonPresistent2

    Persistent Collection

    持久收集

    You can add the Owners collection instead of the Owner reference property:

    您可以添加"所有者"集合,而不是"所有者"引用属性:

    [DomainComponent, DefaultClassOptions]
    public class NonPersistentObject{
        // ... 
        public string Name { get; set; }
        private IList<Person> owners = new List<Person>();
        public IList<Person> Owners {
            get {
                return owners;
            }
        }
    }

    Users can add and remove Owners via the Link and Unlink Actions after adding the code from the previous section.

    用户可以在添加上一节中的代码后,通过链接和取消链接操作添加和删除所有者。

    PersistentInNonPresistent_Collection

    Initialize Persistent Property Values

    初始化持久性属性值

    Implement the IObjectSpaceLink interface in your non-persistent class and use the IObjectSpace.GetObjects<T> method to get required persistent objects.

    在非持久性类中实现 IObjectSpaceLink 接口,并使用 IObjectSpace.GetObjects<T> 方法获取所需的持久对象。

    [DomainComponent, DefaultClassOptions]
    public class NonPersistentObject : IObjectSpaceLink {
        public string Name { get; set; }
        private Person owner;
        public Person Owner {
            get {
                if (owner == null) {
                    owner = ObjectSpace.GetObjects<Person>(CriteriaOperator.Parse("FirstName='Sam'")).FirstOrDefault();
                }
                return owner;
            }
            set { owner = value; }
        }
        private IList<Person> owners;
        public IList<Person> Owners {
            get {
                if (owners == null) {
                    owners = ObjectSpace.GetObjects<Person>(CriteriaOperator.Parse("StartsWith(FirstName, 'B')")) ;
                }
                return owners;
            }
        }
        private IObjectSpace objectSpace;
        [Browsable(false)]
        public IObjectSpace ObjectSpace {
            get { return objectSpace; }
            set { objectSpace = value; }
        }
    }

    If you create a new NonPersistentObject in UI, its Owner property and Owner collection are initialized:

    如果在 UI 中创建新的非持久对象,则初始化其所有者属性和所有者集合:

    PersistentInNonPresistent_Init

    Important Notes

    重要说明

    Note the following if your non-persistent class contains persistent properties and implements IObjectSpaceLink.

    如果非持久性类包含持久性属性并实现 IObjectSpaceLink,请注意以下事项。

    When loading such an object from a separate NonPersistentObjectSpace, you get the same object instance linking to the initial Object Space via IObjectSpaceLink.ObjectSpace. If this behavior is unwanted, you can update the Object Space in the NonPersistentObjectSpace.ObjectsGetting event handler. Modify the MySolutionModule class demonstrated in the Persistent Reference Property section as follows:

    从单独的非持久对象空间加载此类对象时,您会通过 IObjectSpace.ObjectSpace 获得链接到初始对象空间的相同对象实例。如果此行为是不需要的,则可以更新非持久对象空间中的对象空间。修改持久参考属性部分中演示的 MySolutionModule 类,如下所示:

    using DevExpress.ExpressApp;
    //...
    public class MySolutionNameModule : ModuleBase {
        private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
            if(e.ObjectSpace is NonPersistentObjectSpace) {
                IObjectSpace additionalObjectSpace = Application.CreateObjectSpace(typeof(Person));
                ((NonPersistentObjectSpace)e.ObjectSpace).AdditionalObjectSpaces.Add(additionalObjectSpace);
                ((NonPersistentObjectSpace)e.ObjectSpace).ObjectGetting += ObjectSpace_ObjectGetting;
                e.ObjectSpace.Disposed += (s, args) => {
                    ((NonPersistentObjectSpace)s).ObjectGetting -= ObjectSpace_ObjectGetting;
                    additionalObjectSpace.Dispose();
                };
            }
        }
        private void ObjectSpace_ObjectGetting(object sender, ObjectGettingEventArgs e) {
            if(e.SourceObject is IObjectSpaceLink) {
                e.TargetObject = e.SourceObject;
                ((IObjectSpaceLink)e.TargetObject).ObjectSpace = (IObjectSpace)sender;
            }
        }
        public override void Setup(XafApplication application) {
            base.Setup(application);
            application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
        }
    }
  • 相关阅读:
    2021/9/20 开始排序算法
    快速排序(自己版本)
    2021/9/17(栈实现+中后缀表达式求值)
    2021/9/18+19(中缀转后缀 + 递归 迷宫 + 八皇后)
    20212021/9/13 稀疏数组
    2021/9/12 线性表之ArrayList
    开发环境重整
    Nginx入门
    《财富的帝国》读书笔记
    Linux入门
  • 原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Show-Persistent-Objects-in-a-Non-Persistent-Object-s-View.html
Copyright © 2020-2023  润新知