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
{
get
{
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();
cfg.Configure();
cfg.AddInputStream(stream);
_session = cfg.Configure().BuildSessionFactory().OpenSession();
}
return _session;
}
}
}
}
No comments:
Post a Comment