Alice
Alice

Reputation: 909

How does NHibernate implement change tracking?

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?

Upvotes: 10

Views: 5902

Answers (2)

Diego Mijelshon
Diego Mijelshon

Reputation: 52753

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.

Upvotes: 15

Michael Maddox
Michael Maddox

Reputation: 12489

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.

Upvotes: 10

Related Questions