Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 5f6ca39

Browse files
committed
Add new Bind overload
1 parent 971e992 commit 5f6ca39

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/Microsoft.Extensions.Configuration.Binder/ConfigurationBinder.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public static object Get(this IConfiguration configuration, Type type)
5656
return BindInstance(type, instance: null, config: configuration);
5757
}
5858

59+
/// <summary>
60+
/// Attempts to bind the given object instance to the configuration section specified by the key by matching property names against configuration keys recursively.
61+
/// </summary>
62+
/// <param name="configuration">The configuration instance to bind.</param>
63+
/// <param name="sectionKey">The key of the configuration section to bind.</param>
64+
/// <param name="instance">The object to bind.</param>
65+
public static void Bind(this IConfiguration configuration, string sectionKey, object instance)
66+
=> configuration.GetSection(sectionKey).Bind(instance);
67+
5968
/// <summary>
6069
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
6170
/// </summary>
@@ -79,49 +88,49 @@ public static void Bind(this IConfiguration configuration, object instance)
7988
/// </summary>
8089
/// <typeparam name="T">The type to convert the value to.</typeparam>
8190
/// <param name="configuration">The configuration.</param>
82-
/// <param name="key">The configuration key for the value to convert.</param>
91+
/// <param name="sectionKey">The key of the configuration section's value to convert.</param>
8392
/// <returns>The converted value.</returns>
84-
public static T GetValue<T>(this IConfiguration configuration, string key)
93+
public static T GetValue<T>(this IConfiguration configuration, string sectionKey)
8594
{
86-
return GetValue(configuration, key, default(T));
95+
return GetValue(configuration, sectionKey, default(T));
8796
}
8897

8998
/// <summary>
9099
/// Extracts the value with the specified key and converts it to type T.
91100
/// </summary>
92101
/// <typeparam name="T">The type to convert the value to.</typeparam>
93102
/// <param name="configuration">The configuration.</param>
94-
/// <param name="key">The configuration key for the value to convert.</param>
103+
/// <param name="sectionKey">The key of the configuration section's value to convert.</param>
95104
/// <param name="defaultValue">The default value to use if no value is found.</param>
96105
/// <returns>The converted value.</returns>
97-
public static T GetValue<T>(this IConfiguration configuration, string key, T defaultValue)
106+
public static T GetValue<T>(this IConfiguration configuration, string sectionKey, T defaultValue)
98107
{
99-
return (T)GetValue(configuration, typeof(T), key, defaultValue);
108+
return (T)GetValue(configuration, typeof(T), sectionKey, defaultValue);
100109
}
101110

102111
/// <summary>
103112
/// Extracts the value with the specified key and converts it to the specified type.
104113
/// </summary>
105114
/// <param name="configuration">The configuration.</param>
106115
/// <param name="type">The type to convert the value to.</param>
107-
/// <param name="key">The configuration key for the value to convert.</param>
116+
/// <param name="sectionKey">The key of the configuration section's value to convert.</param>
108117
/// <returns>The converted value.</returns>
109-
public static object GetValue(this IConfiguration configuration, Type type, string key)
118+
public static object GetValue(this IConfiguration configuration, Type type, string sectionKey)
110119
{
111-
return GetValue(configuration, type, key, defaultValue: null);
120+
return GetValue(configuration, type, sectionKey, defaultValue: null);
112121
}
113122

114123
/// <summary>
115124
/// Extracts the value with the specified key and converts it to the specified type.
116125
/// </summary>
117126
/// <param name="configuration">The configuration.</param>
118127
/// <param name="type">The type to convert the value to.</param>
119-
/// <param name="key">The configuration key for the value to convert.</param>
128+
/// <param name="sectionKey">The key of the configuration section's value to convert.</param>
120129
/// <param name="defaultValue">The default value to use if no value is found.</param>
121130
/// <returns>The converted value.</returns>
122-
public static object GetValue(this IConfiguration configuration, Type type, string key, object defaultValue)
131+
public static object GetValue(this IConfiguration configuration, Type type, string sectionKey, object defaultValue)
123132
{
124-
var value = configuration.GetSection(key).Value;
133+
var value = configuration.GetSection(sectionKey).Value;
125134
if (value != null)
126135
{
127136
return ConvertValue(type, value);

test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ public void CanBindIConfigurationSection()
111111
Assert.Null(options.Section.Value);
112112
}
113113

114+
[Fact]
115+
public void CanBindWithKeyOverload()
116+
{
117+
var dic = new Dictionary<string, string>
118+
{
119+
{"Section:Integer", "-2"},
120+
{"Section:Boolean", "TRUe"},
121+
{"Section:Nested:Integer", "11"},
122+
{"Section:Virtual", "Sup"}
123+
};
124+
var configurationBuilder = new ConfigurationBuilder();
125+
configurationBuilder.AddInMemoryCollection(dic);
126+
var config = configurationBuilder.Build();
127+
128+
var options = new DerivedOptions();
129+
config.Bind("Section", options);
130+
131+
Assert.True(options.Boolean);
132+
Assert.Equal(-2, options.Integer);
133+
Assert.Equal(11, options.Nested.Integer);
134+
Assert.Equal("Derived:Sup", options.Virtual);
135+
}
136+
114137
[Fact]
115138
public void CanBindIConfigurationSectionWithDerivedOptionsSection()
116139
{

0 commit comments

Comments
 (0)