Saving common fields in all entities

Posts   
 
    
imakimak
User
Posts: 62
Joined: 18-Mar-2010
# Posted on: 27-Apr-2010 04:23:30   

LLBL Gen Pro v 2.6/SQL Server 2008

In all my tables, I have a few columns that store some auditing type information. Whenever an entity get saved, I will have this repititive code that I have to copy + paste everywhere in order to save these columns. Is there any better way of addressing this type of problem?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Apr-2010 07:15:42   

Depending on how and when you want to grab the audit information. You for example can override OnInitialized method on CommonEntityBase and in there you can set such common audit fields:

this.SetNewFieldValue("thefield", value);

If the audit data in in another table (AuditInfo for example), you can take advantage of LLBLGen Auditors.

If you can set such fields before save, you can write an Validator an override OnValidateBeforeSave and set values there. Then you just need to assign such validator to your entities. You can use DependencyInjection for that, or set it in CommonEntityBase initialized methods.

David Elizondo | LLBLGen Support Team
imakimak
User
Posts: 62
Joined: 18-Mar-2010
# Posted on: 27-Apr-2010 17:58:05   

These additional columns exists in all tables (they are basically things like when the rows was last modified, who modified it etc). I can set these before save but it the same repetitive code, e.g.
myentity.lastmodifedtime = DateTime.Now(); myentity.modifedby = “whoever” ; etc. But I don’t want to set this code for all my 100+ entities before saving. Do you think Validator can work for this situation? If so any link to code examples will helpful.

I am not sure I understand it correct, but reading some other forum entries about templates make me think that those can be used here too?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 27-Apr-2010 20:48:27   

Have a look at the documentation about hooking into entity actions

http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/gencode_tapinroutinesevents.htm

The best place for the sort of code you descibe is in OnBeforeEntitySave

Matt

imakimak
User
Posts: 62
Joined: 18-Mar-2010
# Posted on: 27-Apr-2010 21:38:00   

Thanks, Since I am using the SelfService model, I guess OnSave will be right place for me, correct?

Also I am assuming that I will have to change the LLBL generated code here. In that case when the next time I generate LLBL, can I loose my work?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 27-Apr-2010 21:39:42   

Correct on OnSave.

You can place your code in either a partial class which extends your entity, a sub class which inherits from your entity, or a user code region within your entity. All 3 of these methods will ensure that you do not lose your customisations when you regenerate.

Matt

imakimak
User
Posts: 62
Joined: 18-Mar-2010
# Posted on: 27-Apr-2010 22:45:05   

MTrinder wrote:

You can place your code in either a partial class which extends your entity, a sub class which inherits from your entity, or a user code region within your entity. All 3 of these methods will ensure that you do not lose your customisations when you regenerate.

Matt

Ok but this is exactly the thing that I do NOT want to do. I have over 100 entities and all of those have these common fields that will be updated with the same values. I think I wrote this example above but let me write it again

myentity.lastmodifedtime = DateTime.Now(); 
myentity.modifedby = “whoever” ;

So I have to write this same code for all those 100 entities. Isn’t there any better way of doing this type of thing in LLBL framework?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 28-Apr-2010 00:39:51   

Override OnSave in either a partial class or a user code region of the CommonEntityBase.cs class - this is the base class that all of the entities inherit from.

Matt