Often times we are required to store complex data in persistent business objects in compact form (as a string or a byte array), but to show and edit this complex data as objects using standard XAF UI. To address this task in XAF, use non-persistent objects nested in persistent business objects as reference or collection properties. This example demonstrates possible implementations for such scenarios.
To create built-in functionality that can applied to a combination of persistent and non-persistent objects, follow the steps below:
- In the common Module, subscribe to the XafApplication.ObjectSpaceCreated event.
- Call the CompositeObjectSpace.PopulateAdditionalObjectSpaces method.
- Enable the AutoCommitAdditionalObjectSpaces, AutoRefreshAdditionalObjectSpaces, and AutoSetModifiedOnObjectChange options, and set up helpers (adapters) that will handle NonPersistentObjectSpace events.
Warning
We created this example for demonstration purposes and it is not intended to address all possible usage scenarios. If this example does not have certain functionality or you want to change its behavior, you can extend this example. Note that such an action can be complex and would require good knowledge of XAF: UI Customization Categories by Skill Level and a possible research of how our components function. Refer to the following help topic for more information: Debug DevExpress .NET Source Code with PDB Symbols. We are unable to help with such tasks as custom programming is outside our Support Service purview: Technical Support Scope.
If you have a string field in a persistent business object, you can display this field in the UI using a lookup editor so that a user can choose from existing values or add a new value. The list of existing values is created dynamically.
To implement this scenario, do the following:
- This scenario is demonstrated by the Product business object. Add a hidden persistent
GroupName
string property and a visible non-persistentGroup
property. The non-persistentGroup
class defines existing string values and has theName
property that is also a key property. - In the
Product
class, override theOnLoaded
method to create aGroup
based on the storedGroupName
value. - Also, override the
OnChanged
method to update theGroupName
property when theGroup
property is changed. - To populate the lookup list view, subscribe to the NonPersistentObjectSpace.ObjectsGetting event and collect unique group names from all existing Product objects.
In this scenario, a persistent business object includes a string field. This field holds a collection of complex data items serialized to XML. You need to display that collection in the UI as a List View (and not as just a text field that contains XML code). This example creates such a List View that allows users to browse and modify the collection and its individual items.
- This solution is demonstrated by the Project business object. The non-persistent Feature class represents complex collection items. The
Feature
class has a compound key that consists of theOwnerKey
andLocalKey
parts. TheOwnerKey
is used to locate the owner object (Project
). TheLocalKey
is used to identify aFeature
object within the collection. These keys are not serialized and exist at runtime only. - The
Project
class has a hidden persistentFeatureList
string property and a visible non-persistentFeatures
aggregated collection property. - Override the
OnLoaded
andOnSaving
methods to serialize and deserialize theFeatures
collection. Note that after deserialization, you should initialize the local key property and the owner key property. - Call the NonPersistentObjectSpace.GetObject method to avoid creation of duplicated objects and to apply deserialized data to the found object.
- Subscribe to the
IBindingList.ListChanged
event of theFeatures
collection to initialize keys of a newly added object and update the persistentFeatureList
property whenever the collection is modified. - The
NPFeatureAdapter
class (derived from the commonNonPersistentObjectAdapter
helper class) is used to subscribe to NonPersistentObjectSpace events and maintain an object identity map. - In the overridden
LoadObjectByKey
method (called when the ObjectByKeyGetting event is raised), parse the compound key, locate the owner (Project
) usingOwnerKey
, and search for the desiredFeature
in itsFeatures
collection usingLocalKey
.
- This solution is demonstrated by the Department business object. The non-persistent Agent class stores complex collection items and has a simple key.
- In
WinApplication
andWebApplication
, descendants override theGetObjectSpaceToShowDetailViewFrom
method to reuse the source object space for windows that displayAgent
objects. This approach simplifies code, but changes made to non-persistent objects in separate windows cannot be undone. As a result, these windows do not have Save and Cancel actions. - The
NPAgentAdapter
class (derived from the commonNonPersistentObjectAdapter
helper class) is used to subscribe to NonPersistentObjectSpace events and maintain an object identity map.
In a persistent business object, you have a string field where we store a sequence of key values. These keys correspond to objects stored elsewhere (in the application model or in an external service). You need to show these objects in the UI as a nested list view and allow users to edit the collection by adding and removing items.
- This scenario is demonstrated by the Epoch business object. The non-persistent Technology class represents complex collection items. In this example, a static dictionary stores
Technology
objects. - The
Epoch
class has a hidden persistentTechnologyList
string property and a visible non-persistentTechnologies
collection property. - Override the
OnLoaded
andOnSaving
methods to serialize and deserialize theTechnologies
collection. - After deserialization, call the GetObjectByKey method to load related
Technology
objects. - The
NPTechnologyAdapter
class (derived from the commonNonPersistentObjectAdapter
helper class) is used to subscribe to NonPersistentObjectSpace events and maintain an object identity map. - In the overridden
LoadObjectByKey
method (called when the ObjectByKeyGetting event is raised), loadTechnology
data from storage and create object instances. - In the overridden
CommitChanges
method (called when the CustomCommitChanges event is raised), saveTechnology
object data to storage.
- How to implement CRUD operations for Non-Persistent Objects stored remotely in eXpressApp Framework
- How to edit Non-Persistent Objects nested in a Persistent Object
- How to: Display a List of Non-Persistent Objects
- How to filter and sort Non-Persistent Objects
- How to refresh Non-Persistent Objects and reload nested Persistent Objects
- How to edit a collection of Persistent Objects linked to a Non-Persistent Object
We don't have an EF Core version of this example. If you need this version, please create a ticket in our Support Center and describe your ultimate goal in detail. We will do our best to assist you.
(you will be redirected to DevExpress.com to submit your response)