IsDirty question

IsDirty question

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


albruan posted on Friday, December 08, 2006

I have a UserRole object that I want to add to my collection of UserRoles if there currently isn't an instance of the UserRole object for a particular UserID.  If there is an instance for a UserID, I want to simply update the UserRole object and persist it to the database.  Straightforward stuff.  The problem I'm having is that the BusinessListBase doesn't recognize the object as having been changed; consequently, it doesn't get persisted to the database.  How can I "force" the object to be dirty in order for me to save the change?

mr_lasseter replied on Friday, December 08, 2006

If the object does not exist call MarkNew, if the object does exist you can call MarkDirty or modify one of the Public Properties that implements PropertyHasChanged.

There is also an overloaded method of of Save that allows you to force an update to the database.  I am assuming you are using 2.1

Mike

albruan replied on Friday, December 08, 2006

My UserRoles object inherits from the BusinessListBase class and the UserRole objects from BusinessBase.

I noticed the overloaded method of Save in the BusinessBase class, but can't find anything similar to it in the BusinessListBase class.  Merely copying it over won't work since IsNew, MarkOld and MarkDirty aren't defined in the BusinessListBase class context either.  Since they aren't available there, I'm unable to call them from the UI code.

Any other suggestions?

mr_lasseter replied on Friday, December 08, 2006

In the IsDirty method of the BusinessListBase it checks to see if any children are dirty, if they are it returns true.  So it should be enough to mark the UserRole object as dirty for the list to be dirty. 
In addition, when you call save on the BusinessListBase it checks to see if the IsDirty flag is set to true.  If the flag is set to true then the DataPortal.Update of the BusinessListBase class is called.  In this method you should cycle through each child in the list and save the records if necessary.

Hope that helps.

albruan replied on Friday, December 08, 2006

I must be dense today or it's the pressure I'm under, but I can't set the IsDirty flag to true from the UI code nor is MarkDirty available in it for me to use.

ajj3085 replied on Friday, December 08, 2006

That's how it should be.

An object marks itself as dirty, the UI should not be able to directly modify that part of the state.

IsDirty is typically set in the property setters, via a call to PropertyHasChanged, which, besides raising a PropertyChanged event, updates the IsDirty flag and causes rules for that property to be run.

HTH
Andy

albruan replied on Friday, December 08, 2006

The new value is getting set in the property setter, but it still doesn't get persisted to the database.  First thing I do is check if the UserID already exists in the UserRoles object; if it doesn't, I perform an AddNew, set the values and Save.  If the UserID does exist, I return the UserRole object, modify the RoleID for it, and try to update the object.  Still, the UserRules object doesn't recognize that UserRole object is now dirty so the check for IsDirty in Update returns without doing anything.

I don't know why this is giving me so much trouble.  I have other updates throughout my application, but none of them inherit from the BusinessListBase class.  Maybe if I revert to inheriting from BusinessBase...

Wal972 replied on Friday, December 08, 2006

Have you set the business list to check the children's dirty status and have you set the child to MarkOld  in the fetch command ?

Just to clarify, is the new UserID being save to the database or is it the updated one saving correctly  or neither

albruan replied on Friday, December 08, 2006

New user roles are being persisted to the database; updated user roles weren't, but you got me pointed in the right direction.  As is the case so many times, and especially when time is tight, I couldn't see the tree for the forest.  In this case, I neglected to override the Fetch command so I could pass in the User ID.  Anyway, I have it working correctly now.  Thanks!

Wal972 replied on Sunday, December 10, 2006

Pleasure to be of assistance.

Copyright (c) Marimer LLC