1. 程式人生 > >初識ABP vNext(8):ABP特徵管理

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

Tips:本篇已加入系列文章閱讀目錄,可點選檢視更多相關文章。 [TOC] # 前言 上一篇提到了ABP功能管理(特徵管理),它來自ABP的FeatureManagement模組,ABP官方文件貌似還沒有這個模組的相關說明,但是個人感覺這個模組非常實用,下面就簡單介紹一個特徵管理的基本應用。 # 開始 在租戶管理中,有一個“管理功能”按鈕,預設是沒有資料的,介面上也沒有地方維護。 ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200902161359148-1307148214.png) 特徵管理簡單來說就是在同一套系統中為不同的租戶提供一些差異化的功能。比如免費使用者,提供的是基礎功能,VIP使用者則會多一些高階功能。 ## 定義特徵 在Application.Contracts專案中新增Features資料夾。 src\Xhznl.HelloAbp.Application.Contracts\Features\HelloAbpFeatures.cs: ```csharp 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: ```csharp 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"。 ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200902175143473-290258241.png) 現在可以為不同租戶設定不同的特徵值。 ## 應用特徵 特徵值定義好了,接下來就是如何應用了,首先看一下使用者數量如何控制。 ### 使用者數量 目前使用者是通過`/identity/users`介面來新增的,那麼我們重寫這個介面對應的服務方法就好了。關於重寫服務可以參考:[重寫服務](https://docs.abp.io/zh-Hans/abp/latest/Customizing-Application-Modules-Overriding-Services) ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903114748250-736690648.png) 對應的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: ```csharp [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); } } ``` 下面可以將某租戶的使用者數量設定一下,測試是否有效果: ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903151521350-701776470.png) ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903151333237-231611813.png) 這樣,就實現了對不同租戶使用者數量的限制。 ### 社交登入 特徵值也可以在前端使用,在`/abp/application-configuration`中就可以獲取到。 ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903152805234-582385026.png) 拿到特徵值,前端也可以做一些差異化功能,比如這裡的是否支援社交登入。 ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903154249927-186656135.png) ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903154232397-191624765.png) ---- 關於Feature就簡單介紹到這裡,本專案原始碼放在:https://github.com/xiajingren/HelloAbp 另外非常感謝熱心小夥@[jonny-xhl](https://github.com/jonny-xhl)給新增的設定模組(來自EasyAbp的[Abp.SettingUi](https://github.com/EasyAbp/Abp.SettingUi))。 ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903155640781-1377849112.png) ![](https://img2020.cnblogs.com/blog/610959/202009/610959-20200903155721372-28113862.png) # 最後 本文只是對Feature的最基本介紹,關於Feature,還有很多實用的API方法,基於Feature可以滿足很多定製化需求,想深入瞭解的話可以看下Abp.FeatureManagement原始碼。 感謝@jonny-xhl的pr。