nHibernate 映射到自定义类型 [英] nHibernate mapping to custom types

查看:25
本文介绍了nHibernate 映射到自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Oracle 数据库,其中一个字段是日期范围字段.它基本上只是作为 VARCHAR(40) 以 YYYY/MM/DD-YYYY/MM/DD 格式存储在数据库中.我想将它在 nHibernate 中映射到我这样创建的自定义类

I have a Oracle database and one of the fields is a date range field. It is basically just stored in the database as a VARCHAR(40) in the format YYYY/MM/DD-YYYY/MM/DD. I want to map it in nHibernate to a custom class I have created like this

public class DateTimeRange
{
    public DateTimeRange(DateTime fromTime, DateTime toTime)
    {
        FromTime = fromTime;
        ToTime = toTime;
    }

    public override string ToString()
    {
        return String.Format("{0} to {1}", FromTime.ToString("HH:mm:ss"), ToTime.ToString("HH:mm:ss"));
    }

    public DateTime FromTime { get; set; }

    public DateTime ToTime { get; set; }
}

如何映射到这样的自定义类?

How can I map to custom classes like this?

推荐答案

您需要实现自己的 IUserType.

You need to implement your own IUserType.

请参阅此博文 了解详情.如果博客消失了,我也会粘贴下面的相关部分.

See this blog post for details. I'll also paste the relevant section below in case the blog disappears.

在 NHibernate 中,自定义映射类型是派生自 IUserType 或 ICompositeUserType 接口的类.这些接口包含几个必须实现的方法,但出于我们的目的,我们将关注其中的 2 个.考虑以下.

In NHibernate, a custom mapping type is a class that derives from either the IUserType or ICompositeUserType interfaces. These interfaces contain several methods that must be implemented, but for our purposes here, we’re going to focus on 2 of them. Consider the following.

  public class TypeClassUserType : IUserType
  {


    object IUserType.NullSafeGet(IDataReader rs, 
      string[] names, 
     object owner) {

     string name = NHibernateUtil.String.NullSafeGet(rs, 
     names[0]) as string;

     TypeClassFactory factory = new TypeClassFactory();
     TypeClass typeobj = factory.GetTypeClass(name);
     return typeobj;
   }

    void IUserType.NullSafeSet(IDbCommand cmd, 
    object value, 
     int index) {

      string name = ((TypeClass)value).Name;
     NHibernateUtil.String.NullSafeSet(cmd, name, index);
    }
  }

创建这个类后,我现在可以将 ActualClass 和 TypeClass 之间的关联显式映射为 ActualClass 映射上的一个简单属性.

Having created this class, I can now explicitly map the association between ActualClass and TypeClass as a simple property on the ActualClass mapping.

<property
  name="Type"
  column="TypeName"
  type="Samples.NHibernate.DataAccess.TypeClassUserType, 
        Samples.NHibernate.DataAccess" />

由于 NHibernate 正在保存 ActualType 的实例,它会加载并创建一个新的 TypeClassUserType 实例并调用 NullSafeSet 方法.从方法体中可以看出,我只是从映射属性中提取名称(作为值参数传入)并将提取的名称设置为要在数据库中设置的参数值.最终结果是,尽管 ActualClass 的 Type 属性在域模型中是 TypeClass,但只有 TypeClass 对象的 Name 属性被存储在数据库中.反过来也是如此.当 NHibernate 从数据库加载 ActualType 的实例并找到我的自定义映射类型的属性时,它会加载我的自定义类型并调用 NullSafeGet 方法.如您所见,我的方法从返回的数据中获取名称,调用我的享元工厂以获取 TypeClass 的正确实例,然后实际返回该实例.类型解析过程对我的数据访问类(甚至对 NHibernate 本身而言)是透明的.

As NHibernate is in the process of saving an instance of ActualType, it will load and create a new instance of TypeClassUserType and call the NullSafeSet method. As you can see from the method body, I am simply extracting the name from the mapped property (passed in as the value parameter) and setting the extracted name as the value of the parameter to be set in the database. The net result is that although the Type property of ActualClass is TypeClass in the domain model, only the Name property of the TypeClass object gets stored in the database. The converse is also true. When NHibernate is loading an instance of ActualType from the database and the finds a property of my custom mapping type, it loads my custom type and calls the NullSafeGet method. As you can see, my method gets the name from the returned data, calls my flyweight factory to get the correct instance of TypeClass, and then actually returns that instance. The type resolution process happens transparently to my data access classes (and even to NHibernate itself for that matter).

这篇关于nHibernate 映射到自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆