Timeline for AutoDictionary for objects with a key property
Current License: CC BY-SA 3.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 21, 2016 at 18:52 | vote | accept | t3chb0t | ||
Jun 21, 2016 at 18:37 | comment | added | t3chb0t |
This becomes more and more interesting ;-) operator != cannot be applied to TKey and TKey when I try to key != _keySelector(value) I'll need to take a look into their source code how they do it.
|
|
Jun 21, 2016 at 18:29 | comment | added | Mathieu Guindon |
@t3chb0t I would reuse the validation method I mentioned the AutoDictionary(IDictionary<TKey, TItem> source, Func<TItem, TKey> keySelector) constructor overload would use, and throw the same exception if the key mismatches the value returned by the _keySelector for the specified value .
|
|
Jun 21, 2016 at 18:27 | comment | added | t3chb0t |
One more thing: IDictionary requires the Add(TKey key, TValue value) method but at this point there is no way to guarantee that the key actually comes from the value. I could ignore it and use the key-selector anyway but it wouldn't be right too. I think it will have to stay a workaround for the defect dictionary with the hardcoded KVP struct. I'll just change the name into keyed-collection or something similar.
|
|
Jun 21, 2016 at 18:12 | comment | added | t3chb0t |
Well, there isn't really a good name for it but it's a pity that the dictionary doesn't allow to exchange the KVP. You're right, by bad, it wasn't a good usage example. However you have the KVP problem in a loop when the element is a KVP or when adding elements you need to write .Add(item.SomeProperty, item) or dic[item.SomeProperty] = item ... oh I see I need to remove the setter or better override the + operator.
|
|
Jun 21, 2016 at 17:58 | comment | added | Mathieu Guindon |
Dic["foo1"] doesn't return a KVP AFAIK... and you can definitely implement ICollection<KeyValuePair<TKey, TValue>> on top of ICollection<TValue> - make it an explicit interface implementation if you don't want to expose these members directly.
|
|
Jun 21, 2016 at 17:57 | comment | added | Mathieu Guindon |
Then IMO what you have is more like a ISet<T> and the class name is misleading; the key is merely used as an index. What if one wanted to iterate the keys?
|
|
Jun 21, 2016 at 17:55 | comment | added | t3chb0t |
I prefer to use a dictionary like dic["foo1"].Bar rahter then Dic["foo1"].Value.Bar . Why is the value there? It doesn't help in any way.
|
|
Jun 21, 2016 at 17:53 | comment | added | t3chb0t |
As to CopyTo - I really forgot to implement it as I have never ever used it before ;-)
|
|
Jun 21, 2016 at 17:53 | comment | added | t3chb0t |
I cannot implement the IDictionary interface because it requires the KeyValuePair struct via the ICollection<KeyValuePair<TKey, TValue>> that is the problem. It's harcoded for this interface and you cannot override it. I has always bothered me in the .net that the dictionary is treated like a holy grail that cannot have any other implementation whatsoever :-( the same for the Value property. It's one of the most usless properties in .net that always comes inbetween the dictionary and the item especially if the item is an object.
|
|
Jun 21, 2016 at 16:20 | history | edited | Mathieu Guindon | CC BY-SA 3.0 |
added 237 characters in body
|
Jun 21, 2016 at 16:11 | history | answered | Mathieu Guindon | CC BY-SA 3.0 |