# HG changeset patch # User nelopauselli # Date 1305779096 10800 # Node ID c8099df941bd7388109ebe74af2d53f86b34b011 # Parent 240a20cdbcc8eb393bbc51a8ce4985bbbbcb48df Implementando persistencia con NHibernate en el proyecto web diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs --- a/Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs Thu May 19 01:24:56 2011 -0300 @@ -1,15 +1,19 @@ -using AltNetHispano.Agendas.Domain; -using AltNetHispano.Agendas.Repositories.Memory; +using Agendas.NHibernate; +using AltNetHispano.Agendas.Domain; +using AltNetHispano.Agendas.Repositories.NHibernate; +using NHibernate; + namespace AltNetHispano.Agendas.Factories { - public static class AgendaFactory - { - private static Agenda _agenda; + public static class AgendaFactory + { + private static Agenda _agenda; - public static Agenda GetAgenda() - { - return _agenda ?? (_agenda = new Agenda(null, null, new EventoRepository(), new PonenteRepository())); - } - } + public static Agenda GetAgenda() + { + ISessionFactory sessionFactory = NhHelper.GetSessionFactory(); + return _agenda ?? (_agenda = new Agenda(null, null, new EventoRepository(sessionFactory), new PonenteRepository(sessionFactory))); + } + } } \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj --- a/Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj Thu May 19 01:24:56 2011 -0300 @@ -31,8 +31,12 @@ 4 + + ..\packages\NHibernate.3.1.0.4000\lib\Net35\NHibernate.dll + + @@ -48,9 +52,13 @@ {A14907DF-02E4-4FA7-BE27-4292AF50AA22} Agendas.Domain - - {28C5EBFB-EE69-4765-A880-D4DE0BC89F48} - Agendas.Repositories.Memory + + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A} + Agendas.NHibernate + + + {0973DF44-3B90-4D2A-B579-C64C93B6C853} + Agendas.Repositories.NHibernate diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Factories/AttributeFactory.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Factories/AttributeFactory.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,13 @@ +using System.Web.Mvc; +using Agendas.NHibernate; + +namespace AltNetHispano.Agendas.Factories +{ + public class AttributeFactory + { + public static ActionFilterAttribute GetNHibernateSessionPerAction() + { + return new NHibernateSessionPerActionAttribute(NhHelper.GetSessionFactory()); + } + } +} \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.NHibernate/Agendas.NHibernate.csproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/Agendas.NHibernate.csproj Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,77 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A} + Library + Properties + Agendas.NHibernate + Agendas.NHibernate + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\ConfOrm.1.0.1.5\lib\Net35\ConfOrm.dll + + + ..\packages\ConfOrm.1.0.1.5\lib\Net35\ConfOrm.Shop.dll + + + ..\packages\Iesi.Collections.3.1.0.4000\lib\Net35\Iesi.Collections.dll + + + ..\packages\NHibernate.3.1.0.4000\lib\Net35\NHibernate.dll + + + + + + + + + + + + + + + + + + + + + {A14907DF-02E4-4FA7-BE27-4292AF50AA22} + Agendas.Domain + + + + + \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.NHibernate/NHibernateSessionPerActionAttribute.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/NHibernateSessionPerActionAttribute.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,35 @@ +using System.Web.Mvc; +using NHibernate; +using NHibernate.Context; + +namespace Agendas.NHibernate +{ + public class NHibernateSessionPerActionAttribute : ActionFilterAttribute + { + private readonly ISessionFactory _sessionFactory; + + public NHibernateSessionPerActionAttribute(ISessionFactory sessionFactory) + { + _sessionFactory = sessionFactory; + } + + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + var session = _sessionFactory.OpenSession(); + CurrentSessionContext.Bind(session); + + base.OnActionExecuting(filterContext); + } + + public override void OnResultExecuted(ResultExecutedContext filterContext) + { + base.OnResultExecuted(filterContext); + + var session = _sessionFactory.GetCurrentSession(); + + session.Flush(); + session.Close(); + + } + } +} \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,63 @@ +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(); + orm.TablePerClass(); + + orm.Cascade(Cascade.None); + orm.Cascade(Cascade.None); + + 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(Exception ex) + { + var schemaExport = new SchemaExport(cfg); + schemaExport.Create(false, true); + } + +#endif + + _sessionfactory = cfg.BuildSessionFactory(); + } + return _sessionfactory; + } + } +} \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.NHibernate/Properties/AssemblyInfo.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/Properties/AssemblyInfo.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Agendas.NHibernate")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Agendas.NHibernate")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("b9429941-9ddb-4741-99da-cc05ed518f18")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.NHibernate/packages.config --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/packages.config Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Repositories.NHibernate/Agendas.Repositories.NHibernate.csproj --- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/Agendas.Repositories.NHibernate.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/Agendas.Repositories.NHibernate.csproj Thu May 19 01:24:56 2011 -0300 @@ -53,7 +53,6 @@ - diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Repositories.NHibernate/NHibernateSessionPerRequestAttribute.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/NHibernateSessionPerRequestAttribute.cs Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,36 @@ +using System.Web.Mvc; +using NHibernate; +using NHibernate.Context; + +namespace AltNetHispano.Agendas.Repositories.NHibernate +{ + public class NHibernateSessionPerRequestAttribute: ActionFilterAttribute + { + private readonly ISessionFactory _sessionFactory; + + public NHibernateSessionPerRequestAttribute(ISessionFactory sessionFactory) + { + _sessionFactory = sessionFactory; + } + + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + //iniciar session + var session = _sessionFactory.OpenSession(); + CurrentSessionContext.Bind(session); + + base.OnActionExecuting(filterContext); + } + + public override void OnResultExecuted(ResultExecutedContext filterContext) + { + base.OnResultExecuted(filterContext); + + //cerrar session + var session = _sessionFactory.GetCurrentSession(); + session.Flush(); + session.Close(); + + } + } +} \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Repositories.NHibernate/NhHelper.cs --- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/NhHelper.cs Tue May 17 16:59:39 2011 -0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -using AltNetHispano.Agendas.Domain; -using ConfOrm; -using ConfOrm.NH; -using NHibernate; -using NHibernate.Cfg; - -namespace AltNetHispano.Agendas.Repositories.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(); - orm.TablePerClass(); - - orm.Cascade(Cascade.None); - orm.Cascade(Cascade.None); - - 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(); - _sessionfactory=cfg.BuildSessionFactory(); - } - return _sessionfactory; - } - } -} \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj --- a/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Thu May 19 01:24:56 2011 -0300 @@ -89,6 +89,10 @@ {45804F1F-EB72-4F82-A227-088CAD9B785D} Agendas.Google + + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A} + Agendas.NHibernate + {28C5EBFB-EE69-4765-A880-D4DE0BC89F48} Agendas.Repositories.Memory diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Tests/Cruds/EventoCrudNhTests.cs --- a/Agendas/trunk/src/Agendas.Tests/Cruds/EventoCrudNhTests.cs Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/Cruds/EventoCrudNhTests.cs Thu May 19 01:24:56 2011 -0300 @@ -1,4 +1,5 @@ using System; +using Agendas.NHibernate; using AltNetHispano.Agendas.Domain; using AltNetHispano.Agendas.Repositories.NHibernate; using Moq; diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj --- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Thu May 19 01:24:56 2011 -0300 @@ -74,6 +74,9 @@ + + Always + Designer @@ -184,4 +187,8 @@ + + copy "$(SolutionDir)packages\NHibernate.Castle.3.1.0.4000\lib\Net35\NHibernate.ByteCode.Castle.dll" "$(TargetDir)" & +copy "$(SolutionDir)packages\Castle.Core.2.5.2\lib\NET35\Castle.Core.dll" "$(TargetDir)" + \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Web/Global.asax.cs --- a/Agendas/trunk/src/Agendas.Web/Global.asax.cs Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Global.asax.cs Thu May 19 01:24:56 2011 -0300 @@ -13,6 +13,7 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); + filters.Add(AttributeFactory.GetNHibernateSessionPerAction()); } public static void RegisterRoutes(RouteCollection routes) diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Web/Web.config --- a/Agendas/trunk/src/Agendas.Web/Web.config Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Web.config Thu May 19 01:24:56 2011 -0300 @@ -9,6 +9,8 @@ + + diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.Web/hibernate.cfg.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/hibernate.cfg.xml Thu May 19 01:24:56 2011 -0300 @@ -0,0 +1,11 @@ + + + + NHibernate.Driver.SqlClientDriver + AltNetHispano + NHibernate.Dialect.MsSql2008Dialect + + web + NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle + + \ No newline at end of file diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/Agendas.sln --- a/Agendas/trunk/src/Agendas.sln Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/Agendas.sln Thu May 19 01:24:56 2011 -0300 @@ -24,6 +24,8 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.Repositories.NHibernate", "Agendas.Repositories.NHibernate\Agendas.Repositories.NHibernate.csproj", "{0973DF44-3B90-4D2A-B579-C64C93B6C853}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.NHibernate", "Agendas.NHibernate\Agendas.NHibernate.csproj", "{9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -62,6 +64,10 @@ {0973DF44-3B90-4D2A-B579-C64C93B6C853}.Debug|Any CPU.Build.0 = Debug|Any CPU {0973DF44-3B90-4D2A-B579-C64C93B6C853}.Release|Any CPU.ActiveCfg = Release|Any CPU {0973DF44-3B90-4D2A-B579-C64C93B6C853}.Release|Any CPU.Build.0 = Release|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff -r 240a20cdbcc8 -r c8099df941bd Agendas/trunk/src/install packages.bat --- a/Agendas/trunk/src/install packages.bat Tue May 17 16:59:39 2011 -0300 +++ b/Agendas/trunk/src/install packages.bat Thu May 19 01:24:56 2011 -0300 @@ -1,4 +1,5 @@ ..\tools\nuget i Agendas.Tests\packages.config -o packages ..\tools\nuget i Agendas.Repositories.NHibernate\packages.config -o packages +..\tools\nuget i Agendas.NHibernate\packages.config -o packages PAUSE \ No newline at end of file