Free NHibernate and XML Columns

I am creating a new site from scratch and am considering using Fluent NHibernate for my ORM. I think that everything will be easy to handle, with the possible exception of my XML columns. I never built a site with NHibernate at all (although I used Hibernate for Java), so consider me n00b.

Ideally, I would like XML to be treated as an XElement, as Linq-to-SQL does.

Can I do this with (smooth) NHibernate? What if I want to use automatic matching?

+4
source share
2 answers

You can use the IUserType specified here: https://nhibernate.jira.com/secure/attachment/12905/XmlType.cs

It uses an XmlDocument, but you can easily change it to work with XElement.

Update: This is included in NHibernate 3. Both XmlDocument and XDocument are available.

+6
source

Since I was struggling to find a solution, I would like to share my solution here (with XDocument instead of XElement, but at least for XML columns).

First create this agreement;

using System.Xml.Linq; using FluentNHibernate.Conventions; using FluentNHibernate.Conventions.AcceptanceCriteria; using FluentNHibernate.Conventions.Inspections; using FluentNHibernate.Conventions.Instances; public class XmlTypeConvention : IUserTypeConvention { public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) { criteria.Expect(x => x.Type == typeof(XDocument)); } public void Apply(IPropertyInstance instance) { instance.CustomType<NHibernate.Type.XDocType>(); } } 

Then be sure to add an agreement;

 Conventions.Add<XmlTypeConvention>(); 

Now, if your domain object has an XDocument property, it will turn into an XML column in the database.

+2
source

All Articles