Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use NHibernate to store an object in xml serialized form?

Say I have a class like this:

public class MyClass
{
    public int Id { get; set; }

    public DateTime Date { get; set; }
    public string String1 { get; set; }
    public string String2 { get; set; }
    public string String3 { get; set; }
    public string String4 { get; set; }
}

Is it possible to get NHibernate to store it in the following schema?

CREATE TABLE [dbo].[MyClass](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Xml] [varchar](max) NOT NULL,
)

Where the Id maps to Id, but then all other fields get serialized into XML (or otherwise)? I don't mind if these other fields have to go on a child object like the below, if that helps:

public class MyClass
{
    public int Id { get; set; }

    public AllOtherOptions Options { get; set; }
}

public class AllOtherOptions
{
    public DateTime Date { get; set; }
    public string String1 { get; set; }
    public string String2 { get; set; }
    public string String3 { get; set; }
    public string String4 { get; set; }
}
like image 677
Andrew Bullock Avatar asked Nov 24 '25 06:11

Andrew Bullock


1 Answers

I am thinking about doing something similar for an upcoming project. The project requires collecting a lot of data but only a few elements need to be stored in a relational database. I haven't started experimenting but these are my thoughts so far.

You can map an XML data type by creating a type that implements IUserType. If the child class (AllOtherOptions) is serializable, you should be able to map the XML field as a private member in MyClass and serialize/deserialize AllOtherOptions as needed. You could either dynamically maintain the XML field (sounds like a lot of work) or create an interceptor to do it. My thinking is that MyClass would implement an interface such as

public interface IXmlObjectContainer
{
    void SerializeChildObjects();
    void DeSerializeChildObjects();
}

and the interceptor would call those methods as needed. That's a proof of concept idea. I would probably refine that by exposing pairs of xml fields and serializable objects to remove the work of serializing from IXmlObjectContainer implementers. Or maybe handle serialization through the XML field's get/set accessors.

More info:

  1. Working with XML Fields in NHibernate
  2. Another XML implementation of IUserType
like image 143
Jamie Ide Avatar answered Nov 28 '25 12:11

Jamie Ide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!