Skip to content

ockham-net/exp-ockham.net.convert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ockham.Data

Basic data inspection and conversion utilities. Part of the Ockham.NET project.

The Problem

Every Ockham component should solve a clear problem that is not solved in the .Net BCL, or in the particular libraries it is meant to augment.

The utilities in this library provide robust, configurable data inspection and conversion utilities. These are particularly useful when serializing and deserializing to databases or other external sources, and when working with dynamic data.

Data Conversion

The existing basic data conversions available from the System.Convert and Microsoft.VisualBasic.CompilerServices.Conversions classes fail in several cases that arise frequently when working with serialization and deserialization:

The different conversion and inspection functions of the Convert static class and Converter instance class, along with the various flags of the ConvertOptions enumeration, provide succinct and intuitive conversions that can also be adjusted for the desired specifics, such as:

  • Treating an empty string as null (or not)
  • Recognizing hexadecimal strings (or not)
  • Converting null values to the default value of a value type (or not)
  • Coercing any value to a particular target type, with or without an explicit fallback value (or not)

Dynamic Value Inspection

The BCL lacks succinct methods for checking if a value can be treated as numeric, if it is the default value of a value type (e.g 0 for any numeric type), or if it is null / empty / DBNull. These basic inspection actions are often required when validating or reasoning about input from external services or data sources.

Architecture

The core conversion logic is implemented in the internal method Ockham.Data.Convert.To:

internal static object To(
  object value, 
  Type targetType, 
  ConvertOptions options, 
  bool ignoreError, 
  object defaultValue
)

The various overloads of To* and Force* on the static Convert class and configurable Converter class instances internally resolve to this single conversion logic, just with different variations of the input arguments, and sometimes a direct cast to the required output type.

The conversion logic does not replace built-in BCL conversion tools, but rather arranges and combines them into an efficient whole.

About

Basic data conversion utilities for .NET primitive values

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published