diff Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs @ 67:c8099df941bd

Implementando persistencia con NHibernate en el proyecto web
author nelopauselli
date Thu, 19 May 2011 01:24:56 -0300
parents
children c2d98fd6593f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs	Thu May 19 01:24:56 2011 -0300
@@ -0,0 +1,63 @@
+using System;
+using AltNetHispano.Agendas.Domain;
+using ConfOrm;
+using ConfOrm.NH;
+using NHibernate;
+using NHibernate.Cfg;
+using NHibernate.Tool.hbm2ddl;
+
+namespace Agendas.NHibernate
+{
+	public static class NhHelper
+	{
+		private static ISessionFactory _sessionfactory;
+		private static Configuration _cfg;
+
+		public static Configuration GetConfiguration()
+		{
+			if (_cfg == null)
+			{
+				var orm = new ObjectRelationalMapper();
+				orm.TablePerClass<Evento>();
+				orm.TablePerClass<Ponente>();
+
+				orm.Cascade<Evento, Ponente>(Cascade.None);
+				orm.Cascade<Ponente, Evento>(Cascade.None);
+
+				var mapper = new Mapper(orm);
+
+				var mapping = mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes());
+				
+				_cfg = new Configuration();
+				_cfg.Configure();
+				_cfg.AddDeserializedMapping(mapping, null);
+			}
+			return _cfg;
+		}
+
+		public static ISessionFactory GetSessionFactory()
+		{
+			if (_sessionfactory == null)
+			{
+				var cfg = GetConfiguration();
+
+#if DEBUG
+				var schemaValidator = new SchemaValidator(cfg);
+				try
+				{
+					schemaValidator.Validate();
+				}
+				catch(Exception ex)
+				{
+					var schemaExport = new SchemaExport(cfg);
+					schemaExport.Create(false, true);
+				}
+
+#endif
+
+				_sessionfactory = cfg.BuildSessionFactory();
+			}
+			return _sessionfactory;
+		}
+	}
+}
\ No newline at end of file