microsoft / FeatureManagement-Dotnet

Microsoft.FeatureManagement provides standardized APIs for enabling feature flags within applications. Utilize this library to secure a consistent experience when developing applications that use patterns such as beta access, rollout, dark deployments, and more.
MIT License
1.04k stars 114 forks source link

FeatureGate filter question #341

Open WeihanLi opened 9 months ago

WeihanLi commented 9 months ago

Currently, the FeatureGate is a kind of ActionFilter, should we consider using ResourceFilter or AuthorizationFilter instead, that would be more efficient to avoid some unnecessary flow when feature disabled

https://github.com/microsoft/FeatureManagement-Dotnet/blob/e4adec40c8397e0d537bb053aef0e9fc8ed622bf/src/Microsoft.FeatureManagement.AspNetCore/FeatureGateAttribute.cs#L18

image

zhiyuanliang-ms commented 8 months ago

Hi, @WeihanLi

The Feature Management ASP.NET library provides FeatureGateAttribute and an extension method of AspNetCore.MVC.FilterCollection called AddForFeature which will add a FeatureGatedAsyncActionFilter.

Here what you pointed out is the FeatureGateAttribute which is built on ActionFilterAttribute.

The ASP.NET Core includes some built-in filter attributes: ActionFilterAttribute ExceptionFilterAttribute ResultFilterAttribute FormatFilterAttribute ServiceFilterAttribute TypeFilterAttribute

There is no built-in filter attributes for resource filter and authorization filter.

The way to use FeatureGateAttribute is to place it on MVC controllers, controller actions, or Razor pages. The FeatureGateAttribute implements the OnActionExecutionAsync method which will [run before any of the action's filters] (https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-8.0#controller-level-filters).

According to the diagram, it seems like the stage of "Model Binding" can be skipped. I don't think using AuthorizationFilter is sensible.

Therefore, it brings two questions:

  1. Should the stage of "Model Binding" be skipped?
  2. How to implement an Attribute for resource filter?

I need to do more investigation.

WeihanLi commented 8 months ago

Thanks for the reply

  1. I think it should be skipped if the feature is not enabled, it seems no need to go through the model binding
  2. Need to inherit the Attribute to make it work as an attribute and implement the IResourceFilter/IAuthorizationFilter or/and the async interface ...

I tried to propose an AuthorizationFilterAttribute like the ActionFilterAttribute, but seemed the asp.net team does not want to include it in the framework https://github.com/dotnet/aspnetcore/issues/40542

zhiyuanliang-ms commented 8 months ago

It makes more sense to use resourse filter for the feature gate scenario. I will take a look at your PR and learn how to implement a filter attribute.