New Child and IsDirty always = True question

New Child and IsDirty always = True question

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


Pavel posted on Wednesday, January 20, 2010

Hi everybody,

I have a child collection.
As soon as I create a new child and add it to ChildParent, my Child  IsDirty=True even if child properties contain no value.

But here is my problem.
I want to check IsDirty status before processing my children before saving.

I do not want to read all property values, I assume then if for my Child IsDirty=False then it works for me.
But it always True.

How to resolve this issue?

thanks,

Pavel replied on Wednesday, January 20, 2010

Is MarkOld the only solution?

Marjon1 replied on Wednesday, January 20, 2010

MarkOld() on the child will work for you but probably isn't the best option. If you don't want your objects to be valid without the user entering in at least one value, then a validation rule should be added to enforce that rule. Otherwise there is nothing stopping a user typing in a value, then removing it and then still being able to save.

Your parent then should become invalid as well, if you are using managed properties.

ajj3085 replied on Thursday, January 21, 2010

You should probably read the first few chapters of the book.  In a nutshell though, IsDirty indicates if the object has been modified from what's in the database.  Since a new object by definition does not exist in the database, it's dirty.  What you want is IsValid, which is driven off of ValidationRules.

Pavel replied on Thursday, January 21, 2010

ajj3085:
You should probably read the first few chapters of the book.  In a nutshell though, IsDirty indicates if the object has been modified from what's in the database.  Since a new object by definition does not exist in the database, it's dirty.  What you want is IsValid, which is driven off of ValidationRules.


Sometimes it is a wrong assumption that a new object is always IsDirty=True.
I would see it Dirty only if any value changed or entered.
If object is empty I would not save it to a database anyway.

Another question is:
I mark my object as MarkOld(), but if user enters value and later removes it object will be IsDirty=True even if there is no value in any properties.
Validation is not working for me as empty or Null value accepted.
I would like to see it IsDirty=False.

I am a new to a CSLA, and sometimes reading a well written book cannot help to resolve an issue.
Experience is more than that.



Pavel replied on Thursday, January 21, 2010

Dan Billingsley:
Now, the subtle thing I hear you say is more just that you want your persistence behavior to be that an invalid item is ignored if it is New when a save is attempted.


Exactly, object available but users enter values as it required, as a result some Child in collection never touched.
I would prefer to check status IsDirty before doing anything to the child object.

I accept the CSLA rules and  now I am thinking how to go around. --))

ajj3085 replied on Thursday, January 21, 2010

Well, my explaination as to why you're seeing the IsDirty / IsValid is based on how Csla defines what dirty or valid means.  And if you want to know the reasoning behind those choices, your only option is the book.

If you are trying to require a property value be entered and its not, I would check your AddBusinessRules override and property definitions. 

Pavel replied on Thursday, January 21, 2010

ajj3085:


If you are trying to require a property value be entered and its not, I would check your AddBusinessRules override and property definitions. 



Unfortunately, an empty value accepted as well as any numeric value and I cannot use Validation to catch it.
Thanks for help and it looks like I have to write a few lines of codes. There is no a simple way to resolve my reqs.


Copyright (c) Marimer LLC