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 GroupNamestring property and a visible non-persistentGroupproperty. The non-persistentGroupclass defines existing string values and has theNameproperty that is also a key property.
- In the Productclass, override theOnLoadedmethod to create aGroupbased on the storedGroupNamevalue.
- Also, override the OnChangedmethod to update theGroupNameproperty when theGroupproperty 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 Featureclass has a compound key that consists of theOwnerKeyandLocalKeyparts. TheOwnerKeyis used to locate the owner object (Project). TheLocalKeyis used to identify aFeatureobject within the collection. These keys are not serialized and exist at runtime only.
- The Projectclass has a hidden persistentFeatureListstring property and a visible non-persistentFeaturesaggregated collection property.
- Override the OnLoadedandOnSavingmethods to serialize and deserialize theFeaturescollection. 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.ListChangedevent of theFeaturescollection to initialize keys of a newly added object and update the persistentFeatureListproperty whenever the collection is modified.
- The NPFeatureAdapterclass (derived from the commonNonPersistentObjectAdapterhelper class) is used to subscribe to NonPersistentObjectSpace events and maintain an object identity map.
- In the overridden LoadObjectByKeymethod (called when the ObjectByKeyGetting event is raised), parse the compound key, locate the owner (Project) usingOwnerKey, and search for the desiredFeaturein itsFeaturescollection 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 WinApplicationandWebApplication, descendants override theGetObjectSpaceToShowDetailViewFrommethod to reuse the source object space for windows that displayAgentobjects. 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 NPAgentAdapterclass (derived from the commonNonPersistentObjectAdapterhelper 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 Technologyobjects.
- The Epochclass has a hidden persistentTechnologyListstring property and a visible non-persistentTechnologiescollection property.
- Override the OnLoadedandOnSavingmethods to serialize and deserialize theTechnologiescollection.
- After deserialization, call the GetObjectByKey method to load related Technologyobjects.
- The NPTechnologyAdapterclass (derived from the commonNonPersistentObjectAdapterhelper class) is used to subscribe to NonPersistentObjectSpace events and maintain an object identity map.
- In the overridden LoadObjectByKeymethod (called when the ObjectByKeyGetting event is raised), loadTechnologydata from storage and create object instances.
- In the overridden CommitChangesmethod (called when the CustomCommitChanges event is raised), saveTechnologyobject 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)