ReadOnly Fields

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 03-Jan-2007 22:15:37   

Environment: LLBLGenPro 2.0 and SelfServicing

I have some fields that are not to be changed after an entity is initially created and persisted. In order to enforce this behaviour I would like to put code in my ValidateEntityBeforeSave() that detects whether the value of the specific readonly fields has been changed (I don't want to rely on the programmer always remembering to enable/disable screen fields). This will involve comparison of original vs current values for the designated fields.

How can I best go about this?

Thanks

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 04-Jan-2007 04:16:49   

You can compare the entity fields' currentValue to the DBValue. Something along these lines


public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
        {
            MyEntity toValidate = (MyEntity)involvedEntity;
            if (!toValidate.IsNew)
            {
                if (toValidate.Fields["Name"].DbValue != toValidate.Fields["Name"].CurrentValue)
                    throw new ORMEntityValidationException("Name cannot be changed after creation", involvedEntity);
            }
            base.ValidateEntityBeforeSave(involvedEntity);

        }