1

My domain objects have a "CreateDate" and "ModidfyDate" members (DateTime).

When a user update a domain object (Asp.Net MVC) my view model does not hold these values. (It's to be set in my repository "Create" and "Update" methods)

So when I Update an object, I do not have the "CreateDate" available and therefore the Update method will fail. I seems to have 2 options, and I don't like either: 1) Have my viewmodel tag along the "CreateDate" property (hidden field in form) so I have the original CreateDate available. 2) Or, in my "Update" repository method, first get the original record from the database and set the object I'm about to update's CreateDate member (an unnecessary call to the db)

What is the "normal" way to work with this?

Krokonoster
  • 126
  • 8

1 Answers1

1

A nice way to handle CreateDate and ModifyDate would be by using NHibernate's event listeners.

Here are some samples on how to create simple auditing using IPreInsertEventListener and IPreUpdateEventListener:

http://ayende.com/blog/3987/nhibernate-ipreupdateeventlistener-ipreinserteventlistener
http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx


On a side note, you shouldn't have problems with CreateDate on your Update method. How does your Update method look like?

The usual workflow is to get your view model in POST ActionMethod, load the entity object from the database using NHibernate's ISession, or your custom repository and then map your properties from the view model to the entity, either by hand, or using a tool like AutoMapper. Properties like CreateDate should be ignored in the viewmodel to entity mapping.

Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
  • 1
    Coincidently just went with your reminder of how one usually do it ("reload" the entity in the action method and then update it's members. Manually for now, but was about to do that with AutoMapper) – Krokonoster Jun 17 '12 at 21:21
  • I just started looking (1st time) into IInterceptor. You know how does that compare to this IPreInsert/Update Event listeners? – Krokonoster Jun 17 '12 at 21:24
  • Here's a SO question about the difference between Interceptors and Event Listeners: http://stackoverflow.com/questions/867341/nhibernate-difference-interceptor-and-listener – Miroslav Popovic Jun 17 '12 at 21:26
  • Thanks for the link. As for the "other thing", seems AutoMapper go and reset my Create and Update datetime members. 'Interest interest = _service.GetInterestById(model.Id); interest = Mapper.Map(model);' – Krokonoster Jun 17 '12 at 21:41