CompositeStrategyBuilderBase
A builder that is used to create an instance of ResilienceStrategy.
using Polly.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace Polly
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class CompositeStrategyBuilderBase
{
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class Entry : IEquatable<Entry>
{
[CompilerGenerated]
private Type EqualityContract {
[CompilerGenerated]
get {
return typeof(Entry);
}
}
public Func<StrategyBuilderContext, ResilienceStrategy> Factory { get; set; }
public ResilienceStrategyOptions Options { get; set; }
public Entry(Func<StrategyBuilderContext, ResilienceStrategy> Factory, ResilienceStrategyOptions Options)
{
Factory = Factory;
Options = Options;
base..ctor();
}
[CompilerGenerated]
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("Entry");
stringBuilder.Append(" { ");
if (PrintMembers(stringBuilder))
stringBuilder.Append(' ');
stringBuilder.Append('}');
return stringBuilder.ToString();
}
[CompilerGenerated]
private bool PrintMembers(StringBuilder builder)
{
RuntimeHelpers.EnsureSufficientExecutionStack();
builder.Append("Factory = ");
builder.Append(Factory);
builder.Append(", Options = ");
builder.Append(Options);
return true;
}
public static bool operator !=(Entry left, Entry right)
{
return !(left == right);
}
public static bool operator ==(Entry left, Entry right)
{
if ((object)left != right)
return left?.Equals(right) ?? false;
return true;
}
[CompilerGenerated]
public override int GetHashCode()
{
return (EqualityComparer<Type>.Default.GetHashCode(EqualityContract) * -1521134295 + EqualityComparer<Func<StrategyBuilderContext, ResilienceStrategy>>.Default.GetHashCode(Factory)) * -1521134295 + EqualityComparer<ResilienceStrategyOptions>.Default.GetHashCode(Options);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[CompilerGenerated]
public override bool Equals(object obj)
{
return Equals(obj as Entry);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[CompilerGenerated]
public bool Equals(Entry other)
{
if ((object)this != other) {
if ((object)other != null && EqualityContract == other.EqualityContract && EqualityComparer<Func<StrategyBuilderContext, ResilienceStrategy>>.Default.Equals(Factory, other.Factory))
return EqualityComparer<ResilienceStrategyOptions>.Default.Equals(Options, other.Options);
return false;
}
return true;
}
[CompilerGenerated]
private Entry(Entry original)
{
Factory = original.Factory;
Options = original.Options;
}
[CompilerGenerated]
public void Deconstruct(out Func<StrategyBuilderContext, ResilienceStrategy> Factory, out ResilienceStrategyOptions Options)
{
Factory = this.Factory;
Options = this.Options;
}
}
private readonly List<Entry> _entries = new List<Entry>();
private bool _used;
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public string Name {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
[System.Runtime.CompilerServices.NullableContext(2)]
set;
}
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public string InstanceName {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
[System.Runtime.CompilerServices.NullableContext(2)]
set;
}
public ResilienceProperties Properties { get; } = new ResilienceProperties();
[Required]
internal TimeProvider TimeProvider { get; set; } = TimeProvider.System;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
[EditorBrowsable(EditorBrowsableState.Never)]
[field: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
public Action<IList<ResilienceStrategy>> OnCreatingStrategy {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
get;
[param: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
set;
}
[System.Runtime.CompilerServices.Nullable(2)]
[EditorBrowsable(EditorBrowsableState.Never)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public DiagnosticSource DiagnosticSource {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
[System.Runtime.CompilerServices.NullableContext(2)]
set;
}
[EditorBrowsable(EditorBrowsableState.Never)]
[Required]
public Func<double> Randomizer { get; set; } = RandomUtil.Instance.NextDouble;
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<ResilienceValidationContext> Validator { get; set; } = ValidationHelper.ValidateObject;
private protected CompositeStrategyBuilderBase()
{
}
private protected CompositeStrategyBuilderBase(CompositeStrategyBuilderBase other)
{
Name = other.Name;
Properties = other.Properties;
TimeProvider = other.TimeProvider;
OnCreatingStrategy = other.OnCreatingStrategy;
Randomizer = other.Randomizer;
DiagnosticSource = other.DiagnosticSource;
}
[RequiresUnreferencedCode("This call validates the options using the data annotations attributes.\r\nMake sure that the options are included by adding the '[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(OptionsType))]' attribute to the calling method.")]
internal void AddStrategyCore(Func<StrategyBuilderContext, ResilienceStrategy> factory, ResilienceStrategyOptions options)
{
Guard.NotNull(factory, "factory");
Guard.NotNull(options, "options");
Validator(new ResilienceValidationContext(options, "The '" + TypeNameFormatter.Format(options.GetType()) + "' are invalid."));
if (_used)
throw new InvalidOperationException("Cannot add any more resilience strategies to the builder after it has been used to build a strategy once.");
_entries.Add(new Entry(factory, options));
}
internal ResilienceStrategy BuildStrategy()
{
Validator(new ResilienceValidationContext(this, "The 'CompositeStrategyBuilder' configuration is invalid."));
_used = true;
List<ResilienceStrategy> list = _entries.Select(CreateResilienceStrategy).ToList();
OnCreatingStrategy?.Invoke(list);
if (list.Count == 0)
return NullResilienceStrategy.Instance;
if (list.Count == 1)
return list[0];
return CompositeResilienceStrategy.Create(list);
}
private ResilienceStrategy CreateResilienceStrategy(Entry entry)
{
StrategyBuilderContext arg = new StrategyBuilderContext(Name, InstanceName, Properties, entry.Options.Name, TimeProvider, DiagnosticSource, Randomizer);
ResilienceStrategy resilienceStrategy = entry.Factory(arg);
resilienceStrategy.Options = entry.Options;
return resilienceStrategy;
}
}
}