PolicyRegistry

reisenberger edited this page Nov 7, 2017 · 9 revisions

PolicyRegistry

available from v5.2 onwards

Purpose

PolicyRegistry provides a registry for storing configured policy instances and retrieving them later for use.

PolicyRegistry promotes separation of policy definition and usage, enabling common patterns such as defining policies centrally on start-up, and accessing them at point-of-use as an injected dependency.

Syntax

PolicyRegistry has Dictionary<string, Policy>-like semantics.

Create a registry

PolicyRegistry registry = new PolicyRegistry();

Populate a registry with policies

registry.Add("StandardHttpResilience", myStandardHttpResiliencePolicy);
// Or:
registry["StandardHttpResilience"] = myStandardHttpResiliencePolicy;

Provide the registry to usage sites

// Pass the registry instance to usage sites by DI, perhaps
public class MyServiceGateway 
{
    public void MyServiceGateway(..., IReadOnlyPolicyRegistry<string> registry, ...)
    {
       ...
    } 
}

Other patterns are of course possible: for example, a PolicyRegistry instance could be exposed as a thread-safe singleton, allowing it to be used as an ambient context.

For those who want to enforce separation of registry population (perhaps at startup) from registry usage, an IReadOnlyPolicyRegistry<in TKey> interface exists, allowing you to pass a read-only registry to usage sites.

Use policies from the registry

var policy = registry.Get<IAsyncPolicy<HttpResponseMessage>>("StandardHttpResilience");
HttpResponseMessage response = await policy.ExecuteAsync<HttpResponseMessage>(...);

// Or:
HttpResponseMessage response = await 
    registry.Get<IAsyncPolicy<HttpResponseMessage>>("StandardHttpResilience")
    .ExecuteAsync<HttpResponseMessage>(...);

// Or:
HttpResponseMessage response = await 
    ((IAsyncPolicy<HttpResponseMessage>)registry["StandardHttpResilience"])
    .ExecuteAsync<HttpResponseMessage>(...);

Further syntax

PolicyRegistry exposes further semantics, with standard dictionary-like behaviour:

  • bool ContainsKey(TKey key)
  • bool TryGet<TPolicy>(TKey key, out TPolicy policy)
  • int Count
  • void Clear()
  • bool Remove(TKey key).

Operation

The default implementation is in-memory, backed by ConcurrentDictionary.

What does the key represent?

The default implementation is (intentionally) agnostic about what the string key represents, for maximum flexibility. Users may define their own patterns for string keys.

Implementing an alternative registry with IPolicyRegistry<in TKey>

Polly also exposes an IPolicyRegistry<in TKey> interface. You may fulfil this interface with your own implementation. This could be used, for example:

  • to provide an implementation backed by an alternative store
  • to provide an implementation with a compound TKey of some kind.
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.