How to check business validation rules in the child objects of a parent object

How to check business validation rules in the child objects of a parent object

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

tikluganguly posted on Wednesday, December 01, 2010

Hi All,

I have written a function to check the validation in the children of a parent business object like this

public List<string> CheckRules()

{

//check rule for all its childs

foreach (var child in this.FieldManager.GetChildren())

{

//first check if the child is modelbase

var po = child as IPhoenixModelBase;

if (po != null)

{

po.CheckRules();

}

else

{

//check if it is a list

var list = child as IList;

foreach (var itm in list)

{

//if the item in list is model base then run the rules

var po1 = itm as IPhoenixModelBase;

if (po1 != null)

{

po1.CheckRules();

}

}

}

}

this.BusinessRules.CheckRules();

this.OnUnknownPropertyChanged();

List<string> msgs = new List<string>();

foreach (var rule in this.BrokenRulesCollection)

{

msgs.Add(rule.Description);

}

return msgs;

}

What this function is doing is taking a business object, then taking all of its childs and then recursively calling the checkrules method in them. This function is actually behaving a bit peculearly and for evry checkrules it is returning a validation completed in the viewmodel. But what I want is to get only one validation completed when all the businessrules are being checked.

I am sure I must be doing something wrong in here. So can you please do let me know what exactly I am doing wrong?

Regards

TIklu

tikluganguly replied on Friday, December 10, 2010

Hi All,

It seems like I was not been too clear about my requirement. I just need to know how can i at the same time validate both the parent business object and all its child business object.

Regards

Tiklu

RockfordLhotka replied on Friday, December 10, 2010

You need to decide which object(s) will become invalid if the rule breaks. That's where you need to put the rule.

So if the broken rule should appear in the UI on some parent object property, then the rule needs to go in the parent object. If the rule should appear in the UI on some child object property then the rule needs to go in the child object.

Once you've decided that, then it is a matter of technique.

If the rule should run in the parent object, trigger the rule by overriding OnChildChanged (that's how you know a child has changed).

If the rule should run in the child object, then trigger the rule on the child property like normal, and use the Parent property to interact with the parent.

Using the Parent property is much easier in CSLA 4 than earlier versions. In 3.8 and earlier you may need to implement your own internal or public Parent properties, because the CSLA property is protected. In CSLA 4 the Parent properties are public.

tikluganguly replied on Monday, December 13, 2010

Hi Rocky,

The idea is, I have a form with master details. Now the user can click pressing the add button and a new row will be added in the grid. As you can assume the grid is basically holding the CSLA child business objects of the parent object bound to the master view. Now when the user without making any changes to any of the forms hits the save button I want to display a validation error on the grid as well as the master view.

I hope I was been able to tell you about my requirement

Regards

Tiklu

RockfordLhotka replied on Monday, December 13, 2010

This is for a web application then? Smart client applications would tell the user about the error immediately, only a web app would make the user click Save to find out they did something wrong.

There are a whole set of things you can do for web apps, especially if you know you'll never use your business layer for anything except a web UI.

But in summary, what you'll probably want to do is put the rule in the parent - attach it to the Children property. Then override Save:

This will force all rules to run before the Save, and will result in an exception if any rules are broken. You can handle the exception and render the appropriate UI.

tikluganguly replied on Wednesday, December 15, 2010

Hi Rocky,

First of all thank you for your reply. And sorry for replying you back so late. My app is a silverlight 4 application. And I am using CSLA 4. Actually in my case the user is allowed to add as many rows to the grid as he wants. And if the user just keeps adding the rows and then hits the save button. I am getting able to only validate the parent object and not the child object.