All business logic in a root BO?All business logic in a root BO?
Old forum URL: forums.lhotka.net/forums/t/5050.aspx
jureleskovec posted on Wednesday, July 02, 2008
This is a general questions which arose as a problem in my project i don't know how to solve (properly).
REQUIREMENTS:
Let's say we have 3 BOs (classes):
- 'Root' class with properties 'P1' (of type Child1) and 'P2' (of type Child2)
- 'Child1' class with property 'Value'.
- 'Child2' with property 'Value'
When 'root.P1.Value' changes to true, the 'root.P2' should be set to some other 'XXX' object (maybe a new one).
Child1 class should also have the validation rule which includes also check of the 'root.P2.Value':
if (root.P2.Value)
{....}
Also (which is obvious), this validation method (in Child1) should also be triggered by the change of the 'root.P2.value'.
PROBLEM(S):
The first problem is that Child1 can't reference the root nor the root.P2 objects since this would result into a circular reference which as far as I know are in csla forbidden.
The second one is about the fact that validation method will invoke before the 'PropertyHasChanged' where the 'root.P2' is set when property 'Child1.Value' has changed -> validation method will be checkin the 'root.P2.Value' of the old 'root.P2'.
Thx for any help,
-- jure
triplea replied on Thursday, July 03, 2008
Well as you say in your subject why not move the logic from the child BOs to the parent? I would do the following:
- In OnPropertyChanged of P1 you raise an event say P1ValueChanged which 'Root' subscribes to.
- Write a handler in 'Root' for P1Valuechanged (dont forget to += in all appropriate locations) which does whatever you want it to do on Root.P2
- Write your validation rule on 'Root' rather than P1 or P2
That should do the trick?
jureleskovec replied on Thursday, July 03, 2008
Thanks, man
So, do you thing that in general all the business rules should be defined in root BO? In my case the Child1 should (is responsible for) actually create/update root.P2, or....
And about the validation method. If validation method validates values of properties of the Child1 class then it should be defined in the 'Child1':
- it should be called by the 'Child' object when needed (in Child_Create, ...).
- in WPF i have binding set to the 'root.P1' and I supose the only way the error provider can work is to have the validation method in the class of the 'root.P1'.
-- Jure
JoeFallon1 replied on Thursday, July 03, 2008
In general the root BO should have only its own rules plus those rules which require coordination between its children. The child BOs should validate themsleves internally most of the time.
Joe
triplea replied on Friday, July 04, 2008
Well Joe gave you the definition.
So, do you thing that in general all the business rules should be defined in root BO?
No and I will quote Joe: "the root BO should have only its own rules plus those rules which require coordination between its children"
In my case the Child1 should (is responsible for) actually create/update root.P2
No (again :-) ) in my view Child1 is responsible for generating the trigger for creating/updating Child2. Root is responsible of noticing the trigger went off and create/update Child2. Remember that Child2 "belongs" to Root so Root can access and modify it whereas Child1 can't and shouldn't.
Regarding the validation... From what I understood from your previous post, your validation goes something like this: If Root.Child1.Value = true then Root.Child2.Value = "something" (or anyway something liek that). This rule fits in Root. If you are purely validating Child1 properties and dont care about Child2 then indeed it fits in Child1.
Finally, I havent played too much with WPF yet so cant help you there. But from what I understand, yuor issue is how to notify the user that Child2 is invalid if the rule gets implemented in Root. Well you cant but since Child2 is set by your business rule/logic, you cant expect the user to do much about it since it would be a bug! So just get it right and dont worry about the error provider in this instance :-)
Anyway the above are just my view. I am sure you can take the points you need and make them work for you.
jureleskovec replied on Monday, July 07, 2008
Thanks folks, this makes sense but it is a complicated solution. It's a pitty circular references are not allowed.
As for validation rules, they are in general 'bound' to particular property. So they should be defined in class that has that property. My example is more complex because a validation method for some property takes into account also some property from another object.
-- Jure
Copyright (c) Marimer LLC