2010-04-22 74 views
1

我正在使用NHibernate(流利)以一堆表格訪問舊的第三方數據庫,這些表格與任何顯式方式都沒有關係。這是一個子表有parentID列,其中包含父表的主鍵,但沒有外鍵關係確保這些關係。理想情況下,我想添加一些外鍵,但不能觸及數據庫模式。使用NHibernate限制刪除

我的申請工作正常,但我真的很喜歡強加一個參照完整性規則,如果他們有孩子,e.i.將禁止刪除父對象。類似的'ON DELETE RESTRICT',但由NHibernate維護。

任何想法如何處理這將不勝感激。我應該看看IInterceptor接口上的OnDelete()方法,還是有其他解決方法?

當然,任何解決方案都會帶來性能損失,但我可以忍受這一點。

回答

1

我不能想到在NHibernate中這樣做的方式,因爲它會要求NHibernate有一些關係的知識。我會在代碼中使用sepecification pattern來處理這個問題。例如(使用與Employee對象鏈接的Company對象):

public class CanDeleteCompanySpecification 
{ 
    bool IsSatisfiedBy(Company candidate) 
    { 
     // Check for related Employee records by loading collection 
     // or using COUNT(*). 
     // Return true if there are no related records and the Company can be deleted. 
     // Hope that no linked Employee records are created before the delete commits. 
    } 
}