2010-05-21 21 views
3

我有一個Date類包裝DateTime?類(幫助嘲笑DateTime.Now,我們的域名等)。即使所有的屬性都爲空,我如何告訴NHibernate將組件加載爲非null?

Date類的類只有一個受保護的屬性:DateTime?日期

public class Date 
{ 
    protected DateTime? date; 
} 

// mapping in hbm 
<component name="CompletedOn"> 
    <property column="StartedOn" name="date" access="field" not-null="false" /> 
</component> 

從NHibernate的文檔:

Like all value types, components do not support shared references. The null value semantics of a component are ad hoc. When reloading the containing object, NHibernate will assume that if all component columns are null, then the entire component is null. This should be okay for most purposes.

我可以覆蓋這種行爲?即使日期爲空,我也希望Date類能夠實例化。

謝謝,

回答

4

我不認爲你可以重寫該行爲。

但是,通常情況下讓NH工作如往常一樣容易(檢索null)並將其包裝在您的域模型中。

但是從我看到的情況來看,您似乎正在爲此進行演示(數據綁定?)關注。只是揭露DateTime?難道不容易嗎?

+0

嘲笑datetime.now是一個問題,然後... – 2010-05-21 17:39:48

+0

無論哪種方式,你不能嘲笑一種靜態方法。你應該注入一個「當前時間」服務。你試圖在錯誤的層面上處理這個問題。 – 2010-05-21 18:33:09

4

我想你可以通過使用實現IPostLoadEventListener的偵聽器來實現這種行爲。實體加載後,您可以檢查組件是否爲空,如果是,則檢查它是否爲新組件。

相關問題