<<ABP Framework >> Function Management

Source: Internet
Author: User
Tags app service

Document Directory

The content of this section:

    • Brief introduction
      • About Ifeaturevaluestore
    • function type
      • Boolean feature
      • Value function
    • Defining features
      • Basic Function Properties
      • Additional Feature Properties
      • Functional hierarchy
    • Check function
      • Using the Requiresfeature feature
        • Requiresfeature Characteristics Considerations
      • Using Ifeaturechecker
        • IsEnabled
        • GetValue
      • Client
        • IsEnabled
        • GetValue
    • Feature Manager
    • A hint to the version

Brief introduction

Most SaaS (multi-tenant) apps have different versions (packages) of features, so you can offer different price and feature options to tenants (customers).

ABP provides a functional system that makes it easy to use, we can define features, check whether a feature is available for a tenant, and integrate functional systems into other ABP concepts (such as authorization and navigation).

About Ifeaturevaluestore

The function system uses Ifeaturevaluestore to get the value of the function. Although you can implement it your way, it is fully implemented in the Module-zero project. If it is not implemented, Nullfeaturevaluestore is used to return NULL for all functions (in which case the default function value is used).

function type

There are two basic functional types.

Boolean feature

Can be "true" or "false", a feature of this type can be enabled or disabled (for a version or a tenant).

Value function

Can be any value, it saves and gets a string, and the number is also saved as a string.

For example, our application may be a task management application, one months only create a limited number of tasks, suppose we have two different versions/packages, there is a allow to create 1000 tasks per month, but the other allows us to create 5,000 tasks per month, so this feature should be stored as a value type, Not a simple true/false.

Defining features

Before checking the functionality, define it, a module can define its own functionality by inheriting the Featureprovider class, where a very simple feature provider defines 3 features:

 Public classappfeatureprovider: featureprovider{ Public Override voidSetfeatures (Ifeaturedefinitioncontext context) {varSamplebooleanfeature = context.Create("samplebooleanfeature", DefaultValue:"false"); Samplebooleanfeature. createchildfeature ("samplenumericfeature", DefaultValue:"Ten"); Context. Create ("sampleselectionfeature", DefaultValue:"B"); }}

After creating a feature provider, we should register it in the Preinitialize method of our module, as follows:

configuration.features. Providers.add<appfeatureprovider> ();

Basic Function Properties

A feature definition requires a minimum of two properties:

Name: a unique name (string) that is the hallmark of this feature.

DefaultValue: A default value, we need a default value when we need the value of this function and cannot get it from the current tenant.

Here, we define a Boolean function named "Samplebooleanfeature" with the default value "false" (not available), At the same time we define two value functions (Samplenumericfeature is defined as Samplebooleanfeature sub-function).

Tip: Create a string constant as a feature name and avoid input errors wherever you use them.

Additional Feature Properties

Although the unique name and default value properties are required, there is also an optional property that provides detail control.

    • Scope: A Featurescopes enumeration value, which can be edition (if the feature can only set the version level), Tenant (if this feature can only set tenant level) or all (if this feature can be set for version and tenant, the tenant setting overrides the version setting). The default value is all.
    • DisplayName: A localized string that displays the name of this feature for the user.
    • Description: A localized string that displays a detailed description of this feature for the customer.
    • InputType: A UI input type for this feature, which can be defined, which can be used when creating an automatic function screen.
    • Attribute: A user Dictionary of key-value pairs that associates this function.

Let's look at the more detailed definition of the above feature:

 Public classappfeatureprovider: featureprovider{ Public Override voidSetfeatures (Ifeaturedefinitioncontext context) {varSamplebooleanfeature =context. Create (Appfeatures.samplebooleanfeature, DefaultValue:"false", Displayname:l ("Sample boolean feature"), InputType:NewCheckboxinputtype ()); Samplebooleanfeature.createchildfeature (Appfeatures.samplenumericfeature, DefaultValue:"Ten", Displayname:l ("Sample Numeric Feature"), InputType:NewSinglelinestringinputtype (NewNumericvaluevalidator (1,1000000))            ); Context. Create (Appfeatures.sampleselectionfeature, DefaultValue:"B", Displayname:l ("Sample selection Feature"), InputType:NewComboboxinputtype (NewStaticlocalizablecomboboxitemsource (NewLocalizablecomboboxitem ("A"L"Selection A")),                    NewLocalizablecomboboxitem ("B"L"Selection B")),                    NewLocalizablecomboboxitem ("C"L"Selection C"))                    )                )            ); }    Private StaticIlocalizablestring L (stringname) {        return Newlocalizablestring (name, abpzerotemplateconsts.localizationsourcename); }}

Note: The input type definition is not used by the ABP, it can be used when creating input to the function, and the ABP simply provides the underlying framework to make it easier to use.

Functional hierarchy

As shown in the example feature provider, a feature can have sub-functions. A parent feature is typically defined as a Boolean feature that can only be obtained if the parent function is available. ABP is not mandatory but is recommended to do so, the application should handle it with care.

Check function

We define a feature to check its value in the app, allowing or blocking some app functionality for each tenant. There are several different ways of checking.

Using the Requiresfeature feature

We can use Requiredfeature for a method or class, as follows:

[Requiresfeature ("exporttoexcel")]    Public Async Task<filedto> getreporttoexcel (...) {    ...}

This method can only be performed when the "exportToExcel" feature of the current tenant (obtained from iabpsession) is available, and automatically throws a abpauthorizationexception if it is not available.

Of course, the Requiresfeature attribute should be used on a Boolean-type feature, otherwise you will receive an exception.

Requiresfeature Characteristics Considerations

The ABP uses powerful dynamic method interception for feature checking, so there are some limitations on using the Requiresfeature feature on methods:

    • cannot be used on private methods.
    • cannot be used on static methods.
    • cannot be used on methods that do not inject classes (we must use dependency injection).

At the same time, it can be used:

    • Any public method called through an interface, such as using an app service through an interface
    • A virtual method that is called directly from a class reference, such as an ASP. NET MVC or Web API controller.
    • A protected virtual method.

Using Ifeaturechecker

We can inject ifeaturechecker and use it to manually check a feature (it is automatically injected into the app service, MVC and Web API controller, and is automatically used).

IsEnabled

Simply check if a given feature is available, such as:

 Public Async Task<filedto> getreporttoexcel (...) {    if (await Featurechecker.isenabledasync ("exporttoexcel" )      {        thrownew abpauthorizationexception (" you don ' t has this feature:exporttoexcel");    }    ...}

Isenabledasync and other methods also have asynchronous versions.

Of course, the IsEnabled method should be used by the Boolean type function, otherwise you will get an exception.

If you just want to check a feature and throw an exception, as shown in the example above, you can use the Checkenabled method.

GetValue

Gets the current value of a value type feature, such as:

var createdtaskcountinthismonth = getcreatedtaskcountinthismonth (); if Featurechecker.getvalue ("maxtaskcreationlimitpermonth"). to<int>()){    throwNew Abpauthorizationexception ("exceed Task creation limit for this month, sorry:(" );}

The Featurechecker method also provides the ability to work for a specified Tenantid, not just for the current tenantid.

Client

In the client (Javascript), we can use the Abp.features namespace to get the current value of the feature.

IsEnabled

var isenabled = abp.features.isEnabled ('samplebooleanfeature');

GetValue

var value = Abp.features.getValue ('samplenumericfeature');

Feature Manager

If you need to use the definition of a feature, you can inject ifeaturemanager and use it.

A hint to the version

The ABP framework does not have a content version of the system because such a system requires a database (storage version, version feature, tenant version mapping ...). ), so the version system is implemented in module zero, and you can easily use it and get a version of the system, or implement it yourself.

<<ABP Framework >> Function Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.