-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document advanced configuration binding scenarios and limitations #51541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d464f39
Initial plan
Copilot 38a8015
Add comprehensive binding scenarios documentation with code examples
Copilot 961ae07
Address code review feedback: improve clarity and examples
Copilot 1ef0dca
human edits
gewarren 6bab73a
Apply suggestions from code review
gewarren d1ab9b0
Update package versions to 10.0.2 and add ai-usage metadata
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...sions/snippets/configuration/binding-scenarios/DictionaryBinding/DictionaryBinding.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.2" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.2" /> | ||
| </ItemGroup> | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| </Project> | ||
98 changes: 98 additions & 0 deletions
98
docs/core/extensions/snippets/configuration/binding-scenarios/DictionaryBinding/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| // <DictionaryWithCollectionValues> | ||
| IConfiguration config = new ConfigurationBuilder() | ||
| .AddInMemoryCollection() | ||
| .Build(); | ||
|
|
||
| config["Queue:0"] = "Value1"; | ||
| var dict = new Dictionary<string, string[]>() { { "Queue", new[] { "InitialValue" } } }; | ||
|
|
||
| Console.WriteLine("=== Dictionary Binding with Collection Values ==="); | ||
| Console.WriteLine($"Initially: {string.Join(", ", dict["Queue"])}"); | ||
|
|
||
| // In .NET 7+, binding extends the collection instead of replacing it. | ||
| config.Bind(dict); | ||
| Console.WriteLine($"After Bind: {string.Join(", ", dict["Queue"])}"); | ||
|
|
||
| config["Queue:1"] = "Value2"; | ||
| config.Bind(dict); | ||
| Console.WriteLine($"After 2nd Bind: {string.Join(", ", dict["Queue"])}"); | ||
| // </DictionaryWithCollectionValues> | ||
|
|
||
| Console.WriteLine(); | ||
|
|
||
| // <IReadOnlyCollections> | ||
| Console.WriteLine("=== IReadOnly* Types (NOT Directly Supported) ==="); | ||
|
|
||
| var readonlyConfig = new ConfigurationBuilder() | ||
| .AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["Settings:Values:0"] = "Item1", | ||
| ["Settings:Values:1"] = "Item2", | ||
| ["Settings:Values:2"] = "Item3", | ||
| }) | ||
| .Build(); | ||
|
|
||
| // This class uses List<string> for binding, exposes as IReadOnlyList<string>. | ||
| var settings = new SettingsWithReadOnly(); | ||
| readonlyConfig.GetSection("Settings").Bind(settings); | ||
|
|
||
| Console.WriteLine("Values bound to mutable List, exposed as IReadOnlyList:"); | ||
| foreach (var value in settings.ValuesReadOnly) | ||
| { | ||
| Console.WriteLine($" {value}"); | ||
| } | ||
| // </IReadOnlyCollections> | ||
|
|
||
| Console.WriteLine(); | ||
|
|
||
| // <ParameterizedConstructor> | ||
| Console.WriteLine("=== Parameterized Constructor Binding ==="); | ||
|
|
||
| var ctorConfig = new ConfigurationBuilder() | ||
| .AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["AppSettings:Name"] = "MyApp", | ||
| ["AppSettings:MaxConnections"] = "100", | ||
| ["AppSettings:Timeout"] = "30" | ||
| }) | ||
| .Build(); | ||
|
|
||
| // Binding to a type with a single parameterized constructor | ||
| var appSettings = ctorConfig.GetSection("AppSettings").Get<AppSettings>(); | ||
| if (appSettings != null) | ||
| { | ||
| Console.WriteLine($"Name: {appSettings.Name}"); | ||
| Console.WriteLine($"MaxConnections: {appSettings.MaxConnections}"); | ||
| Console.WriteLine($"Timeout: {appSettings.Timeout}"); | ||
| } | ||
| // </ParameterizedConstructor> | ||
|
|
||
| // <SettingsWithReadOnly> | ||
| class SettingsWithReadOnly | ||
| { | ||
| // Use mutable type for binding | ||
| public List<string> Values { get; set; } = []; | ||
|
|
||
| // Expose as read-only for consumers | ||
| public IReadOnlyList<string> ValuesReadOnly => Values; | ||
| } | ||
| // </SettingsWithReadOnly> | ||
|
|
||
| // <AppSettings> | ||
| // Immutable type with single parameterized constructor. | ||
| class AppSettings | ||
| { | ||
| public string Name { get; } | ||
| public int MaxConnections { get; } | ||
| public int Timeout { get; } | ||
|
|
||
| public AppSettings(string name, int maxConnections, int timeout) | ||
| { | ||
| Name = name; | ||
| MaxConnections = maxConnections; | ||
| Timeout = timeout; | ||
| } | ||
| } | ||
| // </AppSettings> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.