這篇博客的目的:1.瞭解ABP的特徵這個模塊,2.在AbpVue的模板中添加實時通訊的特徵。
一、特性的定義。
1.關於特性,來自官方文檔的解釋:https://aspnetboilerplate.com/Pages/Documents/Feature-Management
在大多數SAAS系統中。都提供【版本】這個概念。不同的版本提供不同的功能特性。通過這種方式,一個SAAS系統可以提供不同的價格與功能組合給不同的租戶。
ABP內置了特性(功能)模塊以簡化此類功能開發。您可以定義各種特性(功能),檢測某個指定特性(功能)是否對某個指定租戶開放,或者根據後臺設置的特性(功能)開放對應的頁面或是生成對應的導航。
Feature是什麼?Feature就是對function的分類方法,其與function的關係就比如Role和User的關係一樣。
ABP中Feature具有以下屬性: 其中最重要的屬性是name,用以表示feature的Identity,一個feature一個name. 一個Feature可以有一組子Features,從而構成Feature樹。
2.值類型
系統已有兩個基本的功能類型。
①. Boolean 功能
可以是true或者false。利用這個類型我們可以爲租戶啓用(enable)或者禁用(disable)某些功能。
②. Value 功能
可以爲任意值。我們可以用它來存儲並者取得一個字符串,我們也可以很容易的用數字作爲字符串來存儲。
二、
第一步 在Core層的Features下的XXXFeatureProvider添加代碼,作用是定義
var sampleBooleanFeature = context.Create("SignalR", defaultValue: "true",
inputType: new CheckboxInputType());
sampleBooleanFeature.CreateChildFeature("SignalR.AspNetCore", defaultValue: "true", inputType: new CheckboxInputType());
第二步,修改應用層SessionsAppservice中的GetCurrentLoginInformations方法。
[DisableAuditing]
public async Task<GetCurrentLoginInformationsOutput> GetCurrentLoginInformations()
{
var output = new GetCurrentLoginInformationsOutput
{
Application = new ApplicationInfoDto
{
Version = AppVersionHelper.Version,
ReleaseDate = AppVersionHelper.ReleaseDate,
Features = new Dictionary<string, bool>()
}
};
if (AbpSession.TenantId.HasValue)
{
output.Tenant = ObjectMapper.Map<TenantLoginInfoDto>(await GetCurrentTenantAsync());
Dictionary<string, bool> dicFeatures = new Dictionary<string, bool>();
var dicFeatureList = _iFeatureManager.GetAll();
for (int i = 0; i < dicFeatureList.Count; i++)
{
var feature = await _featureValueStore.GetValueOrNullAsync(AbpSession.TenantId.Value, dicFeatureList[i]);
if(feature!=null&&feature=="True")
{
dicFeatures.Add(dicFeatureList[i].Name, true);
}
}
output.Application.Features = dicFeatures;
}
if (AbpSession.UserId.HasValue)
{
output.User = ObjectMapper.Map<UserLoginInfoDto>(await GetCurrentUserAsync());
}
return output;
}
第三部,在數據庫中加入兩個方法。
語句如圖:
INSERT INTO [dbo].[AbpFeatures]
([CreationTime]
,[CreatorUserId]
,[Discriminator]
,[Name]
,[Value]
,[EditionId]
,[TenantId])
VALUES
(GETDATE()
,1
,'EditionFeatureSetting'
,'SignalR'
,'true'
,1
,1)
INSERT INTO [dbo].[AbpFeatures]
([CreationTime]
,[CreatorUserId]
,[Discriminator]
,[Name]
,[Value]
,[EditionId]
,[TenantId])
VALUES
(GETDATE()
,1
,'EditionFeatureSetting'
,'AspNetCore'
,'true'
,1
,1)
GO
其餘收穫
1.IgnoreQueryFilters在查詢中禁用全局過濾器,如邏輯刪除等等。
相關參考:
官方文檔:https://aspnetboilerplate.com/api-docs/html/N_Abp_Application_Features.htm