How to check Business Rules from a parent BO into a Child BO


Author
Message
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
I have a Parent BO and a Child BO (using a Chikd Dialog) . I am at  the point where I want to save the Parent and the child out to the Database and want to check the rules first. So I have code like:

if (this.ParentBO1.CheckRules(true) && this.childBO1.CheckRules(true) )
{
this.childBo1.Save();
this.parentBO1.Save();
MessageBox.Show("Document has been saved", "Kernel Software",MessageBoxButtons.OK, MessageBoxIcon.Information);
}


All works fine. But I want to put validation code into the Parent Business object which will be dependent on what's in the child buisness object. e.g. Particular Customers (on the Parent Bo)  can only purchase certain Products (on the child BO). So at the stage I am validating the Parent BO, I need to loop through the Child BO .  I would like to put this valididation code into the CheckRulesOnCurrentRow on the parent BO but am not clear on how I will access the Child Business Object on the Parent Business Object. ( It cant really be done from the ChildBO because the Customer could be changed after a whole pile of products have been added ).
Replies
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Hi Gerald,

I'd tend to be more modular. I.e. I'd likely have methods to enforce each of these rules and have each rule in the BO it is associated with. Then in the OnCheckRulesCurrentRow method, I'd call each of these rules. The reason is to decouple the data from the U.I.  It also is better encapsulation, as each BO implements its own rules. By putting Child rules in the parent, you'd break this rule (but then sometimes ya just need to break the rules...so nothing is set in stone). This means that even if you edit the child data independent of the parent data, it's rules are still enforced.   In other words I'd think of the this as a collection of rules, rather than one monolithic "rule".  Also note that the checking of data validity of the child items when the parent customer value changes isn't really happening at the same time as other validity checks (in OnCheckRulesCurrentRow); it is happening when the parent field changes. 

All Business Objects have a ParentBusinessObject property. For this to work, you need to setup a parent relationship on the BO itself. Then on instances of a BO, you can set the ParentBusinessObject to an instance of the parent BO.  There is also a ChildBusinessObjects property of a BusinessObject.  If there is only one child BO, then this is easy, it's the item. This gets set when the ParentBusinessObject is set on a child.  So, assuming you have both the parent BO and the Child BO on the form, and that you've configured the child BO's ParentRelationship and then set the ParentBusinessObject on the child instance on the form to the parent BO instance on the form, then:

'-- From within parent BO, access Child BO (assume only one child)
Dim childBO As MyChildBO = DirectCast(Me.ChildBusinessObjects(0), MyChildBO)

'-- From within the child BO, access the parent BO
Dim parentBO As MyParentBO = DirectCast(Me.ParentBusinessObject, MyParentBO)


Note that this code is in the BOs, but the ParentBusinessObject is set on the instance dropped on the form.

Another approach would be to create a custom property on the parent for this specific type of child and program against that.

Another long post. I hope it helps! BigGrin
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
Hi Greg.

From the purist point of view I can sort of see what you are saying.. but it will take me some time just to get into that way of thinking.

I cannot find a property called ChildBusinessObjects in the Buisness Object Properties . Does this have to be input in code only ? Also, I would appreciate it if you could translate your code (with the DirectCast line) to C# as I cannot find a Directcast C# command and am not sure of what the equivalent is.

I want to put this code into the CheckRulesOnCurrentRow of the ParentBusiness object..... I will need to spend more time reviewing your earlier replies and consider moving code to the Child Business Objects and the various events
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Not sure why I did it VB.NET...

DirectCast is a cast:

MyChildBO childBO = (MyChildBO)Me.ChildBusinessObjects[0];


Yes, ChildBusinessObjects is code only. It is a List<BusinessLayer> property. You don't set the children, just access them. This list is filled via the setting of the ParentBusinessObject on the children. Again, if you have just one child, then it'll be easy. If you have more than on child BO at a time, then you'd just need to check the type.

My ideas are just suggestions. There are many ways to get this done.  You'll figure out what works for you. I'm just trying to point out some things I've learned along the way that have really helped improve my code. BigGrin
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
Hi Greg

I have that working very well now checking the Child Business Object from the Parent. Many thanks for all your help.

I will definitely revisit this when I have a bit more time , and have a look at a more purist approach as per your suggestions
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Glad it helped and you have it working.  I'll be glad to continue the discussion and hear what you'll have learned by then regarding where to put validation/business rules. BigGrin
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search