[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Feature Center
 [Expand]Design-Time Features
   How to: Access Data in SQL Query Results
   How to: Bind an XPCollection to a LookUp
   How to: Bind an XPCollection to Standard Controls
   How to: Bind an XPCollection to the DataGrid
   How to: Bind an XPCollection to the Grid
   How to: Build Complex Criteria
   How to: Build Simple Criteria
   How to: Change Inheritance Mapping
   How to: Connect to a SQL Server
   How to: Connect to SQL Azure
   How to: Connect XPO to a Database Other than MS SQL or MS Access
   How to: Control Automatic Saving of Objects when Editing in a Grid
   How to: Create an Aggregated Object
   How to: Delay Property Loading
   How to: Filter By DateTime Values
   How to: Filter Persistent Objects by Type
   How to: Handle Persistent Object Initialization
   How to: Implement a Custom Criteria Language Function Operator
   How to: Implement a Full-Text Search
   How to: Implement Custom Functions and Criteria in LINQ to XPO
   How to: Implement Many-to-Many Relationships
   How to: Implement One-to-One Relationships
   How to: Link Classes Located in Different Assemblies
   How to: Make a Class or Property Non-Persistent
   How to: Make XPCollection Create Objects of Specific Type when They are Created via Bound Controls
   How to: Map to Custom Tables (Views) and Columns
   How to: Obtain a Collection of Persistent Objects by a Set of Their IDs
   How to: Obtain a Collection of Persistent Objects for Processing
   How to: Persist a BLOB Field
   How to: Prevent Changes in a Persistent Object from being Automatically Committed
   How to: Restore Deleted Objects
   How to: Use Pageable Collections
   How to: Use Read-Only Persistent Properties
   How to: Use the ContainsOperator for Objects in a Many-to-Many Relationship
   How to: Validate an Object
   How to: Work with Structures
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Handle Persistent Object Initialization

Persistent objects represent an object that exists in some manner between program runs. During program execution, it is represented by non-persistent object instances, thus loading an object from a database is not actually the construction of a new object, but the restoration of an object's state. Here we consider two types of object constructors: actual constructor and default persistent object instance constructor. The former is used to create an object for the first time. The latter is called before loading an object's state from a database. Base XPObject classes have only a default constructor without parameters to prepare an object's instance for loading. Avoid placing any new object initialization logic into the default constructors, create a new constructor and use it to create new persistent objects instead:

Naturally, object properties will be assigned during object state loading, but this assignment is not associated with object interface logic, and sometimes must be handled differently. As an example, consider the NameModified property that is set to true whenever the Name property is changed. When the object is loaded from a database, the Name property is assigned a value and this should be treated not as a name modification, but as an initialization. You can use the PersistentBase.IsLoading property to detect an object's initialization. During initialization it will be set to true, otherwise it will be false:

The IXPObject.OnLoaded virtual method is called when an object's state is completely restored from a database. You can override it if you want to perform additional housekeeping after initialization, because you cannot use constructors for this purpose.

Expanded See Also

Is this topic helpful?​​​​​​​