Table of Contents

Patterns Class

Definition

Namespace
Cuemon
Assemblies
Cuemon.Kernel.dll
Source
src/Cuemon.Kernel/Patterns.cs

Provides a generic way to support different types of design patterns and practices with small utility methods.

public sealed class Patterns
Inheritance
Patterns

Examples

The following example demonstrates how to use the class to safely invoke delegates, configure options via the Options pattern, and guard against fatal exceptions.

using System;
using System.Threading;
using Cuemon;
using Cuemon.Threading;

namespace Contoso.Infrastructure;

public sealed class PatternsExample
{
    public static void Run()
    {
        var options = Patterns.Configure<AsyncOptions>(setup =>
        {
            setup.CancellationToken = CancellationToken.None;
        });

        var profile = Patterns.CreateInstance<EndpointProfile>(instance =>
        {
            instance.Name = "health";
        });

        bool wroteMessage = Patterns.TryInvoke(() => Console.WriteLine(profile.Name));
        int fallbackPort = Patterns.InvokeOrDefault(() => int.Parse("not-a-number"), -1);
        bool recoverable = Patterns.IsRecoverableException(new InvalidOperationException("Transient."));

        Console.WriteLine($"Token can cancel: {options.CancellationToken.CanBeCanceled}");
        Console.WriteLine($"TryInvoke succeeded: {wroteMessage}");
        Console.WriteLine($"Fallback port: {fallbackPort}");
        Console.WriteLine($"Recoverable: {recoverable}");
    }

    private sealed class EndpointProfile
    {
        public string Name { get; set; }
    }
}

Properties

Name Description
Use

Gets the singleton instance of the Patterns functionality allowing for extensions methods like: Patterns.Use.SomeIngeniousMethod().

Methods

Name Description
ConfigureExchange<TSource, TResult>(Action<TSource>, Action<TSource, TResult>)

Returns the delegate that will configure the public read-write properties of TResult.

ConfigureRevertExchange<TSource, TResult>(TSource, Action<TSource, TResult>)

Returns a delegate that will be initialized by initializer with the values from options.

ConfigureRevert<TOptions>(TOptions)

Returns the delegate that will configure the public read-write properties of TOptions.

Configure<TOptions>(Action<TOptions>, Action<TOptions>, Action<TOptions>)

Returns the default parameter-less constructed instance of TOptions configured with setup delegate.

CreateInstance<T>(Action<T>)

Provides a generic way to initialize the default, parameterless constructed instance of T.

InvokeOrDefault<TResult>(Func<TResult>, TResult)

Returns an object of TResult, or a default value if the specified method throws an exception.

IsFatalException(Exception)

Determines whether the specified exception is considered fatal and should not be swallowed.

IsRecoverableException(Exception)

Determines whether the specified exception is considered recoverable and can be safely caught.

SafeInvoke<TResult>(Func<TResult>, Func<TResult, TResult>, Action<Exception>)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvoke<T, TResult>(Func<TResult>, Func<TResult, T, TResult>, T, Action<Exception, T>)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvoke<T1, T2, TResult>(Func<TResult>, Func<TResult, T1, T2, TResult>, T1, T2, Action<Exception, T1, T2>)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvoke<T1, T2, T3, TResult>(Func<TResult>, Func<TResult, T1, T2, T3, TResult>, T1, T2, T3, Action<Exception, T1, T2, T3>)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvoke<T1, T2, T3, T4, TResult>(Func<TResult>, Func<TResult, T1, T2, T3, T4, TResult>, T1, T2, T3, T4, Action<Exception, T1, T2, T3, T4>)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvoke<T1, T2, T3, T4, T5, TResult>(Func<TResult>, Func<TResult, T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, Action<Exception, T1, T2, T3, T4, T5>)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

TryInvoke(Action)

Returns a value that indicates whether the specified method can be invoked without an exception.

TryInvoke<TResult>(Func<TResult>, out TResult)

Returns a value that indicates whether the specified method can be invoked without an exception.