ORMLite - Add attribute to ignore property on updates
It'd be nice to have an attribute to apply to data DTOs that would cause them to be excluded if an update statement is executed.
Use case: Auditing
I have an IAudit interface that looks like (basically copied from Demis' answer here http://stackoverflow.com/questions/21803999/are-interceptors-possible-in-servicestack-ormlite)
public IAudited{
string CreatedBy {get; set;}
DateTime CreatedDate {get;set;}
string ModifiedBy {get;set;}
string ModifiedDate {get;set;}
}
Using OrmLite's insert filter makes it easy to generate values for the interface members when setting, as I can get the current user and Date and use them for both the Created and Modified fields. On an update though, I need to fetch the CreatedBy and CreatedDate fields for the record so I can populate the DTO before sending to the database
i.e. this will throw a null exception unless I previously populated the CreatedDate
OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
{
var auditRow = row as IAudit;
if (auditRow != null)
{
auditRow.ModifiedDate = DateTime.UtcNow;
}
};
I'd like to be able to
public Audited : IAudited{
[IgnoreOnUpdate]
public string CreatedBy {get;set;}
[IgnoreOnUpdate]
public DateTime CreatedDate {get;set;}
public string ModifiedBy {get;set;}
public DateTime ModifiedDate {get;set;}
}
and have the generated sql not try to update the CreatedBy and CreatedDate fields and then the auditing would be transparent to the repository code

You can use the [IgnoreOnUpdate] and [IgnoreOnInsert] to ignore properties on Update/Insert
-
Intuiste commented
This already exists and does pretty much what you are explaining:
[IgnoreOnInsert]
[IgnoreOnUpdate]On the DB side you set the field to automatically add the date (e.g. on SQl Server: GENERATED ALWAYS AS ROW START).
I've used this with SQLServer Temporal tables with success.