view Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs @ 206:beeb06da4398

Al querés ingresar a un lugar que requiere autenticación y luego de autenticarse, vuelve a la página donde quería entrar
author nelopauselli
date Wed, 24 Aug 2011 10:38:13 -0300
parents 5346c0500594
children f23ee59ef1bd
line wrap: on
line source

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
{
	public static class NhHelper
	{
		private static ISessionFactory _sessionfactory;
		private static Configuration _cfg;

		public static Configuration GetConfiguration()
		{
			if (_cfg == null)
			{
				var mapping = GetMapping();
				
				_cfg = new Configuration();
				_cfg.Configure();
				_cfg.AddDeserializedMapping(mapping, null);
			}
			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)
			{
				var cfg = GetConfiguration();

#if DEBUG
				var schemaValidator = new SchemaValidator(cfg);
				schemaValidator.Validate();
#endif

				_sessionfactory = cfg.BuildSessionFactory();
			}
			return _sessionfactory;
		}
	}
}