PolicyBuilder
Builder class that holds the list of current exception predicates.
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Polly
{
public sealed class PolicyBuilder
{
private readonly IList<ExceptionPredicate> _exceptionPredicates;
internal IList<ExceptionPredicate> ExceptionPredicates => _exceptionPredicates;
internal PolicyBuilder(ExceptionPredicate exceptionPredicate)
{
_exceptionPredicates = new List<ExceptionPredicate> {
exceptionPredicate
};
}
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString()
{
return base.ToString();
}
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode()
{
return base.GetHashCode();
}
[EditorBrowsable(EditorBrowsableState.Never)]
public new Type GetType()
{
return base.GetType();
}
public PolicyBuilder Or<TException>() where TException : Exception
{
ExceptionPredicate item = (Exception exception) => exception is TException;
((ICollection<ExceptionPredicate>)ExceptionPredicates).Add(item);
return this;
}
public PolicyBuilder Or<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
{
ExceptionPredicate item = delegate(Exception exception) {
if (exception is TException)
return exceptionPredicate((TException)exception);
return false;
};
((ICollection<ExceptionPredicate>)ExceptionPredicates).Add(item);
return this;
}
public PolicyBuilder<TResult> OrResult<TResult>(Func<TResult, bool> resultPredicate)
{
return new PolicyBuilder<TResult>(ExceptionPredicates).OrResult(resultPredicate);
}
public PolicyBuilder<TResult> OrResult<TResult>(TResult result)
{
return OrResult(delegate(TResult r) {
if (r == null || !r.Equals(result)) {
if (r == null)
return result == null;
return false;
}
return true;
});
}
}
}