Validation problem with NoDuplicates Rule in ProjectTrackerValidation problem with NoDuplicates Rule in ProjectTracker
Old forum URL: forums.lhotka.net/forums/t/7772.aspx
j055 posted on Tuesday, October 13, 2009
Hi
I hope I haven't missed something obvious here but it looks like the NoDuplicates Rule in the Role class of the Project Tracker may not work correctly. If I run this MSTest method the Assert fails when it should not.
[TestMethod()]
public void GetRolesTest()
{
var roles = Roles.GetRoles();
var role = roles[0];
role.Id = 2;
role = roles[1];
role.Id = 1;
WriteBrokenRules(roles);
Assert.IsTrue(roles.IsValid, "Add a dup number then correct it");
}
The second row is still marked as not valid.
Thoughts, suggestions appreciated.
Andrewj055 replied on Tuesday, October 13, 2009
I wish I had a better memory. I opened a similar topic about this a couple of months ago.
http://forums.lhotka.net/forums/post/34849.aspx
As the Project Tracker sample is used as an important reference it would be good if it could include an expert update to the Roles and Role classes (or base classes), to deal with this issue.
Thanks
Andrew
j055 replied on Wednesday, October 21, 2009
Hi
Decided to have a go at something myself. I had got something working based on some advice here:
http://forums.lhotka.net/forums/thread/34858.aspx
What I wanted to achieve is something which by default has no impact on existing classes but can be switched on and configured easily. I think this goes some way (hope it's self explanatory):
public interface IEditableBusinessObject : Csla.Core.IEditableBusinessObject
{
void CheckRules();
}
public abstract class BusinessBase : Csla.BusinessBase, IEditableBusinessObject where T : BusinessBase
{
#region IEditableBusinessObject Members
void IEditableBusinessObject.CheckRules()
{
CheckRules();
}
#endregion
internal virtual void CheckRules()
{
throw new NotImplementedException();
}
}
public abstract class BusinessListBase : Csla.BusinessListBase
where T : BusinessListBase
where C : IEditableBusinessObject
{
protected override void OnChildChanged(ChildChangedEventArgs e)
{
if (ValidateCollection)
{
var target = e.ChildObject;
CheckChildrenRules(target);
}
base.OnChildChanged(e);
}
private void CheckChildrenRules(object target)
{
foreach (var item in this)
if (!(ReferenceEquals(item, target)))
item.CheckRules();
}
protected override void RemoveItem(int index)
{
if (ValidateCollection)
{
var item = this[index];
base.RemoveItem(index);
CheckChildrenRules(item);
}
else
{
base.RemoveItem(index);
}
}
// Better name might be 'EnableCollectionValidation'
protected bool ValidateCollection { get; set; }
}
To enable add 'ValidateCollection = true' to derived BusinessListBase constructor and override 'CheckRules' in the derived BusinessBase, something like:
internal override void CheckRules()
{
ValidationRules.CheckRules(NumberProperty);
}
Comments please
Andrew
Copyright (c) Marimer LLC