Code for every entity

Posts   
 
    
MWHITEHEAD
User
Posts: 8
Joined: 18-Sep-2007
# Posted on: 27-Oct-2007 13:08:15   

Self-Servicing Templates

Just wondering where the best place is to put code which needs to be executed for each entity upon save or update; such as "UpdateTimestamp", setting "LastModifiedBy".

Also on non-identity PK fields, generating the key.

Thanks Mark

stefcl
User
Posts: 210
Joined: 23-Jun-2007
# Posted on: 27-Oct-2007 15:06:28   

About timestamps : This should be done by a trigger because if you do it on the client side you would probably use the system date (which might be inconsistent).

About LastModifiedBy : You could override the OnValidateEntityBeforeSave method of your Entity to set the "LastModifiedBy" field to its value but you have to find a way to communicate that value to your data access layer (for example by creating a CurrentConfig class with a "CurrentUser" static field). If you don't want to do that, then you'll have to set the fields manually before calling Save() in your business layer.

If you choose the "OnValidateEntityBeforeSave" way but don't want to write specific code for each entity, you can use an external validator base class.

MWHITEHEAD
User
Posts: 8
Joined: 18-Sep-2007
# Posted on: 28-Oct-2007 13:11:53   

What about using the CommonEntityBase class, as the code would be the same for every entity?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Oct-2007 06:26:35   

If you want to audit the LastUpdated, or UpdatedTimestamp, etc, I would go on **Auditor **functionallity (now in V2.5).

For autoincrement by-code fields I would go on each Entity class. The thing is that making a general rule for all entities seems kind of dark to me (IMHO). So for save coding time I would modify the templates so all my entity classes would contain such functionality.

David Elizondo | LLBLGen Support Team
MWHITEHEAD
User
Posts: 8
Joined: 18-Sep-2007
# Posted on: 30-Oct-2007 10:26:15   

I'll take a look at the auditing functionality. I didn't get the dark comment, isn't putting common code and data into the base class of a heirarchy just standard OOPs design.

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 30-Oct-2007 12:41:02   

You may override the OnValidateEntityBeforeSave, in the CommonEntityBase.

Another option is to put this code in a validator class, and again you can override the CreateValidator method in the CommonEntityBase.

Also you can use a validator class and set it to each entity instance by using Dependancy Injection.

MWHITEHEAD
User
Posts: 8
Joined: 18-Sep-2007
# Posted on: 31-Oct-2007 01:02:47   

Food for thought indeed! Thanks guys simple_smile