471,355 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

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

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

public Dictionary<string, FileStreamDict
{
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

May 29 '07 #1
6 1698
Various options; you could expose the dictionary as the interface
IDictionary<string, FileStreamrather 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,FileStreamas a constructor
argument (meaning: it will work with any dictionary-esque object).

Marc
May 29 '07 #2
Thanks Marc for your fast reply. It helped a lot.
By the way, which base class(es) should my class
DictionaryExhibitor<TKey, TValueinherit from? Only
IDictionary<TKey, TValueor 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

On May 29, 4:48 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Various options; you could expose the dictionary as the interface
IDictionary<string, FileStreamrather 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,FileStreamas a constructor
argument (meaning: it will work with any dictionary-esque object).

Marc

May 31 '07 #3
Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValuewould 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
May 31 '07 #4
On May 31, 3:00 pm, daohuy....@gmail.com wrote:
Thanks Marc for your fast reply. It helped a lot.
By the way, which base class(es) should my class
DictionaryExhibitor<TKey, TValueinherit from? Only
IDictionary<TKey, TValueor 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

May 31 '07 #5
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

On May 31, 10:12 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValuewould 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

Jun 1 '07 #6
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

On May 31, 10:12 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValuewould 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

Jun 1 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Brett Romero | last post: by
2 posts views Thread by Shimon Sim | last post: by
44 posts views Thread by Zytan | last post: by
4 posts views Thread by Peter K | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.