C# 選項模式

註冊 IOptions<T> 繫結 appsettings.json

結論

建立選項類別

public class TestOptions
{
    public const string Position = "Position";

    public string Title { get; set; } = String.Empty;
    public int Age { get; set; } = 0;
}

appsettings.json

"Position": {
    "Title": "Editor",
    "Age": 18
  }

ABP Module (原 Startup.cs)

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var configuration = context.Services.GetConfiguration();
    // 繫結設定值到選項
    Configure<TestOptions>(configuration.GetSection(TestOptions.Position));
}

使用

[Fact]
public void Should_Get_Options()
{
    // Arrange
    var options = GetRequiredService<IOptions<TestOptions>>().Value;

    // Act
    var result = options.Age;

    // Assert
    result.ShouldBe(18);
}

參照

ASP.NET Core 中的選項模式 | Microsoft Learn

PS5