I have a interface which currently extends IDictionary<> (and an implementation which Extends Dictionary<>), but I want to have an implementation of this interface which does not allow entries to be added or removed (I want to allow existing entries to be changed though). I could just take the ReadOnlyCollection approach and throw NotSupportedException, but this feels a bit wrong.
Instead I'd like to break the interfaces up so I had one for the accessor bits and one for the mutator bits. This is all ok, except that to do this I ended up with something like this (most methods removed for brevity):
public interface IAccessor<TKey, TValue>
{
TValue this [TKey key] { get; set; }
}
and then my original interface became:
public interface IAttributeDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IAccessor<TKey, TValue>
{
new TValue this [TKey key] { get; set; }
}
And an implementation class defined as:
public class AttributeDictionary<TKey,TValue>: Dictionary<TKey, TValue>, IAttributeDictionary<TKey, TValue>
I had to make the indexer new to avoid ambiguities between the indexers in IDictionary
and IAccessor
. the real issue though is that the behaviour of the setter indexer on a Dictionary
is to create a new entry in the dictionary. As I want the IAccessor
interface to only allow entries to be modified and not created what should I do in the inmplementation of AttributeDictionary
? should I have an explicit implementation of the IAccessor
indexer method which first checks the given key is in the dictionary and throws an exception if not, or would having 2 indexers with different behaviour be a bad idea? Or should I ditch the indexer in the IAccessor
interface and just have a GetValue
and SetValue
methods instead and avoid the confusion?