Using validation rule
Old forum URL: forums.lhotka.net/forums/t/2359.aspx
kids_pro posted on Friday, February 16, 2007
Dear Rocky,
I have had a problem when I tried to use business rule as follow:
protected override void AddBusinessRules() {
ValidationRules.AddRule(ValidatePaymentDate, "PayDate");
}
private bool ValidatePaymentDate(object target, Csla.Validation.RuleArgs e) {
/* prevent payment in future date */
if (_payDate.Date > DateTime.Today) {
e.Description = "Please do not make payment in the future date.";
return false;
}
return true;
}
Here is the factory method:
public static Payment GetPayment(Int32 payment_id) {
if (!CanGetObject())
throw new System.Security.SecurityException(
"You are not authorized to view payment information.");
return DataPortal.Fetch<Payment>(new Criteria(payment_id)); // error raised here ...
}
Error:
DataPortal.Fetch failed (System.InvalidOperationException: Invalid rule method (instance methods of the target object not allowed): ValidatePaymentDate
How is that happen?
RRorije replied on Friday, February 16, 2007
Hi,
Your problem is that you are trying to add an instance rule as a type rule.
If you have the 2.1 csla e-book you might check that because it explains why this is not possible.
In short there are 2 options to solve this.
1. Add this rule as an instance rule (using ValidationRules.AddInstanceRule).
2. Make this rule static.
Number 2 is preferred, because this rule then will be loaded once per AppDomain, I.e. it will be loaded once for all businessobjects. But check the book for a more in-depth discussion.
Copyright (c) Marimer LLC