abp
feature
Table of contents
- Intro
- Create feature constant
- Create feature definition provider
- Expose api base on feature
- IFeatureChecker service
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