OnFieldValueChanged implementation

Posts   
 
    
he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 27-Oct-2009 10:04:09   

I need to know when a field of entity within an entity collection has been changed so I can do some validation on it. In this case the validation is a via spelling checker.

I thought I would give using OnFieldValueChanged overridable method a tryout.

Now there is probably better ways of doing this but if _**you could humour me here **_it would be appreciated. Some of the alternatives that may spring to mind may not be that easy to implement in my circumstances (using loadable modules in a CAB framework).

I have an entity called Motd and have generated by llblgen pro classes. All works well no problems yet. Until . . . . . . .

I tried to create a partial class to extent my MotdEntity class except the OnFieldValueChanged appears not be overridable!

I am very new at even trying this out. I was wondering if you could help me out.

Here is my partial class

namespace PathWest.DataAccess.WardenModuleMOTD.Data.EntityClasses
{
    public partial class MotdEntity
    {
        protected override void OnFieldValueChanged(object originalValue, IEntityField field)
        {
            bool toReturn = true;
            switch ((MotdFieldIndex)field.FieldIndex)
            {
                case MotdFieldIndex.MessageTitle:
                    // do stuff here
                    Debug.WriteLine("MotdEntity OnFieldValueChanged");
                    break;
                default:
                    
                    break;
            }
            return;
        }

    }
}

The reference documentation indicates that it is.frowning

**OnFieldValueChanged Method ** C#
protected virtual void OnFieldValueChanged( object originalValue, IEntityField field )

Parameters _originalValue _ The original value of the field, can be null if the field didn't have a value. _field _ The field which value was set. Remarks In an override, cast the field's fieldindex to the fieldindex of the entity to quickly determine which field you're dealing with

If I take out the override and build it, lots of my other llblgen pro statements are no longer compilable. For example

private EntityCollection<MotdEntity> deletedMessages;

is no longer valid.cry

The type 'PathWest.DataAccess.WardenModuleMOTD.Data.EntityClasses.MotdEntity' cannot be used as type parameter 'TEntity' in the generic type or method 'PathWest.DataAccess.WardenModuleMOTD.Data.HelperClasses.EntityCollection<TEntity>'. There is no implicit reference conversion from 'PathWest.DataAccess.WardenModuleMOTD.Data.EntityClasses.MotdEntity' to 'SD.LLBLGen.Pro.ORMSupportClasses.IEntity2'. C:\Projects\Sandbox\WardenCAB\WardenModuleMOTD\BusinessLogic\MessageOfTheDay.cs 26 46 WardenModuleMOTD

So how does one do this?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Oct-2009 10:17:31   

I need to know when a field of entity within an entity collection has been changed so I can do some validation on it. In this case the validation is a via spelling checker

If you want to validate a field, then you should be using Field Validation

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 27-Oct-2009 10:29:45   

Thanks for the reply. However I was hoping for a more didactic reply. I know there are other ways of doing this but as I requested

Now there is probably better ways of doing this but _**if you could humour me here **_it would be appreciated. Some of the alternatives that may spring to mind may not be that easy to implement in my circumstances (using loadable modules in a CAB framework).

Rather than remain ignorant and go down a path of lesser resistance, I would very much like to know how to do this. Knowing its possible but not knowing how, is a cause of angst that I would rather not have.

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 27-Oct-2009 10:45:10   

Ok I tried to put the ValidateFieldValue into my partial class

       // C# in validator class deriving from ValidatorBase.
        protected override bool ValidateFieldValue(IEntityCore involvedEntity, int fieldIndex, object value)
        {
            bool toReturn = true;
            //switch ((OrderFieldIndex)fieldIndex)
            //{
            //  case OrderFieldIndex.OrderId:
            //      // id is valid if the value is > 0
            //      toReturn = ((int)value > 0);
            //      break;
            //  default:
            //      toReturn = true;
            //      break;
            //}
            return toReturn;
        }

But it tells me its not overridable. The same as the other non "overridable" overridable method.

Clearly I am doing something wrong! But what, currently eludes me.confused

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Oct-2009 11:00:27   

OnFieldValueChanged is perfectly overriden (just tried it). Most probably if you are using Adapter model, you need to change the signature to the following.

protected override void OnFieldValueChanged(object originalValue, IEntityField2 field)

note: IEntityField2

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 28-Oct-2009 01:03:33   

Great, it works for you but it won't for me! disappointed

You are correct in that both the object browser and reference manual agree with what you say. confused Despite that, VS2008 doesn't. There must be some other factor that is in play that is frustrating my attempts. frowning

Is it possible to provide a more complete example? You mentioned you just tested it and it was fine. Can you post that?

Is this covered somewhere in some demo/example? I will check them out in anycase.

I am obviously doing something quite stupid. The question is, what?

Please see attachments

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 28-Oct-2009 01:24:45   

Ok I found the cause of the problem.

It was really stupid.

It was me!!!!!!!!!!!!! flushed

If you have a look at the attachments, it says

protected override void OnFieldValueChanged(object originalValue, IEntityField field)

because I forgot to save the change

It did say

protected override void OnFieldValueChanged(object originalValue, IEntityField2 field)

The class was in the wrong place!!!! In the wrong place it still says there is no overrideable method. I have moved to where it should be and volia - it works. sunglasses

See new attachments. Despite that, I feel I have learnt something and that is quite valuable to me.smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Oct-2009 04:16:13   

he00273 wrote:

Despite that, I feel I have learnt something and that is quite valuable to me.smile

Good you have it working now simple_smile

David Elizondo | LLBLGen Support Team