自定义类型的Fluent nHibernate配置

自定义类型的Fluent nHibernate配置,nhibernate,fluent-nhibernate,nhibernate-mapping,fluent-nhibernate-mapping,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,Fluent Nhibernate Mapping,我试图让fNH映射到自定义类型,但遇到了困难 我希望fNH通过接口将其值分配给自定义类型。我还需要nHibernate在实体上保留自定义类型的实例。当访问属性时,它将始终被实例化,不要覆盖实例,只需设置包装值 当我尝试下面的映射时,它抛出一个异常“在类“Entities.User”中找不到属性“Value”的getter” 想法 fNH映射: Map(x =>((IBypassSecurity<string>)x.SecuredPinNumber).Value,"[PinNum

我试图让fNH映射到自定义类型,但遇到了困难

我希望fNH通过接口将其值分配给自定义类型。我还需要nHibernate在实体上保留自定义类型的实例。当访问属性时,它将始终被实例化,不要覆盖实例,只需设置包装值

当我尝试下面的映射时,它抛出一个异常“在类“Entities.User”中找不到属性“Value”的getter”

想法

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;}
}
Map(x=>((IBypassSecurity)x.SecuredPinNumber).Value,“[PinNumber]”;
域示例:

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;}
}
公共类用户
{
public SecureField SecuredPinNumber{get;private set;}
}
公共类安全字段:IBypassSecurity
{
公共T值{get;set;}//将应用安全规则,以供“正常”使用
T IBypassSecurity.Value{get;set;}//获取/设置值directy,无安全性。
}
//允许nHibernate在不进行任何安全检查的情况下分配值
公共接口ibypassecurity
{
T值{get;set;}
}
Map()方法是一个表达式生成器,用于将属性名称提取为字符串。因此,您的映射告诉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映射受保护属性的文章。

谢谢您的帖子链接。关于使用CustomType,我看了一个例子,从这个例子的外观来看,它创建了一个SecureField类型的新实例,并返回它,这是我不想要的。有没有办法避免这种情况?