我有一个 DTO 对象,其属性类型为“ObservableDictionary”,名为“Test”,它也是可序列化的!
DTO 字段“Test”现在映射到类型为 nvarchar(max) 的 Datbasfield“Test”。
当我保存和加载时,一切正常,但数据库中的序列化不可读,它充满了特殊字符。
是否可以告诉 NHibernate 使用 XMLSerializer?
我有一个 DTO 对象,其属性类型为“ObservableDictionary”,名为“Test”,它也是可序列化的!
DTO 字段“Test”现在映射到类型为 nvarchar(max) 的 Datbasfield“Test”。
当我保存和加载时,一切正常,但数据库中的序列化不可读,它充满了特殊字符。
是否可以告诉 NHibernate 使用 XMLSerializer?
好,我知道了!在 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;
}
}