PolicyBuilder<TResult>
Builder class that holds the list of current execution predicates filtering TResult result values.
using System;
using System.ComponentModel;
namespace Polly
{
public sealed class PolicyBuilder<TResult>
{
internal ExceptionPredicates ExceptionPredicates { get; }
internal ResultPredicates<TResult> ResultPredicates { get; }
private PolicyBuilder()
{
ExceptionPredicates = new ExceptionPredicates();
ResultPredicates = new ResultPredicates<TResult>();
}
internal PolicyBuilder(Func<TResult, bool> resultPredicate)
: this()
{
OrResult(resultPredicate);
}
internal PolicyBuilder(ExceptionPredicate predicate)
: this()
{
ExceptionPredicates.Add(predicate);
}
internal PolicyBuilder(ExceptionPredicates exceptionPredicates)
: this()
{
ExceptionPredicates = exceptionPredicates;
}
[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<TResult> OrResult(Func<TResult, bool> resultPredicate)
{
ResultPredicate<TResult> predicate = (TResult result) => resultPredicate(result);
ResultPredicates.Add(predicate);
return this;
}
public PolicyBuilder<TResult> OrResult(TResult result)
{
return OrResult(delegate(TResult r) {
if (object.Equals(r, default(TResult)) || !r.Equals(result)) {
if (object.Equals(r, default(TResult)))
return object.Equals(result, default(TResult));
return false;
}
return true;
});
}
public PolicyBuilder<TResult> Or<TException>() where TException : Exception
{
this.ExceptionPredicates.Add(delegate(Exception exception) {
if (!(exception is TException))
return null;
return exception;
});
return this;
}
public PolicyBuilder<TResult> Or<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
{
this.ExceptionPredicates.Add(delegate(Exception exception) {
TException val = exception as TException;
if (val == null || !exceptionPredicate(val))
return null;
return exception;
});
return this;
}
public PolicyBuilder<TResult> OrInner<TException>() where TException : Exception
{
this.ExceptionPredicates.Add(PolicyBuilder.HandleInner((Exception ex) => ex is TException));
return this;
}
public PolicyBuilder<TResult> OrInner<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
{
this.ExceptionPredicates.Add(PolicyBuilder.HandleInner(delegate(Exception ex) {
TException val = ex as TException;
if (val != null)
return exceptionPredicate(val);
return false;
}));
return this;
}
}
}