How to: Display a Non-Persistent Object's Detail View
- 4 minutes to read
Show a Non-Persistent Object from the Navigation
Declare a non-persistent class, apply the DomainComponentAttribute to it, and add an object key property.
using System.ComponentModel; using DevExpress.ExpressApp.DC; // ... [DomainComponent] public class NonPersistentObject { [Browsable(false)] [DevExpress.ExpressApp.Data.Key] public int Oid { get; set; } public string Name { 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 The Importance of Property Change Notifications for Automatic UI Updates and Non-Persistent Objects).
- Rebuild the solution.
Run the Model Editor for a module project and add a new navigation item. Set the IModelNavigationItem.View property to the identifier of the Detail View to be displayed (for example, NonPersistentObject_DetailView). Set the IModelNavigationItem.ObjectKey property to an arbitrary integer value. Note that this value should be unique if you want to display different non-persistent objects of this type.
Open the application’s Startup.cs and ensure that the Non-Persistent Object Space Provider is registered in the Application Builder code. The Solution Wizard adds this code automatically.
File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs, MySolution.WebApi/Startup.cs
// ... builder.ObjectSpaceProviders // ... .AddNonPersistent(); // ...
In the Application Builder code, subscribe to the
OnObjectSpaceCreated
event. In the event handler, subscribe to the NonPersistentObjectSpace.ObjectByKeyGetting event. In theObjectByKeyGetting
event handler, check if the requested object type isNonPersistentObject
. If so, pass aNonPersistentObject
instance with the key value specified in step 3 as thee.Object
event parameter.File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs, MySolution.WebApi/Startup.cs
using System; using DevExpress.ExpressApp; // ... builder.ObjectSpaceProviders.Events.OnObjectSpaceCreated = context => { var nonPersistentObjectSpace = context.ObjectSpace as NonPersistentObjectSpace; if (nonPersistentObjectSpace != null) { nonPersistentObjectSpace.ObjectByKeyGetting += nonPersistentObjectSpace_ObjectByKeyGetting; } }; // ... private void nonPersistentObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) { IObjectSpace objectSpace = (IObjectSpace)sender; if (e.ObjectType.IsAssignableFrom(typeof(NonPersistentObject))) { if (((int)e.Key) == 138) { NonPersistentObject obj138 = objectSpace.CreateObject<NonPersistentObject>(); obj138.Oid = 138; obj138.Name = "Sample Object"; e.Object = obj138; } } }
To create a new non-persistent object for each Detail View, leave the
ObjectKey
value empty in the Model Editor and create the following View Controller instead of the code above:using DevExpress.ExpressApp; // ... public class NonPersistentObjectActivatorController : ObjectViewController<DetailView, NonPersistentObject> { protected override void OnActivated() { base.OnActivated(); if ((ObjectSpace is NonPersistentObjectSpace) && (View.CurrentObject == null)) { View.CurrentObject = ObjectSpace.CreateObject(View.ObjectTypeInfo.Type); View.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit; } } }
When you use the CreateObject(Type) method to create an object, the Object Space considers this object as new. In this case, the Save confirmation dialog is displayed when you close the object View. If you do not want to show this dialog, call the NonPersistentObjectSpace.RemoveFromModifiedObjects method that marks this object as existing and not changed.
The NonPersistentObjectSpace.CreateObject method uses the default constructor to create an object within the Object Space. If you want to use another constructor, create a new object and add it to the Object Space manually. For this purpose, pass this object to the NonPersistentObjectSpace.GetObject method. This allows the Object Space to track changes made to the new object (if it implements the INotifyPropertyChanged interface and the AutoSetModifiedOnObjectChange option is enabled).
Show Non-Persistent Objects in a Dashboard View
To show non-persistent objects in a DashboardView, follow the steps from the previous section and create a dashboard item instead of the navigation item. Refer to the DashboardView class description for information on how a Detail View binds to a Dashboard View item.
Show a Non-Persistent Object in a Modal Dialog Window
- Implement a PopupWindowShowAction Action.
- Handle its PopupWindowShowAction.CustomizePopupWindowParams event.
In the event handler:
- Use the CreateObjectSpace(Type) method to create an Object Space for the non-persistent type.
- Use the XafApplication.CreateDetailView method to create a Detail View for the non-persistent type.
- Set the CustomizePopupWindowParamsEventArgs.View property to this Detail View.
Refer to the following topics for more information and examples:
Show a Non-Persistent Dialog from a Business Class
To execute simple business logic and prompt a user for parameters, use the ActionAttribute as shown in the How to: Create an Action Using the Action Attribute topic.