in my Silverlight 4 project, I have a simple class implementing the interface like:
public interface IMyClass
{
string Name { get; set; }
anyotherclass Value { get; set; }
}
a parent class contains a collection of IMyClass elements like:
public class ParentClass
{
ObservableCollection<IMyClass> Children { get; }
}
Now I want to make sure, that IMyClass.Name is unique in the Children collection. The user can change the IMyClass.Name – so I need to validate if the name is already in the collection. I want to use the Silverlight exception and validation mechanism, my XAML-textbox looks like:
<TextBox Text="{Binding
ValidatesOnExceptions=true,
NotifyOnValidationError=true,
Mode=TwoWay,
Path=Name}"/>
So I need to check in the setter of the Name property like:
public string Name
{
get { return _name; }
set
{
if (value == _name)
return;
if (CollectionAlreadyContainsName(this, value))
throw new ArgumentException("The name already exists");
else
_name = value;
OnPropertyChanged("Name");
}
}
And here comes the problem: CollectionAlreadyContainsName needs to know the collection, but in an IMyClass-object I do not know in which collection I am. I would need to keep a reference to the parent like
public interface IMyClass
{
string Name { get; set; }
anyotherclass Value { get; set; }
ParentClass Parent { get; }
}
which tastes bad to me because of cross referencing and the need to asure that the parent is always set properly. So I look for another way to do this. One idea was to let the ParentClass listen to a NameChanged-event of IMyClass and reverting the name change if necessary. But this would not work with the mentioned Silverlight exception and validation mechanism.
Any ideas how to address this?
Thanks in advance,
Frank
You are giving the IMyClass the wrong responsibility. It doesn’t know about the collection and it shouldn’t.
It is the responsibility of the collection class to only add objects that differ in name.
So override/create an add method that does this and either make the name readonly or subscribe to the change of the name in the collection and throw an error there.
EDIT
Do not revert the property. That is not the responsibility of the collection.