How can I implement a global Entity OnSave()?

Posts   
 
    
Posts: 48
Joined: 14-Oct-2008
# Posted on: 16-Oct-2008 04:29:05   

I have a 'DateModified' field on every entity in my database. I would like to automatically update this any time I call the Save() method. I think that I could override OnSave in every entity's Custom Entity code, but I'm hoping there is a better way.

Any suggestions?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Oct-2008 05:18:49   

Yes, there's a better way: Auditing. You could download the Auditing example at example's download section.

You could write an Auditor that should be injected to all your IEntity(s) objects, so every time you entities would be saved (insert/update) the field DateModified will be updated.

David Elizondo | LLBLGen Support Team
Posts: 48
Joined: 14-Oct-2008
# Posted on: 16-Oct-2008 06:11:15   

daelmo wrote:

Yes, there's a better way: Auditing. You could download the Auditing example at example's download section.

You could write an Auditor that should be injected to all your IEntity(s) objects, so every time you entities would be saved (insert/update) the field DateModified will be updated.

OK I have an auditor that calls:

entity.SetNewFieldValue("DateModified", DateTime.UtcNow);

but the database doesn't seem to pick up the change, even though the field is updated in the object. Is there something I need to call to tell the persistence code that this field has been changed when doing this from an Auditor? The doc on the function says pretty clearly that it sets the field dirty.

Also I'm having a bit of trouble getting the dependency injection to work, either in auto mode or manual mode, but that's a different thread.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 16-Oct-2008 12:05:01   

The Auditor is called after the actual save action. Instead please use a Validator, specifically OnValidateEntityBeforeSave.