Use IReadonlyDictionary<TKey, TValue> instead of IEnumerable<KeyValuePair<TKey, TValue>>
See original GitHub issueI’m proposing that we change methods that use IEnumerable<KeyValuePair<TKey,TValue>>
to IReadonlyDictionary<TKey,TValue>
. I believe that using IReadonlyDictionary
maintains the semantics of an immutable map but is easier to work with e.g. we can use indexer map[key]
and also access Count
property.
If agreed we can make the following changes:
interface ISpanContext {
IReadonlyDictionary<string, string> BaggageItems { get; }
}
interface ISpan
{
Log(IReadonlyDictionary<string, string> fields);
Log(DateTimeOffset timestamp, IReadonlyDictinary<string, string> fields)
}
Using IReadonlyDictionary<TKey,TValue>
simplifies implementations e.g.
- Duplicate key behavior is undefined for
Log(IEnumerable)
so usingIReadOnlyDictionary
averts that behavior - In the
Log(IEnumerable)
it’s simpler to check for semantic fields e.g. Brave library checks forevent
key
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top Results From Across the Web
IReadOnlyDictionary<TKey,TValue> Interface
The IReadOnlyDictionary<TKey,TValue> interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order. The ...
Read more >c# - Extension methods for both IDictionary and ...
You only need one extension method. Redefine it as follows: public static TValue? GetNullableKey<TKey, TValue>(this ...
Read more >Dictionary<TKey,TValue>.IEnumerable<KeyValuePair< ...
Returns an enumerator that iterates through the collection.
Read more >Why does IDictionary not implement IReadOnlyDictionary
It actually means "a Dictionary that's at least readable (but perhaps also writable).
Read more >.NET collections part 2: Generics
The generic struct KeyValuePair<TKey,TValue> has two: the type of the key TKey, ... The main use case for generics is of course collections....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe we also change
string ISpan.GetBaggageItem(string key)
tobool ISpan.TryGetBaggageItem(string key, out string value)
. This makes explicit that the key could be missing and doesn’t require the client to check for nulls.@ndrwrbgs whilst the specification doesn’t explicitly tell us how to handle duplicate keys, I think that the
ISpan.GetBaggageItem
&ISpan.SetTag
implicitly expect single keys in the baggage items otherwise the 1st API should returnIEnumerable<string>
and the 2nd API should beISpan.AddTag
.The idea about the un-serialized baggage item is interesting but one could make an implementation of
IReadOnlyDictionary
that deserializes on access to any of it’s APIs. Also we expect the client to have as few baggage items as possible; I think the simplicity of usingIReadOnlyDictionary
far outweighs the deserialization costs.Your comment about
null
is spot on 😃, if we agreed to change baggage items toIReadOnlyDictionary
I’d probably changeISpanContext
to:The readonly property has the same semantics as the currently defined method.