comparison 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
comparison
equal deleted inserted replaced
199:39ce09df76dc 200:5346c0500594
1 using AltNetHispano.Agendas.Domain; 1 using System;
2 using AltNetHispano.Agendas.Domain;
2 using ConfOrm; 3 using ConfOrm;
3 using ConfOrm.NH; 4 using ConfOrm.NH;
4 using NHibernate; 5 using NHibernate;
5 using NHibernate.Cfg; 6 using NHibernate.Cfg;
7 using NHibernate.Cfg.MappingSchema;
6 using NHibernate.Tool.hbm2ddl; 8 using NHibernate.Tool.hbm2ddl;
7 9
8 namespace Agendas.NHibernate 10 namespace Agendas.NHibernate
9 { 11 {
10 public static class NhHelper 12 public static class NhHelper
14 16
15 public static Configuration GetConfiguration() 17 public static Configuration GetConfiguration()
16 { 18 {
17 if (_cfg == null) 19 if (_cfg == null)
18 { 20 {
19 var orm = new ObjectRelationalMapper(); 21 var mapping = GetMapping();
20 orm.TablePerClass<Persona>();
21 orm.TablePerClass<Evento>();
22
23 orm.Complex<Evento>(e => e.Estado);
24
25 orm.TablePerClass<Track>();
26 orm.TablePerClass<TrackLog>();
27 orm.TablePerClass<Cuenta>();
28 orm.TablePerClass<Patrocinador>();
29
30 orm.Cascade<Evento, Persona>(Cascade.None);
31 orm.Cascade<Persona, Evento>(Cascade.None);
32
33 orm.Cascade<Persona, Cuenta>(Cascade.All | Cascade.DeleteOrphans);
34
35 var mapper = new Mapper(orm);
36
37 mapper.Customize<Persona>(a => a.Property(p => p.Nombre, m => m.Unique(true)));
38 mapper.Customize<Persona>(a => a.Property(p => p.Twitter, m => m.Unique(true)));
39
40 mapper.Customize<Evento>(a => a.Property(p => p.Titulo, m => m.Unique(true)));
41
42 mapper.AddPropertyPattern(p => p.DeclaringType == typeof(Evento) && p.Name == "Estado", a => a.Type<EventoStateType>());
43
44 var mapping = mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes());
45 22
46 _cfg = new Configuration(); 23 _cfg = new Configuration();
47 _cfg.Configure(); 24 _cfg.Configure();
48 _cfg.AddDeserializedMapping(mapping, null); 25 _cfg.AddDeserializedMapping(mapping, null);
49 } 26 }
50 return _cfg; 27 return _cfg;
28 }
29
30 public static HbmMapping GetMapping()
31 {
32 var orm = new ObjectRelationalMapper();
33 orm.TablePerClass<Persona>();
34 orm.TablePerClass<Evento>();
35
36 orm.Complex<Evento>(e => e.Estado);
37
38 orm.TablePerClass<Track>();
39 orm.TablePerClass<TrackLog>();
40 orm.TablePerClass<Cuenta>();
41 //orm.TablePerClass<Patrocinador>();
42
43 orm.Cascade<Evento, Persona>(Cascade.None);
44 orm.Cascade<Persona, Evento>(Cascade.None);
45
46 orm.Cascade<Persona, Cuenta>(Cascade.All | Cascade.DeleteOrphans);
47
48 orm.Bag<Persona>(p => p.Roles);
49
50 var mapper = new Mapper(orm);
51
52 mapper.Customize<Persona>(a => a.Property(p => p.Nombre, m => m.Unique(true)));
53 mapper.Customize<Persona>(a => a.Property(p => p.Twitter, m => m.Unique(true)));
54
55 mapper.Class<Persona>(
56 ca => ca.Bag(
57 p => p.Roles,
58 cm => cm.Key(em => em.Column("Persona")),
59 m => m.Element(em => em.Column("Rol"))));
60
61 mapper.Customize<Evento>(a => a.Property(p => p.Titulo, m => m.Unique(true)));
62
63 mapper.AddPropertyPattern(p => p.DeclaringType == typeof(Evento) && p.Name == "Estado", a => a.Type<EventoStateType>());
64
65 return mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes());
66
51 } 67 }
52 68
53 public static ISessionFactory GetSessionFactory() 69 public static ISessionFactory GetSessionFactory()
54 { 70 {
55 if (_sessionfactory == null) 71 if (_sessionfactory == null)
56 { 72 {
57 var cfg = GetConfiguration(); 73 var cfg = GetConfiguration();
58 74
59 #if DEBUG 75 #if DEBUG
60 var schemaValidator = new SchemaValidator(cfg); 76 var schemaValidator = new SchemaValidator(cfg);
61 try 77 schemaValidator.Validate();
62 {
63 schemaValidator.Validate();
64 }
65 catch
66 {
67 try
68 {
69 var schemaUpdate = new SchemaUpdate(cfg);
70 schemaUpdate.Execute(false, true);
71
72 schemaValidator.Validate();
73 }
74 catch
75 {
76 var schemaExport = new SchemaExport(cfg);
77 schemaExport.Create(false, true);
78 }
79 }
80
81 #endif 78 #endif
82 79
83 _sessionfactory = cfg.BuildSessionFactory(); 80 _sessionfactory = cfg.BuildSessionFactory();
84 } 81 }
85 return _sessionfactory; 82 return _sessionfactory;