SimplePatterns.FunctionExtensions
1.2.0
dotnet add package SimplePatterns.FunctionExtensions --version 1.2.0
NuGet\Install-Package SimplePatterns.FunctionExtensions -Version 1.2.0
<PackageReference Include="SimplePatterns.FunctionExtensions" Version="1.2.0" />
paket add SimplePatterns.FunctionExtensions --version 1.2.0
#r "nuget: SimplePatterns.FunctionExtensions, 1.2.0"
// Install SimplePatterns.FunctionExtensions as a Cake Addin #addin nuget:?package=SimplePatterns.FunctionExtensions&version=1.2.0 // Install SimplePatterns.FunctionExtensions as a Cake Tool #tool nuget:?package=SimplePatterns.FunctionExtensions&version=1.2.0
About
SimplePatterns.FunctionExtensions provides some useful extensions over Action and Func<bool> delegates to hide boiler plate code like try-catch block and retries.
ExecuteWithRetry is an extension defined on Func<bool> delegate that executes the delegate until it is true or for the specified number of retries + 1, whichever is earlier.
WrapInExceptionHandler is an extension defined on Action delegate that wraps the action within the specified exception handler and returns a Func<bool> delegate which can be used with ExecuteWithRetry. An additonal extension of this method is provided for Func<bool> delegate. This is useful for handling multiple exceptions from the action. Though it does not produce an optimized implementation, this approach enhances the code readability as shown below.
Action a = someAction;
a.WrapInExceptionHandler<SomeException>().WrapInExceptionHandler<SomeOtherException>().ExecuteWithRetry(2);
These extensions are useful in scenarios like trying to access a file while it is locked by different process or attempting a DB connection when the connection count has reached maximum. In these scenarios, the same operations can be conveniently retried using the above extensions.
Usage
public void ExecuteWithRetry()
{
...
bool status = ((Func<bool>)executor.Executor)
.ExecuteWithRetry(retries);
...
}
public void WrapInExceptionHandler()
{
...
var status = ((Action)executor.Action)
.WrapInExceptionHandler<NotImplementedException>()
.ExecuteWithRetry(retries);
...
}
private sealed class MockExecutor
{
public bool Executor()
{
return true;
}
public void Action()
{
throw new NotImplementedException();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Readme updates.