510 likes | 659 Views
Become the nhibernate Guru. Torkel Ödegaard. torkel.odegaard@gmail.com. www.codinginstinct.com. Nhibernate?. ORM (Object-Relational Mapper). Transparent persistance. Persistence ignorance. Become the Guru. Identity map (L1 Cache) Unit Of Work (Session Flush) Cascading
E N D
Torkel Ödegaard torkel.odegaard@gmail.com www.codinginstinct.com
Nhibernate? ORM (Object-Relational Mapper) Transparent persistance Persistence ignorance
Become the Guru • Identity map (L1 Cache) • Unit Of Work (Session Flush) • Cascading • Attached / Detached • Proxy / Lazy loading • ID generation / unsaved-value • Concurrency • Legacy databases • Performance
unit of work session.Flush()
Flush Mode • Auto • Commit • Never • Unspecified
Transaction Unit Of Work
identity map An identity map is a database access design pattern used to improve performance by providing a context-specific in-memory cache to prevent duplicate retrieval of the same object data from the database.
identity map True
identity map True
identity map - Problems Same instance Will return instance from identity map
identity map - Problems NHibernate.NonUniqueObjectException A different object with the same identifier value was already associated with the session Two instances with same id
identity map - Problems NHibernate.HibernateException Identifier of an instance of TieFighter was altered …
cascading • None • Save-update • Delete • Delete-orphan • All • All-delete-orphan
Cascade & Save-update Transient (unsaved) instance Transient (unsaved) instance INSERT INTO Pilot .... INSERT INTO TieFighter ...
Cascade & delete DELETE TieFighter ... DELETE Pilot ...
Cascade & none Transient (unsaved) instance Transient (unsaved) instance NHibernate.TransientObjectException Object references an unsaved transient instance -save the transient instance before flushing
Cascade & none UPDATE Pilot ....
legacy • Composite primary keys • Relations on non-primary keys • Join • SQL Queries • DB Generated fields
performance • Batching • Eager loading (eliminate N+1) • Query Cache • L2 Cache
i could go on an on... • NHibernate = endless number of features