patterncsharpModerate
Wrapping tuples to be used as keys in a Dictionary
Viewed 0 times
usedkeyswrappingdictionarytuples
Problem
I'm currently working on a project where I have to map some objects to several properties.
Basically I need a Dictionary with Tuples as keys and instances of MyClass as values.
As using tuples can quickly lead to an obscure (because of item1, item2... properties) and verbose code I decided to make a class to wrap the tuples.
That way I should take advantage of the Tuple class and avoid its inconvenients.
So I can write:
Instead of:
And look up the dictionary like this:
Is this implementation correct ? Is there a more proper way to achieve this ?
Basically I need a Dictionary with Tuples as keys and instances of MyClass as values.
As using tuples can quickly lead to an obscure (because of item1, item2... properties) and verbose code I decided to make a class to wrap the tuples.
class Key
{
private Tuple _impl;
public Key(string unitID, int address, int comPort, int id)
{
_impl = new Tuple(unitID, address, comPort, id);
}
public override int GetHashCode()
{
return _impl.GetHashCode();
}
public override bool Equals(object obj)
{
return _impl.Equals(obj);
}
public override string ToString()
{
return _impl.ToString();
}
}That way I should take advantage of the Tuple class and avoid its inconvenients.
So I can write:
Dictionary mapping = new Dictionary();Instead of:
Dictionary, MyClass> mapping = new Dictionary, MyClass>();And look up the dictionary like this:
Key key = new Key("abc", 1, 5, 5464);
MyClass myInstance = mapping[key];Is this implementation correct ? Is there a more proper way to achieve this ?
Solution
public Key(string unitID, int address, int comPort, int id)Clearly this class represents something much more specialized than an all-purpose "key". Name the type for what it stands for!
private Tuple _impl;The private field should be
readonly. But why don't you have this instead?private readonly string _unitId;
private readonly int _address;
private readonly int _comPort;
private readonly int _id;public override int GetHashCode()
{
return _impl.GetHashCode();
}Ideally your
GetHashCode implementation should rely on immutable fields. Your _impl field isn't marked readonly, which doesn't quite communicate that intent. Using actual fields and dropping the Tuple altogether, you can come up with a more standard GetHashCode implementation.That way I should take advantage of the Tuple class and avoid its inconvenients.
You're not getting anything out the
Tuple. Get rid of it, it's only obfuscating the class' implementation.Also, you're not instantiating your
Tuple the idiomatic way. This:_impl = new Tuple(unitID, address, comPort, id);Should be written like this and leverage generic type parameter inference:
_impl = Tuple.Create(unitID, address, comPort, id);Code Snippets
public Key(string unitID, int address, int comPort, int id)private Tuple<string, int, int, int> _impl;private readonly string _unitId;
private readonly int _address;
private readonly int _comPort;
private readonly int _id;public override int GetHashCode()
{
return _impl.GetHashCode();
}_impl = new Tuple<string, int, int, int>(unitID, address, comPort, id);Context
StackExchange Code Review Q#110570, answer score: 13
Revisions (0)
No revisions yet.