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

查看:121
本文介绍了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接口派生的类.这些接口包含几种必须实现的方法,但出于我们此处的目的,我们将重点介绍其中的两种.考虑以下.

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映射上的简单属性来显式映射ActualClass和TypeClass之间的关联.

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方法.从方法主体可以看到,我只是从映射的属性中提取名称(作为value参数传入),并将提取的名称设置为要在数据库中设置的参数的值.最终结果是,尽管DomainClass在域模型中为ActualClass的Type属性,但只有TypeClass对象的Name属性被存储在数据库中.反之亦然.当NHibernate从数据库中加载ActualType的实例并找到我的自定义映射类型的属性时,它将加载我的自定义类型并调用NullSafeGet方法.如您所见,我的方法从返回的数据中获取名称,调用我的flyweight工厂以获取正确的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天全站免登陆