How to: Show Persistent Objects in a Non-Persistent Object's View How to: Show persistent objects in view non-persistent object

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

This topic describes how to reference or set of attributes and displays it in the user interface, and assign default values ​​may be selected in a non-persistent class declaration persistence type.

  • Persistent references to property
  • Persistent collection
  • Initialization persistent property values
  • important

Persistent Reference Property

Persistent references to property

Consider the following non-persistent class:

Consider the following non-persistent classes:

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 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).
This example omitted INotifyPropertyChanged, IXafEntityObject and IObjectSpaceLink interface. However, it is recommended to support these interfaces in practical applications (see Business class and non-persistent object property change events).
 
 
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.
Use: Methods "how to use non-persistent object to perform CRUD operations" topic in the presentation, save and load support non-persistent objects from the local cache.

 

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.

Here, Person from Business persistent class library (or frame for Entity XPO). Refer to the Business Class Libraries (EF) or add classes (XPO) theme Add Class from Business library, learn how to add the class from the library. In addition, you can also use a custom business class instead of staff.

 

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.

At this stage, details view is invoked when you create a new look for non-persistent object will appear Editor Properties "owner." But look empty, you can not select any of the existing staff.

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:

Created for the current view of the non-persistent object space can not handle Person persistent objects. Create a persistent object for this type of space, follow these steps:

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. processing Xaf application module or controller .ObjectSpace create an event.

2. In the event handler, create an additional space for the Person object type, and add the object space to non-persistent object space. A collection of "additional object space."

 

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

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

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.

The results are as follows.

PersistentInNonPresistent2

Persistent Collection

Persistent collection

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

You can add the "owner" of the collection, rather than "owner" attribute references:

[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;
    }
}

 

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Show-Persistent-Objects-in-a-Non-Persistent-Object-s-View.html