The purpose of the Global Feature System is to **add a module to your application but disable the features you don't want to use** (or enable only the ones you need). Notice that the features are not determined on runtime, you must select the features **on development time**. Because it will not create database tables, APIs and other stuff for unused features, which is not possible to change then on the runtime.
Global Feature system is used to enable/disable an application feature on development time. It is done on the development time, because some **services** (e.g. controllers) are removed from the application model and **database tables** are not created for the disabled features, which is not possible on runtime.
Global Features system is especially useful if you want to develop a reusable application module with optional features. If the final application doesn't want to use some of the features, it can disable these features.
> If you are looking for a system to enable/disable features based on current tenant or any other condition, please see the [Features](Features.md) document.
## Installation
> This package is already installed by default with the startup template. So, most of the time, you don't need to install it manually.
@ -12,73 +16,57 @@ Open a command line window in the folder of the project (.csproj file) and type
abp add-package Volo.Abp.GlobalFeatures
```
## Implementation
Global Feature system aims module based feature management . A module has to have own Global Features itself.
### Define a Global Feature
## Defining a Global Feature
A feature class is something like that:
```csharp
[GlobalFeatureName(Name)]
public class PaymentFeature : GlobalFeature
[GlobalFeatureName("Shopping.Payment")]
public class PaymentFeature
{
public const string Name = "Shopping.Payment";
public PaymentFeature(GlobalModuleFeatures module) : base(module)
{
}
}
```
### Define Global Module Features
## Enable/Disable Global Features
All features of a module have to be defined in a Global Module Features class.
Use `GlobalFeatureManager.Instance` to enable/disable a global feature.
```csharp
public class GlobalShoppingFeatures : GlobalModuleFeatures
{
public const string ModuleName = "Shopping";
// Able to Enable/Disable with generic type parameter.
> Global Features are disabled unless they are explicitly enabled.
### Enable/Disable Features
### Where to Configure Global Features?
Global features are managed by modules. Module Features have to be added to Modules of GlobalFeatureManager.
Global Features have to be configured before application startup. Since the `GlobalFeatureManager.Instance` is a singleton object, one-time, static configuration is enough. It is suggested to enable/disable global features in `PreConfigureServices` method of your module. You can use the `OneTimeRunner` utility class to make sure it runs only once:
```csharp
// GerOrAdd might be useful to be sure module features are added.
var shoppingGlobalFeatures = GlobalFeatureManager.Instance.Modules
.GetOrAdd(
GlobalShoppingFeatures.ModuleName,
()=> new GlobalShoppingFeatures(GlobalFeatureManager.Instance));
// Able to Enable/Disable with generic type parameter.
shoppingGlobalFeatures.Enable<PaymentFeature>();
shoppingGlobalFeatures.Disable<PaymentFeature>();
// Also able to Enable/Disable with string feature name.
Both methods return `bool`. So, you can write conditional logic as shown below:
```csharp
if (GlobalFeatureManager.Instance.IsEnabled<PaymentFeature>())
@ -87,31 +75,72 @@ if (GlobalFeatureManager.Instance.IsEnabled<PaymentFeature>())
}
```
Beside the manual check, there is `[RequiresGlobalFeature]` attribute to check it declaratively for a controller or page. ABP returns 404 if the related feature was disabled.
### RequiresGlobalFeature Attribute
Beside the manual check, there is `[RequiresGlobalFeature]` attribute to check it declaratively for a controller or page. ABP returns HTTP Response `404` if the related feature was disabled.
```csharp
[RequiresGlobalFeature(typeof(CommentsFeature))]
[RequiresGlobalFeature(typeof(PaymentFeature))]
public class PaymentController : AbpController
{
// ...
}
```
## When to configure Global Features?
Global Features have to be configured before application startup. So best place to configuring it is `PreConfigureServices` with **OneTimeRunner** to make sure it runs one time.
## Grouping Features of a Module
It is common to group global features of a module to allow the final application developer easily discover and configure the features.
Following example shows how to group features of a module.
```csharp
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
public override void PreConfigureServices(ServiceConfigurationContext context)
[GlobalFeatureName("Ecommerce.Subscription")]
public class SubscriptionFeature : GlobalFeature
{
OneTimeRunner.Run(() =>
public SubscriptionFeature(GlobalModuleFeatures module) : base(module)
Features are used to switch on/off application feature for each tenant. So Features, only hides disabled ones, but with Global Features, disabled features pretends like never existed in application.