Skip to content

Commit

Permalink
Add interface providing read-only access to a policy registry (#283)
Browse files Browse the repository at this point in the history
* Add interface providing read-only access to a policy registry

Adds an interface providing read-only access to a policy registry. Useful to prevent consumers changing policies in registry; for example to enforce separation of population of policy registry from usage.

* Add IReadOnlyPolicyRegistrySpecs
  • Loading branch information
reisenberger authored Jul 26, 2017
1 parent 5351447 commit f4ee744
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 5.3.0
- Fix ExecuteAndCapture() usage with PolicyWrap
- Allow Fallback delegates to take execution Context
- Provide IReadOnlyPolicyRegistry interface

## 5.2.0
- Add PolicyRegistry for storing and retrieving policies.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ registry["StandardHttpResilience"] = myStandardHttpResiliencePolicy;
// Pass the registry instance to usage sites by DI, perhaps
public class MyServiceGateway
{
public void MyServiceGateway(..., IPolicyRegistry<string> registry, ...)
public void MyServiceGateway(..., IReadOnlyPolicyRegistry<string> registry, ...)
{
...
}
Expand Down Expand Up @@ -905,6 +905,7 @@ For details of changes by release see the [change log](https://github.com/App-vN
* [@reisenberger](https://github.com/reisenberger) - Add mutable Context and extra overloads taking Context. Allows different parts of a policy execution to exchange data via the mutable Context travelling with each execution.
* [@ankitbko](https://github.com/ankitbko) - Add PolicyRegistry for storing and retrieving policies.
* [@reisenberger](https://github.com/reisenberger) - Add interfaces by policy type and execution type.
* [@seanfarrow](https://github.com/SeanFarrow) - Add IReadOnlyPolicyRegistry interface.

# Sample Projects

Expand Down
1 change: 1 addition & 0 deletions src/Polly.Net40Async.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
---------------------
- Fix ExecuteAndCapture() usage with PolicyWrap
- Allow Fallback delegates to take execution Context
- Provide IReadOnlyPolicyRegistry interface

5.2.0
---------------------
Expand Down
1 change: 1 addition & 0 deletions src/Polly.Shared/Polly.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="$(MSBuildThisFileDirectory)PolicyBuilder.OrSyntax.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PolicyResult.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Policy.TResult.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\IReadOnlyPolicyRegistry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\PolicyRegistry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\IPolicyRegistry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ResultPredicate.cs" />
Expand Down
44 changes: 5 additions & 39 deletions src/Polly.Shared/Registry/IPolicyRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Polly.Registry
/// Represents a collection of policies keyed by <typeparamref name="TKey"/>.
/// </summary>
/// <typeparam name="TKey">The type of keys in the policy registry.</typeparam>
public interface IPolicyRegistry<in TKey>
public interface IPolicyRegistry<in TKey> : IReadOnlyPolicyRegistry<TKey>
{
/// <summary>
/// Adds an element with the provided key and policy to the registry.
Expand All @@ -21,49 +21,15 @@ public interface IPolicyRegistry<in TKey>
void Add<TPolicy>(TKey key, TPolicy policy) where TPolicy : IsPolicy;

/// <summary>
/// Gets of sets the <see cref="IsPolicy"/> with the specified key.
/// <remarks>To retrieve a policy directly as a particular Policy type or Policy interface (avoiding a cast), use the <see cref="Get{TPolicy}"/> method.</remarks>
/// Gets or sets the <see cref="IsPolicy"/> with the specified key.
/// <remarks>To retrieve a policy directly as a particular Policy type or Policy interface (avoiding a cast), use the <see cref="IReadOnlyPolicyRegistry{TKey}.Get{TPolicy}"/> method.</remarks>
/// </summary>
/// <param name="key">The key of the value to get or set.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="key" /> is null.</exception>
/// <exception cref="KeyNotFoundException">The given key was not present in the dictionary.</exception>
/// <returns>The value associated with the specified key.</returns>
IsPolicy this[TKey key] { get; set; }

/// <summary>
/// Gets the policy stored under the provided key, casting to <typeparamref name="TPolicy"/>.
/// </summary>
/// <typeparam name="TPolicy">The type of Policy.</typeparam>
/// <returns>The policy stored in the registry under the given key.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
TPolicy Get<TPolicy>(TKey key) where TPolicy : IsPolicy;

/// <summary>
/// Gets the policy stored under the provided key, casting to <typeparamref name="TPolicy"/>.
/// </summary>
/// <param name="key">The key of the policy to get.</param>
/// <param name="policy">
/// This method returns the policy associated with the specified <paramref name="key"/>, if the
/// key is found; otherwise null.
/// This parameter is passed uninitialized.
/// </param>
/// <typeparam name="TPolicy">The type of Policy.</typeparam>
/// <returns>True if Policy exists for the provided Key. False otherwise.</returns>
bool TryGet<TPolicy>(TKey key, out TPolicy policy) where TPolicy : IsPolicy;

/// <summary>
/// Total number of policies in the registry.
/// </summary>
int Count { get; }

/// <summary>
/// Determines whether the specified <paramref name="key"/> exists.
/// </summary>
/// <param name="key">The Key to locate in the registry</param>
/// <returns>True if <paramref name="key"/> exists otherwise false</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null</exception>
bool ContainsKey(TKey key);

new IsPolicy this[TKey key] { get; set; }

/// <summary>
/// Removes the specified <see cref="Polly.Policy"/> from the registry.
/// </summary>
Expand Down
57 changes: 57 additions & 0 deletions src/Polly.Shared/Registry/IReadOnlyPolicyRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using Polly;

namespace Polly.Registry
{
/// <summary>
/// Represents a read-only collection of policies keyed by <typeparamref name="TKey"/>.
/// </summary>
/// <typeparam name="TKey">The type of keys in the policy registry.</typeparam>
public interface IReadOnlyPolicyRegistry<in TKey>
{
/// <summary>
/// Gets the <see cref="IsPolicy"/> with the specified key.
/// <remarks>To retrieve a policy directly as a particular Policy type or Policy interface (avoiding a cast), use the <see cref="Get{TPolicy}"/> method.</remarks>
/// </summary>
/// <param name="key">The key of the value to get or set.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="key" /> is null.</exception>
/// <exception cref="KeyNotFoundException">The given key was not present in the dictionary.</exception>
/// <returns>The value associated with the specified key.</returns>
IsPolicy this[TKey key] { get; }

/// <summary>
/// Gets the policy stored under the provided key, casting to <typeparamref name="TPolicy"/>.
/// </summary>
/// <typeparam name="TPolicy">The type of Policy.</typeparam>
/// <returns>The policy stored in the registry under the given key.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
TPolicy Get<TPolicy>(TKey key) where TPolicy : IsPolicy;

/// <summary>
/// Gets the policy stored under the provided key, casting to <typeparamref name="TPolicy"/>.
/// </summary>
/// <param name="key">The key of the policy to get.</param>
/// <param name="policy">
/// This method returns the policy associated with the specified <paramref name="key"/>, if the
/// key is found; otherwise null.
/// This parameter is passed uninitialized.
/// </param>
/// <typeparam name="TPolicy">The type of Policy.</typeparam>
/// <returns>True if Policy exists for the provided Key. False otherwise.</returns>
bool TryGet<TPolicy>(TKey key, out TPolicy policy) where TPolicy : IsPolicy;

/// <summary>
/// Total number of policies in the registry.
/// </summary>
int Count { get; }

/// <summary>
/// Determines whether the specified <paramref name="key"/> exists.
/// </summary>
/// <param name="key">The Key to locate in the registry</param>
/// <returns>True if <paramref name="key"/> exists otherwise false</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null</exception>
bool ContainsKey(TKey key);
}
}
1 change: 1 addition & 0 deletions src/Polly.SharedSpecs/Polly.SharedSpecs.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="$(MSBuildThisFileDirectory)PolicyTResultAsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PolicyTResultSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\PolicyRegistrySpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\IReadOnlyPolicyRegistrySpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetryAsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetryForeverAsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetryForeverSpecs.cs" />
Expand Down
Loading

0 comments on commit f4ee744

Please sign in to comment.