Setting a InsertedOn Field with Castle ActiveRecord
May 26, 2009 by Daniel HoelblingIf I want a field to contain the date of insertion I’d usually have to hook into the Create method somewhere be it my repository or whatever.
With Castle ActiveRecord it’s as simple as overriding the Create() method in your Entity:
[ActiveRecord] public class Comment : ActiveRecordBase<Comment> { [PrimaryKey] public long Id { get; set; } [Property] public DateTime CreatedOn { get; set; } [Property] public string Message { get; set; } private void SetInsertionTimestamp() { CreatedOn = DateTime.Now; } public override void Create() { SetInsertionTimestamp(); base.Create(); } public override void CreateAndFlush() { SetInsertionTimestamp(); base.CreateAndFlush(); } }