在Fluent NHibernate中存储一个对象[]作为列 [英] Storing an object[] as column in Fluent NHibernate

查看:151
本文介绍了在Fluent NHibernate中存储一个对象[]作为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(部分)模型:

 类LogMessage {
字符串格式;
object [] args;

我想存储 args [] 作为表中的单个列,事先了解格式参数通常是可序列化的,或者可以被转换为字符串先验

我不想存储格式化的消息,我想分开存储格式和参数(这有几个优点)。

我怎么能告诉流利的NHibernate使用BLOB类型来存储该列,并执行简单的二进制序列化/反序列化时持久的实体?

> Map(x => x.Args).CustomType< ObjectArrayType>();
$ b $ class ObjectArrayType:IUserType
{
public object NullSafeGet(IDBReader reader,string [] names,object owner)
{
byte [] bytes = NHibernateUtil.BinaryBlob.NullSafeGet(reader,names [0]);
返回反序列化(字节);

$ b $ public void NullSafeSet(IDBCommand cmd,object value,int index)
{
var args =(object [])value;
NHibernateUtil.BinaryBlob.NullSafeSet(cmd,Serialize(args),index);


类型返回类型
{
get {return typeof(object []); }

$ b $ public SqlType [] SqlTypes
{
get {return new [] {SqlTypeFactory.BinaryBlob}}
}
}


I have the following (partial) model:

class LogMessage{
    string Format;
    object[] args;
}

I want to store args[] as a single column in the table, taking advance of the fact that format arguments are generally serializable or can be converted to a string a priori.

I don't want to store the formatted message, I want to separately store format and args (this has several advantages).

How can I tell Fluent NHibernate to use a BLOB type to store that column and perform simple binary serialization/deserialization when persisting the entity?

解决方案

Map(x => x.Args).CustomType<ObjectArrayType>();

class ObjectArrayType : IUserType
{
    public object NullSafeGet(IDBReader reader, string[] names, object owner)
    {
        byte[] bytes = NHibernateUtil.BinaryBlob.NullSafeGet(reader, names[0]);
        return Deserialize(bytes);
    }

    public void NullSafeSet(IDBCommand cmd, object value, int index)
    {
        var args = (object[])value;
        NHibernateUtil.BinaryBlob.NullSafeSet(cmd, Serialize(args), index);
    }

    public Type ReturnType
    {
        get { return typeof(object[]); }
    }

    public SqlType[] SqlTypes
    {
        get { return new [] { SqlTypeFactory.BinaryBlob } }
    }
}

这篇关于在Fluent NHibernate中存储一个对象[]作为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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