Project's Logo

stevedunn/bindingtodefaultablelist

0/100

This project, stevedunn/bindingtodefaultablelist, presents a solution to a common issue when binding collections from configuration files such as appsettings.json in .NET applications. The problem arises when you want to use default values if the configuration does not exist, but not have both the default values and the config values.

For instance, consider the MyImageProcessingOptions class with a ResizeWidths property of type IList<int>. If the config does not contain an entry for ResizeWidths, the default values should be used. However, if the config does contain an entry, those values should replace the defaults. The current behavior when binding collections is to append any values in the config to the collection created in the code.

To address this issue, the project proposes a hand-rolled DefaultableCollection<T> class that functions as an ICollection<T>. This class keeps track of whether the collection has been overridden by config values and clears the default items when config values are present. By declaring your collection as a DefaultableCollection<int> instead of IList<int>, you can achieve the desired behavior without requiring any Runtime API changes.

Project Information

Contribution Opportunities

  • Issues are available for contributions.
  • Project boards are active.
  • Wiki is available for documentation and guides.

License

No license information available.

Recent Contributors

SteveDunn's avatar

SteveDunn

4 Contributions