Class Patterns
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Provides a generic way to support different types of design patterns and practices with small utility methods.
public sealed class Patterns
- Inheritance
-
Patterns
Properties
Use
Gets the singleton instance of the Patterns functionality allowing for extensions methods like: Patterns.Use.SomeIngeniousMethod()
.
public static Patterns Use { get; }
Property Value
- Patterns
The singleton instance of the Patterns functionality.
Methods
Configure<TOptions>(Action<TOptions>, Action<TOptions>, Action<TOptions>)
Returns the default parameter-less constructed instance of TOptions
configured with setup
delegate.
public static TOptions Configure<TOptions>(Action<TOptions> setup, Action<TOptions> initializer = null, Action<TOptions> validator = null)
where TOptions : class, IParameterObject, new()
Parameters
setup
Action<TOptions>The delegate that will configure the public read-write properties of
TOptions
.initializer
Action<TOptions>The optional delegate that will initialize the default parameter-less constructed instance of
TOptions
. Should only be used with third party libraries or for validation purposes.validator
Action<TOptions>The optional delegate that will validate the
TOptions
configured by thesetup
delegate.
Returns
- TOptions
A default constructed instance of
TOptions
initialized with the options ofsetup
.
Type Parameters
TOptions
The type of the configuration options class having a default constructor.
Remarks
Often referred to as part the Options pattern: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
ConfigureExchange<TSource, TResult>(Action<TSource>, Action<TSource, TResult>)
Returns the delegate that will configure the public read-write properties of TResult
.
public static Action<TResult> ConfigureExchange<TSource, TResult>(Action<TSource> setup, Action<TSource, TResult> initializer = null)
where TSource : class, IParameterObject, new()
where TResult : class, new()
Parameters
setup
Action<TSource>The delegate that will configure the public read-write properties of
TSource
.initializer
Action<TSource, TResult>The delegate that will exchange the parameter of
setup
fromTSource
toTResult
.
Returns
Type Parameters
TSource
The type of the configuration options class having a default constructor.
TResult
The type of the configuration options class having a default constructor.
ConfigureRevert<TOptions>(TOptions)
Returns the delegate that will configure the public read-write properties of TOptions
.
public static Action<TOptions> ConfigureRevert<TOptions>(TOptions options)
where TOptions : class, new()
Parameters
options
TOptionsAn instance of the configured options.
Returns
Type Parameters
TOptions
The type of the configuration options having a default constructor.
Exceptions
- ArgumentNullException
options
cannot be null.
ConfigureRevertExchange<TSource, TResult>(TSource, Action<TSource, TResult>)
Returns a delegate that will be initialized by initializer
with the values from options
.
public static Action<TResult> ConfigureRevertExchange<TSource, TResult>(TSource options, Action<TSource, TResult> initializer = null)
where TSource : class, IParameterObject, new()
where TResult : class, new()
Parameters
options
TSourceThe configured options to apply an instance of
TResult
.initializer
Action<TSource, TResult>The delegate that will initialize a default instance of
TResult
with the values fromoptions
.
Returns
Type Parameters
TSource
The type of the configuration options having a default constructor.
TResult
The type of the configuration options having a default constructor.
Exceptions
- ArgumentNullException
options
cannot be null -or-initializer
cannot be null.
CreateInstance<T>(Action<T>)
Provides a generic way to initialize the default, parameterless constructed instance of T
.
public static T CreateInstance<T>(Action<T> factory)
where T : class, new()
Parameters
factory
Action<T>The delegate that will initialize the public write properties of
T
.
Returns
- T
A default constructed instance of
T
initialized withfactory
.
Type Parameters
T
The type of the class having a default constructor.
InvokeOrDefault<TResult>(Func<TResult>, TResult)
Returns an object of TResult
, or a default value if the specified method
throws an exception.
public static TResult InvokeOrDefault<TResult>(Func<TResult> method, TResult fallbackResult = null)
Parameters
method
Func<TResult>The function delegate that will return an instance of
TResult
.fallbackResult
TResultThe value to return when the specified
method
throws an exception. Default isdefault
ofTResult
.
Returns
- TResult
An object of
TResult
when the specifiedmethod
can be invoked without an exception; otherwisefallbackResult
is returned.
Type Parameters
TResult
The type of the return value of the
method
.
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).
public static TResult SafeInvoke<TResult>(Func<TResult> initializer, Func<TResult, TResult> tester, Action<Exception> catcher = null)
where TResult : class, IDisposable
Parameters
initializer
Func<TResult>The function delegate that initializes an object implementing the IDisposable interface.
tester
Func<TResult, TResult>The function delegate that is used to ensure that operations performed on
TResult
abides CA2000.catcher
Action<Exception>The delegate that will handle any exceptions that might have been thrown by
tester
.
Returns
- TResult
The return value of the function delegate
initializer
if the operations succeeded; otherwise null if the operation failed.
Type Parameters
TResult
The type of the return value of the function delegate
initializer
.
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).
public static TResult SafeInvoke<T, TResult>(Func<TResult> initializer, Func<TResult, T, TResult> tester, T arg, Action<Exception, T> catcher = null)
where TResult : class, IDisposable
Parameters
initializer
Func<TResult>The function delegate that initializes an object implementing the IDisposable interface.
tester
Func<TResult, T, TResult>The function delegate that is used to ensure that operations performed on
TResult
abides CA2000.arg
TThe parameter of the function delegate
tester
and delegatecatcher
.catcher
Action<Exception, T>The delegate that will handle any exceptions that might have been thrown by
tester
.
Returns
- TResult
The return value of the function delegate
initializer
if the operations succeeded; otherwise null if the operation failed.
Type Parameters
T
The type of the parameter of the function delegate
tester
and delegatecatcher
.TResult
The type of the return value of the function delegate
initializer
.
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).
public static TResult SafeInvoke<T1, T2, TResult>(Func<TResult> initializer, Func<TResult, T1, T2, TResult> tester, T1 arg1, T2 arg2, Action<Exception, T1, T2> catcher = null)
where TResult : class, IDisposable
Parameters
initializer
Func<TResult>The function delegate that initializes an object implementing the IDisposable interface.
tester
Func<TResult, T1, T2, TResult>The function delegate that is used to ensure that operations performed on
TResult
abides CA2000.arg1
T1The first parameter of the function delegate
tester
and delegatecatcher
.arg2
T2The second parameter of the function delegate
tester
and delegatecatcher
.catcher
Action<Exception, T1, T2>The delegate that will handle any exceptions that might have been thrown by
tester
.
Returns
- TResult
The return value of the function delegate
initializer
if the operations succeeded; otherwise null if the operation failed.
Type Parameters
T1
The type of the first parameter of the function delegate
tester
and delegatecatcher
.T2
The type of the second parameter of the function delegate
tester
and delegatecatcher
.TResult
The type of the return value of the function delegate
initializer
.
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).
public static TResult SafeInvoke<T1, T2, T3, TResult>(Func<TResult> initializer, Func<TResult, T1, T2, T3, TResult> tester, T1 arg1, T2 arg2, T3 arg3, Action<Exception, T1, T2, T3> catcher = null)
where TResult : class, IDisposable
Parameters
initializer
Func<TResult>The function delegate that initializes an object implementing the IDisposable interface.
tester
Func<TResult, T1, T2, T3, TResult>The function delegate that is used to ensure that operations performed on
TResult
abides CA2000.arg1
T1The first parameter of the function delegate
tester
and delegatecatcher
.arg2
T2The second parameter of the function delegate
tester
and delegatecatcher
.arg3
T3The third parameter of the function delegate
tester
and delegatecatcher
.catcher
Action<Exception, T1, T2, T3>The delegate that will handle any exceptions that might have been thrown by
tester
.
Returns
- TResult
The return value of the function delegate
initializer
if the operations succeeded; otherwise null if the operation failed.
Type Parameters
T1
The type of the first parameter of the function delegate
tester
and delegatecatcher
.T2
The type of the second parameter of the function delegate
tester
and delegatecatcher
.T3
The type of the third parameter of the function delegate
tester
and delegatecatcher
.TResult
The type of the return value of the function delegate
initializer
.
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).
public static TResult SafeInvoke<T1, T2, T3, T4, TResult>(Func<TResult> initializer, Func<TResult, T1, T2, T3, T4, TResult> tester, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Action<Exception, T1, T2, T3, T4> catcher = null)
where TResult : class, IDisposable
Parameters
initializer
Func<TResult>The function delegate that initializes an object implementing the IDisposable interface.
tester
Func<TResult, T1, T2, T3, T4, TResult>The function delegate that is used to ensure that operations performed on
TResult
abides CA2000.arg1
T1The first parameter of the function delegate
tester
and delegatecatcher
.arg2
T2The second parameter of the function delegate
tester
and delegatecatcher
.arg3
T3The third parameter of the function delegate
tester
and delegatecatcher
.arg4
T4The fourth parameter of the function delegate
tester
and delegatecatcher
.catcher
Action<Exception, T1, T2, T3, T4>The delegate that will handle any exceptions that might have been thrown by
tester
.
Returns
- TResult
The return value of the function delegate
initializer
if the operations succeeded; otherwise null if the operation failed.
Type Parameters
T1
The type of the first parameter of the function delegate
tester
and delegatecatcher
.T2
The type of the second parameter of the function delegate
tester
and delegatecatcher
.T3
The type of the third parameter of the function delegate
tester
and delegatecatcher
.T4
The type of the fourth parameter of the function delegate
tester
and delegatecatcher
.TResult
The type of the return value of the function delegate
initializer
.
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).
public static TResult SafeInvoke<T1, T2, T3, T4, T5, TResult>(Func<TResult> initializer, Func<TResult, T1, T2, T3, T4, T5, TResult> tester, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Action<Exception, T1, T2, T3, T4, T5> catcher = null)
where TResult : class, IDisposable
Parameters
initializer
Func<TResult>The function delegate that initializes an object implementing the IDisposable interface.
tester
Func<TResult, T1, T2, T3, T4, T5, TResult>The function delegate that is used to ensure that operations performed on
TResult
abides CA2000.arg1
T1The first parameter of the function delegate
tester
and delegatecatcher
.arg2
T2The second parameter of the function delegate
tester
and delegatecatcher
.arg3
T3The third parameter of the function delegate
tester
and delegatecatcher
.arg4
T4The fourth parameter of the function delegate
tester
and delegatecatcher
.arg5
T5The fifth parameter of the function delegate
tester
and delegatecatcher
.catcher
Action<Exception, T1, T2, T3, T4, T5>The delegate that will handle any exceptions that might have been thrown by
tester
.
Returns
- TResult
The return value of the function delegate
initializer
if the operations succeeded; otherwise null if the operation failed.
Type Parameters
T1
The type of the first parameter of the function delegate
tester
and delegatecatcher
.T2
The type of the second parameter of the function delegate
tester
and delegatecatcher
.T3
The type of the third parameter of the function delegate
tester
and delegatecatcher
.T4
The type of the fourth parameter of the function delegate
tester
and delegatecatcher
.T5
The type of the fifth parameter of the function delegate
tester
and delegatecatcher
.TResult
The type of the return value of the function delegate
initializer
.
TryInvoke(Action)
Returns a value that indicates whether the specified method
can be invoked without an exception.
public static bool TryInvoke(Action method)
Parameters
method
ActionThe delegate that to invoke.
Returns
- Boolean
true
ifmethod
was called without raising an exception; otherwisefalse
.
Remarks
Actually an anti-pattern in regards to swallowing exception. That said, there are situations where this is a perfectly valid approach.
TryInvoke<TResult>(Func<TResult>, out TResult)
Returns a value that indicates whether the specified method
can be invoked without an exception.
public static bool TryInvoke<TResult>(Func<TResult> method, out TResult result)
Parameters
method
Func<TResult>The function delegate that will resolve
result
.result
TResultWhen this method returns, contains the value returned from
method
; otherwise the default value for the type of theresult
parameter if an exception is thrown.
Returns
- Boolean
true
if an instance ofTResult
has been created; otherwisefalse
.
Type Parameters
TResult
The type of the return value of the
method
.
Remarks
Often referred to as the Try-Parse pattern: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions-and-performance