public class Document : BusinessBase<Document>
{
#region Business Methods
Guid _id;
string _name;
Guid _folder;
/*
......... MORE members here
*/
public Guid FolderId
{ [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _folder;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (!_folder.Equals(value))
{
_folder = value;
PropertyHasChanged();
//ValidationRules.CheckRules();
}
}
}
protected override void AddBusinessRules()
{
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Name");
ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 150));
ValidationRules.AddRule<Document>(CheckForEmptyGuid, "FolderID");
}
private static bool CheckForEmptyGuid<T>(
T target, Csla.Validation.RuleArgs e) where T : Document
{
if ( target.FolderId.Equals(Guid.Empty) )
{
e.Description = "The value of the FolderId should not be empty ";
return false;
}
else
return true;
}
}
Document d = Document.NewDocument();
d.Name = "Test";
d.FolderId = Guid.NewGuid();
if (d.IsValid)
MessageBox.Show("Valid");
else
MessageBox.Show("Invalid - " + d.BrokenRulesCollection[0].Description);
//ValidationRules.CheckRules();line in Property setter everything will be OK. It seems that PropertyChanged() does not trigger CheckRules for some reason.
Copyright (c) Marimer LLC