c# - Add event to cascade update -
i'm using fluent nhinernate in application cascade option.
i have complex model bunch of entities , relations in it. held 1 entity (contract) has collections of other entities. these entities have references other entities / lists of entities.
when want save whole thing this:
mysession.save(mycontract); mytransaction.commit()
works fine, saves entities cascade.
the problem need set properties modifiedby, modifiedtime, etc on update. how can this?
the easiest way create interface properties need update:
public interface iaudit { string modifiedby { get; set; } datetime modifiedtime { get; set; } }
and add nhibernateinterceptor
update properties:
public class auditinterceptor : emptyinterceptor { public override bool onsave(object entity, object id, object[] state, string[] propertynames, itype[] types) { var auditableentity = entity iaudit; if (auditableentity != null) { auditableentity.modifiedtime = datetime.now; // ... } return base.onsave(entity, id, state, propertynames, types); } // ... }
you need tell nhibernate use interceptor if remember correctly done follows:
var session = sessionfactory.opensession(new auditinterceptor());
depending on how manage sessions etc depend goes, , since auditinterceptor
has no state exist singleton without worry of multi threading issues.
Comments
Post a Comment