The title is because there are people who think that having four properties to store a DateTime of an entity creation, the User who have created it, the DateTime of the last modification and the User who have modified it mean that his application has auditing.
If you are a NHibernate’s user you know that we are providing many events to catch and even override NHibernate behavior. As I said long time ago, just after implement all those events/listeners, "what you can do with writing or overriding NHibernate default events is limited only by your imagination" (sorry to be auto-referential).
In the NET you can find various examples about how implement a simple “audit” using NHibernate. Most of those implementations come from some very old JAVA examples based on IPreInsertEventListener and IPreUpdateEventListener. Are those examples completely wrong ? No, they aren’t wrong!! They are just some simple examples that may work or may not work in your case. Scott (Findlater) have pointed me to the fact that even in the “NHibernate 3.0 cookbook” there is an example using IPreInsertEventListener and IPreUpdateEventListener and that is true… as true is that there is another example without using NH’s events at all (page 244) and there is the full list of NH’s events (page 238).
IPreInsertEventListener and IPreUpdateEventListener
If you look inside NHibernate, you can see that NHibernate does not have a default implementation of those two events and this mean that, for NHibernate, those two events are there just for your usage (in NH nothing is less important than an interface without implementation). You can do various things with those events as just log changes (NHibernate 3.0 cookbook page 235) or vetoing the action (as we are doing in NHibernate.Validator). Can you change the internal state maintained by NHibernate ? yes because what you can do “is limited only by your imagination”; does it mean that NHibernate will follow your imagination even when you have no idea what mean its internal state and how work with it ? No, it just mean that your imagination about how work NHibernate may fail.
Your anxiety will not change the passing of events
What you should do if NHibernate does not follow your imagination ?
Well… the last thing to do is bore me with +20 public posts just to then discover that instead apply my solution you have interpreted it, and you have introduced a bug in your code, or write a WIKI (that is the abbreviation of “What I Know Is”) just to say that you don’t know why your issue was closed as “Duplicated” of an issue closed as “Not an issue” where the first user, after an explication with code included, has recognized that his interpretation had a bug. Instead this “little boy” behavior you can ask for help in the nhusers group then, perhaps, you can write a simple solution with some tests and where nobody can help you, you may send a mail to me asking, with a nice “please”, help for free… perhaps I’ll write a public blog post with your problem and the solution… perhaps.
The pour auditing listener
Some entities may implements this interface:
Filip have published his domain and his test here, then he sent me a zip. I took his tests and I have added some more tests finding some bugs. The pour auditing listener passing all tests is this:
This is not indented to be THE SOLUTION of every kind of similar situation. A similar situation may have a similar solution. This is the solution to pass some tests provided by Filip Kinský, that’s all.
The advanced Audit solution
(This section is dedicated to the master AJL to prevent his question)
If the above is a “pour Audit” which is the “rich Audit” ?
The rich audit using NHibernate was started by Simon Duduica, finished by Roger Kratz with some very little touch implemented by me.
The name of the most powerful audit system for NHibernate is: NHibernate.Envers
The code of NHibernate.Envers is in bitbucket.org : https://bitbucket.org/RogerKratz/nhibernate.envers
A presentation of its power is available in Spanish : http://www.altnethispano.org/wiki/van-2011-02-26-audit-parallel-model-con-nhibernate-3.ashx
What I should do if I want a “rich audit” without use NHibernate.Envers ?
You should study NHibernate.Envers and make it a little bit better, not worst, and then share your code.
1) Your anxiety will not change the passing of events.
2) when I say you that your issue is not a bug try to do everything possible to explain your case instead insist in your position.
3) If I send you some code, first use it as is, then run your tests, and only then try to make it “more elegant“ (red, green, think, refactor).
I forgot to share the full configuration of NHibernate:
that is NHibernate 3.2 sexy mapping