NHibernate - 映射复杂类型,以便序列化为String

时间:2013-02-19 08:55:54

标签: c# sql-server-2008 nhibernate mapping

我有一个DTO对象,其属性类型为“ObservableDictionary”,名为“Test”,也是可序列化的!

DTO字段“测试”现在映射到数据字段“测试”,其类型为nvarchar(最大)。

当我保存并加载时,一切正常,但数据库中的序列化不可读,它充满了特殊的字符。

所以我想知道,是否有可能告诉NHibernate使用XMLSerializer ???

1 个答案:

答案 0 :(得分:0)

好的,我懂了!在Rippo的帮助下!

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;

namespace MCC.Common.DL.BaseObjects
{
    public class SerializableUserType<originalType> : IUserType
    {
        public SqlType[] SqlTypes
        {
            get
            {
                SqlType[] types = new SqlType[1];
                types[0] = new SqlType(DbType.String);
                return types;
            }
        }

        public System.Type ReturnedType
        {
            get { return typeof(originalType); }
        }

        public new bool Equals(object x, object y)
        {
            if (x == null)
            {
                return false;
            }
            else
            {
                return x.Equals(y);
            }
        }

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

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            string txt = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
            return StringSerializer<originalType>.DeSerialize(txt);
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                NHibernateUtil.String.NullSafeSet(cmd, null, index);
                return;
            }
            var wrt = StringSerializer<originalType>.Serialize((originalType)value);

            NHibernateUtil.String.NullSafeSet(cmd, wrt, index);
        }

        public object DeepCopy(object value)
        {
            if (value == null) return null;
            return StringSerializer<originalType>.DeSerialize(StringSerializer<originalType>.Serialize((originalType)value));            
        }

        public bool IsMutable
        {
            get { return false; }
        }

        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;
        }
    }
}

这个我在Fluent中用过:

Map(x => x.ScriptedTagScript).CustomType(typeof(SerializableUserType<ObservableDictionary<string, string>>));

你需要这个助手类:

public class StringSerializer<T>
{
    public static string Serialize(T obj)
    {
        if (obj == null)
            return string.Empty;

        // XML-Serialisieren in String
        XmlSerializer serializer = new XmlSerializer(obj.GetType());

        // Serialisieren in MemoryStream
        MemoryStream ms = new MemoryStream();

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.Indent = true;

        XmlWriter writer = XmlWriter.Create(ms, settings);

        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
        namespaces.Add(string.Empty, string.Empty);

        serializer.Serialize(writer, obj, namespaces);

        // Stream in String umwandeln 
        StreamReader r = new StreamReader(ms);
        r.BaseStream.Seek(0, SeekOrigin.Begin);

        return r.ReadToEnd();
    }        

    public static T DeSerialize(string txt)
    {
        T retVal = default(T);
        if (string.IsNullOrEmpty(txt))
        {
            return retVal;
        }

        try
        {
            XmlSerializer ser = new XmlSerializer(typeof (T));
            StringReader stringReader = new StringReader(txt);
            XmlTextReader xmlReader = new XmlTextReader(stringReader);
            retVal = (T) ser.Deserialize(xmlReader);
            xmlReader.Close();
            stringReader.Close();
        }
        catch (Exception)
        {
        }

        return retVal;
    }        
}