microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.25k stars 2.2k forks source link

reorder of dictionary values when loading from IConfiguration into a Dictionary<string,string> using bind #1420

Open 3GDXC opened 7 months ago

3GDXC commented 7 months ago

Configuration section "Menu": { "Home": "#", "General Feedback": "/Questionnaire?form=1", "Request Callback": "/Questionnaire?form=2", "Raise Complaint": "/Questionnaire?form=3" }

var menu = new Dictionary<string,string>(); configuration.Bind("Menu", menu);

foreach(var item in menu) Console.WriteLine($"KEY :{item.Key}, VALUE: {item.Value}");

--- OUTPUT KEY: General Feedback, VALUE: /Questionnaire?form=1 KEY: Home, VALUE: # KEY: Raise Complaint, VALUE: /Questionnaire?form=3 KEY: Request Callback, VALUE: /Questionnaire?form=2

Note the order of the values has changed, and this is a unexpected behavior which can cause issues; please allow options to prevent reordering of values during the binding

rahul-s-bhatt commented 6 months ago

Hey @3GDXC I'm not a contributor or anything, so apologies if this sounds out of the blue... But why is ordered matter here?

3GDXC commented 6 months ago

Hey @3GDXC I'm not a contributor or anything, so apologies if this sounds out of the blue... But why is ordered matter here?

the order is preserved as the configuration may be used for things like; menu items creation or middleware injection and doing so in the correct order would be wrong; a developer could of create a indexed object structure, but the behavior of config IMHO shouldn't introduce any ordering unless EXPLICITLY required by the developer.