From 04019d32fcf6c2b1576e2c01d0ee62e41bf3a8b1 Mon Sep 17 00:00:00 2001 From: Martin Tomka Date: Tue, 4 Jul 2023 07:55:58 +0200 Subject: [PATCH] Allow adding generic strategies to generic builder --- .../ResilienceStrategyBuilderExtensions.cs | 17 +++++++++++++++++ .../GenericResilienceStrategyBuilderTests.cs | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs index ff8e9cdeef3..89a96652ea1 100644 --- a/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs @@ -25,6 +25,23 @@ public static TBuilder AddStrategy(this TBuilder builder, ResilienceSt return builder.AddStrategy(_ => strategy, EmptyOptions.Instance); } + /// + /// Adds an already created strategy instance to the builder. + /// + /// The type of the result. + /// The builder instance. + /// The strategy instance. + /// The same builder instance. + /// Thrown when is null. + /// Thrown when this builder was already used to create a strategy. The builder cannot be modified after it has been used. + public static ResilienceStrategyBuilder AddStrategy(this ResilienceStrategyBuilder builder, ResilienceStrategy strategy) + { + Guard.NotNull(builder); + Guard.NotNull(strategy); + + return builder.AddStrategy(strategy.Strategy); + } + /// /// Adds a strategy to the builder. /// diff --git a/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs b/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs index 07462ca655d..c0adcb7742d 100644 --- a/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs +++ b/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs @@ -51,4 +51,19 @@ public void Build_Ok() strategy.Should().NotBeNull(); strategy.Strategy.Should().BeOfType().Subject.Strategies.Should().HaveCount(2); } + + [Fact] + public void AddGenericStrategy_Ok() + { + // arrange + var testStrategy = new ResilienceStrategy(new TestResilienceStrategy()); + _builder.AddStrategy(testStrategy); + + // act + var strategy = _builder.Build(); + + // assert + strategy.Should().NotBeNull(); + strategy.Strategy.Should().Be(testStrategy.Strategy); + } }