OnPropertyChanged(int fieldIndexChanged) method to override?

Posts   
 
    
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 23-Dec-2005 20:00:32   

Is there any method that I can override on a particular entity so that I can do something when certain fields change.

like override OnPropertyChanged(int fieldChanged) { if(filedChaned == x) do something else do something else }

What I want to do is set an IsSynced property to false when a field is changed. I could override the save method by I do not don't want this behavior when only the IsRead property is changed (and perhaps others). In other words, if IsRead is the only property changed; I want to do a normal save. If any other property has changed I want to mark IsSynced false. What is the best way to do with (hopefully without overriding all OnXXXChanged methods).

I have seen the SetNewFieldValue method but overriding that in an entity will do no good cause the code is never called!

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 24-Dec-2005 06:07:05   

Sam,

Try using the IsDirty property.

Example:

if (Customer.Name.IsDirty)
{
do something;
}
else
{
do something else;
}
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Dec-2005 09:58:51   

Either override OnFieldValidateComplete() and in there check if the validationResult is true. If so, the field is set and you should set the field you want to set. You can also override PostFieldValueSetAction, and be sure to call the base method afterwards.

Another way, not-intrusive, is to bind to the EntityContentsChanged event and act accordingly in the handler.

Yet another way is in an IEntityValidator implementation, check the fields there. As your action always involves dirty entities this last option works as expected. In adapter, you can also override OnBeforeEntitySave in DataAccessAdapter to perform actions prior to save.

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 27-Dec-2005 16:34:15   

Paul.Lewis wrote:

Sam,

Try using the IsDirty property.

Example:

if (Customer.Name.IsDirty)
{
do something;
}
else
{
do something else;
}

Ahhh confused missed that here was an IsDirty on each property! I thought that is dirty was only an entity property.

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 27-Dec-2005 17:22:42   

The example paul gave isn't correct code, it's pseudocode. The flag is called 'IsChanged' and you can examine it in the Fields object:

bool isChanged = entity.Fields[index].IsChanged;

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 30-Dec-2005 19:20:39   

When I try to override the OnFieldValidateComplete method I get a stack overflow if I try to set any properties in it as it re-fires the event and gets caught in an infinite loop. Also, the overriding PostFieldValueSetAction doesn't seem to work as the code never seems to get called. Any ideas?


        protected override void OnFieldValidateComplete(IEntityField field, bool validationResult)
        {
            IsSynced = false;
            base.OnFieldValidateComplete(field, validationResult);
        }

Buffer overflow frowning


        protected override void PostFieldValueSetAction(bool fieldValueSet)
        {
            IsSynced = false;
            base.PostFieldValueSetAction(fieldValueSet);
        }

Never gets fired frowning

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 30-Dec-2005 20:26:25   

Sam wrote:

When I try to override the OnFieldValidateComplete method I get a stack overflow if I try to set any properties in it as it re-fires the event and gets caught in an infinite loop. Also, the overriding PostFieldValueSetAction doesn't seem to work as the code never seems to get called. Any ideas?


        protected override void OnFieldValidateComplete(IEntityField field, bool validationResult)
        {
            IsSynced = false;
            base.OnFieldValidateComplete(field, validationResult);
        }

Buffer overflow frowning

With a stack overflow, you call the same methods in a recursive way. I don't see how that can be the case with your code. If you break in your override during a debug session, (so step into the set of the field) you should be able to determine the call stack and thus which methods call which methods. Either way, you don't have to call the base method, that one is empty.


        protected override void PostFieldValueSetAction(bool fieldValueSet)
        {
            IsSynced = false;
            base.PostFieldValueSetAction(fieldValueSet);
        }

Never gets fired frowning

That's because the exception occurs before this method is ever called.

Frans Bouma | Lead developer LLBLGen Pro