view Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs @ 298:9bc60d166c8a

Se corrigieron los tests por el cambio de Patrocinador, para que no persista el logo en disco. Se comentó el código de PatrocinadorApiController, que no se utiliza.
author juanjose.montesdeocaarbos
date Sun, 19 Feb 2012 16:00:38 -0300
parents eeca9ddb330a
children
line wrap: on
line source

using System.Reflection;
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.Bag<Evento>(e=>e.Ponentes);
			orm.ManyToMany<Evento, Persona>();
			
			orm.TablePerClass<Track>();
			orm.TablePerClass<TrackLog>();
			orm.TablePerClass<Cuenta>();
			orm.TablePerClass<Patrocinador>();
			orm.ManyToMany<Evento, 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);
			ColumnsNamingPatterns(mapper);

			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.Customize<Evento>(e=>e.Property(p=>p.Estado, a=>a.Type<EventoStateType>()) );

			return mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes());

		}

		private static void ColumnsNamingPatterns(Mapper mapper)
		{
			//mapper.AddPoidPattern(mi => IsMemberOfClassOfAgendas(mi) && mi.Name == "Id",
			//(mi, idm) => idm.Column(mi.ReflectedType.Name + "Id"));
			//mapper.AddCollectionPattern(IsMemberOfClassOfAgendas, (mi, cm) => cm.Key(km => km.Column(mi.DeclaringType.Name + "Id")));
			//mapper.AddManyToOnePattern(IsMemberOfClassOfAgendas, (mi, idm) => idm.Column(mi.GetPropertyOrFieldType().Name + "Id"));
		}

		private static bool IsMemberOfClassOfAgendas(MemberInfo mi)
		{
			return mi.ReflectedType.Assembly == typeof(Evento).Assembly;
		}

		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;
		}
	}
}