diff Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs @ 200:5346c0500594 deploy pre 1.0

Pasando a estructura de db fija, ya no se crea automáticamente. Se comenta lo referente a Patrocinadores que queda para alguna futura versión. Script para tablas de v1.0
author nelopauselli
date Wed, 17 Aug 2011 17:54:45 -0300
parents 5a1f7233aa5a
children f23ee59ef1bd
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs	Mon Aug 15 20:00:02 2011 -0400
+++ b/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs	Wed Aug 17 17:54:45 2011 -0300
@@ -1,8 +1,10 @@
-using AltNetHispano.Agendas.Domain;
+using System;
+using AltNetHispano.Agendas.Domain;
 using ConfOrm;
 using ConfOrm.NH;
 using NHibernate;
 using NHibernate.Cfg;
+using NHibernate.Cfg.MappingSchema;
 using NHibernate.Tool.hbm2ddl;
 
 namespace Agendas.NHibernate
@@ -16,32 +18,7 @@
 		{
 			if (_cfg == null)
 			{
-				var orm = new ObjectRelationalMapper();
-				orm.TablePerClass<Persona>();
-				orm.TablePerClass<Evento>();
-
-				orm.Complex<Evento>(e => e.Estado);
-
-				orm.TablePerClass<Track>();
-				orm.TablePerClass<TrackLog>();
-				orm.TablePerClass<Cuenta>();
-				orm.TablePerClass<Patrocinador>();
-
-				orm.Cascade<Evento, Persona>(Cascade.None);
-				orm.Cascade<Persona, Evento>(Cascade.None);
-
-				orm.Cascade<Persona, Cuenta>(Cascade.All | Cascade.DeleteOrphans);
-
-				var mapper = new Mapper(orm);
-
-			    mapper.Customize<Persona>(a => a.Property(p => p.Nombre, m => m.Unique(true)));
-                mapper.Customize<Persona>(a => a.Property(p => p.Twitter, m => m.Unique(true)));
-
-                mapper.Customize<Evento>(a => a.Property(p => p.Titulo, m => m.Unique(true)));
-
-				mapper.AddPropertyPattern(p => p.DeclaringType == typeof(Evento) && p.Name == "Estado", a => a.Type<EventoStateType>());
-
-				var mapping = mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes());
+				var mapping = GetMapping();
 				
 				_cfg = new Configuration();
 				_cfg.Configure();
@@ -50,6 +27,45 @@
 			return _cfg;
 		}
 
+		public static HbmMapping GetMapping()
+		{
+			var orm = new ObjectRelationalMapper();
+			orm.TablePerClass<Persona>();
+			orm.TablePerClass<Evento>();
+
+			orm.Complex<Evento>(e => e.Estado);
+
+			orm.TablePerClass<Track>();
+			orm.TablePerClass<TrackLog>();
+			orm.TablePerClass<Cuenta>();
+			//orm.TablePerClass<Patrocinador>();
+
+			orm.Cascade<Evento, Persona>(Cascade.None);
+			orm.Cascade<Persona, Evento>(Cascade.None);
+
+			orm.Cascade<Persona, Cuenta>(Cascade.All | Cascade.DeleteOrphans);
+
+			orm.Bag<Persona>(p => p.Roles);
+
+			var mapper = new Mapper(orm);
+
+			mapper.Customize<Persona>(a => a.Property(p => p.Nombre, m => m.Unique(true)));
+			mapper.Customize<Persona>(a => a.Property(p => p.Twitter, m => m.Unique(true)));
+
+			mapper.Class<Persona>(
+				ca => ca.Bag(
+					p => p.Roles,
+					cm => cm.Key(em => em.Column("Persona")),
+					m => m.Element(em => em.Column("Rol"))));
+
+			mapper.Customize<Evento>(a => a.Property(p => p.Titulo, m => m.Unique(true)));
+			
+			mapper.AddPropertyPattern(p => p.DeclaringType == typeof(Evento) && p.Name == "Estado", a => a.Type<EventoStateType>());
+
+			return mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes());
+
+		}
+
 		public static ISessionFactory GetSessionFactory()
 		{
 			if (_sessionfactory == null)
@@ -58,26 +74,7 @@
 
 #if DEBUG
 				var schemaValidator = new SchemaValidator(cfg);
-				try
-				{
-					schemaValidator.Validate();
-				}
-				catch
-				{
-					try
-					{
-						var schemaUpdate = new SchemaUpdate(cfg);
-						schemaUpdate.Execute(false, true);
-
-						schemaValidator.Validate();
-					}
-					catch
-					{
-						var schemaExport = new SchemaExport(cfg);
-						schemaExport.Create(false, true);
-					}
-				}
-
+				schemaValidator.Validate();
 #endif
 
 				_sessionfactory = cfg.BuildSessionFactory();