Storing an object[] as column in Fluent NHibernate

孤街醉人 提交于 2019-12-12 01:07:59

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/16310044/storing-an-object-as-column-in-fluent-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!