IConvertible
Overview
When the underlying primitive type implements the System.IConvertible interface (such as int, float, decimal, DateTime, etc.), Vogen automatically generates the IConvertible interface on the value object as well.
This enables seamless integration with .NET APIs that work with IConvertible types, such as:
Convert.ChangeType()for dynamic type conversionData mapping frameworks and ORMs that expect
IConvertibleReflection-based serialization and conversion utilities
Why Vogen Generates IConvertible
The IConvertible interface provides a standardized way for types to participate in runtime type conversion. Many .NET frameworks rely on this interface to perform dynamic conversions without knowing the specific type at compile time.
Example scenarios where IConvertible is essential:
Dynamic ORM mapping - Entity Framework, Dapper, and other ORMs may use
Convert.ChangeType()when mapping database columns to object properties with types determined at runtime.Framework reflection - ASP.NET Core parameter binding, data binding in WPF, and other reflection-based frameworks may rely on
IConvertiblefor flexible type conversion.Generic conversion utilities - Libraries that provide generic data transformation pipelines often depend on
IConvertibleto convert values to arbitrary target types.
By hoisting IConvertible from the underlying primitive to the value object, Vogen ensures your value objects work seamlessly with these frameworks without requiring special-case logic.
How It Works
Vogen hoists the IConvertible methods from the underlying primitive to the value object. These methods include:
ToBoolean(IFormatProvider)ToByte(IFormatProvider)ToChar(IFormatProvider)ToDateTime(IFormatProvider)ToDecimal(IFormatProvider)ToDouble(IFormatProvider)ToInt16(IFormatProvider)ToInt32(IFormatProvider)ToInt64(IFormatProvider)ToSByte(IFormatProvider)ToSingle(IFormatProvider)ToString(IFormatProvider)ToType(Type, IFormatProvider)GetTypeCode()
Method behavior: Each hoisted method delegates to the corresponding method on the underlying primitive value, allowing the conversion logic to be inherited from the primitive while respecting the value object's initialization state.
Example Usage
Basic Conversion with Convert.ChangeType
Using IConvertible Methods Directly
Dynamic Type Conversion in Data Mapping
Custom Implementations
You can provide custom implementations for specific IConvertible methods if the default hoisted behavior doesn't meet your needs. Vogen will respect your custom implementations and not override them.
Initialization State
Hoisted IConvertible methods check if the value object is initialized before delegating to the underlying primitive. If uninitialized, they return the default value for the target type.
When to Use IConvertible vs. Other Conversion Mechanisms
While IConvertible is powerful, Vogen provides several conversion mechanisms suited to different scenarios:
Mechanism | Use Case | Type Safety | Performance |
|---|---|---|---|
IConvertible | Dynamic runtime conversion, framework integration | Medium (runtime dispatch) | Lower (reflection-based) |
Explicit Casting | Type-safe domain logic conversions | High (compile-time) | High (direct code) |
TypeConverter | Framework parameter binding (ASP.NET Core, WPF) | Medium (string-based) | Medium |
Serialization (JSON, BSON) | API/database integration | High (schema-driven) | Depends |
For more detailed guidance, see Conversion Mechanisms.
See Also
Hoisting - General hoisting strategy in Vogen
Integration - Converting and serializing value objects
Casting Operators - Alternative conversion via explicit/implicit casts