Acolyte.NET is a helper library with a lot of useful classes and extension methods that you need in your everyday work. So, do not reinvent the wheel, use this library instead!
Install NuGet package.
Library contains classes, helper static classes and many extension methods. Each of these is divided into one of the following categories.
Table of content [click to expand]
- ThrowsExtensions — contains extension methods to check certain conditions.
- AsyncEnumerableExtensions — contains useful methods to work with
async
enumerable items (IAsyncEnumerable
was introduced in C# 8.0); - ConcurrentHashSet — represents a thread-safe set of values;
- DictionaryExtensions — contains extension methods to simplify work with associative collections;
- EnumerableExtensions – extends
LINQ
methods to work with enumerable items; - InverseComparer — allows to invert every object of
IComparer
type; - IHaveCreationTime — provides read-only
DateTime
property to use byTimeBasedConcurrentDictionary
; - TimeBasedConcurrentDictionary — extends the standard
ConcurrentDictionary
class with time-based logic (this collection cleans up expired objects when calling any method).
- Constants — provides some constants (say “No” to magic numbers and strings);
- EnumExtensions — contains extension methods for enumeration values;
- EnumHelper — contains common logic to work with enumeration values (like static methods in the standard
Enum
class); - GuidExtensions — contains extension methods for
Guid
values; - IClonable
<
T>
— provides generic interface to clone objects (the standardIClonable
interface providesClone
method with theobject
return type); - MathHelper — provides a set of useful mathematic methods;
- Maybe — represents similar interface to
Nullable
struct but for reference types; - TypeExtensions — contains extension methods for
Type
class;
- BitConverterExtensions — contains extension methods to work with bits;
- MonadExtensions — provides a set of monadic functions (simplify using functional style in C#);
- DataReaderExtensions — contains extension methods for
IDataReader
class;
- IUnhandledExceptionHandler — provides interface for exception handler used by
AppDomainUnhandledExceptionManager
; - AppDomainUnhandledExceptionManager — allows to set up exception handlers when an exception is not caught;
- ExceptionsExtensions — contains extension methods to work with exceptions;
- ExceptionsHelper — provides methods to process or transform exceptions;
- MultipleArgumentException — represents the exception that is thrown when several arguments provided to a method are not valid;
- NotFoundException — represents the exception that is thrown when object is not found;
- TaskFaultedException — represents the exception that is thrown when processing method encounters task in faulted state.
- StreamExtensions — contains extension methods to work with streams.
- NoneResult — represents an object with no result (used for track
Task
objects); - ResultOrException — represents an object with result or exception value from completed tasks;
- TaskExtensions — contains extension methods to work with tasks;
- TaskHelper — contains common logic to work with tasks (like static methods in the standard
Task
class); - ThreadHelper — contains additional logic to work with
Thread
class; - ThreadPoolHelper — contains additional logic to work with
ThreadPool
class.
- XDocumentParser — represents a XML document parser;
- XmlHelper — provides serialization and deserialization methods to work with XML.
Check examples project here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Acolyte.Assertions;
using Acolyte.Collections;
using Acolyte.Threading;
namespace Acolyte.Examples
{
internal static class Samples
{
internal static IEnumerable<T> GenerateCollection<T>(Func<T> generator, int count)
{
generator.ThrowIfNull(nameof(generator));
count.ThrowIfValueIsOutOfRange(
nameof(count), includedLowerBound: 1, includedUpperBound: 1000
);
return Enumerable.Range(0, count)
.Select(_ => generator());
}
internal static IEnumerable<string> GetItemsByPattern(IEnumerable<string> source,
string pattern)
{
source.ThrowIfNull(nameof(source));
pattern.ThrowIfNullOrEmpty(nameof(pattern));
return source
.Where(item => Regex.IsMatch(item, pattern));
}
internal static void OutputCollection<T>(IEnumerable<T> collection)
{
collection.ThrowIfNull(nameof(collection));
Console.WriteLine($"Collection: [{collection.EnumerableToOneString()}].");
}
internal static int DistanceBetweenMinAndMax(IEnumerable<int> source)
{
source.ThrowIfNullOrEmpty(nameof(source));
(int minValue, int maxValue) = source.MinMax();
return maxValue - minValue;
}
internal static async Task ExecuteAllTasksSafe(params Task[] tasks)
{
IReadOnlyList<ResultOrException<NoneResult>> resultObjects =
await TaskHelper.WhenAllResultsOrExceptions(tasks);
IReadOnlyList<Exception> exceptions = resultObjects.UnwrapResultsOrExceptions();
string separator = Environment.NewLine;
const string emptyCollectionMessage = "No exceptions occurred.";
string exceptionsToLog = exceptions.EnumerableToOneString(
separator, emptyCollectionMessage, selector: ex => ex.ToString()
);
Console.WriteLine($"{separator}{exceptionsToLog}{separator}");
}
}
}
Target .NET Standard is 2.1 for libraries. Version of C# is 8.0, version of F# is 4.7.
This project is licensed under the terms of the Apache License 2.0.