-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
A pattern used in some libraries is to pass an anonymous object around to act as a key-value pairing. For example in Dapper
conn.ExecuteScalar<int>(@"select count(*) from dbo.some_table
where name=@name and date=@date",new{name="Joe",date=DateTime.Today})To enable this lightweight key-value pairing the library authors has to write somewhat annoying reflection based code to find the properties to get their name and values. The authors of said libraries also often are required to create delegates to parse this anonymous object to keep it quick.
Unfortunately, anonymous objects shipped without this feature. It would be nice to maybe with an alternative syntax allow declaring anonymous objects that implement IDictionary<string,object>.
Another alternative, could be basically a lightweight syntax to declare Dictionary literals. Array is nice and light, but dictionaries are pretty noisy. Even the "new" syntax is pretty lousy.
var new_style_dict = new Dictionary<string,object>{["name"]="joe",["date"]=DateTime.Today};
var old_style_dict = new Dictionary<string,object>{{"name","joe"},{"date",DateTime.Today}};
But it'd probably nice to keep it immutable so you don't have to worry about the parameter being changed by what you are calling.