I'm trying to add Polly policy in a base class. The problem is that the method I want to retry is an abstract method with a parameter, and the children classes define that parameter. Code sample:
public abstract class BaseClass
{
protected BaseClass()
{
// original code
// someEvent += OnChange;
someEvent += OnChangeBase;
}
private void OnChangeBase()
{
var policy = GetRetryPolicy();
// how do I use the bool parameter from children class?
policy.Execute(() => OnChange(??));
}
protected abstract void OnChange(bool param);
}
How do I achieve this?
This can be solved with standard abstract class patterns. The solution isn't particularly different because Polly is involved.
You could just add to the base class:
protected abstract bool GetBoolParameterValue(); // choose a name more meaningful to your use case
And then the code line executing through the policy becomes:
policy.Execute(() => OnChange(GetBoolParameterValue()));
This uses an abstract method, but you could also use an abstract property; or a bool field defined on the base class.
Alternatively: The question presents that the signature of OnChangeBase
is OnChangeBase()
. But if the original signature of OnChange
was OnChange(bool)
, can you just do this?
public abstract class BaseClass
{
protected BaseClass()
{
// original code
// someEvent += OnChange;
someEvent += OnChangeBase;
}
private void OnChangeBase(bool foo)
{
var policy = GetRetryPolicy();
policy.Execute(() => OnChange(foo));
}
protected abstract void OnChange(bool param);
}