The options pattern uses classes to provide strongly typed access to groups of related settings.
Setup
- Install the following libraries
1 | <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" /> |
- Create your appsettings.json file - there can be nesting like
Services:FooService
1 | { |
- Create the options class file, example
RedisServerOptions
.
1 | namespace RedisAdmin.Infrastructure.IntegrationTests.Common |
For the nested Services:FooService
example the const string would be
1 | namespace RedisAdmin.Infrastructure.IntegrationTests.Common |
- Populate the file using
ConfigurationBuilder
1 | public class CollectionFixture |
The example above is manual for integration tests. You can use your applications start up instead and pop the options file into the dependency injection container.
1 | services |
This can then be injected into your applications constructor as follows
1 | public class FooServiceClient : IFooServiceClient |