You need to ensure that an exception is thrown when color names are set to less than two characters. What should you do?
A.
Add the following method to the Color partial class in Model\Color.cs:
protected overrride void OnPropertyChanged(string property)
{
if (property == “Name” && this.Name.Length < 2)
throw new ArgumentOutOfRangeException(“Name must be at least two characters”);
}
B.
Add the following code segment to the ContosoEntities partial class in Model\ContosoEntities.cs:
public override in SaveChanges(System.Data.Objects.SaveOptions options)
{
var changes = this.ObjectStateManager.GetObjectSateEntries(System.Data.EntityState.Added);
foreach (var change in changes)
{
if (change.Entity is Color)
if (((Color)change.Entity.Name.Length < 2) throw new ArgumentException(“Name too short”);
}
return base.SaveChanges(options);
}
C.
Add the following attribute to the Name property of the Color class in the entity designer file:
[StringLength(256, MinimumLength = 2)]
D.
Add the following method to the Color partial class in Model\Color.cs:
protected overrride void OnPropertyChanging(string property)
{
if (property == “Name” && this.Name.Length < 2)
throw new ArgumentOutOfRangeException(“Name must be at least two characters”);
}
Isn’t D a better answer?
I do not think so – because this.Name in answer D would return the previous value
Good call.
C is better:
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx
PropertyChanged is too late to throw exception.
I take this back (C) – this attribute works only in MVC applications.