Patterns Class
Definition
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
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: |
Methods
| Name | Description |
|---|---|
| ConfigureExchange<TSource, TResult>(Action<TSource>, Action<TSource, TResult>) | Returns the delegate that will configure the public read-write properties of |
| ConfigureRevertExchange<TSource, TResult>(TSource, Action<TSource, TResult>) | Returns a delegate that will be initialized by |
| ConfigureRevert<TOptions>(TOptions) | Returns the delegate that will configure the public read-write properties of |
| Configure<TOptions>(Action<TOptions>, Action<TOptions>, Action<TOptions>) | Returns the default parameter-less constructed instance of |
| CreateInstance<T>(Action<T>) | Provides a generic way to initialize the default, parameterless constructed instance of |
| InvokeOrDefault<TResult>(Func<TResult>, TResult) | Returns an object of |
| IsFatalException(Exception) | Determines whether the specified |
| IsRecoverableException(Exception) | Determines whether the specified |
| 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 |
| TryInvoke<TResult>(Func<TResult>, out TResult) | Returns a value that indicates whether the specified |