An NHibernate Helper for working with the NHibernate.Mapping.Attributes API

Authoring, maintaining and managing NHibernate mapping files, hbm.xml, can quickly turn into a full-time job on large projects. One way of effectively accomplishing this vital, but burdensome task is to use the NHibernate contrib, NHibernate.Mapping.Attributes.

This will requires configuration information to be placed in an app.config file as documented in the NHibernate documentation. One is then required to have a single well known location from where to obtain an instance of the ISession(think PersistenceManager) type. The code below shows how to configure the serializer and return an ISession:

using System.IO;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.Attributes;

namespace Zainco.YCW.Components.Utils
public sealed class NHibernateHelper
private static ISession _session;
public static ISession Session
if (_session == null)
using (MemoryStream stream = new MemoryStream())
HbmSerializer.Default.Validate = true;
HbmSerializer.Default.HbmNamespace = "Zainco.YCW.Components.Domain";
HbmSerializer.Default.HbmAssembly = "Zainco.YCW.Components";
HbmSerializer.Default.Serialize(stream, Assembly.GetExecutingAssembly());

stream.Position = 0;

Configuration cfg = new Configuration();
_session = cfg.Configure().BuildSessionFactory().OpenSession();


return _session;

No comments:

Purging Broadcom Drivers for Broadcom Wifi Card on Ubuntu sudo apt-get purge bcmwl-kernel-source   Reference: https://askubuntu.com/qu...