开发者

NHibernate + Generated value (client side) for property on each update

开发者 https://www.devze.com 2023-03-21 16:16 出处:网络
Basically I have a simple base class: public abstract class BasePoco { public virtual Guid Id { get; protected set; }

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号