初識ABP vNext(8):ABP特徵管理

Tips:本篇已加入系列文章閱讀目錄,可點選檢視更多相關文章。

目錄

前言

開始定義特徵應用特徵使用者數量社交登入

最後

前言

上一篇提到了ABP功能管理(特徵管理),它來自ABP的FeatureManagement模組,ABP官方文件貌似還沒有這個模組的相關說明,但是個人感覺這個模組非常實用,下面就簡單介紹一個特徵管理的基本應用。

開始

在租戶管理中,有一個“管理功能”按鈕,預設是沒有資料的,介面上也沒有地方維護。

初識ABP vNext(8):ABP特徵管理

特徵管理簡單來說就是在同一套系統中為不同的租戶提供一些差異化的功能。比如免費使用者,提供的是基礎功能,VIP使用者則會多一些高階功能。

定義特徵

在Application。Contracts專案中新增Features資料夾。

src\Xhznl。HelloAbp。Application。Contracts\Features\HelloAbpFeatures。cs:

public class HelloAbpFeatures{ public const string GroupName = “HelloAbp”; public const string SocialLogins = GroupName + “。SocialLogins”; public const string UserCount = GroupName + “。UserCount”;}

src\Xhznl。HelloAbp。Application。Contracts\Features\HelloAbpFeatureDefinitionProvider。cs:

public class HelloAbpFeatureDefinitionProvider : FeatureDefinitionProvider{ public override void Define(IFeatureDefinitionContext context) { var group = context。AddGroup(HelloAbpFeatures。GroupName); group。AddFeature(HelloAbpFeatures。SocialLogins, “true”, L(“Feature:SocialLogins”) , valueType: new ToggleStringValueType()); group。AddFeature(HelloAbpFeatures。UserCount, “10”, L(“Feature:UserCount”) , valueType: new FreeTextStringValueType(new NumericValueValidator(1, 1000))); } private static LocalizableString L(string name) { return LocalizableString。Create(name); }}

以上程式碼添加了2個特徵:SocialLogins,UserCount。

SocialLogins(社交登入),valueType為ToggleStringValueType,意味著它是個勾選框,預設值為“true”。

UserCount(使用者數量),valueType為FreeTextStringValueType,意味著它是個輸入框,預設值為“10”。

初識ABP vNext(8):ABP特徵管理

現在可以為不同租戶設定不同的特徵值。

應用特徵

特徵值定義好了,接下來就是如何應用了,首先看一下使用者數量如何控制。

使用者數量

目前使用者是透過/identity/users介面來新增的,那麼我們重寫這個介面對應的服務方法就好了。關於重寫服務可以參考:重寫服務

初識ABP vNext(8):ABP特徵管理

對應的ABP原始碼在:abp\modules\identity\src\Volo。Abp。Identity。Application\Volo\Abp\Identity\IdentityUserAppService。cs中。

在我們的Application專案中新增一個服務類繼承IdentityUserAppService,重寫CreateAsync方法,使用FeatureChecker獲取到特徵值,然後做個使用者數量校驗即可。

src\Xhznl。HelloAbp。Application\Identity\HelloIdentityUserAppService。cs:

[RemoteService(IsEnabled = false)][Dependency(ReplaceServices = true)][ExposeServices(typeof(IIdentityUserAppService), typeof(IdentityUserAppService))]public class HelloIdentityUserAppService : IdentityUserAppService, IHelloIdentityUserAppService{ private readonly IStringLocalizer _localizer; public HelloIdentityUserAppService(IdentityUserManager userManager, IIdentityUserRepository userRepository, IIdentityRoleRepository roleRepository, IStringLocalizer localizer) : base(userManager, userRepository, roleRepository) { _localizer = localizer; } public override async Task CreateAsync(IdentityUserCreateDto input) { var userCount = (await FeatureChecker。GetOrNullAsync(HelloAbpFeatures。UserCount))。To(); var currentUserCount = await UserRepository。GetCountAsync(); if (currentUserCount >= userCount) { throw new UserFriendlyException(_localizer[“Feature:UserCount。Maximum”, userCount]); } return await base。CreateAsync(input); }}

下面可以將某租戶的使用者數量設定一下,測試是否有效果:

初識ABP vNext(8):ABP特徵管理

初識ABP vNext(8):ABP特徵管理

這樣,就實現了對不同租戶使用者數量的限制。

社交登入

特徵值也可以在前端使用,在/abp/application-configuration中就可以獲取到。

初識ABP vNext(8):ABP特徵管理

拿到特徵值,前端也可以做一些差異化功能,比如這裡的是否支援社交登入。

初識ABP vNext(8):ABP特徵管理

初識ABP vNext(8):ABP特徵管理

關於Feature就簡單介紹到這裡,本專案原始碼放在:https://github。com/xiajingren/HelloAbp

另外非常感謝熱心小夥@jonny-xhl給新增的設定模組(來自EasyAbp的Abp。SettingUi)。

初識ABP vNext(8):ABP特徵管理

初識ABP vNext(8):ABP特徵管理

最後

本文只是對Feature的最基本介紹,關於Feature,還有很多實用的API方法,基於Feature可以滿足很多定製化需求,想深入瞭解的話可以看下Abp。FeatureManagement原始碼。

感謝@jonny-xhl的pr。

轉自 https://www。cnblogs。com/xhznl/p/13608697。html

喜歡的朋友點一下關注 點關注不迷路