Show only needed methods from a Dictionary<TKey, TValue> class member

D

daohuy.hua

The context is that I have a C# class named MainModel which has a
private Dictionary<string, FileStream> member named dict. I also have
a property Dict to access to this member:

public Dictionary<string, FileStream> Dict
{
get { return this.dict; }
}

The problem with this implementation is that I only want to give
access to some of the methods from the Dictionary class since I don't
want another class which has a MainModel instance to have full access
to the dict member and do whatever he wants to. What should I do in
this case?

I'm not an advanced programmer and I'm not too familiar with the
design patterns, so I would like to have some code to base on and some
explainations too.

What I would like to have is somekind of:

public DictionaryExhibitor<Dictionary<string, FileStream>> Dict
{
get { return DictionaryExhibitor<this.dict>; }
}

Please correct me if I'm wrong.

Thanks
WuFei
 
M

Marc Gravell

Various options; you could expose the dictionary as the interface
IDictionary<string, FileStream> rather than the concrete
Dictionary<string, FileStream>, but this doesn't gain you much since a
determined borker can still investiagte GetType() and cast etc...

What access *do* you want to expose? If you just want to expose the
values, then perhaps an indexer is the answer?

// (notepad code; untested)
public FileStream this[string key] {
get {return dict[key];}
}

Now the dictionary is not visible outside *at all*. If you want a
middle ground of access members, then yes some form of wrapper class
would seem in order - although it could probably be (using your terms)
DictionaryExhibitor<TKey,TValue> (i.e.
DictionaryExhibitor<string,FileStream>) rather than
DictionaryExhibitor<Dictionary<string,FileStream>>, and might take (as
an example) an IDictionary<string,FileStream> as a constructor
argument (meaning: it will work with any dictionary-esque object).

Marc
 
D

daohuy.hua

Thanks Marc for your fast reply. It helped a lot.
By the way, which base class(es) should my class
DictionaryExhibitor<TKey, TValue> inherit from? Only
IDictionary<TKey, TValue> or some other classes too?

DictionaryExhibitor<TKey, TValue> : IDictionary<TKey, TValue>

For those methods I want to hide, should I simply implement them by
putting an empty body? For example, the Clear() method:

public void Clear() { }

Thanks
WuFei
 
M

Marc Gravell

Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValue> would seem a
good candidate.

For instance, if you (for whatever reason) need to implement a
specific interface, then you can use "explicit implementation" for the
ones you want to hide, and perhaps throw a NotSupportedException or
InvalidOperationException. If it can't be used, there is no point
exposing it on the public class definition.

Marc
 
J

Jon Skeet [C# MVP]

Thanks Marc for your fast reply. It helped a lot.
By the way, which base class(es) should my class
DictionaryExhibitor<TKey, TValue> inherit from? Only
IDictionary<TKey, TValue> or some other classes too?

Potentially, nothing. If you only want to expose very limited access,
I'd just start from scratch. Work out what clients will need, and put
that into your class. If you find you've naturally implemented a whole
interface, it probably wouldn't hurt to add that to the list of
interfaces you actually implement.

Jon
 
D

daohuy.hua

Thanks Marc and Jon. It's one of my first time I'm writing an
interface, so I did want to ensure that I'm doing the right things.
Really appreciate to get answers from experimented people.

WuFei
 
D

daohuy.hua

Thanks Marc and Jon. Since I'm pretty new to C# and to design
patterns, I did want to make sure I'm doing the right things.
It's appreciated to get advices from experimented people.

WuFei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top