Hi!
I am totally new to CSLA (So might have missed something obvious) and have a question about validation of a BusinessBase object. The object contains an collection (BusinessListBase) and the buissness rule is that there need to be 1-n instances. So basicly I need to put the object in an invalid state if something is removed (if it was the last item) or if the object is new and nothing is added. How should I/would you go about doing that?
Thanks in advance
/Mikael Ohlsson
Edit:
At the moment I am overriding IsValid. So the question is really.. is there a nicer way?
Yes there is a nicer way.
Let's call your classes Person, Interests, Interest where we're defining a collection of interests for the person. Everyone has got to be interested in _something_, right?
On Person, you'll have a validation rule...
MustHaveAnInterest. It'll be false/invalid if Person.Interests.Count == 0.
You need to trigger this at the right time. Because you're adding it to the business rules of the root, you'll automatically trigger it upon new people being created.
However, when the list changes, you need to detect that and have broken rules establish themselves. We want, when the list changes of interests, to test that validation rule.
So, you can do this by saying Person.Interests.ListChanged += Interests_ListChangedHandler..... that is, subscribe to the interests list from the root object.
In the event handler, you can just say CheckRules("Interests"); where you've added that validation rule to the interests property (via AddBusinessRules) and then any time items are removed and added and such, you'll trigger that validation event and make the business object valid or not valid as needed.
Hope that helps,
Chris
Good Idea.
A followup question
Where would you wire up the event handler? Is there any problem loosing that handler through the wcf dataportal?
You'll need to rehook it in OnDeserialized.
I tend to hook up events in a private LinkEvents() method in the object that I call from the factory methods and OnDeserialized.
Copyright (c) Marimer LLC