How do you implement audit trail for your objects (Programming)?

Database triggers are the preferred way to go here, if you can However, recently I had to do this in client-side code and I ended up writing a class that created a deep (value) copy of the object when it was opened for editing, compared the two objects at save time (using ToString() only) and wrote any changes to an audit table Edit: I had an Audit attribute on each property I wanted to consider auditable and used reflection to find them, making the method non-specific to the objects being audited.

Database triggers are the preferred way to go here, if you can. However, recently I had to do this in client-side code and I ended up writing a class that created a deep (value) copy of the object when it was opened for editing, compared the two objects at save time (using ToString() only) and wrote any changes to an audit table. Edit: I had an Audit attribute on each property I wanted to consider auditable and used reflection to find them, making the method non-specific to the objects being audited.

1 I've voted this answer up, but one thing to watch out for when using database triggers is transaction brackets. You sometimes want to audit an event even when an error occurs and the transaction is rolled back. – Andrew Sep 29 '08 at 11:57.

I don't know if it will fit seamlessly with your ORM, but I used Point-in-Time database design for an ERP application and really recommend it. You automatically get story and Audit from this architecture, as well as other benefits.

I know this doesn't answer your question, but for the record, I prefer to handle this type of auditing logic in the database.

I come more from the SW side that the DB side, if you create a set of DAOs (Data access objects) that you use for your interaction with the database. I would then insert the audit functionality into the respective functions in the DAOs that need to be trailed. The database trigger solution is also feasible, it depends where you like to put your functionality, in the DB or in the code There are a lot of ORM (Object relational Mapping) tools out there that create the DAO layer for you.

We've implemented a similar solution, using AOP (aspectJ implementation). Using this particular points can be captured and specific operations can be performed. This can be plugged in and plugged off when we like.

If you really want to do it in the app layer, I would suggest this. Hope it helps..

I've done this in bernate (another ORM) using an Interceptor for the Session. That way the audit code is seperate from your code.

Of the base object, and I store the changes in Audit-AuditTrail (Mast-Det) table, for field changes. Etc. Using some method services called.

How do you implement audit trail in you OOP code? Please share your insights? Any patterns etc? Best practices etc? Another thing is that how to disable audit when running unit test,since I don't need to audit them but since base object has the code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions