Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does NHibernate implement change tracking?

Tags:

nhibernate

Does nhibernate proxies do any smart job to make change tracking efficient? Or does it only support what Entity Framework calls snapshot based change tracking?

like image 911
Alice Avatar asked May 28 '10 07:05

Alice


2 Answers

It is snapshot-based.

When loading an entity, its state is stored in the session as an object[].

When flushing, the current state is converted to an object[] and compared with the original state to determine which properties are dirty.

This is more efficient for many reasons. One of them is that you don't need a proxy to track changes. Another is that, if you set a property to a different value and then revert it, the entity will be considered not-dirty, thus avoiding an unnecessary DB call.

like image 199
Diego Mijelshon Avatar answered Oct 18 '22 20:10

Diego Mijelshon


NHibernate and EntityFramework track changes in very different ways. Entity Framework tracks changes in the entity itself. NHibernate tracks changes in the session.

Tracking changes in the entity requires more memory (because you are storing the before values as well as the after values). Entities can retain change tracking even after disconnecting from the ObjectContext.

Tracking changes in the session is more efficient overall, but if you disconnect an entity from the session, you lose the change tracking.

like image 20
Michael Maddox Avatar answered Oct 18 '22 22:10

Michael Maddox