這是不 NHibernate的代碼(這是Hibernate的Java代碼),但谷歌搜索NHibernate的API使它看起來像類似的代碼可以讓你開始爲NHibernate的,以及(有一個PersistentMap
類具有Owner
財產和CollectionSnapshot
屬性):
public void onCollectionUpdate(Object collection, Serializable id) {
System.out.println("****onCollectionUpdate");
if(collection instanceof PersistentMap) {
PersistentMap newValues = (PersistentMap) collection;
Object owner = newValues != null ? newValues.getOwner() : null;
Set<?> oldValues = newValues != null
? ((Map<?, ?>) newValues.getStoredSnapshot()).keySet()
: null;
System.out.println("owner: " + (owner != null ? owner.toString() : "(null)"));
System.out.println("oldValues: " + (oldValues != null ? oldValues.toString() : "(null)"));
System.out.println("newValues: " + (newValues != null ? newValues.toString() : "(null)"));
} else if (collection instanceof PersistentSet) {
PersistentSet newValues = (PersistentSet) collection;
Object owner = newValues != null ? newValues.getOwner() : null;
Set<?> oldValues = newValues != null
? ((Map<?, ?>) newValues.getStoredSnapshot()).keySet()
: null;
System.out.println("owner: " + (owner != null ? owner.toString() : "(null)"));
System.out.println("oldValues: " + (oldValues != null ? oldValues.toString() : "(null)"));
System.out.println("newValues: " + (newValues != null ? newValues.toString() : "(null)"));
}
}
請注意,我知道這不是C#或NHibernate代碼。請評論,如果這是完全無用的NHibernate(如果API不完全相似,即使我的谷歌搜索說它是相似的),我會刪除帖子。
我沉浸在Java的土地的權利,或者我會嘗試這個爲你:)
我真的不知道這對我有何幫助。我沒有問題檢測正常對象屬性中的更改,只是在集合中 - 並且集合的添加和刪除方法未在代理上調用(另外,這對於未延遲加載的對象不起作用) – Nir
@Nir so你可以攔截修改修改... –
如果我們使用博客數據模型爲例,當有人調用post.Comments。添加評論屬性getter在代理上被調用,並且可以被它捕獲 - 但是這並不告訴我Add是被調用的(它可能是一個來自foreach循環的GetEnumerator,對於我所知道的),它不會告訴我增加了什麼。 – Nir