1

我試圖讓fNH映射到自定義類型,但我遇到了困難。自定義類型的流暢的nHibernate配置

我希望fNH通過接口將其值分配給自定義類型。我還需要nHibernate來保留實體上自定義類型的實例。當訪問屬性時,它將始終被實例化,不會覆蓋實例,只需設置包裝的值。

當我嘗試下面的映射,它拋出一個異常「找不到類「Entities.User屬性‘值’吸氣」

想法?

FNH映射:

Map(x =>((IBypassSecurity<string>)x.SecuredPinNumber).Value,"[PinNumber]"); 

域例如:

public class User 
{ 
public SecureField<string> SecuredPinNumber {get;private set;} 
} 

public class SecureField<T> : IBypassSecurity<T> 
{ 
public T Value { get; set; } // would apply security rules, for 'normal' use 
T IBypassSecurity<T>.Value {get;set;} // gets/sets the value directy, no security. 
} 

// allows nHibernate to assign the value without any security checks 
public interface IBypassSecurity<T> 
{ 
T Value {get;set;} 
} 

回答

2

在地圖()方法是表達構建器以提取屬性名稱爲字符串。因此,您的映射告訴NH,您希望映射類User中的「Value」屬性,當然這不存在。如果您想使用自定義類型,請閱讀關於此的NH參考文檔,並在映射中使用CustomType()方法。

您也可以使用PinNumber的受保護屬性來允許直接訪問。

public class User 
{ 
    protected virtual string PinNumber { get; set; } // mapped for direct access 
    public string SecuredPinNumber 
    { 
     get { /* get value with security checks */ } 
     set { /* set value with security checks */ } 
    } 
} 

您可以閱讀關於使用Fluent映射受保護屬性的此post

+0

感謝您的帖子鏈接。關於使用CustomType,我已經看了一個例子,從例子的外觀來看,它創建了一個SecureField類型的新實例,並返回它,我不想要。有什麼辦法可以避免這種情況? – jasper