Back to Devexpress

ValueConverter.StorageType Property

xpo-devexpress-dot-xpo-dot-metadata-dot-valueconverter.md

latest3.1 KB
Original Source

ValueConverter.StorageType Property

When overridden in a derived class, gets the type that the property’s value is converted to when it’s saved in a data store.

Namespace : DevExpress.Xpo.Metadata

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public abstract Type StorageType { get; }
vb
Public MustOverride ReadOnly Property StorageType As Type

Property Value

TypeDescription
Type

A Type descendant which represents the type that the property’s value is converted to when it’s saved in a data store.

|

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.

csharp
//...       
[Size(1), ValueConverter(typeof(BooleanToStringValueConverter))]
public bool Answer {
    get { return GetPropertyValue<bool>(nameof(Answer)); }
    set { SetPropertyValue<bool>(nameof(Answer), value); }
}

//...
public class BooleanToStringValueConverter : ValueConverter {
    public override object ConvertFromStorageType(object value) {
        return Convert.ToString(value) == "T";
    }
    public override object ConvertToStorageType(object value) {
        return Convert.ToBoolean(value) ? "T" : "F";
    }
    public override System.Type StorageType {
        get { return typeof(string); }
    }
}
vb
'...       
<Size(1), ValueConverter(GetType(BooleanToStringValueConverter))> _
Public Property Answer() As Boolean
    Get
        Return GetPropertyValue(Of Boolean)(NameOf(Answer))
    End Get
    Set(ByVal value As Boolean)
        SetPropertyValue(Of Boolean)(NameOf(Answer), value)
    End Set
End Property

'...
Public Class BooleanToStringValueConverter
    Inherits ValueConverter
    Public Overrides Function ConvertFromStorageType(ByVal value As Object) As Object
        Return Convert.ToString(value) = "T"
    End Function
    Public Overrides Function ConvertToStorageType(ByVal value As Object) As Object
        Return If(Convert.ToBoolean(value), "T", "F")
    End Function
    Public Overrides ReadOnly Property StorageType() As System.Type
        Get
            Return GetType(String)
        End Get
    End Property
End Class

See Also

ValueConverter Class

ValueConverter Members

DevExpress.Xpo.Metadata Namespace