Utilities
Overview
VersaTul.Utilities provides common low-level helpers for conversion, null handling, enum parsing, random value generation, property mapping, CSV-safe encoding, and collection batching.
It is one of the base packages that other VersaTul libraries build on directly or indirectly.
When To Use This Package
Use this package when you want to:
Convert values safely between runtime types.
Centralize null and
DBNullhandling.Generate random strings.
Parse enum names and numeric values consistently.
Split collections into batches.
Map properties between objects with similar shapes.
Installation
Install the package with the .NET CLI:
dotnet add package VersaTul.Utilities
Or with the Package Manager Console:
PM> NuGet\Install-Package VersaTul.Utilities -Version latest
Core Types And Concepts
IUtilityandCommonUtilityThe general-purpose utility surface, including transformation, null filtering, random generation, property-name access, mapping, and CSV encoding.
INullFilteringProvides null and
DBNullfiltering helpers.IGeneratorProvides random string generation.
IMapperUtilitySupports property mapping between different object types.
ICollectionUtility<T>andCollectionUtility<T>Provide collection batching and random sampling helpers.
IEnumParser<T>andEnumParser<T>Provide enum parsing, formatting, and
TryParsesupport.
Key Capabilities
Transform<T>()andTransform(object, Type)convert runtime values into target types.TryTransform()attempts conversions without throwing.NullFilter<T>()andDBNullFilter<T>()normalize missing values.RandomString()generates a random string using a cryptographically secure generator.PropertyMap()copies values between objects that share property names.MakeBatches()groups a collection into smaller slices.EnumParser<T>handles enum-to-string, string-to-enum, int-to-enum, andTryParseflows.
Basic Example
using VersaTul.Utilities;
using VersaTul.Utilities.Contracts;
IUtility utility = new CommonUtility();
var integerValue = utility.Transform<int>("123");
var decimalValue = utility.Transform<decimal>("123.45", useInvariantCulture: true);
var missingString = utility.NullFilter<string>(null);
var csvValue = utility.EncodeCSV("A value, with commas");
var token = utility.RandomString(16);
Enum And Collection Example
using VersaTul.Utilities;
var enumParser = new EnumParser<Color>();
var green = enumParser.Parse("Green");
var blue = enumParser.Parse(3);
var asText = enumParser.ParseToString(Color.Red);
var collectionUtility = new CollectionUtility<int>();
var batches = collectionUtility.MakeBatches(new[] { 1, 2, 3, 4, 5, 6 }, 2);
public enum Color
{
None,
Red,
Green,
Blue
}
Property Mapping Example
using VersaTul.Utilities;
var utility = new CommonUtility();
var source = new CustomerRecord
{
Id = 25,
FirstName = "Bjorn",
LastName = "Williams"
};
var destination = new CustomerDto();
utility.PropertyMap(source, destination);
Notes
Transform<T>()also handles enums and nullable targets.RandomString()usesRandomNumberGeneratorrather thanRandom.MakeBatches()is useful for bulk operations and throttled processing.If you primarily want fluent syntax, use Extensions, which builds on several of these helpers.