【问题标题】:Fluent nHibernate configuration for custom type自定义类型的流畅 nHibernate 配置
【发布时间】:2012-06-24 13:23:27
【问题描述】:

我正在尝试让 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;}
}

【问题讨论】:

    标签: nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping


    【解决方案1】:

    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 */ }
        }
    }
    

    您可以阅读post,了解如何使用 Fluent 映射受保护的属性。

    【讨论】:

    • 感谢发布链接。关于使用 CustomType,我查看了一个示例,从示例的外观来看,它创建了 SecureField 类型的新实例并返回它,这是我不想要的。有什么办法可以避免吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    相关资源
    最近更新 更多