[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]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Fundamentals
 [Expand]Feature Center
 [Collapse]Concepts
   Basics of Creating Persistent Objects for Existing Data Tables
   Creating a Persistent Object
   Creating a Session
   Creating an XPCollection
   Custom Collection Types
   Deferred and Immediate Object Deletion
   Explicit Units of Work
   Filtering Basics
   Generating Persistent Objects for Existing Data Tables
   How to: Add Persistence to an Existing Hierarchy by Changing the Base Inheritance
   How to: Add Persistence to an Existing Hierarchy by Using Session-less Persistent Objects
   How to: Connect to a Data Store
   Inheritance Mapping
   Nested Units of Work
   NULL Value Handling Specifics
   Optimistic Concurrency Control
   Pageable Collections
   Property Descriptors
   Relationships Between Objects
   Saving Persistent Objects
   Simplified Criteria Syntax
   Simplified Property Syntax
   Sorting Basics
   Using Explicit Transactions
   Using Transactions
   Value Converters
   Working with Sessions
   XPCollection Concepts
   XPDataView Concepts
   XPO Classes Comparison
   XPView Concepts
 [Expand]Design-Time Features
 [Expand]Examples
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Value Converters

There are situations when it is necessary to persist non-persistent property type values in a database. While this requirement can be addressed by creating an additional persistent property and performing the conversion between values, XPO provides a built-in mechanism for such scenarios. The Value Converter is a class that encapsulates conversion methods and can be reused for different properties. A value converter's conversion algorithms are usually applied when the property value is loaded from or saved to the database.

To implement a property value converter, create a class inherited from the ValueConverter class and override its abstract members:

The value converter can be associated with a specific property by applying the ValueConverterAttribute attribute, or with a property type by calling the metadata dictionary’s XPDictionary.RegisterValueConverter method.

There’s a built-in ImageValueConverter you can apply to System.Drawing.Image properties to store them as BLOBs in the database.

Note that the TimeSpan and Enum types are persisted using the corresponding built-in Value Converters by default.

Note

The value converter mechanism is designed to allow persisting non-persistent data types by transforming them into a persistable equivalent. Value converters are not intended for introducing encryption, compression, or other business logic into the persistence process.

Important

  • Properties persisted using Value Converters require the client-side conversion logic, and should not be used in criteria expressions evaluated on the database server side. An attempt to use such properties in criteria may lead to undefined behavior. For instance, when a criterion is translated to a database query, a conversion implemented programmatically may not be applied or may be applied multiple times.

  • Value Converters cannot be applied to a property that represents a primary or foreign key. Use the approach described in the How to make user-friendly object identifiers Knowledge Base article if you need to use text values for object identifiers instead of auto-generated numbers.

Do not use value converters to filter or sort by a persistent type value transformed from a stored persistent value. Instead, create an additional calculated property decorated with PersistentAliasAttribute and express a conversion algorithm using the Criteria Language. If the algorithm is complex, implement a Custom Function Operator and use it in the criteria expression.

Expanded Example

The following code example demonstrates how to store your boolean data in the database using the "T" string for true and "F" for false by defining a new ValueConverter descendant. You may need this converter when using an existing database in which boolean properties are stored such form.

Expanded See Also

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