question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use IReadonlyDictionary<TKey, TValue> instead of IEnumerable<KeyValuePair<TKey, TValue>>

See original GitHub issue

I’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.

  1. Duplicate key behavior is undefined for Log(IEnumerable) so using IReadOnlyDictionary averts that behavior
  2. In the Log(IEnumerable) it’s simpler to check for semantic fields e.g. Brave library checks for event key

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
gideonkorircommented, Oct 15, 2017

Maybe we also change string ISpan.GetBaggageItem(string key) to bool 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.

1reaction
gideonkorircommented, Nov 8, 2017

@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 return IEnumerable<string> and the 2nd API should be ISpan.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 using IReadOnlyDictionary far outweighs the deserialization costs.

Your comment about null is spot on 😃, if we agreed to change baggage items to IReadOnlyDictionary I’d probably change ISpanContext to:

public interface ISpanContext
{
  IReadOnlyDictionary<string,string> BaggageItems { get; }
}

The readonly property has the same semantics as the currently defined method.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found