I'm using DDD and NHibernate to persist my domain object. In my database every table that is related to an entity has four extra columns (CreatedBy, UpdatedBy, CreatedDate, UpdatedDate) used for audit tracking. I am using the repository pattern that only allows aggregate roots to be saved. When NHibernate tries to save the entities on the aggregate root, I get a SQL DateTime error because the entities don't have their audit properties set. Is there a way in NHibernate to set properties on objects just before save?
Here is a little example. I have an Order object which is the aggregate root. I also have OrderNote objects that are children of the order. When I add an OrderNote to the Order and then save the Order an exception is thrown because the CreatedDate/UpdatedDate are set to DateTime.MinValue which will cause a SQL DateTime overflow. Since these audit columns are part of persistence and not related to the problem domain, I don't want the Order aggregate root to set these properties when the note is added. The audit columns/properties should only be known by the persistence framework and not the domain. I would like to be able to tell NHibernate to set these properties when saving or updating. Is there a way to do this?