开发者

IPreUpdateEventListener makes object dirty

I'm using IPreUpdateEventListener for audit purposes. My solution is exactly as Ayende Rahien says here. Something like this:

public bool OnPreUpdate(PreUpdateEvent @event)
{
    var audit = @event.Entity as IHaveAuditInformation;
    if (audit == null)
        return false;

    var time = DateTime.Now;
    var name = WindowsIdentity.GetCurrent().Name;

    Set(@event.Persister, @event.State, "UpdatedAt", time);
    Set(@event.Persister, @event.State, "UpdatedBy", name);

    audit.UpdatedAt = time;
    audit.UpdatedBy = name;

    return false;
}

My problems is I must set audit.UpdatedAt = time; to have the value in my entity, but it makes the object dirty and causes another more update to database. I need both the new value in my object but don't want duplic开发者_如何学Cate update. Is there any way?


This should not cause two updates; something else is going on. The problem may be that the database UpdatedAt field resolution is different then the .NET DateTime.Now resolution. My code is nearly identical to yours but I use this method to create the timestamp:

    /// <summary>
    /// Return a DateTime with millisecond resolution to be used as the timestamp. This is needed so that DateTime of an existing instance
    /// will equal one that has been persisted and returned from the database. Without this, the times differ due to different resolutions.
    /// </summary>
    /// <returns></returns>
    private static DateTime GetTime()
    {
        var now = DateTime.Now;
        var ts = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond, DateTimeKind.Local);
        return ts;
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜