The package provides a lightweight, fluent API for runtime validation of objects and conditions. This library helps ensure that parameters meet expected conditions before execution, reducing boilerplate code for argument validation.
$ dotnet add package NetExt.RequireAwaiter is a lightweight package for simplifying the management of multiple asynchronous tasks. It allows you to seamlessly await tasks with or without return values, combining them into a single, structured workflow. With tuple-based result handling, Awaiter helps you reduce boilerplate code and improves the readability of complex asynchronous operations.
Features:
using NetExt.Awaiter;
// Tasks with return values
var task1 = Task.FromResult(10);
var task2 = Task.FromResult("Hello");
// Tasks without return values
var noResultTasks = new[] { Task.Delay(100), Task.Delay(200) };
// Await and combine results
var result = await AwaitExt.TasksAsync(task1, task2, noResultTasks);
Console.WriteLine(result.Item1); // Outputs: 10
Console.WriteLine(result.Item2); // Outputs: Hello
var task1 = Task.FromResult(10);
var task2 = Task.FromResult("Hello");
var task3 = Task.FromResult(true);
var result = await AwaitExt.TasksAsync(task1, task2, task3);
Console.WriteLine(result.Item1); // 10
Console.WriteLine(result.Item2); // Hello
Console.WriteLine(result.Item3); // True
var noResultTasks = new[] { Task.Delay(100), Task.Delay(200) };
await AwaitExt.TasksAsync(noResultTasks);
The NetExt.DateTime package provides a set of extension methods to simplify working with DateTime in .NET. It includes utilities for converting between Unix time (milliseconds) and DateTime, as well as specifying the DateTimeKind explicitly. This lightweight and efficient library enhances DateTime manipulation, ensuring better clarity and correctness in handling time-related data.
The MayBe<T> is a lightweight, readonly wrapper for managing nullable references with enhanced safety and expressiveness. It simplifies handling scenarios where a value may or may not exist, providing built-in methods for validation and error handling.
interface IRepository
{
Task<MayBe<UserEntity>> GetAsync(int id);
}
...
MayBe<UserEntity> entity = await IRepository.GetAsync(123);
// check that entity exists or not
if (entity.Exists)
{
// TBD
}
...
// OR
var entity = (await IRepository.GetAsync(123)).GetOrThrow("Entity");
// OR
var entity = await IRepository.GetAsync(123).GetOrThrow("Entity");
...
// return entity or throw exception
var result = entity.AssumeExists("error message");
The package provides a set of core models and exceptions designed to simplify application development by offering reusable structures and standardized error handling. This package ensures consistency and clarity in managing data and exceptions across your application.
Models:
Exceptions:
The namespace provides an extension method for enums, allowing seamless conversion of enum values to their underlying integer representation. This simplifies handling enums in scenarios where integer values are required, such as database storage, serialization, or calculations.
Sort enum:
Benefits:
Use Cases:
NetExt.Require provides a simple and efficient way to enforce runtime validations in .NET applications. It ensures that objects and conditions meet expected constraints before proceeding with execution, reducing redundant validation logic.
DateTime? testValue = null;
// Throws RequireException with "testValue" as message
RequireExt.ThrowIfNull(testValue);
// Throws RequireException if the condition is false
RequireExt.That(5 > 10, "This condition must be true!");
// Throws ArgumentNullException if name is null
RequireExt.ThrowIfNull(name, ExceptionType.ArgNull);
DateTime? myObject = null;
// Throws RequireException if null
myObject.ThrowIfNullExt();
// Throws ArgumentNullException if null
myObject.ThrowIfNullExt(ExceptionType.ArgNull);
NetExt.Strings is a powerful utility library that extends string manipulation capabilities in .NET. It provides a variety of robust, easy-to-use methods for trimming, validating, transforming, encoding, and replacing string values. This package simplifies common string operations, improves code readability, and enhances productivity for .NET developers.
Example Usages:
using NetExt.Strings;
// Trim a string
var trimmed = " Hello World ".TrimExt(); // Output: "Hello World"
// Validate string
var isNullOrVoid = "".IsNullOrVoidExt(); // Output: true
// Replace keys in a string
var replacements = new Dictionary<string, string> { { "World", "Universe" } };
var replaced = "Hello World".ReplaceExt(replacements); // Output: "Hello Universe"
// Base64 encoding
var base64 = "Hello".ToBase64Ext(); // Output: "SGVsbG8="
var decoded = base64.FromBase64Ext(); // Output: "Hello"
// Get digits from a string
var digits = "A1B2C3".GetOnlyDigitsExt(); // Output: 123
// Join strings
var joined = new[] { "one", "two", "three" }.JoinWithExt(", "); // Output: "one, two, three"
The NetExt.Utils package provides lightweight and efficient extensions for collections and common utilities in .NET. Designed to enhance developer productivity, these extensions simplify handling collections and validating input values.
The namespace class provides utility methods for working with collections in a more intuitive and streamlined way. These extensions help convert individual items into various collection types and simplify iteration over collections.
The ForEachExt method is an extension for IEnumerable that simplifies iterating over a collection by applying an action to each element. It reduces boilerplate code for loops, making your code cleaner and more expressive.
var variable = 99999;
Console.WriteLine(variable.ToListExt());
// Output: List<int>() { 99999 }
var numbers = new List<int> { 1, 2, 3 };
// Apply an action to each element
numbers.ForEachExt(number => Console.WriteLine(number * 2));
// Output:
// 2
// 4
// 6
The GetValidatedValueExt extension method ensures that a provided value is not null, throwing an ArgumentNullException if it is. This utility is useful for enforcing required parameters and improving code safety. Note: In .NET 6+, it leverages [CallerArgumentExpression] to provide the exact variable name in the exception message.
DateTime? variableName = null;
variableName.GetValidatedValueExt();
// Output: Passed null value for required 'variableName' param.