开发者

What's the best practice for setting last modified and who modified using NHibernate?

In my application I have a situation where we need to capture the when a record was created and modified and what user performed those actions. So I might have an object something like:

public class Product
{ 
  int Id;
  int Name;
  DateTime CreatedOn;
  int CreatedByUserId;
  DateTime LastModifiedOn;
  int LastModifiedByUserId;
}

What's the best practice for handling these in NHibernate? Via using an interce开发者_如何学编程ptor something like what's described here?


I don't think there's a "best" practice, but the use of event listeners is more common for this. There's a good example at http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx

One thing you'll need to consider is that you need to store that userId somewhere. I'm currently doing that by assigning a static property on the listener on startup. It's not pretty, but it gets the job done.


I agree with Diego that I don't think there's a best practice. It depends on your application context. In Diego's link, and to use event listeners at the persistence (nHibernate) level, it needs to know how to lookup the current user. This may not make sense depending on your application. For example, if you're writing an ASP.NET MVC app, do you really want your persistence layer to depend on HttpContext to know the user? Yes, you could pass in some type of strategy, but this doesn't seem like it's always going to be the right thing to do.

I think it's perfectly valid to have your service layer construct the object and add the creator itself. Then pass the whole object, with the creator already hydrated, down to nHibernate to persist. The creator would be saved to the database the same way as any other property.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜