Basically I have a simple base class:
public abstract class BasePoco {
public virtual Guid Id { get; protected set; }
public virtual DateTime Created { get; protected set; }
public virtual DateTime LastModified { get; protected set; }
public virtual User LastModifiedBy { get; set; }
}
With the following mapping:
<class name="BasePoco" abstract="true">
<id name=开发者_如何学编程"Id">
<generator class="guid.comb" />
</id>
<property name="Created" generated="insert" />
<property name="LastModified" generated="always" />
<many-to-one name="LastModifiedBy" />
</class>
The Created property is set via a database default value, the LastModified via a database trigger (haven't tested it yet, but I assume this will work). But how can I set the "LastModifiedBy" in a nice clean way? Instead of setting this property in my code every time I want to update a poco, I'd like to have something like a static method NHibernate can call on every update (and of course, this method will return the currently logged on user).
Thanks in advance
You can hook to events that fire before inserts and updates in Nhibernate; you can add audit, autofill some fields, etc... This Ayende post (very recommended) also covers the basics
I managed my createdDate/modifiedDate/createdBy fields this way, and also added audit so i could allow hard-deletions in the database while keeping information about what was deleted
精彩评论