State Pattern using PreProcessValueToSet

Posts   
 
    
Posts: 98
Joined: 09-Feb-2005
# Posted on: 29-Aug-2006 19:31:54   

Please disregard/delete this thread, as on reflection, it appears that my subject doesn't really get to the heart of what I'm trying to determine. I've reposted the question in the more appropriately named thread: "Determining whether a field OR it's related entity is changing"

Posts: 98
Joined: 09-Feb-2005
# Posted on: 29-Aug-2006 21:34:16   

Ok, I've found OnValidateFieldValue, which would actually be even better, since it's more representative of what I want to do, and provides a bool value to say yay or nay. This one also only is triggered by setting the ID value though.

For setting the entity reference, what I've found is that I can use PreProcessValueToSet for the old value, and OnRelatedEntitySet for the new value.

So what I'm working with at the moment is:

private LookupCollateralStatusEntity _prevCollateralStatus;
protected override void PreProcessValueToSet(SD.LLBLGen.Pro.ORMSupportClasses.IEntityField2 fieldToSet, ref object valueToSet)
{
    base.PreProcessValueToSet(fieldToSet, ref valueToSet);          
    if (fieldToSet.Name == CollateralFields.CollateralStatusId.Name)
    {
        _prevCollateralStatus = (LookupCollateralStatusEntity)
            this.Fields[fieldToSet.Name].CurrentValue;

        if (valueToSet != null)
            HandleStateChange(_prevCollateralStatus, (LookupCollateralStatusEntity)valueToSet);
        _prevCollateralStatus = null;
    }
}

protected override void OnRelatedEntitySet(SD.LLBLGen.Pro.ORMSupportClasses.IEntity2 relatedEntity, string fieldName)
{
    bool baseValidate = base.OnValidateFieldValue(fieldIndex, value);
    if (!baseValidate)
        return false;
    if (fieldIndex == (int)CollateralFieldIndex.CollateralStatusId)
    {
        if (_prevCollateralStatus != null)
            HandleStateChange(_prevCollateralStatus, (LookupCollateralStatusEntity)value);
        _prevCollateralStatus = null;
    }
    return true;
}

This is a bit ugly. Is there a better way to do this that anyone can suggest?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 30-Aug-2006 03:18:12   

Hi,

did you have a look at the following user manual's section? Generated code - Validation per field or per entity

External validators may be what you are looking for.

Cheers

Posts: 98
Joined: 09-Feb-2005
# Posted on: 30-Aug-2006 16:40:23   

Hi, thanks for the response. I may be mistaken, but it appears that ValidateFieldValue only fires when setting fields, rather than related entities. So if I have 2 tables, Collateral and CollateralStatus, with a 1:1 join, then in my generated entities, I'll have Collateral.CollateralStatus, and Collateral.CollateralStatusID.

The problem then is that I can easily trap the attempt to set CollateralStatusID, but not CollateralStatus.

Are there any suggestions on how to validate the setting of related entities before they are set?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 30-Aug-2006 17:11:42   

You may validate the related entity or call the validation logic inside the "set" method of the related entity property "CollateralStatus" in the Collateral entity class

Posts: 98
Joined: 09-Feb-2005
# Posted on: 30-Aug-2006 18:15:52   

You may validate the related entity

How?

or call the validation logic inside the "set" method of the related entity property "CollateralStatus" in the Collateral entity class

What happens when I regenerate the code?

EDIT: I've restated the question at the top to be more targeted, and hopefully clearer.