[Study Note] Patterns and Practices in NHibernate
在 NHibernate Forge 上,Patterns&Practices 栏目里面只有两篇文章,我感兴趣的是其中一篇:NHibernate and the Unit of Work Pattern。
[Identity Field, Equality and Hash Code]
“The identity field saves a database ID field in an object to maintain identity between an in-memory object and a database row.” – Martin Fowler, PoEAA
The surrogate key is not derived from application data.
GUID, global unique identifier
A Set is a collection that contains no duplicate elements.
a object should never ever change it’s hash code during its life, that is from the moment the object is instantiated until it is disposed.
[NHibernate and the Unit of Work Pattern]
Part 1
Unit of Work Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems. – Martin Fowler, PEAA
When you’re pulling data in and out of a database, it’s important to keep track of what you’ve changed; otherwise, that data won’t be written back into the database. Similarly you have to insert new objects you create and remove any objects you delete.
A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you’re done, if figures out everything that needs to be done to alter the database as a result of your work.
In NHibernate, the Session object is a Unit of Work (UoW) container…Only when flushing the session will the database be alered.
看完之后才发现这个 Unit of Work 的实现过于的复杂,至少现在我还不能完全的理解其中的奥妙。最近几天看到了大概一共三个 Unit of Work 的实现,繁简不一,有时间的话可以比较一下。