Newbie question: ValidationRules.CheckRules();

Newbie question: ValidationRules.CheckRules();

Old forum URL: forums.lhotka.net/forums/t/4633.aspx


starchild posted on Tuesday, April 08, 2008

This is a very newbie question since I haven't seen others with the same problem and hence I guess this should just work fine if i'd followed the rules...

Anyway, I have a Customer-BO. In my application I have a form users can insert a customer.

Her are a couple of snippets from the Customer-BO:

private string _firstName = string.Empty;

public string FirstName
{
get
{
   
return _firstName;
}
set
{
   _firstName =
value;
}
}

protected override void AddBusinessRules()
{
   
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "FirstName");
}

[RunLocal()]
private void DataPortal_Create(Criteria criteria)
{
   ValidationRules.CheckRules();
}

When clicking Save in the form, following code will run:

Customer customer = Customer.NewCustomer();
customer.FirstName = firstName;
customer.Save();

What I want is to make alle the validationchecks on the saving of data. What happens now is problably that the validationchecks run on instantination of the customer-object(Customer.NewCustomer()) and then BEFORE customer.FirstName = firstName - because I get the message FirstName requires.
I've tried to move the ValidationRules.CheckRules(); to the Insert/Update-code, but then the Checks won't run at all no matter if FirstName-string contains a value or not.

But if I place the validation on the set-section of FirstName like this ValidationRules.CheckRules("FirstName");
it works exactly how I want it to. But due to performanceissues etc. I want to do alle the validations on one place.

Could anyone give me some hints here?

Thanks in advance!! :-)

JoeFallon1 replied on Tuesday, April 08, 2008

Your Property Set is not correct.

public string FirstName
{
get
{
   
return _firstName;
}
set
{

  If _firstName <> Value Then
{
     _firstName =
value;
   
PropertyHasChanged("FirstName");
}


}
}

By calling PropertyHasChanged, your Validation rules will also run.

Joe

 

starchild replied on Wednesday, April 09, 2008

Thanks for your reply. But that actually doesn't solve my problem. I want to validate all rules at the same time(on save) by using ValidationRules.CheckRules();.

richardb replied on Wednesday, April 09, 2008

The propertyhaschanged method tells the the object that something has changed and it's now Dirty.  So it's good to do that.

Im pretty sure tThe Save method does check the rules automatically for you.

But you can do this if you want

If customer.IsValid Then
   customer.Save
Else........

starchild replied on Wednesday, April 09, 2008

I will try customer.IsValid. Thank you!

skagen00 replied on Wednesday, April 09, 2008

customer.IsValid won't re-run your rules by default. Just identify if any rules are broken (as would be maintained in my prior post from object instantiation to rule checking as things change)

starchild replied on Wednesday, April 09, 2008

Thank you for clearifying this for me. I will skip the CheckRules() on the Save-section and rather to the ruleschecks on the set-part of each attribute.

 

skagen00 replied on Wednesday, April 09, 2008

Validation.CheckRules() when trying to save tends to be an inefficient way to do this to be honest with you.

When an object is new, typically ValidationRules.CheckRules() is run to create broken rules on the object, mainly for things such as "required fields", etc. When an object is fetched, I'd wager most people do not call ValidationRules.CheckRules() (there's a bit of personal preference in this one) -- the thinking is that if you've successfully saved your object, it was valid. So when you retrieve it, it should theoretically still be valid. This could change if the rules change or if something else affects your object externally to it.

As the object changes, you trigger very specific rules to be checked by PropertyHasChanged being run. Which rules run is dependent on your code in AddBusinessRules - you tie rules to properties. It's inefficient to run all the rules every time something changes, so PropertyHasChanged works to identify the rules mainly associated with that property and checks them.

By keeping your object in an accurate state as far as validity and broken rules go, there is very little reason for you to need to call ValidationRules.CheckRules prior to a save. There may be very targeted rules you might want to check prior to saving, but in very few conditions would you ever want to just call ValidationRules.CheckRules() in a parameterless fashion for an object prior to saving.

Perhaps there's something you're omitting, but I suspect you may wish to get a better feel for the best practices of CSLA in terms of rules and object validity. I think what you're wanting to do may be misguided.

JMHO,

Chris Hlusak

 

Copyright (c) Marimer LLC