/Acolyte.NET

Acolyte.NET is a helper library with a lot of useful classes and extension methods that you need in your everyday work.

Primary LanguageC#Apache License 2.0Apache-2.0

Acolyte.NET

Acolyte.NET icon

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!

nuget License FOSSA Status

Build and Release Status

Stable (master) Pre-release (develop)
Build AppVeyor branch AppVeyor branch
GitHub Release GitHub release (latest by date)

Installation

Install NuGet package.

About library

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]

Assertions (C#)

Collections (C#)

  • 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 by TimeBasedConcurrentDictionary;
  • TimeBasedConcurrentDictionary — extends the standard ConcurrentDictionary class with time-based logic (this collection cleans up expired objects when calling any method).

Common (C#)

  • 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 standard IClonable interface provides Clone method with the object 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;

Bits (C#)

Monads (C#)

  • MonadExtensions — provides a set of monadic functions (simplify using functional style in C#);

Data (C#)

Exceptions (C#)

IO (C#)

Threading (C#)

  • 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.

XML (C#)

  • XDocumentParser — represents a XML document parser;
  • XmlHelper — provides serialization and deserialization methods to work with XML.

Functional (F#)

  • Throw — represents F#-style usage of some assertion extensions;
  • Utils — provides useful methods to work with F# values;
  • SeqEx — contains additional methods to work with seq (i.e. IEnumerable<T>).

Usage examples

Check examples solution 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.Common;
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.ToSingleString()}].");
        }

        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<Result<NoneResult, Exception>> resultObjects =
                await TaskHelper.WhenAllResultsOrExceptions(tasks);

            IReadOnlyList<Exception> exceptions = resultObjects.UnwrapResultsOrExceptions();

            string separator = Environment.NewLine;
            const string emptyCollectionMessage = "No exceptions occurred.";
            string exceptionsToLog = exceptions.ToSingleString(
                emptyCollectionMessage, separator, selector: ex => ex.ToString()
            );

            Console.WriteLine($"{separator}{exceptionsToLog}{separator}");
        }
    }
}

Dependencies

Target .NET Standard versions are 2.0 and 2.1 for libraries. Version of C# is 9.0, version of F# is 5.0.

License information

This project is licensed under the terms of the Apache License 2.0.

FOSSA Status