初識ABP vNext(10):ABP設置管理


Tips:本篇已加入系列文章閱讀目錄,可點擊查看更多相關文章。

前言

上一篇介紹了ABP模塊化開發的基本步驟,完成了一個簡單的文件上傳功能。通常的模塊都有一些自己的配置信息,比如上篇講到的FileOptions類,其中配置了文件的上傳目錄,允許的文件大小和允許的文件類型。配置信息可以通過Configuration(配置)和Options(選項)來完成,ABP還提供了另一種更靈活的方式: Settings(設置),本篇就來介紹一下ABP的設置管理。

開始

回顧一下上篇的FileOptions

首先定義了一個FileOptions類,其中包含了幾個配置,然后在需要的地方中注入IOptions<FileOptions>就可以使用這些信息了。

當然,模塊啟動時可以做一些配置修改,比如:

無論是配置文件還是這種代碼形式的配置,都是程序層面的修改;有些配置不太適合這樣做,比如這里的AllowedMaxFileSizeAllowedUploadFormats,它們應該在應用界面上,可以讓管理員自行修改。下面就來改造一下程序。

定義設置

使用設置之前需要先定義它,不同的模塊可以擁有不同的設置。

modules\file-management\src\Xhznl.FileManagement.Domain\Settings\FileManagementSettingDefinitionProvider.cs:

public class FileManagementSettingDefinitionProvider : SettingDefinitionProvider
{
    public override void Define(ISettingDefinitionContext context)
    {
        /* Define module settings here.
         * Use names from FileManagementSettings class.
         */

        context.Add(new SettingDefinition(
            FileManagementSettings.AllowedMaxFileSize,
            "1024",
            L("DisplayName:FileManagement.AllowedMaxFileSize"),
            L("Description:FileManagement.AllowedMaxFileSize")
            )
                .WithProperty("Group1", "File")
                .WithProperty("Group2", "Upload")
                .WithProperty("Type", "number"),

            new SettingDefinition(
                FileManagementSettings.AllowedUploadFormats,
                ".jpg,.jpeg,.png,.gif,.txt",
                L("DisplayName:FileManagement.AllowedUploadFormats"),
                L("Description:FileManagement.AllowedUploadFormats")
            )
                .WithProperty("Group1", "File")
                .WithProperty("Group2", "Upload")
                .WithProperty("Type", "text")
            );
    }

    private static LocalizableString L(string name)
    {
        return LocalizableString.Create<FileManagementResource>(name);
    }
}

以上代碼定了了2個配置:AllowedMaxFileSizeAllowedUploadFormats,設置了它們的默認值、名稱和詳細說明。因為本項目使用了EasyAbp的SettingUi模塊,所以會有一些Group1Group2之類的字段,具體介紹可以參考Abp.SettingUi

使用設置

想讀取設置信息,只需注入ISettingProvider即可。因為父類ApplicationService中已經注入,所以這里直接使用SettingProvider就好。獲取到配置,然后就可以做一些邏輯處理,比如判斷上傳文件的大小和格式是否合法:

public class FileAppService : FileManagementAppService, IFileAppService
{
    ......

    [Authorize]
    public virtual async Task<string> CreateAsync(FileUploadInputDto input)
    {
        var allowedMaxFileSize = await SettingProvider.GetAsync<int>(FileManagementSettings.AllowedMaxFileSize);//kb
        var allowedUploadFormats = (await SettingProvider.GetOrNullAsync(FileManagementSettings.AllowedUploadFormats))
            ?.Split(",", StringSplitOptions.RemoveEmptyEntries);

        if (input.Bytes.Length > allowedMaxFileSize * 1024)
        {
            throw new UserFriendlyException(L["FileManagement.ExceedsTheMaximumSize", allowedMaxFileSize]);
        }

        if (allowedUploadFormats == null || !allowedUploadFormats.Contains(Path.GetExtension(input.Name)))
        {
            throw new UserFriendlyException(L["FileManagement.NotValidFormat"]);
        }

        ......
    }
}

前端設置界面:

下面可以隨便修改下設置,進行測試:

最后

本篇內容較少,希望對你有幫助。代碼已上傳至 https://github.com/xiajingren/HelloAbp ,歡迎star。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM