Support Center & Knowledge base

Default Policy

The resilience system has the concept of a default policy. This is the policy that gets used if there is no specific policy for a type or a named policy.

You don’t have to define a default policy, if no default policy has been defined, a pass-through policy will be used.

Defining the default policy

To define the default policy, all you need to do is add a C# class to your project that implements the IDefineDefaultPolicy 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 DefaultPolicyDefiner : IDefineDefaultPolicy
{
    public Polly.Policy Define()
    {
        return Polly.Policy.NoOps();
    }
}

There can only be one definition of a default policy. An exception will be thrown if you try to add multiple definitions.

Usage

To make use of a the default policy, all you need to do is take a dependency to IPolicies in your constructor and get the default 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.Default;
    }

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