| | | 1 | | namespace ProjectTemplate.Web.Extensions; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Gives list options bound from configuration replace semantics instead of append semantics. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// The configuration binder adds configured entries to a list that already holds code defaults, so a configured list |
| | | 8 | | /// could only ever extend the defaults and never remove one. A configured list replaces the defaults here instead. |
| | | 9 | | /// Blank entries are ignored, which lets a higher-precedence configuration source remove an inherited entry by |
| | | 10 | | /// overriding its index with an empty value. When the section has no entries, the code defaults are kept. |
| | | 11 | | /// </remarks> |
| | | 12 | | internal static class ConfigurationListBinding |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Replaces the contents of <paramref name="target"/> with the non-blank values configured in |
| | | 16 | | /// <paramref name="section"/>, when the section has any entries. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="target">The bound list to replace.</param> |
| | | 19 | | /// <param name="section">The configuration section that holds the list entries.</param> |
| | | 20 | | internal static void ReplaceWithConfiguredValues(List<string> target, IConfigurationSection section) |
| | | 21 | | { |
| | 416 | 22 | | ArgumentNullException.ThrowIfNull(target); |
| | 416 | 23 | | ArgumentNullException.ThrowIfNull(section); |
| | | 24 | | |
| | 416 | 25 | | List<string> configuredValues = []; |
| | 416 | 26 | | bool hasConfiguredEntries = false; |
| | | 27 | | |
| | 4518 | 28 | | foreach (IConfigurationSection child in section.GetChildren()) |
| | | 29 | | { |
| | 1843 | 30 | | hasConfiguredEntries = true; |
| | | 31 | | |
| | 1843 | 32 | | if (!string.IsNullOrWhiteSpace(child.Value)) |
| | | 33 | | { |
| | 1840 | 34 | | configuredValues.Add(child.Value); |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 416 | 38 | | if (!hasConfiguredEntries) |
| | | 39 | | { |
| | 4 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 412 | 43 | | target.Clear(); |
| | 412 | 44 | | target.AddRange(configuredValues); |
| | 412 | 45 | | } |
| | | 46 | | } |