Skip to content

Commit 2078340

Browse files
Options pattern CSAT (#18335)
* Options pattern CSAT * Options pattern CSAT * work * work * work * work * work * work * work * work * work * work * work * work * work * work * work * work * work * work * Apply suggestions from code review Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * work * work * work * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/fundamentals/configuration/options.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * work Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com>
1 parent d3d4985 commit 2078340

72 files changed

Lines changed: 40482 additions & 335 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aspnetcore/fundamentals/configuration/index.md

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,41 +75,9 @@ The default <xref:Microsoft.Extensions.Configuration.Json.JsonConfigurationProvi
7575

7676
<a name="optpat"></a>
7777

78-
#### Bind hierarchical configuration data using the options pattern
78+
### Bind hierarchical configuration data using the options pattern
7979

80-
The preferred way to read related configuration values is using the [options pattern](xref:fundamentals/configuration/options). For example, to read the following configuration values:
81-
82-
```json
83-
"Position": {
84-
"Title": "Editor",
85-
"Name": "Joe Smith"
86-
}
87-
```
88-
89-
Create the following `PositionOptions` class:
90-
91-
[!code-csharp[](index/samples/3.x/ConfigSample/Options/PositionOptions.cs?name=snippet)]
92-
93-
All the public read-write properties of the type are bound. Fields are ***not*** bound.
94-
95-
The following code:
96-
97-
* Calls [ConfigurationBinder.Bind](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Bind*) to bind the `PositionOptions` class to the `Position` section.
98-
* Displays the `Position` configuration data.
99-
100-
[!code-csharp[](index/samples/3.x/ConfigSample/Pages/Test22.cshtml.cs?name=snippet)]
101-
102-
[`ConfigurationBinder.Get<T>`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Get*) binds and returns the specified type. `ConfigurationBinder.Get<T>` may be more convenient than using `ConfigurationBinder.Bind`. The following code shows how to use `ConfigurationBinder.Get<T>` with the `PositionOptions` class:
103-
104-
[!code-csharp[](index/samples/3.x/ConfigSample/Pages/Test21.cshtml.cs?name=snippet)]
105-
106-
An alternative approach when using the ***options pattern*** is to bind the `Position` section and add it to the [dependency injection service container](xref:fundamentals/dependency-injection). In the following code, `PositionOptions` is added to the service container with <xref:Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure*> and bound to configuration:
107-
108-
[!code-csharp[](index/samples/3.x/ConfigSample/Startup.cs?name=snippet)]
109-
110-
Using the preceding code, the following code reads the position options:
111-
112-
[!code-csharp[](index/samples/3.x/ConfigSample/Pages/Test2.cshtml.cs?name=snippet)]
80+
[!INCLUDE[](~/includes/bind.md)]
11381

11482
Using the [default](#default) configuration, the *appsettings.json* and *appsettings.*`Environment`*.json* files are enabled with [reloadOnChange: true](https://github.com/dotnet/extensions/blob/release/3.1/src/Hosting/Hosting/src/Host.cs#L74-L75). Changes made to the *appsettings.json* and *appsettings.*`Environment`*.json* file ***after*** the app starts are read by the [JSON configuration provider](#jcp).
11583

@@ -174,7 +142,7 @@ To test that the preceding commands override *appsettings.json* and *appsettings
174142

175143
Call <xref:Microsoft.Extensions.Configuration.EnvironmentVariablesExtensions.AddEnvironmentVariables*> with a string to specify a prefix for environment variables:
176144

177-
[!code-csharp[](index/samples/3.x/ConfigSample/Program.cs?name=snippet4&highlight=12)]
145+
[!code-csharp[](~/fundamentals/configuration/index/samples/3.x/ConfigSample/Program.cs?name=snippet4&highlight=12)]
178146

179147
In the preceding code:
180148

@@ -708,12 +676,36 @@ The following code displays configuration data in a Razor Page:
708676

709677
[!code-cshtml[](index/samples/3.x/ConfigSample/Pages/Test5.cshtml)]
710678

679+
In the following code, `MyOptions` is added to the service container with <xref:Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure*> and bound to configuration:
680+
681+
[!code-csharp[](~/fundamentals/configuration/options/samples/3.x/OptionsSample/Startup3.cs?name=snippet_Example2)]
682+
683+
The following markup uses the [`@inject`](xref:mvc/views/razor#inject) Razor directive to resolve and display the options values:
684+
685+
[!code-cshtml[](~/fundamentals/configuration/options/samples/3.x/OptionsSample/Pages/Test3.cshtml)]
686+
711687
## Access configuration in a MVC view file
712688

713689
The following code displays configuration data in a MVC view:
714690

715691
[!code-cshtml[](index/samples/3.x/ConfigSample/Views/Home2/Index.cshtml)]
716692

693+
## Configure options with a delegate
694+
695+
Options configured in a delegate override values set in the configuration providers.
696+
697+
Configuring options with a delegate is demonstrated as Example 2 in the sample app.
698+
699+
In the following code, an <xref:Microsoft.Extensions.Options.IConfigureOptions%601> service is added to the service container. It uses a delegate to configure values for `MyOptions`:
700+
701+
[!code-csharp[](~/fundamentals/configuration/options/samples/3.x/OptionsSample/Startup2.cs?name=snippet_Example2)]
702+
703+
The following code displays the options values:
704+
705+
[!code-csharp[](options/samples/3.x/OptionsSample/Pages/Test2.cshtml.cs?name=snippet)]
706+
707+
In the preceding example, the values of `Option1` and `Option2` are specified in *appsettings.json* and then overridden by the configured delegate.
708+
717709
<a name="hvac"></a>
718710

719711
## Host versus app configuration

aspnetcore/fundamentals/configuration/index/samples/3.x/ConfigSample/Options/PositionOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#region snippet
44
public class PositionOptions
55
{
6+
public const string Position = "Position";
7+
68
public string Title { get; set; }
79
public string Name { get; set; }
810
}

aspnetcore/fundamentals/configuration/index/samples/3.x/ConfigSample/Pages/Test21.cshtml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public Test21Model(IConfiguration configuration)
1717
}
1818

1919
public ContentResult OnGet()
20-
{
21-
positionOptions = Configuration.GetSection("Position").Get<PositionOptions>();
20+
{
21+
positionOptions = Configuration.GetSection(PositionOptions.Position)
22+
.Get<PositionOptions>();
2223

2324
return Content($"Title: {positionOptions.Title} \n" +
2425
$"Name: {positionOptions.Name}");

aspnetcore/fundamentals/configuration/index/samples/3.x/ConfigSample/Pages/Test22.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Test22Model(IConfiguration configuration)
1818
public ContentResult OnGet()
1919
{
2020
var positionOptions = new PositionOptions();
21-
Configuration.GetSection("Position").Bind(positionOptions);
21+
Configuration.GetSection(PositionOptions.Position).Bind(positionOptions);
2222

2323
return Content($"Title: {positionOptions.Title} \n" +
2424
$"Name: {positionOptions.Name}");

aspnetcore/fundamentals/configuration/index/samples/3.x/ConfigSample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
3030

3131
// Use this to test reading config keys in Startup
3232
// And anything else that doesn't add
33-
// services.Configure<PositionOptions>(Configuration.GetSection("Position"));
33+
// services.Configure<PositionOptions>(Configuration.GetSection(PositionOptions.Position));
3434
// to startup
3535
// remove comments from webBuilder.UseStartup<ConfigSampleKey.Startup>();
3636
#if MAIN2

aspnetcore/fundamentals/configuration/index/samples/3.x/ConfigSample/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public Startup(IConfiguration configuration)
1919
#region snippet
2020
public void ConfigureServices(IServiceCollection services)
2121
{
22-
services.Configure<PositionOptions>(Configuration.GetSection("Position"));
22+
services.Configure<PositionOptions>(Configuration.GetSection(
23+
PositionOptions.Position));
2324
services.AddRazorPages();
2425
}
2526
#endregion

0 commit comments

Comments
 (0)