The UtilityLib libraries provides a set of helper classes to facilitate the implementation of applications by providing standardized implementations in various contexts. An application class is derived from the corresponding base class using the default implementation (constructor, properties, methods). Other helper classes are for default exit codes, data status, property validation, json converters, and custom attributes.
The following interfaces and classes are provided:
- BaseClass
- NumberConverter
- IPAddressConverter
- IPEndPointConverter
- TimeSpanConverter
- SpecialDoubleConverter
- DataStatus
- ExitCodes
- JsonExtensions
- PropertyExtensions
- ValidationExtensions
- GuidAttribute
- UriAttribute
- IPAddressAttribute
- IPEndPointAttribute
- BaseCommand
- BaseOption
- BaseRootCommand
- ArgumentExtensions
- CommandExtensions
- HostExtensions
- OptionExtensions
- ParserExtensions
- BaseController
- PingHealthCheck
The BaseClass class provides a standard logger data member using dependency injection.
protected readonly ILogger<BaseClass> _logger;
The BaseCommand class for console applications provides logger and default json serialization data members and is derived from the Command class.
protected readonly ILogger<BaseCommand> _logger;
The BaseRootCommand class for console applications provides logger and default json serialization data members and is derived from the RootCommand class. The basic options verbose, settings, and configuration are provided. Note that the verbose option is a global command option.
protected readonly ILogger<BaseRootCommand> _logger;
The BaseOptions class for console applications provides base options data members used by the BaseRootCommand class.
The BaseController class for an API controller provides logger and configuration data members. A helper method maps a data status to corresponding ObjectResult.
protected readonly ILogger<BaseController> _logger;
protected readonly IConfiguration _configuration;
This converter is used to convert strings containing numbers.
This converter is used to convert TimeSpans (note: should use ISO8601).
This converter allows special values (NaN, Infinity, -Infinity).
Class to provide OPC UA compatible result codes.
public long Timestamp { get; set; }
public uint Code { get; set; }
public string Name { get; set; }
public string Explanation { get; set; }
public bool IsGood { get; }
public bool IsNotGood { get; }
public bool IsUncertain { get; }
public bool IsNotUncertain { get; }
public bool IsBad { get; }
public bool IsNotBad { get; }
The ExitCodes class provides standard exit codes (enum) for a console application.
Exit Code Name | Code |
---|---|
SuccessfullyCompleted | 0 |
IncorrectFunction | -1 |
FileNotFound | 2 |
PathNotFound | 3 |
CantOpenFile | 4 |
AccessDenied | 5 |
InvalidData | 13 |
ProgramNotRecognized | 9009 |
Exit Code Name | Code |
---|---|
OperationCanceled | 0xC000013A |
UnhandledException | 0xE0434F4D |
Exit Code Name | Code |
---|---|
OperationCanceled | 130 - SIGINT |
UnhandledException | 134 - SIGABRT |
The last two exit codes are OS platform dependent.
The JsonExtensions class provides helper extension methods dealing with JSON serializer options.
public static JsonSerializerOptions DefaultSerializerOptions
public static void AddDefaultOptions(this JsonSerializerOptions options)
Extension class to provide property helper methods for an object.
public static bool IsProperty(this Type type, string name)
public static object? GetPropertyValue(this object obj, string name)
public static void SetPropertyValue(this object obj, string name, object value)
Extension class to provide a validate an object based on its DataAnnotations, possibly throwing an exception if the object is not valid.
public static ICollection<ValidationResult> Validate<T>(this T obj)
public static T ValidateAndThrow<T>(this T obj)
- Guid: Validates using Guid.TryParse().
- IPAddress: Validates using IPAddress.TryParse().
- IPEndPoint: Validates using IPEndPoint.TryParse().
- Uri: Validates using Uri.TryCreate().