Wrapper around Polly Framework so that implementation can stay at a single place
I am able to create a wrapper based on the above link . but i am not sure how can i make this as generic . I want to have this wrapper for more than result bool check such as handling exception too.
Here is my code:
public class RetryWrapper
{
public static bool Execute(Func<bool> func)
{
RetryPolicy<bool> retryPolicyNeedsTrueResponse =
Policy.HandleResult<bool>(b => b != true)
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(15)
});
return retryPolicyNeedsTrueResponse.Execute(func);
}
}
Any help is much appreciated. Thanks in advance
To make it generic, I guess you could do something like this
public static T Execute<T>(Func<T> func, Func<T,bool> success)
{
RetryPolicy<T> retryPolicyNeedsTrueResponse =
Policy.HandleResult<T>(b => success(T))
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(15)
});
return retryPolicyNeedsTrueResponse.Execute(func);
}
Note : the benefit of this seems to be iffy and suspect at best, also you would need another version for async
.
You could probably just get away with an array of timespan, reuse that, and let your code be more declarative and adaptable to all the other features of polly.
Note 2 : This is completely untested.