dzenesiz
dzenesiz

Reputation: 1562

nHibernate transaction logging

Our application uses nHibernate, and there is a requirement to log all changes to a certain table. I don't need to log the entire queries generated by the ORM, just the data and the date.

For example, let's say I have an entity called Employee:

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
}

The only solution I can think of is manually keeping track of changes (new, updated and deleted entities) and logging that data to a table. Something along the lines of:

public void SaveEmployees(List<Employee> employees)
{
    employees.ForEach(x => {
        if (x.ID == 0) // add do LOG list as new
        else // add to LOG list as old
    });

    // save employees like usual
    // save LOG list
}

This isn't difficult to do, even with fetching the old data to see what's missing, but I was wondering if there was a better way?

Upvotes: 1

Views: 287

Answers (1)

David Osborne
David Osborne

Reputation: 6821

I think an Interceptor might give you what you need. You can use the OnFlushDirty() or the OnSave() method to write your changes. Documentation here.

Upvotes: 0

Related Questions