Hi
I have searched and searched for a good sample on how to apply the static validation in 2.1 (in a correct way) but not found any good one.
Anyone who can point me to an C# example of that? (just so that I don't do anything wrong.....)
//andy
Hi Andy
I'm not much of a C# coder so bare over with the syntax. This is how I do it internally in an object (let's call it Person) in VB
Private Shared Function ValidateName(ByVal target As Person, ByVal e As RuleArgs) As Boolean
If (target._name.Trim.Length > 0) Then
e.Description = "Name is required!"
Return False
End If
Return True
End Function
I guess the C# syntax would be
private static bool ValidateName<T>(T target, RuleArgs e)
{
if (target._name.Length() <= 0)
{
e.Description = "Name is required!"
return false;
}
return true;
}
To add the rule in AddBusinessRules you use the following syntax:
VB
.AddRule(Of Person)(AddressOf ValidateName, "Name")
C#
ValidationRules.AddRule<Person>(ValidateName<Person>, "Name");
Hope this helps
/Henrik
Thanks
I should put it in the static contructor (if I am not wrong) do you know I should write that to make it correct?
or just simple
static MyCSLAObject()
ValidationRules.AddRule<Person>(ValidateName<Person>, "Name");
}
would that be correct?
Andy,
No you must override the AddBusinessRules method and add your rules in there. This method get's called automatically by the framework when an object is instantiated.
protected override void AddBusinessRules()
{
ValidationRules.AddRule<Person>(ValidateName<Person>, "Name");
}
I recommend that you download Rocky's ProjectTracker sample app, and have a look at the source files in the ProjectTracker.Library.
/Henrik
Aha.
Correct, Thanks. Now I get it .
I didn't get the difference between
AddBusinessRules()
and
AddInstanceBusinessRules()
until now.
Copyright (c) Marimer LLC