Browse Source

Docs - Update Global Features documentation

pull/8509/head
enisn 5 years ago
parent
commit
a7bb0bdf76
  1. 9
      docs/en/Differences-between-features-and-global-features.md
  2. 129
      docs/en/Global-Features.md

9
docs/en/Differences-between-features-and-global-features.md

@ -1,9 +0,0 @@
# Differences between Features & Global Features
[Features](Features.md) & [Global Features](Global-Features.md) are totally different systems.
- Features can be switched at runtime but global features can't be.
- Features can be managed by users & system administrators while application running but Global Features system is for Developers and can be managed at development time.
- Features can be used as Feature-Flag but Global Features can't.
- Features loads all disabled features to application and just hide them but Global Features system doesn't load disabled features and even prevents table creating in database.

129
docs/en/Global-Features.md

@ -2,110 +2,107 @@
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. 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.
## Installation ## Installation
Global Feature system is pre-installed in abp startup templates. No need any installation manually. > This package is already installed by default with the startup template. So, most of the time, you don't need to install it manually.
## Usage ### Using the ABP CLI
### Enable/Disable Existing Features Open a command line window in the folder of the project (.csproj file) and type the following command:
```csharp ```bash
//Enable all the CMS Kit Features abp add-package Volo.Abp.GlobalFeatures
GlobalFeatureManager.Instance.Modules.CmsKit().EnableAll();
//Disable all the CMS Kit Features (while it is already disabled by default)
GlobalFeatureManager.Instance.Modules.CmsKit().DisableAll();
//Enable a feature
GlobalFeatureManager.Instance.Modules.CmsKit().Comments.Enable();
GlobalFeatureManager.Instance.Modules.CmsKit().Enable<CommentsFeature>(); //Alternative: use the feature class
GlobalFeatureManager.Instance.Modules.CmsKit().Enable(CommentsFeature.Name); //Alternative: use the feature name
GlobalFeatureManager.Instance.Modules.CmsKit().Enable("CmsKit.Comments"); //Alternative: Use magic string
``` ```
### Check if a feature is enabled
```csharp
GlobalFeatureManager.Instance.IsEnabled<CommentsFeature>();
GlobalFeatureManager.Instance.IsEnabled(CommentsFeature.Name); //Alternative
```
Both methods return `bool`.
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.
```csharp
[RequiresGlobalFeature(typeof(CommentsFeature))]
public class CommentController : AbpController
{
// ...
}
```
## Implementation ## Implementation
Global Feature system aims module based feature management . A module has to have own Global Features itself. Global Feature system aims module based feature management . A module has to have own Global Features itself.
### Define a Global Feature ### Define a Global Feature
A feature class is something like that: A feature class is something like that:
```csharp ```csharp
[GlobalFeatureName(Name)] [GlobalFeatureName(Name)]
public class FooBarFeature : GlobalFeature public class PaymentFeature : GlobalFeature
{ {
public const string Name = "Foo.Bar"; public const string Name = "Shopping.Payment";
internal FooBarFeature(
[NotNull] GlobalFooFeatures features public PaymentFeature(GlobalModuleFeatures module) : base(module)
) : base(features)
{ {
} }
} }
``` ```
### Define Global Module Features ### Define Global Module Features
All global features of a module should be provided by a single **GlobalModuleFeatures** class.
All features of a module have to be defined in a Global Module Features class.
```csharp ```csharp
public class GlobalFooFeatures : GlobalModuleFeatures public class GlobalShoppingFeatures : GlobalModuleFeatures
{ {
public const string ModuleName = "Foo"; public const string ModuleName = "Shopping";
public FooBarFeature FooBar => GetFeature<FooBarFeature>(); public GlobalShoppingFeatures(GlobalFeatureManager featureManager) : base(featureManager)
public GlobalFooFeatures([NotNull] GlobalFeatureManager featureManager)
: base(featureManager)
{ {
AddFeature(new PaymentFeature(this));
// And more features...
} }
} }
``` ```
### Define Global Module Features Dictionary Extensions
An extension method is better to configure global fetures easily.
## Usage
### Enable/Disable Features
Global features are managed by modules. Module Features have to be added to Modules of GlobalFeatureManager.
```csharp ```csharp
public static class GlobalModuleFeaturesDictionaryFooExtensions // 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.
shoppingGlobalFeatures.Enable(PaymentFeature.Name);
shoppingGlobalFeatures.Disable("Shopping.Payment");
```
### Check if a feature is enabled
```csharp
GlobalFeatureManager.Instance.IsEnabled<PaymentFeature>()
GlobalFeatureManager.Instance.IsEnabled("Shopping.Payment")
```
Both methods return `bool`.
```csharp
if (GlobalFeatureManager.Instance.IsEnabled<PaymentFeature>())
{ {
public static GlobalFooFeatures Foo( // Some strong payment codes here...
[NotNull] this GlobalModuleFeaturesDictionary modules)
{
Check.NotNull(modules, nameof(modules));
return modules
.GetOrAdd(
GlobalFooFeatures.ModuleName,
_ => new GlobalFooFeatures(modules.FeatureManager)
)
as GlobalFooFeatures;
}
} }
``` ```
Accessing module & module features look like: 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.
```csharp ```csharp
GlobalFeatureManager.Instance.Modules.Foo(); [RequiresGlobalFeature(typeof(CommentsFeature))]
GlobalFeatureManager.Instance.Modules.Foo().FooBar; public class PaymentController : AbpController
GlobalFeatureManager.Instance.Modules.Foo().FooBar.Enable(); {
GlobalFeatureManager.Instance.Modules.Foo().Enable<FooBarFeature>(); // ...
}
``` ```
## When to configure Global Features? ## When to configure Global Features?
Global Features have to be configured before application startup. So best place to configuring it is `PreConfigureServices` with **OneTimeRunner**. 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.
```csharp ```csharp
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
@ -118,5 +115,7 @@ public override void PreConfigureServices(ServiceConfigurationContext context)
} }
``` ```
## See also ## Features vs Global Features
- [Differences Between Features & Global Features](Differences-between-features-and-global-features.md) [Features](Features.md) & [Global Features](Global-Features.md) are totally different systems.
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.
Loading…
Cancel
Save