2. Reusing Policies for Faster Development and More Robust Applications Flashcards
1
Q
What’s a Polly-provided way of sharing policies between controllers?
A
By instantiating a new PolicyRegistry
object and adding it the policies we’d like to share, as well as the name of these policies as strings, and injecting this object to our controllers using Dependency Injection.
var registry = new PolicyRegistry(); var httpRetryPolicy = ... registry.Add("SimpleRetryPolicy", httpRetryPolicy);
Startup.ConfigureServices:
~~~
services.AddSingleton(registry);
~~~
2
Q
What is the benefit of using simple dependency injection (without PolicyRegistry
) for reusing policies?
A
It ensures that a policy reference exists — you will not have any runtime errors referencing any policy.
3
Q
What is the benefit of using PolicyRegistry
for reusing policies?
A
Policies can be added to the registry at runtime.