NHibernate:自定义属性访问器的 Get 和 Set 方法未被调用

发布于 2024-10-03 12:10:25 字数 3179 浏览 3 评论 0原文

我正在尝试将数据库字段(“LS_RECNUM”)可能的 NULL、“M”和“F”值映射到具有性别枚举类型的属性。

映射如下所示:

Map(x => x.Gender).Column("LS_GENDER").Access.Using<GenderPropertyAccessor>();

...GenderPropertyAccessor 类如下所示:

using System;
using System.Collections;
using System.Reflection;
using Kctc;
using NHibernate.Engine;
using NHibernate.Properties;

public class GenderPropertyAccessor : IPropertyAccessor
{
  #region Setter

  private class GenderGetterSetter : IGetter, ISetter
  {
    PropertyInfo _property;
    public GenderGetterSetter(PropertyInfo property)
    {
      if (property == null) throw new ArgumentNullException("property");
      if (property.PropertyType != typeof(Gender)) throw new ArgumentException("property");
      _property = property;
    }

    public void Set(object target, object value) //Convert string to enum
    {
      _property.SetValue(target, GetGenderFromString(value), null);
    }

    public object Get(object target) //Convert enum back to string
    {
      Gender gender = (Gender)_property.GetValue(target, null);
      return SetGenderToString(gender);
    }

    /// <summary>
    /// Interprets the supplied string as a gender.
    /// </summary>
    /// <param name="strGender">The gender as either 'F' or 'M'.</param>
    /// <returns></returns>
    private Gender GetGenderFromString(object strGender)
    {
      if (strGender == null) return Gender.Unknown;
      switch (strGender.ToString().ToLower())
      {
        case "f":
          return Gender.Female;
        case "m":
          return Gender.Male;
        default:
          return Gender.Unknown;
      }
    }

    /// <summary>
    /// Sets the supplied Gender to the appropriate 'M' or 'F' value.
    /// </summary>
    /// <param name="objGender">The gender.</param>
    /// <returns></returns>
    private string SetGenderToString(object objGender)
    {
      Gender gender = (Gender) objGender;
      switch (gender)
      {
        case Gender.Female:
          return "F";
        case Gender.Male:
          return "M";
        default:
          return null;
      }
    }

    public MethodInfo Method
    {
      get { return null; }
    }

    public string PropertyName
    {
      get { return _property.Name; }
    }

    public object GetForInsert(object owner, IDictionary mergeMap, ISessionImplementor session)
    {
      return Get(owner);
    }

    public Type ReturnType
    {
      get { return typeof(byte[]); }
    }
  }

  #endregion

  public IGetter GetGetter(Type theClass, string propertyName)
  {
    return new GenderGetterSetter(theClass.GetProperty(propertyName));
  }

  public ISetter GetSetter(Type theClass, string propertyName)
  {
    return new GenderGetterSetter(theClass.GetProperty(propertyName));
  }

  public bool CanAccessThroughReflectionOptimizer
  {
    get { return false; }
  }
}

由于不太熟悉反射,我根本不确定 Get 和 Set 方法是否已正确实现。

当我尝试此操作时,我仍然收到错误“无法将 F 解析为性别”。我尝试调试 GenderPropertyAccessor 类。映射文件中的相关行(如上所示)正确执行,GenderGetterSetter 类的构造函数也是如此,但 Get 和 Set 方法从未被调用!

谁能告诉我我可能做错了什么?

I'm attempting to map a database field ("LS_RECNUM") possible values of NULL, 'M' and 'F' to a property with a Gender enumeration type.

The mapping looks like this:

Map(x => x.Gender).Column("LS_GENDER").Access.Using<GenderPropertyAccessor>();

...and the GenderPropertyAccessor class looks like this:

using System;
using System.Collections;
using System.Reflection;
using Kctc;
using NHibernate.Engine;
using NHibernate.Properties;

public class GenderPropertyAccessor : IPropertyAccessor
{
  #region Setter

  private class GenderGetterSetter : IGetter, ISetter
  {
    PropertyInfo _property;
    public GenderGetterSetter(PropertyInfo property)
    {
      if (property == null) throw new ArgumentNullException("property");
      if (property.PropertyType != typeof(Gender)) throw new ArgumentException("property");
      _property = property;
    }

    public void Set(object target, object value) //Convert string to enum
    {
      _property.SetValue(target, GetGenderFromString(value), null);
    }

    public object Get(object target) //Convert enum back to string
    {
      Gender gender = (Gender)_property.GetValue(target, null);
      return SetGenderToString(gender);
    }

    /// <summary>
    /// Interprets the supplied string as a gender.
    /// </summary>
    /// <param name="strGender">The gender as either 'F' or 'M'.</param>
    /// <returns></returns>
    private Gender GetGenderFromString(object strGender)
    {
      if (strGender == null) return Gender.Unknown;
      switch (strGender.ToString().ToLower())
      {
        case "f":
          return Gender.Female;
        case "m":
          return Gender.Male;
        default:
          return Gender.Unknown;
      }
    }

    /// <summary>
    /// Sets the supplied Gender to the appropriate 'M' or 'F' value.
    /// </summary>
    /// <param name="objGender">The gender.</param>
    /// <returns></returns>
    private string SetGenderToString(object objGender)
    {
      Gender gender = (Gender) objGender;
      switch (gender)
      {
        case Gender.Female:
          return "F";
        case Gender.Male:
          return "M";
        default:
          return null;
      }
    }

    public MethodInfo Method
    {
      get { return null; }
    }

    public string PropertyName
    {
      get { return _property.Name; }
    }

    public object GetForInsert(object owner, IDictionary mergeMap, ISessionImplementor session)
    {
      return Get(owner);
    }

    public Type ReturnType
    {
      get { return typeof(byte[]); }
    }
  }

  #endregion

  public IGetter GetGetter(Type theClass, string propertyName)
  {
    return new GenderGetterSetter(theClass.GetProperty(propertyName));
  }

  public ISetter GetSetter(Type theClass, string propertyName)
  {
    return new GenderGetterSetter(theClass.GetProperty(propertyName));
  }

  public bool CanAccessThroughReflectionOptimizer
  {
    get { return false; }
  }
}

Not being particularly familiar with reflection, I'm not at all sure that the Get and Set methods have been implemented correctly.

When I try this, I still get an error 'Can't parse F as Gender'. I've tried debugging the GenderPropertyAccessor class. The relevant line (shown above) in the mapping file is executing correctly, as is the constructor for the GenderGetterSetter class, but the Get and Set methods are never called!!!

Can anyone tell me what I might be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-10-10 12:10:26

我将为此使用 IUserType 的实现。这是 很好的简单例子。在 NullSafeGet 和 NullSafeSet 方法中,您将分别将字符串更改为枚举或返回枚举。为了让 NHibernate 检测到更改,您的 Equals 实现是否正确也很重要。

将属性映射为使用自定义用户类型很简单:

Map(x => x.Gender).Column("LS_GENDER").CustomType(typeof(MyUserType));

I would use an implementation of IUserType for this. Here's a good simple example. In the NullSafeGet and NullSafeSet methods you will mutate the string to an enum and back, respectively. It's also critical that your Equals implementation is correct in order for NHibernate to detect changes.

Mapping the property to use a custom user type is easy:

Map(x => x.Gender).Column("LS_GENDER").CustomType(typeof(MyUserType));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文