1

我在 NHibernate 中有一个类,我希望将属性映射到自定义数据类型。

NHibernate 类看起来像这样

namespace Example1.Models
{
    public class Calendar
    {
        public virtual int ID { get; set; }

        public virtual string Workspace { get; set; }
    }
}

带有映射文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="Example1.Models.Calendar, Example1" lazy="true" table="Calendar">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Workspace" />
  </class>
</hibernate-mapping>

Workspace 属性中的工作区如下所示

http://site/Morning%20meeting?InstanceID=1, Morning meeting

我可以使用 Uri 类将其映射为 URL,但在某些情况下,我还需要将标题与字符串一起提供

public class Calendar
{
    public virtual int ID { get; set; }

    private Uri _workspace;
    public virtual Uri Workspace
    {
        get { return _workspace; }
        set
        {
            if (value != null)
            {
                var workspace = value.ToString().Split(new[] { ',' });
                _workspace = new Uri(workspace[0]);
            }
        }
    }
}

例如,我尝试创建一个自定义类并将工作区类型添加到该类

private Workspace _workspace;
public virtual Workspace Workspace
{
    get { return _workspace; }
    set { _workspace = new Workspace(value.ToString()); }
}

...

public class Workspace
{
    public Uri Uri { get; set; }
    public string Title { get; set; }

    public Workspace(string workspace)
    {
        if(string.IsNullOrEmpty(workspace))
            return;

        var workspaceArray = workspace.Split(new[] { ',' });

        if (workspaceArray.Length > 0)
            Uri = new Uri(workspaceArray[0]);

        if (workspaceArray.Length > 1)
            Title = workspaceArray[1];

    }
}

这将呈现错误

Could not determine type for:Example1.Models.Workspace, Example1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Workspace)

实施此类自定义的推荐方法是什么?

谢谢

4

1 回答 1

2

通过实现 IUserType 解决了

此代码仍在进行中,但确实有效

NHibernate 类中的属性

private Workspace _workspace;
public virtual Workspace Workspace
{
    get { return _workspace; }
    set
    {
        if (value != null)
        {
            _workspace = value;
        }
    }
}

hbm 文件中的属性

<property name="Workspace" type="Example1.Models.WorkspaceType, Example1" />

和 IUserType 实现

public interface IWorkspace
{
    Uri Uri { get; set; }
    string Title { get; set; }
}

public class Workspace : IWorkspace
{
    #region Implementation of IWorkspace

    public Uri Uri { get; set; }
    public string Title { get; set; }

    #endregion

    public Workspace(string workspace)
    {
        if (string.IsNullOrEmpty(workspace))
            return;

        var workspaceArray = workspace.Split(new[] { ',' });

        if (workspaceArray.Length > 0)
            Uri = new Uri(workspaceArray[0]);

        if (workspaceArray.Length > 1)
            Title = workspaceArray[1];

    }
}

public class WorkspaceType : IUserType
{
    #region Implementation of IUserType

    public bool Equals(object x, object y)
    {
        if (x == null && y == null) return true;
        if (x == null || y == null) return false;
        return x.GetType() == y.GetType();
    }

    public int GetHashCode(object x)
    {
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var property0 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if (property0 == null)
        {
            return null;
        }

        Workspace workspace = new Workspace(property0.ToString());

        return workspace;
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        if (value == null)
        {
            ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
        }
        else
        {
            var state = (IWorkspace)value;
            ((IDataParameter)cmd.Parameters[index]).Value = state.GetType().Name;
        }
    }

    public object DeepCopy(object value)
    {
        return value;
    }

    public object Replace(object original, object target, object owner)
    {
        return original;
    }

    public object Assemble(object cached, object owner)
    {
        return cached;
    }

    public object Disassemble(object value)
    {
        return value;
    }

    public SqlType[] SqlTypes { get { return new[] { NHibernateUtil.String.SqlType }; } }
    public Type ReturnedType { get { return typeof(Workspace); } }
    public bool IsMutable { get { return false; } }

    #endregion
}
于 2013-01-29T23:12:53.137 回答