Skip to content

Commit d179551

Browse files
IEvangelistBillWagnergewarren
authored
Added rationale section per #22886 (#22907)
* Added rationale section per #22886 * Apply suggestions from code review Co-authored-by: Bill Wagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 914740e commit d179551

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

docs/core/extensions/options.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Options pattern in .NET
33
author: IEvangelist
44
description: Learn how to use the options pattern to represent groups of related settings in .NET apps.
55
ms.author: dapine
6-
ms.date: 01/21/2021
6+
ms.date: 02/18/2021
77
---
88

99
# Options pattern in .NET
@@ -101,11 +101,19 @@ In the preceding code, changes to the JSON configuration file after the app has
101101
- [Named options](#named-options-support-using-iconfigurenamedoptions)
102102
- [Reloadable configuration](#use-ioptionssnapshot-to-read-updated-data)
103103
- Selective options invalidation (<xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601>)
104-
104+
105105
<xref:Microsoft.Extensions.Options.IOptionsFactory%601> is responsible for creating new options instances. It has a single <xref:Microsoft.Extensions.Options.IOptionsFactory%601.Create%2A> method. The default implementation takes all registered <xref:Microsoft.Extensions.Options.IConfigureOptions%601> and <xref:Microsoft.Extensions.Options.IPostConfigureOptions%601> and runs all the configurations first, followed by the post-configuration. It distinguishes between <xref:Microsoft.Extensions.Options.IConfigureNamedOptions%601> and <xref:Microsoft.Extensions.Options.IConfigureOptions%601> and only calls the appropriate interface.
106106

107107
<xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601> is used by <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> to cache `TOptions` instances. The <xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601> invalidates options instances in the monitor so that the value is recomputed (<xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601.TryRemove%2A>). Values can be manually introduced with <xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601.TryAdd%2A>. The <xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601.Clear%2A> method is used when all named instances should be recreated on demand.
108108

109+
### Options interfaces benefits
110+
111+
Using a generic wrapper type gives you the ability to decouple the lifetime of the option from the DI container. The <xref:Microsoft.Extensions.Options.IOptions%601.Value?displayProperty=nameWithType> interface provides a layer of abstraction, including generic constraints, on your options type. This provides the following benefits:
112+
113+
- The evaluation of the `T` configuration instance is deferred to the accessing of <xref:Microsoft.Extensions.Options.IOptions%601.Value?displayProperty=nameWithType>, rather than when it is injected. This is important because you can consume the `T` option from various places and choose the lifetime semantics without changing anything about `T`.
114+
- When registering options of type `T`, you do not need to explicitly register the `T` type. This is a convenience when you're [authoring a library](options-library-authors.md) with simple defaults, and you don't want to force the caller to register options into the DI container with a specific lifetime.
115+
- From the perspective of the API, it allows for constraints on the type `T` (in this case, `T` is constrained to a reference type).
116+
109117
## Use IOptionsSnapshot to read updated data
110118

111119
When you use <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601>, options are computed once per request when accessed and are cached for the lifetime of the request. Changes to the configuration are read after the app starts when using configuration providers that support reading updated configuration values.

0 commit comments

Comments
 (0)