4

I'm building a new site from scratch and am considering using Fluent NHibernate for my ORM. I think it'll handle everything easily except, possibly, my XML columns. I've never built a site with NHibernate at all (though I've used Hibernate for Java) so consider me a n00b.

Ideally I'd like the XML to be treated as an XElement as Linq-to-SQL does.

Can I do this with (fluent) NHibernate? What if I want to use auto mapping?

2 Answers 2

6

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

It uses XmlDocument, but you can easily modify it to work with XElement instead.

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

Sign up to request clarification or add additional context in comments.

Comments

2

Since I struggled to find a solution, I'd like to share my solution here (with XDocument instead of XElement, but at least valid for XML columns).

First create this convention;

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 remember to add the convention;

Conventions.Add<XmlTypeConvention>();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.