C
cody
Hi!
I have a class Customer which contains Contacts in a ContactsCollection.
class Customer
{
...
public ContactsCollection Contacts
{
get {...}
}
}
class Contact
{
...
Contact(Customer cust)
{
cust.Contacts.Add(this);
}
public void Delete()
{
cust.Contacts.Remove(this);
}
}
The problem now is that the add and remove methods of the
ContactsCollection are public so I cannot ensure that somebody else
tries to remove a contact without deleting it or trying to add a contact
twice and so on.
Is there a way to prevent this so that nobody comes to the idea to call
add/remove manually? I do not want that add/remove is visible outside
these 3 classes.
I know I can throw an exception if somebody tries, or make add/remove
internal but in my assembly are hundreds of classes so internal so it
doesn't really feel right to me.
I wonder how other people get around this problem as there should be
many programs out there with a similar constellation, I suppose .)
I have a class Customer which contains Contacts in a ContactsCollection.
class Customer
{
...
public ContactsCollection Contacts
{
get {...}
}
}
class Contact
{
...
Contact(Customer cust)
{
cust.Contacts.Add(this);
}
public void Delete()
{
cust.Contacts.Remove(this);
}
}
The problem now is that the add and remove methods of the
ContactsCollection are public so I cannot ensure that somebody else
tries to remove a contact without deleting it or trying to add a contact
twice and so on.
Is there a way to prevent this so that nobody comes to the idea to call
add/remove manually? I do not want that add/remove is visible outside
these 3 classes.
I know I can throw an exception if somebody tries, or make add/remove
internal but in my assembly are hundreds of classes so internal so it
doesn't really feel right to me.
I wonder how other people get around this problem as there should be
many programs out there with a similar constellation, I suppose .)