Skip to content

Options Pattern in ConnectSoft Base Template

Purpose & Overview

The Options Pattern in the ConnectSoft Base Template provides strongly typed, validated configuration for all microservice features. Options are registered first during startup via OptionsExtensions.AddMicroserviceOptions(), then consumed by feature extensions and application services.

For Base Template-specific implementation details, how to add new options, and the OptionsExtensions pattern, see the Base Template docs/Options Pattern.md in the ConnectSoft.BaseTemplate repository.

The options pattern provides:

  • Strong Typing: Configuration bound to POCO classes with IntelliSense support
  • Fail-Fast Validation: Startup-time validation prevents misconfigured deployments
  • Reloadable Settings: Runtime services can use IOptionsSnapshot<T> for config changes
  • Options-First Registration: Options registered before feature services that depend on them

Startup-Time vs Runtime Access

During startup (in extension methods), options are accessed via OptionsExtensions.XxxOptions static properties because the IServiceProvider is not yet built. At runtime, application services should inject IOptions<T> or IOptionsSnapshot<T> for dependency injection and testability.

Options-First Registration

AddMicroserviceOptions(configuration) is called first in ConfigureMicroserviceServices so that feature extensions (Swagger, gRPC, MassTransit, etc.) can read validated options when they register their services. Extensions read from OptionsExtensions.XxxOptions during startup; they do not call BuildServiceProvider().

Configuration (IConfiguration)
AddMicroserviceOptions(configuration)
    ├── Binds sections to options classes
    ├── Validates with [OptionsValidator] + DataAnnotations
    ├── Populates OptionsExtensions static properties
    └── Registers IOptions<T> in DI
Feature Extensions (e.g., GrpcExtensions)
    └── Read OptionsExtensions.GrpcOptions, etc.
Runtime Services
    └── Inject IOptions<T> / IOptionsSnapshot<T>

Adding Options in Specialized Templates

For domain-specific options in specialized templates (Identity, Api Library, etc.), use the extension point patterns described in Metrics, Options and Testing Extensibility. Options can be added via IConfigureOptions<T> or custom registration methods that follow the same AddOptionsWithValidation pattern.

Target Framework and Code Standards

The Base Template options target .NET 10 (net10.0). Options classes use modern C# conventions: sealed, required for mandatory properties, file-scoped namespaces, and [OptionsValidator] source-generated validators. For full conventions, see Coding Standards and the Base Template docs/Options Pattern.md.

Summary

The options pattern ensures configuration is strongly typed, validated at startup, and accessible both to feature extensions (via OptionsExtensions) and to application services (via IOptions<T>). For implementation details, see the Base Template docs/Options Pattern.md.