Published on

Application features with dotnet core and ABP. Part 4

Table of Contents

Intro

ABP Feature system is used to enable, disable or change the behavior of the application features on runtime.

Create feature constant

public static class TodoFeatures
{
    public const string Todo = "Todo";
    public const string MaxTodoPerUser = "MaxTodoPerUser";
}

Create feature definition provider

public class TodoFeatureDefinitionProvider : FeatureDefinitionProvider
{
    public override void Define(IFeatureDefinitionContext context)
    {
        var myGroup = context.AddGroup("MyTodoApp");

        myGroup.AddFeature(
            TodoFeatures.Todo,
            defaultValue: "false",
            displayName: L("Todo"),
            valueType: new ToggleStringValueType()
        );

        myGroup.AddFeature(
            TodoFeatures.MaxTodoPerUser,
            defaultValue: "10",
            displayName: L("MaxTodoPerUser"),
            valueType: new FreeTextStringValueType(
                            new NumericValueValidator(0, 1000000))
        );
    }

    private static LocalizableString L(string name)
    {
        return LocalizableString.Create<TodosResource>(name);
    }
}

Expose api base on feature

You can use the RequiresFeature attribute to restrict access to the api endpoint.

[RequiresFeature(TodoFeatures.Todo)]

IFeatureChecker service

you can use the FeatureChecker service to check if the feature is enabled or not and get the value of the feature.

var maxTodoPerUser = await FeatureChecker.GetAsync<int>(TodoFeatures.MaxTodoPerUser);

For more info check the official docs : https://docs.abp.io/en/abp/latest/Features