配置管理

提示

此内容摘自电子书《使用 .NET MAUI 的企业应用程序模式》,可在 .NET 文档上获取,也可作为免费可下载的 PDF 脱机阅读。

Enterprise Application Patterns Using .NET MAUI eBook cover thumbnail.

设置允许将配置应用行为的数据与代码分离,从而允许在不重新生成应用的情况下更改行为。 有两种类型的设置:应用设置和用户设置。

应用设置是应用创建和管理的数据。 它可以包括固定 Web 服务终结点、API 密钥和运行时状态等数据。 应用设置与核心功能相关联,并且仅对该应用有意义。

用户设置是应用的可自定义设置,可影响应用行为,不需要频繁重新调整。 例如,应用可能允许用户指定检索数据的位置以及在屏幕上显示数据的方式。

创建设置接口

虽然首选项管理器可以直接在应用程序中使用,但它确实也带来了不利条件,即会使应用程序紧密耦合到首选项管理器实现。 这种耦合意味着创建单元测试或扩展首选项管理功能将受到限制,因为应用程序没有直接的方法来拦截该行为。 若要解决此问题,可以创建一个接口,以充当首选项管理的代理。 通过接口,可提供符合我们需求的实现。 例如,在编写单元测试时,我们可能想要设置特定的设置,而接口将为我们提供一种简单的方法来一致地为测试设置这些数据。 以下代码示例显示了 eShopOnContainers 多平台应用中的 ISettingsService 接口:

namespace eShopOnContainers.Services.Settings;

public interface ISettingsService
{
    string AuthAccessToken { get; set; }
    string AuthIdToken { get; set; }
    bool UseMocks { get; set; }
    string IdentityEndpointBase { get; set; }
    string GatewayShoppingEndpointBase { get; set; }
    string GatewayMarketingEndpointBase { get; set; }
    bool UseFakeLocation { get; set; }
    string Latitude { get; set; }
    string Longitude { get; set; }
    bool AllowGpsLocation { get; set; }
}

添加设置

.NET MAUI 包含一个首选项管理器,它提供了一种为用户存储运行时设置的方法。 可以使用 Microsoft.Maui.Storage.Preferences 类从应用程序中的任何位置访问此功能。 首选项管理器提供一致、类型安全、跨平台的方法来保留和检索应用和用户设置,同时使用每个平台提供的本机设置管理。 此外,使用数据绑定访问库公开的设置数据也很简单。 有关详细信息,请参阅 Microsoft 开发人员中心的首选项

提示

首选项用于存储相对较小的数据。 如果需要存储更大或更复杂的数据,请考虑使用本地数据库或文件系统来存储数据。

应用程序将使用 Preferences 类来实现 ISettingsService 接口。 下面的代码显示了 eShopOnContainers 多平台应用的 SettingsService 如何实现 AuthTokenAccessUseMocks 属性:

public sealed class SettingsService : ISettingsService
{
    private const string AccessToken = "access_token";
    private const string AccessTokenDefault = string.Empty;

    private const string IdUseMocks = "use_mocks";
    private const bool UseMocksDefault = true;

    public string AuthAccessToken
    {
        get => Preferences.Get(AccessToken, AccessTokenDefault);
        set => Preferences.Set(AccessToken, value);
    }

    public bool UseMocks
    {
        get => Preferences.Get(IdUseMocks, UseMocksDefault);
        set => Preferences.Set(IdUseMocks, value);
    }
}

每个设置由私钥、专用默认值和公共属性组成。 该键始终是定义唯一名称的 const 字符串,设置的默认值是所需类型的静态只读或常量值。 提供默认值可确保在检索到未设置的设置时有效值可用。 可以通过依赖关系注入向应用程序提供此服务实现,以便在整个应用程序的视图模型或其他服务中使用。

数据绑定到用户设置

在 eShopOnContainers 多平台应用中,SettingsView 公开了用户可以在运行时配置的多个设置。 这些设置包括允许配置应用是否应从部署为 Docker 容器的微服务中检索数据,或者应用是否应从不需要 Internet 连接的模拟服务中检索数据。 从容器化微服务检索数据时,必须指定微服务的基本终结点 URL。 下图显示了用户选择从容器化微服务中检索数据时的 SettingsView。

User settings exposed by the eShopOnContainers multi-platform app.

数据绑定可用于检索和设置 ISettingService 接口公开的设置。 这是通过将视图绑定到视图模型属性的控件来实现的,这些属性依次访问 ISettingService 接口中的属性,并在值发生更改时发出属性更改通知。

以下代码示例显示了 SettingsView 中的 Entry 控件,该控件允许用户输入容器化微服务的基本标识终结点 URL:

<Entry Text="{Binding IdentityEndpoint, Mode=TwoWay}" />

Entry 控件使用双向绑定绑定到 SettingsViewModel 类的 IdentityEndpoint 属性。 以下代码示例显示了 IdentityEndpoint 属性:

private readonly ISettingsService _settingsService;

private string _identityEndpoint;

public SettingsViewModel(
    ILocationService locationService, IAppEnvironmentService appEnvironmentService,
    IDialogService dialogService, INavigationService navigationService, ISettingsService settingsService)
    : base(dialogService, navigationService, settingsService)
{
    _settingsService = settingsService;

    _identityEndpoint = _settingsService.IdentityEndpointBase;
}

public string IdentityEndpoint
{
    get => _identityEndpoint;
    set
    {
        SetProperty(ref _identityEndpoint, value);

        if (!string.IsNullOrWhiteSpace(value))
        {
            UpdateIdentityEndpoint();
        }
    }
}

设置 IdentityEndpoint 属性时,将调用 UpdateIdentityEndpoint 方法,前提是提供的值有效。 下面的代码示例说明 UpdateIdentityEndpoint 方法:

private void UpdateIdentityEndpoint()
{
    _settingsService.IdentityEndpointBase = _identityEndpoint;
}

此方法使用用户输入的基本终结点 URL 值更新 ISettingService 接口实现中的 IdentityEndpointBase 属性。 如果提供 SettingsService 类作为 _settingsService 的实现,则该值将保留到特定于平台的存储中。

总结

设置允许将配置应用行为的数据与代码分离,从而允许在不重新生成应用的情况下更改行为。 应用设置是应用创建和管理的数据,而用户设置是应用的可自定义设置,可影响应用行为,不需要频繁重新调整。

Microsoft.Maui.Storage.Preferences 类提供了一种一致、类型安全、跨平台的方法来保留和检索应用和用户设置。