view Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs @ 94:db4b1e2cae49

Cambio del nombre de la clase Ponente a Persona Se agrega la clase Cuenta para identificar cada una de las cuentas con que se puede autenticar una persona Alta Automatica de cuentas de twitter creando la persona
author Nelo@Kenia.neluz.int
date Sat, 04 Jun 2011 12:11:17 -0300
parents c2d98fd6593f
children 90ce239cfa6d
line wrap: on
line source

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<Persona>();
				orm.TablePerClass<Evento>();
				orm.TablePerClass<Track>();
				orm.TablePerClass<Cuenta>();

				orm.Cascade<Evento, Persona>(Cascade.None);
				orm.Cascade<Persona, Evento>(Cascade.None);
				
				orm.Cascade<Persona, Cuenta>(Cascade.All);

				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
				{
					try
					{
						var schemaUpdate = new SchemaUpdate(cfg);
						schemaUpdate.Execute(false, true);

						schemaValidator.Validate();
					}
					catch
					{
						var schemaExport = new SchemaExport(cfg);
						schemaExport.Create(false, true);
					}
				}

#endif

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