├── .npmrc ├── src ├── ConventionalOptions.TestTypes │ ├── ConventionalOptions.TestTypes.csproj │ └── TestOptions.cs ├── ConventionalOptions.Specs │ ├── Model │ │ └── TestOptions.cs │ ├── ConventionalOptions.Specs.csproj │ └── Specifications │ │ ├── DependencyInjectionConventionalOptionsSpecs.cs │ │ ├── NinjectConventionalOptionsSpecs.cs │ │ ├── StructureMapConventionalOptionsSpecs.cs │ │ ├── AutofacConventionalOptionsSpecs.cs │ │ └── LamarConventionalOptionsSpecs.cs ├── ConventionalOptions │ ├── ConventionalOptions.csproj │ └── ConfigureFromConfigurationSectionOptions.cs ├── ConventionalOptions.Autofac │ ├── ConventionalOptions.Autofac.csproj │ └── AutofacOptionsServiceCollectionExtensions.cs ├── ConventionalOptions.Ninject │ ├── ConventionalOptions.Ninject.csproj │ └── NinjectOptionsServiceCollectionExtensions.cs ├── ConventionalOptions.DependencyInjection │ ├── ConventionalOptions.DependencyInjection.csproj │ └── OptionsServiceCollectionExtensions.cs ├── ConventionalOptions.StructureMap │ ├── ConventionalOptions.StructureMap.csproj │ └── StructureMapOptionsServiceCollectionExtensions.cs └── ConventionalOptions.sln ├── CHANGELOG.md ├── README.md ├── package.json └── .gitignore /.npmrc: -------------------------------------------------------------------------------- 1 | script-shell=bash 2 | -------------------------------------------------------------------------------- /src/ConventionalOptions.TestTypes/ConventionalOptions.TestTypes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/ConventionalOptions.TestTypes/TestOptions.cs: -------------------------------------------------------------------------------- 1 | namespace ConventionalOptions.TestTypes 2 | { 3 | public class TestOptions 4 | { 5 | public string StringProperty { get; set; } 6 | public int IntProperty { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/Model/TestOptions.cs: -------------------------------------------------------------------------------- 1 | namespace ConventionalOptions.Specs.Model 2 | { 3 | public class TestOptions 4 | { 5 | public string StringProperty { get; set; } 6 | public int IntProperty { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /src/ConventionalOptions/ConventionalOptions.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/ConventionalOptions.Autofac/ConventionalOptions.Autofac.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/ConventionalOptions.Ninject/ConventionalOptions.Ninject.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/ConventionalOptions.DependencyInjection/ConventionalOptions.DependencyInjection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Library 5 | netstandard2.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/ConventionalOptions.StructureMap/ConventionalOptions.StructureMap.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [2.0.0](https://github.com/derekgreer/conventionalOptions/compare/v1.1.0...v2.0.0) (2022-12-21) 6 | 7 | 8 | ### ⚠ BREAKING CHANGES 9 | 10 | * Remove configuration parameter 11 | 12 | ### Features 13 | 14 | * Remove configuration parameter ([eb56c0e](https://github.com/derekgreer/conventionalOptions/commit/eb56c0e96d4c025960838a8098a6f2641961f719)) 15 | 16 | ## [1.1.0](https://github.com/derekgreer/conventionalOptions/compare/v1.0.0...v1.1.0) (2022-02-14) 17 | 18 | 19 | ### Features 20 | 21 | * Fix for breaking change introduced in .Net 6 ([ecfefc1](https://github.com/derekgreer/conventionalOptions/commit/ecfefc1b429bd5bfde593a889ad7458d198214e7)) 22 | 23 | ## 1.0.0 (2020-11-20) 24 | 25 | ## 1.0.0 (2020-11-18) 26 | 27 | ### 0.0.1 (2020-11-18) 28 | -------------------------------------------------------------------------------- /src/ConventionalOptions/ConfigureFromConfigurationSectionOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.Configuration; 3 | using Microsoft.Extensions.Options; 4 | 5 | namespace ConventionalOptions 6 | { 7 | public class ConfigureFromConfigurationSectionOptions : ConfigureOptions where TOptions : class 8 | { 9 | public ConfigureFromConfigurationSectionOptions(IConfiguration config) 10 | : base(options => 11 | { 12 | var name = typeof(TOptions).Name; 13 | var sectionName = name.Substring(0, name.Length - 7); 14 | var section = config.GetSection(sectionName); 15 | 16 | if (section == null) 17 | throw new Exception($"Could not find section \"{sectionName}\" in configuration."); 18 | 19 | section.Bind(options); 20 | }) 21 | { 22 | if (config == null) 23 | throw new ArgumentNullException("config"); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/ConventionalOptions.Specs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Conventional Options 2 | [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) 3 | 4 | 5 | ConventionalOptions is a convenience library for working with Microsoft's .Net Core configuration API. 6 | 7 | The goal of ConventionalOptions is to simplify creation and use of POCO option types. 8 | 9 | 10 | ## Quickstart 11 | 12 | ### Step 1 13 | Install ConventionalOptions for the target DI container: 14 | 15 | ``` 16 | $> nuget install ConventionalOptions.DependencyInjection 17 | ``` 18 | 19 | ### Step 2 20 | Add Microsoft's Options feature and register option types: 21 | 22 | ``` 23 | services.AddOptions(); 24 | services.RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 25 | ``` 26 | 27 | ### Step 3 28 | Create an Options class: 29 | 30 | ```csharp 31 | public class OrderServiceOptions 32 | { 33 | public string StringProperty { get; set; } 34 | public int IntProperty { get; set; } 35 | } 36 | ``` 37 | 38 | ### Step 4 39 | Provide a corresponding configuration section (e.g. in appsettings.json): 40 | 41 | ```json 42 | { 43 | "OrderService": { 44 | "StringProperty": "Some value", 45 | "IntProperty": 42 46 | } 47 | } 48 | ``` 49 | 50 | ### Step 5 51 | Inject the options into types resolved from the container: 52 | 53 | ```csharp 54 | public class OrderService 55 | { 56 | public OrderService(OrderServiceOptions options) 57 | { 58 | // ... use options 59 | } 60 | } 61 | ``` 62 | 63 | That's it! 64 | 65 | 66 | For more examples, see the [documentation](https://github.com/derekgreer/conventional-options/wiki) or [browse the specifications](https://github.com/derekgreer/conventional-options/tree/master/src/ConventionalOptions.Specs). 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "conventional-options", 3 | "version": "2.0.0", 4 | "description": "An options convenience library", 5 | "config": { 6 | "configuration": "Release", 7 | "projectName": "ConventionalOptions", 8 | "publishSource": "https://www.nuget.org/api/v2/package" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/derekgreer/conventionalOptions.git" 13 | }, 14 | "scripts": { 15 | "ci:release": "standard-version -a --releaseCommitMessageFormat=\"chore(release): {{currentTag}}; [skip ci]\"", 16 | "clean": "rimraf dist build", 17 | "prebuild": "npm run clean", 18 | "build": "npm-run-all build:restore build:compile tests package", 19 | "build:restore": "globstar -- dotnet restore **/*.sln /p:Version=${npm_config_version_prefix:-${npm_package_version}}", 20 | "build:compile": "globstar -- dotnet build **/*.sln --configuration ${npm_package_config_configuration} /p:Version=${npm_config_version_prefix:-${npm_package_version}}", 21 | "tests": "dotnet test src/*.Specs/*.csproj --no-build --configuration ${npm_package_config_configuration}", 22 | "package": "npm-run-all package:*", 23 | "package:main": "globstar -- dotnet pack ./src/**/${npm_package_config_projectName}.csproj -c ${npm_package_config_configuration} --no-build -o dist --include-symbols -p:VersionPrefix=${npm_config_version_prefix:-${npm_package_version}} ${npm_config_version_suffix:+--version-suffix ${npm_config_version_suffix}}", 24 | "package:autofac": "globstar -- dotnet pack ./src/**/${npm_package_config_projectName}.Autofac.csproj -c ${npm_package_config_configuration} --no-build -o dist --include-symbols -p:VersionPrefix=${npm_config_version_prefix:-${npm_package_version}} ${npm_config_version_suffix:+--version-suffix ${npm_config_version_suffix}}", 25 | "package:di": "globstar -- dotnet pack ./src/**/${npm_package_config_projectName}.DependencyInjection.csproj -c ${npm_package_config_configuration} --no-build -o dist --include-symbols -p:VersionPrefix=${npm_config_version_prefix:-${npm_package_version}} ${npm_config_version_suffix:+--version-suffix ${npm_config_version_suffix}}", 26 | "package:ninject": "globstar -- dotnet pack ./src/**/${npm_package_config_projectName}.Ninject.csproj -c ${npm_package_config_configuration} --no-build -o dist --include-symbols -p:VersionPrefix=${npm_config_version_prefix:-${npm_package_version}} ${npm_config_version_suffix:+--version-suffix ${npm_config_version_suffix}}", 27 | "package:structureMap": "globstar -- dotnet pack ./src/**/${npm_package_config_projectName}.StructureMap.csproj -c ${npm_package_config_configuration} --no-build -o dist --include-symbols -p:VersionPrefix=${npm_config_version_prefix:-${npm_package_version}} ${npm_config_version_suffix:+--version-suffix ${npm_config_version_suffix}}", 28 | "publish": "dotnet nuget push --source ${npm_package_config_publishSource} --skip-duplicate dist/**/*.nupkg", 29 | "publish:private": "copyfiles --flat --error dist/*.nupkg ${APPDATA}/Packages/" 30 | }, 31 | "keywords": [ 32 | "configuration", 33 | "options" 34 | ], 35 | "author": "Derek Greer", 36 | "license": "MIT", 37 | "dependencies": {}, 38 | "devDependencies": { 39 | "copyfiles": "^2.2.0", 40 | "globstar": "^1.0.0", 41 | "npm-run-all": "^4.0.2", 42 | "rimraf": "^2.6.1" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/ConventionalOptions.DependencyInjection/OptionsServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Options; 7 | 8 | namespace ConventionalOptions.DependencyInjection 9 | { 10 | public static class OptionsServiceCollectionExtensions 11 | { 12 | public static void RegisterOptionsFromAssemblies(this IServiceCollection services, params Assembly[] assemblies) 13 | { 14 | var types = assemblies.SelectMany(a => 15 | a.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.Name.EndsWith("Options"))).ToList(); 16 | 17 | foreach (var t in types) services.RegisterOptions(t); 18 | } 19 | 20 | public static void RegisterOptions(this IServiceCollection services, IConfiguration configuration) 21 | { 22 | services.RegisterOptions(typeof(TOptions)); 23 | } 24 | 25 | public static void RegisterOptions(this IServiceCollection services, Type optionsType) 26 | { 27 | RegisterChangeTokenSource(services, optionsType); 28 | RegisterConfigureOptions(services, optionsType); 29 | RegisterOptionsInternal(services, optionsType); 30 | } 31 | 32 | static void RegisterOptionsInternal(IServiceCollection services, Type optionsType) 33 | { 34 | services.Add(new ServiceDescriptor(optionsType, serviceProvider => 35 | { 36 | var genericType = typeof(IOptions<>).MakeGenericType(optionsType); 37 | var options = serviceProvider.GetService(genericType); 38 | return genericType.GetProperty("Value").GetValue(options); 39 | }, ServiceLifetime.Singleton)); 40 | } 41 | 42 | static void RegisterConfigureOptions(IServiceCollection services, Type optionsType) 43 | { 44 | var configurationOptionsInterfaceType = typeof(IConfigureOptions<>).MakeGenericType(optionsType); 45 | 46 | services.Add(new ServiceDescriptor(configurationOptionsInterfaceType, serviceProvider => 47 | { 48 | var configuration = serviceProvider.GetService(); 49 | var configurationOptionsType = typeof(ConfigureFromConfigurationSectionOptions<>).MakeGenericType(optionsType); 50 | var configurationOptionsTypeInstance = Activator.CreateInstance(configurationOptionsType, configuration); 51 | 52 | return configurationOptionsTypeInstance; 53 | }, ServiceLifetime.Singleton)); 54 | } 55 | 56 | static void RegisterChangeTokenSource(IServiceCollection services, Type optionsType) 57 | { 58 | var optionsChangeTokenSourceInterfaceType = typeof(IOptionsChangeTokenSource<>).MakeGenericType(optionsType); 59 | 60 | services.Add(new ServiceDescriptor(optionsChangeTokenSourceInterfaceType, serviceProvider => 61 | { 62 | var configuration = serviceProvider.GetService(); 63 | var changeTokenSourceType = typeof(ConfigurationChangeTokenSource<>).MakeGenericType(optionsType); 64 | var configurationChangeTokenSourceInstance = Activator.CreateInstance(changeTokenSourceType, Options.DefaultName, configuration); 65 | return configurationChangeTokenSourceInstance; 66 | }, ServiceLifetime.Singleton)); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/Specifications/DependencyInjectionConventionalOptionsSpecs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using ConventionalOptions.DependencyInjection; 4 | using ConventionalOptions.Specs.Model; 5 | using ExpectedObjects; 6 | using Machine.Specifications; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; 10 | 11 | namespace ConventionalOptions.Specs.Specifications 12 | { 13 | class DependencyInjectionConventionalOptionsSpecs 14 | { 15 | [Subject("DotNetCore DependencyInjection")] 16 | class when_resolving_options_from_the_container 17 | { 18 | static ExpectedObject _expectedOptions; 19 | static TestOptions _actualOptions; 20 | static ServiceProvider _container; 21 | 22 | Establish context = () => 23 | { 24 | var configuration = new ConfigurationBuilder() 25 | .AddInMemoryCollection(new Dictionary 26 | { 27 | {"Test:StringProperty", "TestValue"}, 28 | {"Test:IntProperty", "2"} 29 | }).Build(); 30 | 31 | var services = new ServiceCollection(); 32 | services.AddSingleton(configuration); 33 | services.AddOptions(); 34 | services.RegisterOptions(configuration); 35 | _container = services.BuildServiceProvider(); 36 | 37 | _expectedOptions = new 38 | { 39 | StringProperty = "TestValue", 40 | IntProperty = 2 41 | }.ToExpectedObject(); 42 | }; 43 | 44 | Because of = () => _actualOptions = _container.GetService(); 45 | 46 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 47 | } 48 | 49 | [Subject("DotNetCore DependencyInjection")] 50 | class when_resolving_auto_registered_options_from_the_container 51 | { 52 | static ExpectedObject _expectedOptions; 53 | static TestOptions _actualOptions; 54 | static ServiceProvider _container; 55 | 56 | Establish context = () => 57 | { 58 | var configuration = new ConfigurationBuilder() 59 | .AddInMemoryCollection(new Dictionary 60 | { 61 | {"Test:StringProperty", "TestValue"}, 62 | {"Test:IntProperty", "2"} 63 | }).Build(); 64 | 65 | var services = new ServiceCollection(); 66 | services.AddSingleton(configuration); 67 | services.AddOptions(); 68 | services.RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 69 | _container = services.BuildServiceProvider(); 70 | 71 | _expectedOptions = new 72 | { 73 | StringProperty = "TestValue", 74 | IntProperty = 2 75 | }.ToExpectedObject(); 76 | }; 77 | 78 | Because of = () => _actualOptions = _container.GetService(); 79 | 80 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Ninject/NinjectOptionsServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.Options; 6 | using Ninject; 7 | 8 | namespace ConventionalOptions.Ninject 9 | { 10 | public static class NinjectOptionsServiceCollectionExtensions 11 | { 12 | /// 13 | /// Registers base option types with the Ninject Container. 14 | /// 15 | /// The intent of this method is to provide registration of base option types 16 | /// when used outside the context of an Asp.Net Core Web application. 17 | /// 18 | /// 19 | /// 20 | public static IKernel AddOptions(this IKernel kernel) 21 | { 22 | if (kernel == null) 23 | { 24 | throw new ArgumentNullException(nameof(kernel)); 25 | } 26 | 27 | kernel.Bind(typeof(IOptions<>)).To(typeof(OptionsManager<>)).InSingletonScope(); 28 | kernel.Bind(typeof(IOptionsSnapshot<>)).To(typeof(OptionsManager<>)).InSingletonScope(); 29 | kernel.Bind(typeof(IOptionsMonitor<>)).To(typeof(OptionsMonitor<>)).InSingletonScope(); 30 | kernel.Bind(typeof(IOptionsFactory<>)).To(typeof(OptionsFactory<>)).InSingletonScope(); 31 | kernel.Bind(typeof(IOptionsMonitorCache<>)).To(typeof(OptionsCache<>)).InSingletonScope(); 32 | 33 | return kernel; 34 | } 35 | 36 | public static IKernel RegisterOptionsFromAssemblies(this IKernel builder, params Assembly[] assemblies) 37 | { 38 | var types = assemblies.SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.Name.EndsWith("Options"))).ToList(); 39 | 40 | foreach (var t in types) builder.RegisterOptions(t); 41 | 42 | return builder; 43 | } 44 | 45 | public static IKernel RegisterOptions(this IKernel kernel) 46 | { 47 | kernel.RegisterOptions(typeof(TOptions)); 48 | 49 | return kernel; 50 | } 51 | 52 | public static IKernel RegisterOptions(this IKernel kernel, Type optionsType) 53 | { 54 | RegisterChangeTokenSource(kernel, optionsType); 55 | RegisterConfigureOptions(kernel, optionsType); 56 | RegisterOptionsInternal(kernel, optionsType); 57 | 58 | return kernel; 59 | } 60 | 61 | static void RegisterOptionsInternal(IKernel kernel, Type optionsType) 62 | { 63 | kernel.Bind(optionsType).ToMethod(context => 64 | { 65 | var genericType = typeof(IOptions<>).MakeGenericType(optionsType); 66 | var options = context.Kernel.Get(genericType); 67 | return genericType.GetProperty("Value").GetValue(options); 68 | } 69 | ); 70 | } 71 | 72 | static void RegisterConfigureOptions(IKernel kernel, Type optionsType) 73 | { 74 | var configurationOptionsInterfaceType = typeof(IConfigureOptions<>).MakeGenericType(optionsType); 75 | 76 | kernel.Bind(configurationOptionsInterfaceType).ToMethod(context => 77 | { 78 | var configuration = context.Kernel.Get(); 79 | var configurationOptionsType = typeof(ConfigureFromConfigurationSectionOptions<>).MakeGenericType(optionsType); 80 | var configurationOptionsTypeInstance = Activator.CreateInstance(configurationOptionsType, configuration); 81 | 82 | return configurationOptionsTypeInstance; 83 | }).InSingletonScope(); 84 | } 85 | 86 | static void RegisterChangeTokenSource(IKernel kernel, Type optionsType) 87 | { 88 | var optionsChangeTokenSourceInterfaceType = typeof(IOptionsChangeTokenSource<>).MakeGenericType(optionsType); 89 | 90 | kernel.Bind(optionsChangeTokenSourceInterfaceType).ToMethod(context => 91 | { 92 | var configuration = context.Kernel.Get(); 93 | var changeTokenSourceType = typeof(ConfigurationChangeTokenSource<>).MakeGenericType(optionsType); 94 | var configurationChangeTokenSourceInstance = Activator.CreateInstance(changeTokenSourceType, Options.DefaultName, configuration); 95 | return configurationChangeTokenSourceInstance; 96 | }).InSingletonScope(); 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30523.141 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionalOptions.Autofac", "ConventionalOptions.Autofac\ConventionalOptions.Autofac.csproj", "{4B420DF8-F26E-420C-A883-D0BBEB40C518}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{54A44306-76B1-4F15-8525-B1F4432CAB6A}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionalOptions.DependencyInjection", "ConventionalOptions.DependencyInjection\ConventionalOptions.DependencyInjection.csproj", "{9CD13130-C383-4319-AE6A-80A7B3406D40}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Specifications", "Specifications", "{3658C7D5-1622-4D5A-A639-C20067DBB28D}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionalOptions.Specs", "ConventionalOptions.Specs\ConventionalOptions.Specs.csproj", "{C5421989-6A04-4AB3-8DF0-F34780EA6EDD}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionalOptions.StructureMap", "ConventionalOptions.StructureMap\ConventionalOptions.StructureMap.csproj", "{EFDD6784-02AD-42CF-AD05-754282F0466C}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionalOptions", "ConventionalOptions\ConventionalOptions.csproj", "{FBAB32FB-5E8C-4C3D-A093-41D91A7181CB}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionalOptions.Ninject", "ConventionalOptions.Ninject\ConventionalOptions.Ninject.csproj", "{F88FDDA0-61E9-4443-B397-98F4A57756CC}" 21 | EndProject 22 | Global 23 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 24 | Debug|Any CPU = Debug|Any CPU 25 | Release|Any CPU = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 28 | {4B420DF8-F26E-420C-A883-D0BBEB40C518}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {4B420DF8-F26E-420C-A883-D0BBEB40C518}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {4B420DF8-F26E-420C-A883-D0BBEB40C518}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {4B420DF8-F26E-420C-A883-D0BBEB40C518}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {9CD13130-C383-4319-AE6A-80A7B3406D40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {9CD13130-C383-4319-AE6A-80A7B3406D40}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {9CD13130-C383-4319-AE6A-80A7B3406D40}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {9CD13130-C383-4319-AE6A-80A7B3406D40}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {C5421989-6A04-4AB3-8DF0-F34780EA6EDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {C5421989-6A04-4AB3-8DF0-F34780EA6EDD}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {C5421989-6A04-4AB3-8DF0-F34780EA6EDD}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {C5421989-6A04-4AB3-8DF0-F34780EA6EDD}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {EFDD6784-02AD-42CF-AD05-754282F0466C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {EFDD6784-02AD-42CF-AD05-754282F0466C}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {EFDD6784-02AD-42CF-AD05-754282F0466C}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {EFDD6784-02AD-42CF-AD05-754282F0466C}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {FBAB32FB-5E8C-4C3D-A093-41D91A7181CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {FBAB32FB-5E8C-4C3D-A093-41D91A7181CB}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {FBAB32FB-5E8C-4C3D-A093-41D91A7181CB}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {FBAB32FB-5E8C-4C3D-A093-41D91A7181CB}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {F88FDDA0-61E9-4443-B397-98F4A57756CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {F88FDDA0-61E9-4443-B397-98F4A57756CC}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {F88FDDA0-61E9-4443-B397-98F4A57756CC}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {F88FDDA0-61E9-4443-B397-98F4A57756CC}.Release|Any CPU.Build.0 = Release|Any CPU 52 | EndGlobalSection 53 | GlobalSection(SolutionProperties) = preSolution 54 | HideSolutionNode = FALSE 55 | EndGlobalSection 56 | GlobalSection(NestedProjects) = preSolution 57 | {4B420DF8-F26E-420C-A883-D0BBEB40C518} = {54A44306-76B1-4F15-8525-B1F4432CAB6A} 58 | {9CD13130-C383-4319-AE6A-80A7B3406D40} = {54A44306-76B1-4F15-8525-B1F4432CAB6A} 59 | {C5421989-6A04-4AB3-8DF0-F34780EA6EDD} = {3658C7D5-1622-4D5A-A639-C20067DBB28D} 60 | {EFDD6784-02AD-42CF-AD05-754282F0466C} = {54A44306-76B1-4F15-8525-B1F4432CAB6A} 61 | {FBAB32FB-5E8C-4C3D-A093-41D91A7181CB} = {54A44306-76B1-4F15-8525-B1F4432CAB6A} 62 | {F88FDDA0-61E9-4443-B397-98F4A57756CC} = {54A44306-76B1-4F15-8525-B1F4432CAB6A} 63 | EndGlobalSection 64 | GlobalSection(ExtensibilityGlobals) = postSolution 65 | SolutionGuid = {91B8ADEF-B82A-4904-8C40-06FEC3152D95} 66 | EndGlobalSection 67 | EndGlobal 68 | -------------------------------------------------------------------------------- /src/ConventionalOptions.Autofac/AutofacOptionsServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using Autofac; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.Options; 8 | 9 | namespace ConventionalOptions.Autofac 10 | { 11 | public static class AutofacOptionsServiceCollectionExtensions 12 | { 13 | /// 14 | /// Registers base option types with the Autofac Container. 15 | /// 16 | /// The intent of this method is to provide registration of base option types 17 | /// when used outside the context of an Asp.Net Core Web application as 18 | /// use of Autofac with web apps would typically use the AddOptions() extension 19 | /// method and rely upon Autofac's Populate() method to register services from 20 | /// the ServiceCollection. 21 | /// 22 | /// 23 | /// 24 | public static ContainerBuilder AddOptions(this ContainerBuilder builder) 25 | { 26 | if (builder == null) 27 | { 28 | throw new ArgumentNullException(nameof(builder)); 29 | } 30 | 31 | builder.RegisterGeneric(typeof(OptionsManager<>)).As(typeof(IOptions<>)).SingleInstance(); 32 | builder.RegisterGeneric(typeof(OptionsManager<>)).As(typeof(IOptionsSnapshot<>)).InstancePerLifetimeScope(); 33 | builder.RegisterGeneric(typeof(OptionsMonitor<>)).As(typeof(IOptionsMonitor<>)).SingleInstance(); 34 | builder.RegisterGeneric(typeof(OptionsFactory<>)).As(typeof(IOptionsFactory<>)).InstancePerDependency(); 35 | builder.RegisterGeneric(typeof(OptionsCache<>)).As(typeof(IOptionsMonitorCache<>)).SingleInstance(); 36 | return builder; 37 | } 38 | 39 | public static ContainerBuilder RegisterOptionsFromAssemblies(this ContainerBuilder builder, params Assembly[] assemblies) 40 | { 41 | var types = assemblies.SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.Name.EndsWith("Options"))).ToList(); 42 | 43 | foreach (var t in types) builder.RegisterOptions(t); 44 | 45 | return builder; 46 | } 47 | 48 | public static ContainerBuilder RegisterOptions(this ContainerBuilder builder) 49 | { 50 | builder.RegisterOptions(typeof(TOptions)); 51 | 52 | return builder; 53 | } 54 | 55 | public static ContainerBuilder RegisterOptions(this ContainerBuilder builder, Type optionsType) 56 | { 57 | RegisterChangeTokenSource(builder, optionsType); 58 | RegisterConfigureOptions(builder, optionsType); 59 | RegisterOptionsInternal(builder, optionsType); 60 | 61 | return builder; 62 | } 63 | 64 | static void RegisterOptionsInternal(ContainerBuilder builder, Type optionsType) 65 | { 66 | builder.Register(context => 67 | { 68 | var genericType = typeof(IOptions<>).MakeGenericType(optionsType); 69 | var options = context.Resolve(genericType); 70 | return genericType.GetProperty("Value").GetValue(options); 71 | } 72 | ).As(optionsType).SingleInstance(); 73 | } 74 | 75 | static void RegisterConfigureOptions(ContainerBuilder builder, Type optionsType) 76 | { 77 | var configurationOptionsInterfaceType = typeof(IConfigureOptions<>).MakeGenericType(optionsType); 78 | 79 | builder.Register(context => 80 | { 81 | var configuration = context.Resolve(); 82 | var configurationOptionsType = typeof(ConfigureFromConfigurationSectionOptions<>).MakeGenericType(optionsType); 83 | var configurationOptionsTypeInstance = Activator.CreateInstance(configurationOptionsType, configuration); 84 | 85 | return configurationOptionsTypeInstance; 86 | }).As(configurationOptionsInterfaceType).SingleInstance(); 87 | } 88 | 89 | static void RegisterChangeTokenSource(ContainerBuilder builder, Type optionsType) 90 | { 91 | var optionsChangeTokenSourceInterfaceType = typeof(IOptionsChangeTokenSource<>).MakeGenericType(optionsType); 92 | 93 | builder.Register(context => 94 | { 95 | var configuration = context.Resolve(); 96 | var changeTokenSourceType = typeof(ConfigurationChangeTokenSource<>).MakeGenericType(optionsType); 97 | var configurationChangeTokenSourceInstance = Activator.CreateInstance(changeTokenSourceType, Options.DefaultName, configuration); 98 | return configurationChangeTokenSourceInstance; 99 | }).As(optionsChangeTokenSourceInterfaceType).SingleInstance(); 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.StructureMap/StructureMapOptionsServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.Options; 7 | using StructureMap; 8 | 9 | namespace ConventionalOptions.StructureMap 10 | { 11 | public static class StructureMapOptionsServiceCollectionExtensions 12 | { 13 | /// 14 | /// Registers base option types with the StructureMap Container. 15 | /// 16 | /// The intent of this method is to provide registration of base option types 17 | /// when used outside the context of an Asp.Net Core Web application as 18 | /// use of StructureMap with web apps would typically use the AddOptions() extension 19 | /// method and rely upon StructureMap's Populate() method to register services from 20 | /// the ServiceCollection. 21 | /// 22 | /// 23 | /// 24 | public static ConfigurationExpression AddOptions(this ConfigurationExpression configurationExpression) 25 | { 26 | if (configurationExpression == null) 27 | { 28 | throw new ArgumentNullException(nameof(configurationExpression)); 29 | } 30 | 31 | configurationExpression.For(typeof(IOptions<>)).Use(typeof(OptionsManager<>)).Singleton(); 32 | configurationExpression.For(typeof(IOptionsSnapshot<>)).Use(typeof(OptionsManager<>)).ContainerScoped(); 33 | configurationExpression.For(typeof(IOptionsMonitor<>)).Use(typeof(OptionsMonitor<>)).Singleton(); 34 | configurationExpression.For(typeof(IOptionsFactory<>)).Use(typeof(OptionsFactory<>)).Transient(); 35 | configurationExpression.For(typeof(IOptionsMonitorCache<>)).Use(typeof(OptionsCache<>)).Singleton(); 36 | 37 | return configurationExpression; 38 | } 39 | 40 | public static ConfigurationExpression RegisterOptionsFromAssemblies(this ConfigurationExpression configurationExpression, params Assembly[] assemblies) 41 | { 42 | var types = assemblies.SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.Name.EndsWith("Options"))).ToList(); 43 | 44 | foreach (var t in types) configurationExpression.RegisterOptions(t); 45 | 46 | return configurationExpression; 47 | } 48 | 49 | public static ConfigurationExpression RegisterOptions(this ConfigurationExpression configurationExpression, IConfiguration configuration) 50 | { 51 | configurationExpression.RegisterOptions(typeof(TOptions)); 52 | 53 | return configurationExpression; 54 | } 55 | 56 | public static ConfigurationExpression RegisterOptions(this ConfigurationExpression configurationExpression, Type optionsType) 57 | { 58 | RegisterChangeTokenSource(configurationExpression, optionsType); 59 | RegisterConfigureOptions(configurationExpression, optionsType); 60 | RegisterOptionsInternal(configurationExpression, optionsType); 61 | 62 | return configurationExpression; 63 | } 64 | 65 | static void RegisterOptionsInternal(ConfigurationExpression configurationExpression, Type optionsType) 66 | { 67 | configurationExpression.For(optionsType).Use($"Build ${optionsType}", context => 68 | { 69 | var genericType = typeof(IOptions<>).MakeGenericType(optionsType); 70 | var options = context.GetInstance(genericType); 71 | return genericType.GetProperty("Value").GetValue(options); 72 | }).Singleton(); 73 | } 74 | 75 | static void RegisterConfigureOptions(ConfigurationExpression configurationExpression, Type optionsType) 76 | { 77 | var configurationOptionsInterfaceType = typeof(IConfigureOptions<>).MakeGenericType(optionsType); 78 | 79 | configurationExpression.For(configurationOptionsInterfaceType).Use($"Build ${configurationOptionsInterfaceType}", 80 | context => 81 | { 82 | var configuration = context.GetInstance(); 83 | var configurationOptionsType = typeof(ConfigureFromConfigurationSectionOptions<>).MakeGenericType(optionsType); 84 | var configurationOptionsTypeInstance = Activator.CreateInstance(configurationOptionsType, configuration); 85 | 86 | return configurationOptionsTypeInstance; 87 | }).Singleton(); 88 | } 89 | 90 | static void RegisterChangeTokenSource(ConfigurationExpression registry, Type optionsType) 91 | { 92 | var optionsChangeTokenSourceInterfaceType = typeof(IOptionsChangeTokenSource<>).MakeGenericType(optionsType); 93 | 94 | registry.For(optionsChangeTokenSourceInterfaceType).Use($"Build ${optionsChangeTokenSourceInterfaceType}", 95 | context => 96 | { 97 | var configuration = context.GetInstance(); 98 | var changeTokenSourceType = typeof(ConfigurationChangeTokenSource<>).MakeGenericType(optionsType); 99 | var configurationChangeTokenSourceInstance = Activator.CreateInstance(changeTokenSourceType, Options.DefaultName, configuration); 100 | return configurationChangeTokenSourceInstance; 101 | }).Singleton(); 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/Specifications/NinjectConventionalOptionsSpecs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using ConventionalOptions.Ninject; 4 | using ConventionalOptions.Specs.Model; 5 | using ExpectedObjects; 6 | using Machine.Specifications; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.Options; 9 | using Ninject; 10 | using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; 11 | 12 | namespace ConventionalOptions.Specs.Specifications 13 | { 14 | class NinjectConventionalOptionsSpecs 15 | { 16 | [Subject("Ninject")] 17 | class when_resolving_options_from_the_container 18 | { 19 | static ExpectedObject _expectedOptions; 20 | static TestOptions _actualOptions; 21 | static IKernel _container; 22 | 23 | Establish context = () => 24 | { 25 | var configuration = new ConfigurationBuilder() 26 | .AddInMemoryCollection(new Dictionary 27 | { 28 | {"Test:StringProperty", "TestValue"}, 29 | {"Test:IntProperty", "2"} 30 | }).Build(); 31 | 32 | _container = new StandardKernel(); 33 | _container.Bind().ToConstant(configuration); 34 | _container.AddOptions().RegisterOptions(); 35 | 36 | _expectedOptions = new 37 | { 38 | StringProperty = "TestValue", 39 | IntProperty = 2 40 | }.ToExpectedObject(); 41 | }; 42 | 43 | Because of = () => _actualOptions = _container.Get(); 44 | 45 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 46 | } 47 | 48 | [Subject("Ninject")] 49 | class when_resolving_auto_registered_options_from_the_container 50 | { 51 | static ExpectedObject _expectedOptions; 52 | static TestOptions _actualOptions; 53 | static IKernel _container; 54 | 55 | Establish context = () => 56 | { 57 | var configuration = new ConfigurationBuilder() 58 | .AddInMemoryCollection(new Dictionary 59 | { 60 | {"Test:StringProperty", "TestValue"}, 61 | {"Test:IntProperty", "2"} 62 | }).Build(); 63 | 64 | _container = new StandardKernel(); 65 | _container.Bind().ToConstant(configuration); 66 | _container.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 67 | 68 | _expectedOptions = new 69 | { 70 | StringProperty = "TestValue", 71 | IntProperty = 2 72 | }.ToExpectedObject(); 73 | }; 74 | 75 | Because of = () => _actualOptions = _container.Get(); 76 | 77 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 78 | } 79 | 80 | [Subject("Ninject")] 81 | class when_resolving_an_auto_registered_options_wrapper_from_the_container 82 | { 83 | static ExpectedObject _expectedOptions; 84 | static TestOptions _actualOptions; 85 | static IKernel _container; 86 | 87 | Establish context = () => 88 | { 89 | var configuration = new ConfigurationBuilder() 90 | .AddInMemoryCollection(new Dictionary 91 | { 92 | {"Test:StringProperty", "TestValue"}, 93 | {"Test:IntProperty", "2"} 94 | }).Build(); 95 | 96 | _container = new StandardKernel(); 97 | _container.Bind().ToConstant(configuration); 98 | _container.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 99 | 100 | _expectedOptions = new 101 | { 102 | StringProperty = "TestValue", 103 | IntProperty = 2 104 | }.ToExpectedObject(); 105 | }; 106 | 107 | Because of = () => _actualOptions = _container.Get>().Value; 108 | 109 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 110 | } 111 | 112 | [Subject("Ninject")] 113 | class when_resolving_an_auto_registered_options_snapshot_from_the_container 114 | { 115 | static ExpectedObject _expectedOptions; 116 | static TestOptions _actualOptions; 117 | static IKernel _container; 118 | 119 | Establish context = () => 120 | { 121 | var configuration = new ConfigurationBuilder() 122 | .AddInMemoryCollection(new Dictionary 123 | { 124 | {"Test:StringProperty", "TestValue"}, 125 | {"Test:IntProperty", "2"} 126 | }).Build(); 127 | 128 | _container = new StandardKernel(); 129 | _container.Bind().ToConstant(configuration); 130 | _container.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 131 | 132 | _expectedOptions = new 133 | { 134 | StringProperty = "TestValue", 135 | IntProperty = 2 136 | }.ToExpectedObject(); 137 | }; 138 | 139 | Because of = () => _actualOptions = _container.Get>().Value; 140 | 141 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 142 | } 143 | 144 | [Subject("Ninject")] 145 | class when_resolving_an_auto_registered_options_monitor_from_the_container 146 | { 147 | static ExpectedObject _expectedOptions; 148 | static TestOptions _actualOptions; 149 | static IKernel _container; 150 | 151 | Establish context = () => 152 | { 153 | var configuration = new ConfigurationBuilder() 154 | .AddInMemoryCollection(new Dictionary 155 | { 156 | {"Test:StringProperty", "TestValue"}, 157 | {"Test:IntProperty", "2"} 158 | }).Build(); 159 | 160 | _container = new StandardKernel(); 161 | _container.Bind().ToConstant(configuration); 162 | _container.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 163 | 164 | _expectedOptions = new 165 | { 166 | StringProperty = "TestValue", 167 | IntProperty = 2 168 | }.ToExpectedObject(); 169 | }; 170 | 171 | Because of = () => _actualOptions = _container.Get>().CurrentValue; 172 | 173 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 174 | } 175 | } 176 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | 290 | # Diff/Merge files 291 | **/*.orig 292 | 293 | # Backup files 294 | **/*.bak 295 | 296 | # Ignore VSCode folder 297 | **/.vscode 298 | 299 | # Ignore npm build output 300 | dist/ 301 | build/ 302 | 303 | # Ignore unused generated cert files 304 | scripts/orchestration/secrets/client-ca1-signed.crt 305 | scripts/orchestration/secrets/client.csr 306 | scripts/orchestration/secrets/client.der 307 | scripts/orchestration/secrets/client.key 308 | scripts/orchestration/secrets/client.keystore.p12 309 | scripts/orchestration/secrets/client.pem 310 | scripts/orchestration/secrets/client_keystore_creds 311 | scripts/orchestration/secrets/client_sslkey_creds 312 | scripts/orchestration/secrets/client_truststore_creds 313 | scripts/orchestration/secrets/kafka.client.keystore.jks 314 | scripts/orchestration/secrets/kafka.client.truststore.jks 315 | scripts/orchestration/secrets/kafka1-ca1-signed.crt 316 | scripts/orchestration/secrets/kafka1.csr 317 | scripts/orchestration/secrets/kafka1.der 318 | scripts/orchestration/secrets/kafka1.key 319 | scripts/orchestration/secrets/kafka1.keystore.p12 320 | scripts/orchestration/secrets/kafka1.pem 321 | scripts/orchestration/secrets/snakeoil-ca-1.crt 322 | scripts/orchestration/secrets/snakeoil-ca-1.key 323 | scripts/orchestration/secrets/snakeoil-ca-1.srl 324 | -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/Specifications/StructureMapConventionalOptionsSpecs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using ConventionalOptions.Specs.Model; 4 | using ConventionalOptions.StructureMap; 5 | using ExpectedObjects; 6 | using Machine.Specifications; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.Options; 9 | using StructureMap; 10 | using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; 11 | 12 | namespace ConventionalOptions.Specs.Specifications 13 | { 14 | class StructureMapConventionalOptionsSpecs 15 | { 16 | [Subject("StructureMap")] 17 | class when_resolving_options_from_the_container 18 | { 19 | static ExpectedObject _expectedOptions; 20 | static TestOptions _actualOptions; 21 | static IContainer _container; 22 | 23 | Establish context = () => 24 | { 25 | var configuration = new ConfigurationBuilder() 26 | .AddInMemoryCollection(new Dictionary 27 | { 28 | {"Test:StringProperty", "TestValue"}, 29 | {"Test:IntProperty", "2"} 30 | }).Build(); 31 | 32 | _container = new Container(x => 33 | { 34 | x.For().Use(configuration); 35 | x.AddOptions().RegisterOptions(configuration); 36 | }); 37 | 38 | _expectedOptions = new 39 | { 40 | StringProperty = "TestValue", 41 | IntProperty = 2 42 | }.ToExpectedObject(); 43 | }; 44 | 45 | Because of = () => _actualOptions = _container.GetInstance(); 46 | 47 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 48 | } 49 | 50 | [Subject("StructureMap")] 51 | class when_resolving_auto_registered_options_from_the_container 52 | { 53 | static ExpectedObject _expectedOptions; 54 | static TestOptions _actualOptions; 55 | static IContainer _container; 56 | 57 | Establish context = () => 58 | { 59 | var configuration = new ConfigurationBuilder() 60 | .AddInMemoryCollection(new Dictionary 61 | { 62 | {"Test:StringProperty", "TestValue"}, 63 | {"Test:IntProperty", "2"} 64 | }).Build(); 65 | 66 | _container = new Container(x => 67 | { 68 | x.For().Use(configuration); 69 | x.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 70 | }); 71 | 72 | _expectedOptions = new 73 | { 74 | StringProperty = "TestValue", 75 | IntProperty = 2 76 | }.ToExpectedObject(); 77 | }; 78 | 79 | Because of = () => _actualOptions = _container.GetInstance(); 80 | 81 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 82 | } 83 | 84 | [Subject("StructureMap")] 85 | class when_resolving_an_auto_registered_options_wrapper_from_the_container 86 | { 87 | static ExpectedObject _expectedOptions; 88 | static TestOptions _actualOptions; 89 | static IContainer _container; 90 | 91 | Establish context = () => 92 | { 93 | var configuration = new ConfigurationBuilder() 94 | .AddInMemoryCollection(new Dictionary 95 | { 96 | {"Test:StringProperty", "TestValue"}, 97 | {"Test:IntProperty", "2"} 98 | }).Build(); 99 | 100 | _container = new Container(x => 101 | { 102 | x.For().Use(configuration); 103 | x.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 104 | }); 105 | 106 | _expectedOptions = new 107 | { 108 | StringProperty = "TestValue", 109 | IntProperty = 2 110 | }.ToExpectedObject(); 111 | }; 112 | 113 | Because of = () => _actualOptions = _container.GetInstance>().Value; 114 | 115 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 116 | } 117 | 118 | [Subject("StructureMap")] 119 | class when_resolving_an_auto_registered_options_snapshot_from_the_container 120 | { 121 | static ExpectedObject _expectedOptions; 122 | static TestOptions _actualOptions; 123 | static IContainer _container; 124 | 125 | Establish context = () => 126 | { 127 | var configuration = new ConfigurationBuilder() 128 | .AddInMemoryCollection(new Dictionary 129 | { 130 | {"Test:StringProperty", "TestValue"}, 131 | {"Test:IntProperty", "2"} 132 | }).Build(); 133 | 134 | _container = new Container(x => 135 | { 136 | x.For().Use(configuration); 137 | x.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 138 | }); 139 | 140 | _expectedOptions = new 141 | { 142 | StringProperty = "TestValue", 143 | IntProperty = 2 144 | }.ToExpectedObject(); 145 | }; 146 | 147 | Because of = () => _actualOptions = _container.GetInstance>().Value; 148 | 149 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 150 | } 151 | 152 | [Subject("StructureMap")] 153 | class when_resolving_an_auto_registered_options_monitor_from_the_container 154 | { 155 | static ExpectedObject _expectedOptions; 156 | static TestOptions _actualOptions; 157 | static IContainer _container; 158 | 159 | Establish context = () => 160 | { 161 | var configuration = new ConfigurationBuilder() 162 | .AddInMemoryCollection(new Dictionary 163 | { 164 | {"Test:StringProperty", "TestValue"}, 165 | {"Test:IntProperty", "2"} 166 | }).Build(); 167 | 168 | _container = new Container(x => 169 | { 170 | x.For().Use(configuration); 171 | x.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 172 | }); 173 | 174 | _expectedOptions = new 175 | { 176 | StringProperty = "TestValue", 177 | IntProperty = 2 178 | }.ToExpectedObject(); 179 | }; 180 | 181 | Because of = () => _actualOptions = _container.GetInstance>().CurrentValue; 182 | 183 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 184 | } 185 | } 186 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/Specifications/AutofacConventionalOptionsSpecs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Autofac; 4 | using Autofac.Core; 5 | using ConventionalOptions.Autofac; 6 | using ConventionalOptions.Specs.Model; 7 | using ExpectedObjects; 8 | using Machine.Specifications; 9 | using Microsoft.Extensions.Configuration; 10 | using Microsoft.Extensions.Options; 11 | using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; 12 | 13 | namespace ConventionalOptions.Specs.Specifications 14 | { 15 | class AutofacConventionalOptionsSpecs 16 | { 17 | [Subject("Autofac")] 18 | class when_resolving_options_from_the_container 19 | { 20 | static ExpectedObject _expectedOptions; 21 | static TestOptions _actualOptions; 22 | static IContainer _container; 23 | 24 | Establish context = () => 25 | { 26 | var configuration = new ConfigurationBuilder() 27 | .AddInMemoryCollection(new Dictionary 28 | { 29 | {"Test:StringProperty", "TestValue"}, 30 | {"Test:IntProperty", "2"} 31 | }).Build(); 32 | 33 | var builder = new ContainerBuilder(); 34 | builder.RegisterInstance(configuration).As(); 35 | builder.AddOptions().RegisterOptions(); 36 | _container = builder.Build(); 37 | 38 | _expectedOptions = new 39 | { 40 | StringProperty = "TestValue", 41 | IntProperty = 2 42 | }.ToExpectedObject(); 43 | }; 44 | 45 | Because of = () => _actualOptions = _container.Resolve(); 46 | 47 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 48 | } 49 | 50 | [Subject("Autofac")] 51 | class when_resolving_an_auto_registered_options_from_the_container 52 | { 53 | static ExpectedObject _expectedOptions; 54 | static TestOptions _actualOptions; 55 | static IContainer _container; 56 | 57 | Establish context = () => 58 | { 59 | var configuration = new ConfigurationBuilder() 60 | .AddInMemoryCollection(new Dictionary 61 | { 62 | { "Test:StringProperty", "TestValue" }, 63 | { "Test:IntProperty", "2" } 64 | }).Build(); 65 | 66 | var builder = new ContainerBuilder(); 67 | builder.RegisterInstance(configuration).As(); 68 | builder.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 69 | _container = builder.Build(); 70 | 71 | _expectedOptions = new 72 | { 73 | StringProperty = "TestValue", 74 | IntProperty = 2 75 | }.ToExpectedObject(); 76 | }; 77 | 78 | Because of = () => _actualOptions = _container.Resolve(); 79 | 80 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 81 | } 82 | 83 | [Subject("Autofac")] 84 | class when_resolving_an_auto_registered_options_wrapper_from_the_container 85 | { 86 | static ExpectedObject _expectedOptions; 87 | static TestOptions _actualOptions; 88 | static IContainer _container; 89 | 90 | Establish context = () => 91 | { 92 | var configuration = new ConfigurationBuilder() 93 | .AddInMemoryCollection(new Dictionary 94 | { 95 | {"Test:StringProperty", "TestValue"}, 96 | {"Test:IntProperty", "2"} 97 | }).Build(); 98 | 99 | var builder = new ContainerBuilder(); 100 | builder.RegisterInstance(configuration).As(); 101 | builder.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 102 | _container = builder.Build(); 103 | 104 | _expectedOptions = new 105 | { 106 | StringProperty = "TestValue", 107 | IntProperty = 2 108 | }.ToExpectedObject(); 109 | }; 110 | 111 | Because of = () => _actualOptions = _container.Resolve>().Value; 112 | 113 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 114 | } 115 | 116 | [Subject("Autofac")] 117 | class when_resolving_an_auto_registered_options_snapshot_from_the_container 118 | { 119 | static ExpectedObject _expectedOptions; 120 | static TestOptions _actualOptions; 121 | static IContainer _container; 122 | 123 | Establish context = () => 124 | { 125 | var configuration = new ConfigurationBuilder() 126 | .AddInMemoryCollection(new Dictionary 127 | { 128 | {"Test:StringProperty", "TestValue"}, 129 | {"Test:IntProperty", "2"} 130 | }).Build(); 131 | 132 | var builder = new ContainerBuilder(); 133 | builder.RegisterInstance(configuration).As(); 134 | builder.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 135 | _container = builder.Build(); 136 | 137 | _expectedOptions = new 138 | { 139 | StringProperty = "TestValue", 140 | IntProperty = 2 141 | }.ToExpectedObject(); 142 | }; 143 | 144 | Because of = () => _actualOptions = _container.Resolve>().Value; 145 | 146 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 147 | } 148 | 149 | [Subject("Autofac")] 150 | class when_resolving_an_auto_registered_options_monitor_from_the_container 151 | { 152 | static ExpectedObject _expectedOptions; 153 | static TestOptions _actualOptions; 154 | static IContainer _container; 155 | 156 | Establish context = () => 157 | { 158 | var configuration = new ConfigurationBuilder() 159 | .AddInMemoryCollection(new Dictionary 160 | { 161 | {"Test:StringProperty", "TestValue"}, 162 | {"Test:IntProperty", "2"} 163 | }).Build(); 164 | 165 | var builder = new ContainerBuilder(); 166 | builder.RegisterInstance(configuration).As(); 167 | builder.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 168 | _container = builder.Build(); 169 | 170 | _expectedOptions = new 171 | { 172 | StringProperty = "TestValue", 173 | IntProperty = 2 174 | }.ToExpectedObject(); 175 | }; 176 | 177 | Because of = () => _actualOptions = _container.Resolve>().CurrentValue; 178 | 179 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 180 | } 181 | } 182 | } -------------------------------------------------------------------------------- /src/ConventionalOptions.Specs/Specifications/LamarConventionalOptionsSpecs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using ConventionalOptions.DependencyInjection; 4 | using ConventionalOptions.Specs.Model; 5 | using ExpectedObjects; 6 | using Lamar; 7 | using Machine.Specifications; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.DependencyInjection; 10 | using Microsoft.Extensions.Options; 11 | using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; 12 | 13 | namespace ConventionalOptions.Specs.Specifications 14 | { 15 | class LamarConventionalOptionsSpecs 16 | { 17 | [Subject("Lamar")] 18 | class when_resolving_options_from_the_container 19 | { 20 | static ExpectedObject _expectedOptions; 21 | static TestOptions _actualOptions; 22 | static IContainer _container; 23 | 24 | Establish context = () => 25 | { 26 | var configuration = new ConfigurationBuilder() 27 | .AddInMemoryCollection(new Dictionary 28 | { 29 | {"Test:StringProperty", "TestValue"}, 30 | {"Test:IntProperty", "2"} 31 | }).Build(); 32 | 33 | _container = new Container(services => 34 | { 35 | services.AddSingleton(configuration); 36 | services.AddOptions().RegisterOptions(configuration); 37 | }); 38 | 39 | _expectedOptions = new 40 | { 41 | StringProperty = "TestValue", 42 | IntProperty = 2 43 | }.ToExpectedObject(); 44 | }; 45 | 46 | Because of = () => _actualOptions = _container.GetInstance(); 47 | 48 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 49 | } 50 | 51 | [Subject("Lamar")] 52 | class when_resolving_auto_registered_options_from_the_container 53 | { 54 | static ExpectedObject _expectedOptions; 55 | static TestOptions _actualOptions; 56 | static IContainer _container; 57 | 58 | Establish context = () => 59 | { 60 | var configuration = new ConfigurationBuilder() 61 | .AddInMemoryCollection(new Dictionary 62 | { 63 | {"Test:StringProperty", "TestValue"}, 64 | {"Test:IntProperty", "2"} 65 | }).Build(); 66 | 67 | _container = new Container(services => 68 | { 69 | services.AddSingleton(configuration); 70 | services.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 71 | }); 72 | 73 | _expectedOptions = new 74 | { 75 | StringProperty = "TestValue", 76 | IntProperty = 2 77 | }.ToExpectedObject(); 78 | }; 79 | 80 | Because of = () => _actualOptions = _container.GetInstance(); 81 | 82 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 83 | } 84 | 85 | [Subject("Lamar")] 86 | class when_resolving_an_auto_registered_options_wrapper_from_the_container 87 | { 88 | static ExpectedObject _expectedOptions; 89 | static TestOptions _actualOptions; 90 | static IContainer _container; 91 | 92 | Establish context = () => 93 | { 94 | var configuration = new ConfigurationBuilder() 95 | .AddInMemoryCollection(new Dictionary 96 | { 97 | {"Test:StringProperty", "TestValue"}, 98 | {"Test:IntProperty", "2"} 99 | }).Build(); 100 | 101 | _container = new Container(services => 102 | { 103 | services.AddSingleton(configuration); 104 | services.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 105 | }); 106 | 107 | _expectedOptions = new 108 | { 109 | StringProperty = "TestValue", 110 | IntProperty = 2 111 | }.ToExpectedObject(); 112 | }; 113 | 114 | Because of = () => _actualOptions = _container.GetInstance>().Value; 115 | 116 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 117 | } 118 | 119 | [Subject("Lamar")] 120 | class when_resolving_an_auto_registered_options_snapshot_from_the_container 121 | { 122 | static ExpectedObject _expectedOptions; 123 | static TestOptions _actualOptions; 124 | static IContainer _container; 125 | 126 | Establish context = () => 127 | { 128 | var configuration = new ConfigurationBuilder() 129 | .AddInMemoryCollection(new Dictionary 130 | { 131 | {"Test:StringProperty", "TestValue"}, 132 | {"Test:IntProperty", "2"} 133 | }).Build(); 134 | 135 | _container = new Container(services => 136 | { 137 | services.AddSingleton(configuration); 138 | services.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 139 | }); 140 | 141 | _expectedOptions = new 142 | { 143 | StringProperty = "TestValue", 144 | IntProperty = 2 145 | }.ToExpectedObject(); 146 | }; 147 | 148 | Because of = () => _actualOptions = _container.GetInstance>().Value; 149 | 150 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 151 | } 152 | 153 | [Subject("Lamar")] 154 | class when_resolving_an_auto_registered_options_monitor_from_the_container 155 | { 156 | static ExpectedObject _expectedOptions; 157 | static TestOptions _actualOptions; 158 | static IContainer _container; 159 | 160 | Establish context = () => 161 | { 162 | var configuration = new ConfigurationBuilder() 163 | .AddInMemoryCollection(new Dictionary 164 | { 165 | {"Test:StringProperty", "TestValue"}, 166 | {"Test:IntProperty", "2"} 167 | }).Build(); 168 | 169 | _container = new Container(services => 170 | { 171 | services.AddSingleton(configuration); 172 | services.AddOptions().RegisterOptionsFromAssemblies(Assembly.GetExecutingAssembly()); 173 | }); 174 | 175 | _expectedOptions = new 176 | { 177 | StringProperty = "TestValue", 178 | IntProperty = 2 179 | }.ToExpectedObject(); 180 | }; 181 | 182 | Because of = () => _actualOptions = _container.GetInstance>().CurrentValue; 183 | 184 | It should_resolve_the_expected_options = () => _expectedOptions.ShouldMatch(_actualOptions); 185 | } 186 | } 187 | } --------------------------------------------------------------------------------