Support Center & Knowledge base

Named Policy

Named policies can be very useful is you want to have a policy that is used by multiple different parts of your system and accessible by a well-known name.

You don’t have to define a named policy, if someone tries to access an undefined policy - the default policy will be used.

Defining a named policy

To define a well known policy, all you need to do is add a C# class to your project that implements the IDefineNamedPolicy interface. From this, you leverage the API of The Polly Project and return the actual policy or sets of policies that will be used.

Below is an example of something that will add a default policy:

using Dolittle.Resilience;

public class NamedPolicyDefiner : IDefineNamedPolicy
{
    public string Name => "MyWellKnownPolicy";

    public Polly.Policy Define()
    {
        return Polly.Policy.NoOps();
    }
}
There can only be one definition of a named policy per name. An exception will be thrown if you try to add multiple definitions.

Usage

To make use of a named policy, all you need to do is take a dependency to IPolicies in your constructor and get the named policy. This will then automatically resolve to the correct policy. Then, the code that needs resilience around it simply calls the policy’s correct Execute method.

Below is an example of how to use it:

using Dolittle.Resilience;

public class MyResource
{
    IPolicy _policy;

    public MyResource(IPolicies policies)
    {
        _policy = policies.GetNamed("MyWellKnownPolicy");
    }

    public void DoStuffToResource()
    {
        _policy.Execute(() => {
            /*
                Perform actions on the resource
            */
        });
    }
}