Updating TimeStamp Colum with Default Value

Posts   
 
    
saravana
User
Posts: 63
Joined: 11-Nov-2010
# Posted on: 14-Dec-2011 08:33:39   

Hi,

We decided to add DateModified Columns in all the tables. But this is track the changed data and sync modified data to local database. So we don't want to change the BL code to update this column now. Instead we want LLBLGen to supply the CurrentDateTime to the DateModified column when ever a row is updated. Is it possible to set this through designer/generated code?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Dec-2011 09:24:25   

Can be done with a Validator class. Overriding the ValidateDataBeforeSave() method, and check if this not a new Entity, and then set the Field to the current DateTime.

saravana
User
Posts: 63
Joined: 11-Nov-2010
# Posted on: 15-Dec-2011 19:05:37   

I might need to add the new DateModified column to more than a dozen table. Whether I need to add a new validator class for all those tables? If I add validator class for the modified entities whether the default validation will still works?

Is there any better way to do this inside a single method/class? I planned to use the single field validation like below:


protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            bool toReturn = true;
            switch ((AbstracttermFieldIndex)fieldIndex)
            {
                case AbstracttermFieldIndex.Datemodified:
                    // Check whether the entity is new or existing
                    this.Datemodified = DateTime.Now;
                    toReturn = true;
                    break;
                default:
                    toReturn = true;
                    break;
            }
            return toReturn;
        }


Whether the above code will break the default validation of other fields? If it is how to make the default validation also works?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Dec-2011 22:08:37   

You can put that method (OnValidateFieldValue) in the CommonEntityBase class (USER_REGION_CODE or partial class) and it will apply to all entities.

protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            bool toReturn = true;
            switch (this.Fields[fieldIndex].Name)
            {
                case"Datemodified":
                    // Check whether the entity is new or existing
                    this.SetValue(Fields["Datemodified"], DateTime.Now, true);
                    toReturn = true;
                    break;
                default:
                    toReturn = true;
                    break;
            }
            return toReturn;
        }

Similar, you can use: - EntityBase.OnSave() - DataAccesAdapterBase.OnSaveEntity()

You can also have a validator class and use it in all entities. See the documentation for Validators to see all the options to inject a Validator.

If you decide to refactor those columns in a new table called say "AuditInfo" then you add new records for each event, or just update some records of the LastUpdate, etc. In that case you could use Auditors.

David Elizondo | LLBLGen Support Team
saravana
User
Posts: 63
Joined: 11-Nov-2010
# Posted on: 16-Dec-2011 10:14:33   

I have added the above overrided method in CommonEntityBase class (in user code region). But it is firing only for the field which are modified. So I am unable to update the DateModified column here (because I don't know whether the entity has that column.Is there way to find that??). Also I am not sure whether default validations are firing or not.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Dec-2011 10:42:53   

You can check for the (this.Fields["DateModified"] != null).

saravana
User
Posts: 63
Joined: 11-Nov-2010
# Posted on: 16-Dec-2011 11:09:33   

Hi Waala,

Thanks for the hint. It is working now. But what about my other question whether this 'OnValidateFieldValue' overrided method will ignore default validation for the entity fields?

I have now the over rided method 'OnValidateFieldValue' in commonEntityBase.cs.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Dec-2011 11:52:40   

No it won't. There is a setting for that:

Docs wrote:

Bypassing built-in validation logic It might be that you don't want to use the built-in validation logic and want to use your own validation logic instead. You can make LLBLGen Pro bypass the built-in validation logic using the static (Shared) property on EntityBase (if you're using SeflServicing) or EntityBase2 (if you're using Adapter) called BuildInValidationBypassMode. Set that property to any of the BuildInValidationBypass values, which are NoBypass (value 0, default), AlwaysBypass (value 1) or BypassWhenValidatorPresent (value 2). If you choose to use AlwaysBypass and there's no validator present no validation on the field will be performed. You can also set this property through the application's config file by adding this line to the appSettings section of your application's config file:

<add key="buildInValidationBypassMode" value="0"/>

Where value is one of the values 0, 1 or 2 for resp. NoBypass, AlwaysBypass or BypassWhenValidatorPresent.