Mercurial > altnet-hispano
comparison Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs @ 67:c8099df941bd
Implementando persistencia con NHibernate en el proyecto web
author | nelopauselli |
---|---|
date | Thu, 19 May 2011 01:24:56 -0300 |
parents | |
children | c2d98fd6593f |
comparison
equal
deleted
inserted
replaced
64:240a20cdbcc8 | 67:c8099df941bd |
---|---|
1 using System; | |
2 using AltNetHispano.Agendas.Domain; | |
3 using ConfOrm; | |
4 using ConfOrm.NH; | |
5 using NHibernate; | |
6 using NHibernate.Cfg; | |
7 using NHibernate.Tool.hbm2ddl; | |
8 | |
9 namespace Agendas.NHibernate | |
10 { | |
11 public static class NhHelper | |
12 { | |
13 private static ISessionFactory _sessionfactory; | |
14 private static Configuration _cfg; | |
15 | |
16 public static Configuration GetConfiguration() | |
17 { | |
18 if (_cfg == null) | |
19 { | |
20 var orm = new ObjectRelationalMapper(); | |
21 orm.TablePerClass<Evento>(); | |
22 orm.TablePerClass<Ponente>(); | |
23 | |
24 orm.Cascade<Evento, Ponente>(Cascade.None); | |
25 orm.Cascade<Ponente, Evento>(Cascade.None); | |
26 | |
27 var mapper = new Mapper(orm); | |
28 | |
29 var mapping = mapper.CompileMappingFor(typeof(Evento).Assembly.GetTypes()); | |
30 | |
31 _cfg = new Configuration(); | |
32 _cfg.Configure(); | |
33 _cfg.AddDeserializedMapping(mapping, null); | |
34 } | |
35 return _cfg; | |
36 } | |
37 | |
38 public static ISessionFactory GetSessionFactory() | |
39 { | |
40 if (_sessionfactory == null) | |
41 { | |
42 var cfg = GetConfiguration(); | |
43 | |
44 #if DEBUG | |
45 var schemaValidator = new SchemaValidator(cfg); | |
46 try | |
47 { | |
48 schemaValidator.Validate(); | |
49 } | |
50 catch(Exception ex) | |
51 { | |
52 var schemaExport = new SchemaExport(cfg); | |
53 schemaExport.Create(false, true); | |
54 } | |
55 | |
56 #endif | |
57 | |
58 _sessionfactory = cfg.BuildSessionFactory(); | |
59 } | |
60 return _sessionfactory; | |
61 } | |
62 } | |
63 } |